How to Configure UART on STM32: Step-by-Step Guide
To configure
UART on an STM32, enable the UART peripheral clock, set the baud rate, configure data bits, stop bits, and parity in the UART registers, and enable the UART transmitter and receiver. Use the STM32 HAL library or direct register access to initialize and start UART communication.Syntax
Configuring UART on STM32 involves setting up the UART peripheral registers or using the HAL library functions. Key steps include:
- Enable UART clock in the RCC register.
- Configure UART parameters: baud rate, word length, stop bits, parity.
- Enable UART transmitter and receiver.
- Configure GPIO pins for UART TX and RX.
Using STM32 HAL, the main function is HAL_UART_Init() with a UART_HandleTypeDef structure.
c
UART_HandleTypeDef huart2; void UART2_Init(void) { huart2.Instance = USART2; // Select UART2 peripheral huart2.Init.BaudRate = 9600; // Set baud rate huart2.Init.WordLength = UART_WORDLENGTH_8B; // 8 data bits huart2.Init.StopBits = UART_STOPBITS_1; // 1 stop bit huart2.Init.Parity = UART_PARITY_NONE; // No parity huart2.Init.Mode = UART_MODE_TX_RX; // Enable TX and RX huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; // No hardware flow control huart2.Init.OverSampling = UART_OVERSAMPLING_16; // Oversampling by 16 HAL_UART_Init(&huart2); // Initialize UART }
Example
This example shows how to initialize UART2 on an STM32F4 board using HAL, send a string, and receive data.
c
#include "stm32f4xx_hal.h" UART_HandleTypeDef huart2; void SystemClock_Config(void); void UART2_Init(void); int main(void) { HAL_Init(); SystemClock_Config(); UART2_Init(); char msg[] = "Hello UART!\r\n"; HAL_UART_Transmit(&huart2, (uint8_t*)msg, sizeof(msg)-1, HAL_MAX_DELAY); uint8_t rx_data; while (1) { if (HAL_UART_Receive(&huart2, &rx_data, 1, 1000) == HAL_OK) { HAL_UART_Transmit(&huart2, &rx_data, 1, HAL_MAX_DELAY); // Echo received byte } } } void UART2_Init(void) { __HAL_RCC_USART2_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3; // PA2=TX, PA3=RX GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); huart2.Instance = USART2; huart2.Init.BaudRate = 9600; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; HAL_UART_Init(&huart2); } void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); }
Output
On UART terminal: "Hello UART!" is sent once, then any received character is echoed back.
Common Pitfalls
Common mistakes when configuring UART on STM32 include:
- Not enabling the UART peripheral clock before initialization.
- Incorrect GPIO pin configuration for UART TX and RX pins.
- Mismatched baud rate settings between devices causing communication failure.
- Forgetting to enable UART transmitter and receiver modes.
- Not configuring NVIC interrupts if using interrupt-driven UART.
Always check the STM32 datasheet and reference manual for correct pin and clock settings.
c
/* Wrong: Missing clock enable */ // HAL_UART_Init(&huart2); // Will fail if __HAL_RCC_USART2_CLK_ENABLE() is not called first /* Right: Enable clock before init */ __HAL_RCC_USART2_CLK_ENABLE(); HAL_UART_Init(&huart2);
Quick Reference
Summary tips for STM32 UART configuration:
- Enable UART peripheral clock in RCC.
- Configure GPIO pins to alternate function mode for UART TX/RX.
- Set baud rate, word length, stop bits, and parity correctly.
- Enable UART transmitter and receiver.
- Use HAL library functions for easier setup or direct register access for fine control.
Key Takeaways
Always enable the UART peripheral clock before initialization.
Configure GPIO pins to the correct alternate function for UART TX and RX.
Set matching baud rate and UART parameters on both communicating devices.
Use HAL_UART_Init() with a properly filled UART_HandleTypeDef for easy setup.
Check for common mistakes like missing clock enable or wrong pin modes.