STM32 USART Automatic Baud Rate Detection
STM32 USART Automatic Baud Rate Detection
STM32 USART Automatic Baud Rate Detection
Application note
STM32 USART automatic baud rate detection
Introduction
Correct USART communication requires the transmission and reception baud rates to be
matched reasonably closely, otherwise communication errors may occur.
Automatic baud rate detection is useful when establishing a communication link between
two devices, where the slave device is able to detect the baud rate of the master controller
and self-adjust accordingly. This requires an automatic mechanism to determine the baud
rate.
The USART peripheral embedded in some STM32 devices offers many features, including
automatic baud rate detection hardware.
The purpose of this application note is to present the automatic baud rate detection feature
of STM32 microcontrollers and to give an alternative software approach for STM32 devices
that do not implement this feature in hardware.
This application note applies to the products listed in Table 1.
Contents
4 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
5 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
List of figures
List of tables
Mainstream
STM32F0 Yes
STM32F1 No
STM32F3 Yes
High-performance
STM32F2 No
STM32F4 No
STM32F7 Yes
Ultra-low power
STM32L0 Yes
STM32L1 No
STM32L4 Yes
For STM32 Series devices that embed ABR, not all of the instantiated USART interfaces
support the automatic baud rate feature. This constraint is detailed in Table 3.
STM32F302xB/C
STM32F303xB/C
STM32F303xD/E
STM32F302xD/E
STM32F302x6/8
STM32F303x6/8
STM32F301x6/8
STM32F030x4,
STM32F358xC
STM32F070xB
STM32F030xC
STM32F398xE
STM32F030x6
STM32F030x8
STM32F070x6
STM32F328x8
STM32F334xx
STM32F318x8
STM32F745xx
STM32F746xx
STM32F756xx
STM32L4x1 /
STM32L4x2 /
STM32L4x3 /
STM32L4x5 /
STM32F37xx
Port
STM32F03x
STM32F05x
STM32F04x
STM32F04x
STM32F07x
STM32F09x
STM32L0x1
STM32L0x2
STM32L0x3
STM32L4x6
USART 1 X X X X X X X X X X X X X X X X X X X X X X X
USART 2 0 - - X X 0 - - X X X X - X - - X X X X X X X
USART 3 0 0 0 - - 0 0 0 - X X X - X - - X X X 0 0 0 X
USART 4 0 0 0 - - 0 0 0 - - - - 0 - 0 0 0 X X - - - X
DocID029725 Rev 1
USART 5 0 0 0 0 0 0 0 0 0 - - - 0 - 0 0 0 X X - - - X
USART 6 0 0 0 0 0 0 0 0 0 - - 0 0 0 0 0 0 X X 0 0 0 0
USART 7 0 0 0 0 0 0 0 0 0 - - 0 0 0 0 0 0 X X 0 0 0 0
USART 8 0 0 0 0 0 0 0 0 0 - - 0 0 0 0 0 0 X X 0 0 0 0
1. X: supported
2. -: not supported
3. 0: USART instance not available
AN4908
AN4908 Hardware automatic baud rate detection
The received character can be a character starting
with a bit at 1. In this case, the USART measures
0
the duration of the start bit (falling edge to rising 6WDUWELW 6WRSELW
edge). 7UDQVLWLRQIURPWR
06Y
Any character starting with a 10xx pattern. In this
case, the USART measures the duration of the start
1 and of the 1st data bit. The duration is measured
from falling edge to falling edge, ensuring better 6WDUWELW 6WRSELW
accuracy in the case of slow signal slopes.
06Y
A 0x55 character frame.
In this case, the baud rate is updated first at the end
3 of the start bit, then at the end of bit and finally at the
end of bit 6. In parallel, another check is performed 6WRSELW
6WDUWELW
for each intermediate transition of RX line.
06Y
Before activating the automatic baud rate detection, one of the ABR modes must be
selected through the ABRMOD[1:0] field in the USARTx_CR2 register. In all ABR modes,
the baud rate is measured several times during the synchronization data reception, and is
compared each time to the previous measurement.
Note: In 7-bit data length mode, 0x7F and 0x55 frames detection ABR modes are not supported.
Where:
• desired baud rate is fixed by the transmitter device
• actual baud rate is the baud rate determined by the USART receiver using the
automatic baud rate detection operation.
When hardware auto baud rate detection is not supported, the software approach described
in this section can be adopted.
The idea of the software approach is to send a 0x7F data frame to the USARTx_RX pin.
This is connected to the EXTI line, which is configured to generate an interrupt on each
rising edge.
The duration of the interval between the two rising edges is measured using the Systick
timer. This duration corresponds to the duration of 8 bits, so
• bit time = calculated duration / 8
• baud rate = 1/bit time
The USARTx_BRR register is then programmed, based on the calculated baud rate value.
6WRSELW
6WDUWELW
)LUVWULVLQJHGJH 6HFRQGULVLQJHGJH
06Y
The STM32F303xD/E embedding the hardware automatic baud rate feature, is used for this
set-up example.
The HyperTerminal PC application is used to send and receive data frames to and from the
STM32F303. Consequently, standard baud rates in the range 600 bits/s to 115200 bits/s are
tested.The highest baud-rate value that can be reached (9 Mbits/s) is tested using another
STM32F3 device as a transmitter.
//UartHandle.AdvancedInit.AutoBaudRateMode =
UART_ADVFEATURE_AUTOBAUDRATE_ONSTARTBIT;
//UartHandle.AdvancedInit.AutoBaudRateMode =
UART_ADVFEATURE_AUTOBAUDRATE_ONFALLINGEDGE;
//UartHandle.AdvancedInit.AutoBaudRateMode =
UART_ADVFEATURE_AUTOBAUDRATE_ON0X7FFRAME;
//UartHandle.AdvancedInit.AutoBaudRateMode =
UART_ADVFEATURE_AUTOBAUDRATE_ON0X55FRAME;
if (HAL_UART_Init(&UartHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
Once the whole initialization is complete, the USART waits until data is received from the
HyperTerminal before launching the automatic baud rate detection phase. The end of this
phase is monitored by the ABRF flag.
• If the auto baud rate operation is unsuccessful, the ABRE flag is set
• If the auto baud rate operation is completed successfully, an acknowledgment data is
transmitted to the HyperTerminal.
void EXTI1_IRQHandler()
{
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_1);
if(temp==0)
{
HAL_SYSTICK_Config(0xFFFFFF);
temp++;
External line 1 interrupt }
request: else
First rising: temp=0 start the
{
systick timer
SysTick->CTRL &= SysTick_Counter_Disable;
Second rising:
– disable the systick counter /* Stop the Timer and get the encoding time */
– get the encoding time GETMYTIME(&stop_time_val);
– clear the SysTick counter /* Clear the SysTick Counter */
SysTick->VAL = SysTick_Counter_Clear;
/* Clear the temp flag*/
temp=0;
/*end of interrupt*/
interrupt_flag=1;
}
}
#define SysTick_Counter_Disable ((uint32_t)0xFFFFFFFE)
#define SysTick_Counter_Enable ((uint32_t)0x00000001)
Required project defines
#define SysTick_Counter_Clear ((uint32_t)0x00000000)
#define GETMYTIME(_t) (*_t=SysTick->VAL)
(UURU
0RGH 0RGH 0RGH 0RGH
$XWREDXGUDWHPRGHV
06Y
Figure 3. ABR error comparison (fCK = HSI clock ,Mode 2 for HW detection)
(UURU
%DXGUDWH
6:HUURU
+:HUURU
06Y
Figure 4 shows that in most cases the hardware approach provides better results than the
software approach. The software approach nevertheless provides good results that are in
some cases comparable to those obtained using the hardware approach.
(UURU
%DXGUDWH
6:HUURU
+:HUURU
06Y
ĂƵĚƌĂƚĞ
ϭϬ
ϵ
ϴ
ϳ
ϲ
ϱ
ϰ
ϯ
Ϯ
ϭ
Ϭ
,ĂƌĚǁĂƌĞ
^ŽĨƚǁĂƌĞ
;ZDŽĚĞϮͿ
ĂƵĚͲƌĂƚĞĚĞƚĞĐƚŝŽŶŵĞƚŚŽĚƐ
06Y
4 Conclusion
This application note describes the hardware automatic baud rate detection feature
embedded in some STM32 devices. It also provides a technique for implementing this
feature in software, as a solution for STM32 devices not implementing this feature in
hardware.
Although the automatic baud rate detection is applied at the beginning of the examples, it
could be extended and used whenever the transmitter and receiver devices detect
communication errors. This allows a robust application where the host varies its baud rate
between communications.
5 Revision history
STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and
improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on
ST products before placing orders. ST products are sold pursuant to ST’s terms and conditions of sale in place at the time of order
acknowledgement.
Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or
the design of Purchasers’ products.
Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product.
ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners.
Information in this document supersedes and replaces information previously supplied in any prior versions of this document.