STM32F4 Uart Tutorial By Polling Method
STM32F4 Uart Tutorial By Polling Method

A Universal Synchronous Receiver/Transmitter interface is a device able to transmit data word serially using two I/Os, one acting as transmitter (TX) and one as receiver (RX), plus one additional I/O as one clock line, while a Universal Asynchronous Receiver/Transmitter uses only two RX/TX I/Os. We refer to the first interface with the term USART and to the second one with the term UART.

UARTs transmit / reveive data asynchronously, which means there is no clock signal to synchronize the output of bits from the transmitting UART to the sampling of bits by the receiving UART. Instead of a clock signal, the transmitting UART adds start and stop bits to the data packet being transferred. These bits define the beginning and end of the data packet so the receiving UART knows when to start reading the bits.When the receiving UART detects a start bit, it starts to read the incoming bits at a specific frequency known as the baud rate. Baud rate is a measure of the speed of data transfer, expressed in bits per second (bps).Both UARTs must operate at about the same baud rate

UART transmitted data is organized into packets. Each packet contains 1 start bit, 5 to 9 data bits (depending on the UART), an optional parity bit, and 1 or 2 stop bits:

STM32 microcontrollers provide a variable number of USARTs, which can be configured to work both in synchronous and asynchronous mode. All Nucleo-64 boards are designed so that the USART2 of the target MCU is linked to the ST-LINK interface. For this uart demonstration i will configure UART 3 of STM32F446RE Nucleo board for polling method by using HAL library provided by ST.

To configure the UART first add stm32f4xx_hal_uart.c and stm32f4xx_hal_uart.h files into your project then uncomment the HAL_UART_MODULE_ENABLED in stm32f4xx_hal_conf.h file this will include the UART source files into your project. In this project PC10 and PC11 pin of STM32F446RE microcontroller is configured as UART TX and UART RX respectively. Refer your board schematic and microcontroller data sheet for your requirements if there is any change in board or microcontroller.

UART Initialization and pin configuration

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  GPIO_InitTypeDef GPIO_InitStruct = {0};
   /* Peripheral clock enable */
    /**USART3 GPIO Configuration    
    PC10     ------> USART3_TX
    PC11     ------> USART3_RX 
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

/* stm32f4xx_hal_msp.c */

First enable the clock for the particular UART peripheral and clock of GPIO used by that UART peripheral. By refering the Alternate Function table for GPIO’s in the datasheet of STM32f446xx microcontroller GPIOC 10 is used as USART3_TX ,GPIOC 11 is used as USART3_RX. These pin configuration is under alternate function 7 so assign GPIO_AF7_USART3 for Alternate member of structure.

UART_HandleTypeDef huart3;

int main(void)


  huart3.Instance = USART3;
  huart3.Init.BaudRate = 115200;
  huart3.Init.WordLength = UART_WORDLENGTH_8B;
  huart3.Init.StopBits = UART_STOPBITS_1;
  huart3.Init.Parity = UART_PARITY_NONE;
  huart3.Init.Mode = UART_MODE_TX_RX;
  huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart3.Init.OverSampling = UART_OVERSAMPLING_16;

  uint8_t str[] = "Welcome To Embedded Diaries !\r\n";

  while (1)


/* main.c*/
  1. Instance: Base address of USART uisng for communication.
  2. BaudRate: This parameter refers to the connection speed, expressed in bits per seconds
  3. WordLength: It specifies the number of data bits transmitted or received in a frame.
  4. StopBits: This field specifies the number of stop bits transmitted.
  5. Parity: It indicates the parity mode. when parity is enabled, the computed parity is inserted at the MSB position of the transmitted data
  6. Mode: It specifies whether the RX or TX mode is enabled or disabled.
  7. HwFlowCtl: It specifies whether the RS232⁶ Hardware Flow Control mode is enabled or disabled.
  8. OverSampling: The OverSampling field can assume the value UART_OVERSAMPLING_16 to perform 16 samples for each frame bit or UART_OVERSAMPLING_8 to perform 8 samples

HAL_UART_Transmit( ) is used to transfer the characters through the UART by passing the address of the array that contained the characters and its size. Open a serial terminal in system such as Tera Term or Real Term and set the baudrate for 115200 bps to view the transmitted characters from the microcontroller.