Automatic Baud Rate Detection for 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 convenient?

1

Overview

Regarding the issue of automatically recognizing UART serial port baud rates, I believe those with project experience or who have seriously studied serial ports should know a bit about automatic recognition methods.
Most people know that the baud rate can be matched one by one through software programming, which is the most common and effective method.
The above method is the well-known software-based method for detecting baud rates. In fact, there is another method that is hardware-based baud rate detection.
For STM32, ST has actually 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 allows 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.
  • When the system uses a relatively low-precision clock source, and this mechanism allows 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 series released later, including the latest STM32H7 and G0 series, all support this feature.
Automatic Baud Rate Detection for Serial Ports
Of course, for STM32 series devices with built-in ABR, not all instantiated USART interfaces support automatic baud rate detection.
Automatic Baud Rate Detection for Serial Ports
3. Automatic Baud Rate Detection Modes
ABR refers to the process by which 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;
Automatic Baud Rate Detection for Serial Ports
Note:
A. In all ABR modes, the baud rate is repeatedly detected during synchronous data reception, and each detection value is compared with the previous one.
B. In 7-bit data length mode, 0x7F and 0x55 frame detection in ABR mode is not supported.
4. Code Configuration
Related 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 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 allowing data recovery. 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 frequency of the USART clock source must be compatible with the expected communication rate:
• At 16 times oversampling, the baud rate is between fCK/65535 and fCK/16.
• At 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.
Automatic Baud Rate Detection for 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 operation.
6. Errors
The following figure is from official test data, based on the ABR error calculation at fCK = 72 MHz, with an expected baud rate of 115200 bits/s.
Automatic Baud Rate Detection for Serial Ports
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 for all modes are normal.
Automatic Baud Rate Detection for Serial Ports

Automatic Baud Rate Detection for Serial Ports

Automatic Baud Rate Detection for Serial Ports

Leave a Comment