Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faulty UART baudrate divisor formula #1774

Closed
KisImre opened this issue Jul 23, 2024 · 1 comment
Closed

Faulty UART baudrate divisor formula #1774

KisImre opened this issue Jul 23, 2024 · 1 comment
Assignees
Milestone

Comments

@KisImre
Copy link

KisImre commented Jul 23, 2024

The fractional part of the UART baudrate divisor can overflow for certain baudrates.

baudrate=57861 ibrd=135 fbrd=1
baudrate=57868 ibrd=135 fbrd=0
baudrate=57871 ibrd=134 fbrd=64 <----
baudrate=57874 ibrd=134 fbrd=63
baudrate=57881 ibrd=134 fbrd=62

The used formula results in a fractional divisor value of 64 which gets truncated once it's written into the register, so the effective fractional value will be zero.

The issue is in this line. baud_fbrd = ((baud_rate_div & 0x7f) + 1) / 2 The recommended +0.5 correction is only added to the fractional part but the overflowing carry bit is not added to the integer part. Adding 1 and dividing by 2 before splitting into integer and fractional parts would solve the issue.

I've tested the issue on a Raspberry Pi Pico board using the hello_uart example and changing the baudrate in line 15. I've measured the actual baudrate (with ~10Hz precision) using a logic analyzer and I got the following results:

baudrate=57861 -> 57870
baudrate=57868 -> 57870
baudrate=57871 -> 58300 <----
baudrate=57874 -> 57870
baudrate=57881 -> 57870

The issue occurs for each baudrate where the integer divisor is about to change, or more precisely, where (baud_rate_div & 0x7f) == 0x7f. The baudrate error will be around 1 / ibrd.

Note: Originally I found this issue in rp-hal where they've pointed out that their implementation was inspired by pico-sdk so it might by also affected.

@kilograham kilograham self-assigned this Jul 23, 2024
@kilograham kilograham modified the milestones: 1.6.1, 1.6.2 Jul 23, 2024
@kilograham kilograham modified the milestones: 1.6.2, 2.0.0 Aug 8, 2024
@kilograham
Copy link
Contributor

fixed thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants