cancel
Showing results for 
Search instead for 
Did you mean: 

[STM32F4-Discovery] USART6 Problem

andymememe
Associate II
Posted on April 08, 2014 at 11:36

I wrote this program, but after I uploaded and connect RS232 to my notebook, Putty didn't show anything, what the problem?
The source code is down blow:
 #include ''stm32f4xx.h'' // Device header
#include <stdio.h>
#include ''misc.h''
#include ''stm32f4xx_usart.h'' // Keil::Device:StdPeriph Drivers:USART
void
STM_EVAL_COMInit(USART_InitTypeDef* USART_InitStruct)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
//RCC_AHB1PeriphClockCmd(UART4_TX_PIN | UART4_RX_PIN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); 
//clock a PORTC
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); 
//clock alla UART4!
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig( GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* USART configuration */
USART_Init(USART6, USART_InitStruct);
/* Enable USART */
USART_Cmd(USART6, ENABLE);
}
int
main(
void
){
USART_InitTypeDef Usart6;
Usart6.USART_BaudRate = 38400;
Usart6.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usart6.USART_Mode = USART_Mode_Tx;
Usart6.USART_Parity = USART_Parity_No;
Usart6.USART_StopBits = USART_StopBits_1;
Usart6.USART_WordLength = USART_WordLength_8b;
STM_EVAL_COMInit(&Usart6);
USART_ClearFlag(USART6, USART_FLAG_TC);
USART_ClearITPendingBit(USART6,USART_IT_TXE); 
while
(1){
USART_ClearITPendingBit(USART6,USART_IT_TXE); 
USART_SendData(USART6, (uint16_t)
''Testing...\n''
);
while
(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET);
USART_ClearFlag(USART6, USART_FLAG_TC);
}
return
0;
}

#discovery #usart #stm32f4
9 REPLIES 9
andymememe
Associate II
Posted on April 08, 2014 at 11:40

When I see my debug screen, the Data Register is always empty. I don't know this is problem or not.

Posted on April 08, 2014 at 13:55

USART_SendData() does not send strings.

int main(void){
USART_InitTypeDef Usart6;
Usart6.USART_BaudRate = 38400;
Usart6.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
Usart6.USART_Mode = USART_Mode_Tx;
Usart6.USART_Parity = USART_Parity_No;
Usart6.USART_StopBits = USART_StopBits_1;
Usart6.USART_WordLength = USART_WordLength_8b;
STM_EVAL_COMInit(&Usart6);
while(1){
while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET);
USART_SendData(USART6, 0x54);
}
return 0;
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on April 08, 2014 at 14:33

// STM32 USART6 ECHO (Tx PC.6, Rx PC.7) STM32F4DIS-BB - sourcer32@gmail.com
#include ''stm32f4xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART6 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
/* GPIOC clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PC.6 USART6_TX, PC.7 USART6_RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
}
/**************************************************************************************/
void USART6_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 38400 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 38400;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART6, &USART_InitStructure);
USART_Cmd(USART6, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART6_Configuration();
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART6); // Collect Char
while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART6, Data); // Echo Char
}
}
/**************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
while (1)
{}
}
#endif
/**************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andymememe
Associate II
Posted on April 09, 2014 at 01:46

Thank you very much for your help!

But how can I send a string? I saw somebody use printf(), is that right?

Posted on April 09, 2014 at 02:29

But how can I send a string? I saw somebody use printf(), is that right?

One character at a time.

void USART_putchar(char ch)
{
while(USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART6, ch);
}
void USART_puts(char *s)
{
while(*s)
USART_putchar(*s++);
}

Your ability to use printf(),puts(),etc will depend on your tool chain, and how that allows you to ''retarget'' the character output to functions you provide to support your board hardware. Refer to your tool chain documentation. The terms ''hosting'', ''semi-hosting'' and ''retargeting'' would be things to look for, or ''board support''
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
andymememe
Associate II
Posted on April 09, 2014 at 09:11

Thank you for your help. But I still can't see any result on my Putty. I think it because my USB to RS232 line is not comptiable with this board. I use a line with Prolific chip. Will this effect the result?

frankmeyer9
Associate II
Posted on April 09, 2014 at 10:15

But I still can't see any result on my Putty. I think it because my USB to RS232 line is not comptiable with this board. I use a line with Prolific chip. Will this effect the result?

 

Usually not. The driver chip/vendor matters only to the PC side, which needs a matching driver (Prolific USB drivers caused bluescreens on a Win7 machine I tried recently).

Have you set proper baudrates, including matching startbit, stopbit, parity and handshake settings on both sides ?

Have you attached a scope, to check the actual communication ?

andymememe
Associate II
Posted on April 09, 2014 at 11:55

I will check it out! Thank you!

Posted on April 09, 2014 at 16:37

Use a scope and confirm the bit timing.

Directly to the Prolific chip should be fine, used them myself, be careful using the term ''RS232'' as it implies voltage/signalling levels which are not compatible with the STM32.

Finally watch where your source code is pulled from, the STM32F4-DISCO uses an 8 MHz external clock, the definition of HSE_VALUE in the project, and the PLL settings in system_stm32f4xx.c need to be coherent with this. The EVAL series boards use 25 MHz crystals, and a lot of the firmware library code assumes this.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..