Principle of Automatic Baud Rate Recognition for STM32 Serial Ports

Your device connects to another device, and if it can automatically recognize the baud rate without knowing the baud rate of the other device, wouldn’t that be very convenient?

1 Overview

Regarding the issue of automatically recognizing UART serial port baud rates, those with project experience or who have studied serial ports should know a bit about the methods of automatic recognition.Most people are aware that the baud rate can be matched one by one through software programming, which is the most common and effective method.This method is well-known as the software-based method for detecting baud rates; in fact, there is another method that uses the serial port hardware itself to complete baud rate detection.

For STM32, ST has mentioned this in application notes and reference manuals. Below, I will briefly explain the content of hardware automatic baud rate detection based on the documentation.

2 STM32 Hardware Automatic Baud Rate Detection

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

1. Applications of ABR

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

  • The system is using a relatively low-accuracy clock source, and 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 and F4). The later series, including the newly released STM32H7 and G0 series, all support this feature.

Principle of Automatic Baud Rate Recognition for STM32 Serial Ports

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

Principle of Automatic Baud Rate Recognition for STM32 Serial Ports

3. Automatic Baud Rate Detection Modes

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 built-in modes based on different character patterns:

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

  • Mode 1: Any character starting with the 10xx pattern

  • Mode 2: 0x7F

  • Mode 3: 0x55

Principle of Automatic Baud Rate Recognition for STM32 Serial Ports

Note:

1. In all ABR modes, the baud rate is detected multiple times during synchronous data reception, and each detection value is 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 reference examples based on (Standard Peripheral Library, HAL Library), such as the 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 ongoing or data is in the TDR, it will be sent before effectively 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, which is used to recover data. This can achieve a balance between maximum communication rate and 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 USART clock source frequency must be compatible with the expected communication rate:

  • With 16 times oversampling, the baud rate is between fCK/65535 and fCK/16.

  • With 8 times oversampling, the baud rate is between fCK/65535 and fCK/8.

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

Principle of Automatic Baud Rate Recognition for STM32 Serial Ports

Where:

  • The expected baud rate depends on the sending device;

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

6. Error

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

Principle of Automatic Baud Rate Recognition for STM32 Serial PortsFrom 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.This concludes today’s sharing. If you have any original technical articles related to electronic design or others, feel free to submit them to us, and we will select the best for publication, and you will receive a reward for your article!Principle of Automatic Baud Rate Recognition for STM32 Serial PortsFriendly Reminder:

Due to recent changes in the WeChat public platform push rules, many readers have reported not seeing updated articles in a timely manner. According to the latest rules, it is recommended to click on “Recommended Reading, Share, Collect,” etc., to become a regular user.

Recommended Reading:

  • Foreign Media: ASML to Move Out of the Netherlands

  • A PCB company in Guangzhou is on the verge of bankruptcy: making efforts to save itself!

  • The unfinished era of chip storage 12-inch wafer factory finally has someone taking over!!

Please click 【View】 to give the editor a thumbs up

Principle of Automatic Baud Rate Recognition for STM32 Serial Ports

Leave a Comment