Understanding the Principle of Automatic Baud Rate Detection in STM32

Your device connects to the other device. If it can automatically detect the baud rate without knowing the other party’s serial port baud rate, wouldn’t it be very convenient?

1

Overview

Regarding the issue of automatic detection of UART serial port baud rate, those with project experience or who have studied serial communication should know a little about the methods of automatic detection.
Most people know that the baud rate can be matched one by one through software programming. This method is the most common and quite effective.
The method mentioned above is the well-known software-based method for detecting baud rates. In fact, there is another method, which is to detect baud rates through the serial port hardware itself.

For STM32, the ST official documentation and application notes mention this. Below, I will briefly explain the hardware automatic baud rate detection content based on the documentation.

2

STM32 Hardware Automatic Baud Rate Detection

ABR: Auto Baud Rate, Automatic Baud Rate Detection allows the receiving device to accept data from various sending devices operating at different rates without the need to establish the data rate in advance.

1. Applications of ABR

  • When the communication speed of the system is unknown in advance.

  • When the system is using a relatively low-accuracy clock source, this mechanism allows for obtaining the correct baud rate without measuring clock deviations.
2. Supported ABR Series

In STM32, only certain series support hardware automatic baud rate detection; earlier series do not support it (such as F1 F4), while later series, including the latest STM32H7 and G0 series, do support this function.

Understanding the Principle of Automatic Baud Rate Detection in STM32

Of course, for STM32 series devices with built-in ABR, not all instantiated USART interfaces support automatic baud rate detection.

Understanding the Principle of Automatic Baud Rate Detection in STM32

3. Automatic Baud Rate Detection Mode

ABR refers to the process where the receiving device determines the incoming data rate by checking the first character (usually a pre-selected flag character).

The automatic baud rate detection function on STM32 products has various modes based on different character patterns:

  • Mode 0: Any character starting with a “1” bit

  • Mode 1: Any character starting with 10xx pattern

  • Mode 2: 0x7F

  • Mode 3: 0x55

Understanding the Principle of Automatic Baud Rate Detection in STM32

Note:

1. In all ABR modes, the baud rate will be detected multiple times during synchronous data reception, and each detection value will be compared with the previous one.

2. In 7-bit data length mode, 0x7F and 0x55 frame detection ABR modes are not supported.

4. Code Configuration

The relevant code is provided by the official based on the (Standard Peripheral Library, HAL Library) reference examples, such as F0 Standard Peripheral Library reference code:

static void AutoBauRate_StartBitMethod(void){   /* USART enable */  USART_Cmd(EVAL_COM1, ENABLE);
  /* Configure the AutoBaudRate method */  USART_AutoBaudRateConfig(EVAL_COM1, USART_AutoBaudRate_StartBit);
  /* Enable AutoBaudRate feature */  USART_AutoBaudRateCmd(EVAL_COM1, ENABLE);
  /* Wait until Receive enable acknowledge flag is set */  while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_REACK) == RESET)  {}  
  /* Wait until Transmit enable acknowledge flag is set */    while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TEACK) == RESET)  {}  
  /* Loop until the end of Autobaudrate phase */  while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_ABRF) == RESET)  {}  
  /* If AutoBaudBate error occurred */  if (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_ABRE) != RESET)  {    /* Turn on LED3 */    STM_EVAL_LEDOn(LED3);  }  else  {    /* Turn on LED2 */    STM_EVAL_LEDOn(LED2);
    /* Wait until RXNE flag is set */    while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)    {}
    /* Wait until TXE flag is set */        while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET)    {}
    /* Send received character */    USART_SendData(EVAL_COM1, USART_ReceiveData(EVAL_COM1)); 
    /* clear the TE bit (if a transmission is on going or a data is in the TDR, it will be sent before    efectivelly disabling the transmission) */    USART_DirectionModeCmd(EVAL_COM1, USART_Mode_Tx, DISABLE);
    /* Check the Transfer Complete Flag */    while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TC) == RESET)    {}  }
  /* USART Disable */  USART_Cmd(EVAL_COM1, DISABLE);}

5. ABR Error Calculation

The communication rate range (especially the maximum communication rate) is determined by the USART clock source (fCK). The receiver uses different user-configurable oversampling techniques to distinguish valid input data from noise, thus recovering the data. This can achieve a balance between maximum communication rate and resistance to noise/clock inaccuracies.
The oversampling method can be selected by programming the OVER8 bit in the USARTx_CR1 register, which can be 16 times or 8 times the baud rate clock.

The frequency of the USART clock source must be compatible with the expected communication rate:

  • At 16 times oversampling, the baud rate ranges from fCK/65535 to fCK/16.

  • At 8 times oversampling, the baud rate ranges from fCK/65535 to fCK/8.

The baud rate error depends on the USART clock source, oversampling method, and ABR mode.

Understanding the Principle of Automatic Baud Rate Detection in STM32

Where:

  • The expected baud rate depends on the sending device;

  • The actual baud rate is the baud rate determined by the USART receiver using the automatic baud rate detection operation.

6. Error

The following figure is from official test data, based on: error calculation of ABR at fCK = 72 MHz, with an expected baud rate of 115200 bits/s.

Understanding the Principle of Automatic Baud Rate Detection in STM32
From the above figure, it can be seen that the accuracy of ABR modes 2 and 3 is higher than that of modes 0 and 1; their baud rate error values are lower.

However, since the error between the expected baud rate and the actual baud rate is less than 1%, the results of all modes are normal.

Understanding the Principle of Automatic Baud Rate Detection in STM32

END

Author: strongerHuang

Source: Embedded Column

Copyright belongs to the original author. If there is any infringement, please contact for deletion.
Recommended Reading
The Japanese Operating System That Almost Dominated the World…
Modify a few lines of code to reduce the for loop time from 3.2 seconds to 0.3 seconds
VSCode vs SourceInsight, which is better for reading source code?

→ Follow to avoid getting lost ←

Leave a Comment

Your email address will not be published. Required fields are marked *