Skip to content

Commit 4bb360a

Browse files
author
James Munns
committed
Restore rounding
1 parent 1371587 commit 4bb360a

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/serial.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,12 +1454,18 @@ macro_rules! halUsartImpl {
14541454
let (over8, div) = if (pclk_freq / 16) >= baud {
14551455
// We have the ability to oversample to 16 bits, take
14561456
// advantage of it.
1457-
let div = pclk_freq / baud;
1457+
//
1458+
// We also add `baud / 2` to the `pclk_freq` to ensure
1459+
// rounding of values to the closest scale, rather than the
1460+
// floored behavior of normal integer division.
1461+
let div = (pclk_freq + (baud / 2)) / baud;
14581462
(false, div)
14591463
} else if (pclk_freq / 8) >= baud {
14601464
// We are close enough to pclk where we can only
14611465
// oversample 8.
1462-
let div = (pclk_freq * 2) / baud;
1466+
//
1467+
// See note above regarding `baud` and rounding.
1468+
let div = ((pclk_freq * 2) + (baud / 2)) / baud;
14631469

14641470
// Ensure the the fractional bits (only 3) are
14651471
// right-aligned.

0 commit comments

Comments
 (0)