cancel
Showing results for 
Search instead for 
Did you mean: 

USART example code for Nucleo F401RE

Posted on March 26, 2014 at 04:16

Via Virtual COM port built into ST-LINK

// STM32 USART2 (Tx PA.2, Rx PA.3) STM32F401RE NUCLEO - sourcer32@gmail.com
#include ''stm32f4xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // PA.2 USART2_TX, PA.3 USART2_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(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
}
/**************************************************************************************/
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 115200 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 = 115200;
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(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
/**************************************************************************************/
void OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, *s++); // Send Char
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART2_Configuration();
OutString(''Welcome to Nucleo F401RE\r\n'');
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART2); // Collect Char
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART2, 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\r\n'', 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..
6 REPLIES 6
ashtroid06
Associate
Posted on May 23, 2015 at 10:37

Hi Clive,

I too am using STM32F401RE. I've been able to use the UART2 to communicate with the PC via the ST-Link Virtual COM Port. Right now i want to make UART1 communicate with the PC via the virtual com port. I have a GPS shield from Sparkfun which actually uses the UART2 to tranceive GPS data. I would like to be able to view this on the PC & hence would like to use another UART. I'm not able to figure out how to make changes for this. I've configured the UART1 port same as the UART2 as shown in your code but i still feel i'm missing something. I'm a newbie, so please bear with my stupid questions.

Regards,

Ashwin

Posted on May 23, 2015 at 16:47

Well the problem is the ST-LINK is wired for PA2/PA3 - USART2, and not USART1. These are the same pins used for D0/D1

The GPS receiver I have on the Nucleo connects via pin D2 which is PA10 USART1-RX

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
didos.nicky
Associate II
Posted on August 11, 2015 at 07:43

Hi Clive, I tried your code.

It seems your code perfectly works, but although we desire to send and receive a specific string, we always receive a sequence of strange character. Inside the strange characters we receive, we don't see the string we desired to send.

Please give us some hints.

Best Regards

Posted on August 11, 2015 at 17:08

Unfortunately I don't have an Nucleo F411RE (know this from the other thread)

My concern would be with the clocking configuration of the 411. The Nucleo would be providing an 8 MHz clock on the HSE pin, it would also need to use HSE BYPASS mode. The system_stm32f4xx.c would need to be modified as it looks to be expecting a 25 MHz, and this may also be reflected by the definition of HSE_VALUE within the project. Garbled data is usually a result of incoherent software/hardware understanding of the clocks being used.

Probably also going to want to play with the PLL settings too as the F411 is good to 100 MHz vs F401@84MHz, F405/7@168MHz, F427/9@180MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
didos.nicky
Associate II
Posted on August 11, 2015 at 19:27

Thank you, Clive.

I solved 

changing HSE_VALUE from 25000000 to 

8000000 in the file stm32f4xx.h of 

http://www.st.com/st-web-ui/static/active/en/st_prod_software_internet/resource/technical/software/firmware/stm32f4_dsp_stdperiph_lib.zip

 . 

#define HSE_VALUE    ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */

Maybe

 

I think it's

 

a

 

bug code in ST for my

http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847/PF260320

. I get: <0>Welcome to Nucleo F401RE

Why

is

this

character

<

0

?

Posted on August 11, 2015 at 19:31

Line noise from the USART/pins initializing. An external pull up might help.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..