Skip to content

Automatic UART Baud Rate Detection

Sometimes a product must determine the serial baud rate automatically. This note introduces the hardware-assisted feature available on selected STM32 MCUs: supported devices, the detection principle, appropriate use cases, and accuracy considerations.

Platform: STM32 microcontrollers.

1. STM32 support

Automatic baud-rate detection is available only on some STM32 families. Even on a supported MCU, not every UART instance necessarily supports it; always check the reference manual for the exact peripheral.

STM32 support list

2. How it works

For the same received byte, a different baud rate produces a different bit period. By measuring edges and timing, the UART can infer the baud rate.

Waveform principle

For example, a frame with one start bit, seven data bits, and one stop bit occupies very different time widths at 115200 and 1200 bit/s.

Detection requires incoming data. With no transition on RX, the receiver cannot determine the peer's baud rate.

2.1 Detection modes

STM32 provides four automatic baud-rate modes.

Automatic baud-rate modes

  • Mode 0: detect from the start bit. It cannot work if the relevant first data bit pattern never occurs.
  • Mode 1: detect a 10xx pattern.
  • Mode 2: detect the 0x7F character.
  • Mode 3: detect the 0x55 character.

The last two modes require cooperation from the peer, which must send the expected character. In return, they use more transitions and are generally more reliable.

2.2 Accuracy

Accuracy depends mainly on the detection mode and the peer's actual baud rate.

Detection error comparison

With fCK = 72 MHz and an expected baud rate of 115200 bit/s, modes 2 and 3 have lower error because they measure more bits.

Error by baud rate

At a fixed mode, higher baud rates usually produce more error, although the detailed result also depends on clock divisors and the measurement pattern.

3. STM32 configuration

The peer must send data before auto-detection can complete. STM32 HAL exposes the configuration through the advanced UART initialization fields:

huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
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;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2.AdvancedInit.AdvFeatureInit =
    UART_ADVFEATURE_AUTOBAUDRATE_INIT;
huart2.AdvancedInit.AutoBaudRateEnable =
    UART_ADVFEATURE_AUTOBAUDRATE_ENABLE;
huart2.AdvancedInit.AutoBaudRateMode =
    UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;

if (HAL_UART_Init(&huart2) != HAL_OK) {
    Error_Handler();
}

3.1 Choosing a mode

  • If the two UART endpoints cannot coordinate an initial exchange, start with mode 0 or mode 1.
  • If you control both endpoints, arrange for 0x7F or 0x55 and prefer the corresponding modes for better accuracy.
  • If throughput is not critical, do not select an unnecessarily high baud rate: tolerance usually becomes tighter as baud rate increases.

3.2 Example

ST's UART bootloader uses automatic detection while receiving an update request. The stm32flash project is a useful implementation reference.

For more detail, see ST's USART automatic baud-rate detection application note.

4. Summary

Automatic detection is useful when the receiver does not know the peer configuration in advance. Its reliability depends on incoming data patterns, clock accuracy, and mode selection. A useful design question is what should happen if a link already detected as 115200 later changes to 57600: define a re-synchronization or timeout strategy explicitly.