STM32F4: USART

The nice thing about using the STLINK/V2 debugger inside IAR is that you can print values straight to the terminal watch screen, just by writing

printf("Ok\n");

. This works really well as a method to find out what is going on inside the mystery black box.

To interface to the real world however, we need to use the USART. Again, after googling a bit and reading the very thick manual, its not too difficult to figure out. The code below

void USART_Configuration(void){
// sort out clocks
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Map USART2 to A.02
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
// Initialize USART
USART_InitStructure.USART_BaudRate = 9600;
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_Tx;
/* Configure USART */
USART_Init(USART2, &USART_InitStructure);
/* Enable the USART */
USART_Cmd(USART2, ENABLE);
}

sets up USART2 to output data at 9600 bps. To make life easy, we can “overload” the printf primitive to redirect stdio to USART2:

/**
* @brief Function that printf uses to push characters to serial port
* @param ch: ascii character
* @retval character
*/
int putcharx(int ch)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, (uint8_t)ch);
return ch;
}

Now, when we type “printf(“Hello World\n”);”, it appears on the serial port instead of the watch terminal. Simple :-)

Get the main listing.

7 thoughts on “STM32F4: USART

  1. This is really a great website, can you give examples of USART receiving data from serial port using polling method. Also, please provide full listing for download as in other articles.

    Thanks again, great articles :)

    1. Thanks for spotting that – I have added the code listing. I will try and put up a receiving example soon!

  2. Great example but I can’t achieve the same result. My USART config works great when I make “USART_SendData(USART1, 0×49);” but when I want to overload the printf whith your putcharx function it doesn’t works. I don’t understand

  3. guys,, this program can’t be running,,any “warning” at statement “printf”
    i used keil 4,, how can i cope that problem??

    please help me

    1. As far as I can tell, for Keil you need to retarget the putchar() function (rather than putcharx() in IAR).
      Try this and let us know how you get on.
      Best,
      Andrew

  4. Hi Andrew,
    Thanks a lot for these posts from you, the issue was with the GPIO AF configurations. i am now able to use the UARTs on my STM32F0 discovery boards !! And debugging with printfs is so working :)…

Leave a Reply