RTEMS | stm32f4/usart: fix BRR calculation logical issues (!1143)
Mohamed Ayman (@mohamedayman23)
gitlab at rtems.org
Tue Mar 17 03:07:21 UTC 2026
Mohamed Ayman created a merge request: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/1143
Project:Branches: mohamedayman23/rtems:fix-USART-mathematical-bug-stm32f4 to rtems/rtos/rtems:main
Author: Mohamed Ayman
## Summary
stm32f4/usart: fix BRR calculation logical issues
The USART BRR calculation in usart_get_bbr() contains two issues that can lead to incorrect baud rate configuration.
1. Incorrect fractional divisor formula
{width=900 height=488}
The current implementation computes the fractional part as:
the **important part**: USARTDIV = mantissa + fraction / a
Putting them together From:
USARTDIV = mantissa + fraction / a
and: USARTDIV = pclk / (a × baud)
* multiply both sides by `a`: pclk / baud = a × mantissa + fraction
* solve for `fraction`: fraction = (pclk / baud) - a × mantissa
* convert to integer-safe form: fraction = (pclk - baud × a × mantissa) / baud
This implementation is both mathematically correct and avoids **division by zero**.
2. Incorrect mantissa update for high approximation
When computing the higher approximation of USARTDIV, the current code assigns: div_mantissa_high = div_fraction_low;
This appears to be a copy-paste error. The mantissa must be derived from div_mantissa_low, not from the fractional part.
As written, this produces completely invalid divisor values (mixing mantissa and fraction domains), which can lead to large baud rate errors.
This properly computes the next representable USARTDIV value.
Together, these fixes ensure that BRR values are computed correctly, avoid undefined behavior, and prevent potential baud rate misconfiguration.
##
AI usage: yes\
Prompt used: "why the baud rate is missed in the main equation, make a lot of computations with random numbers and give me the the predicted output and the actual one"\
AI model: claude sonnet 4.6\
How AI was used: for extensive computations and checking the accuracy
--
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/1143
You're receiving this email because of your account on gitlab.rtems.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20260317/68c1fc7f/attachment.htm>
More information about the bugs
mailing list