File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -1454,12 +1454,18 @@ macro_rules! halUsartImpl {
1454
1454
let ( over8, div) = if ( pclk_freq / 16 ) >= baud {
1455
1455
// We have the ability to oversample to 16 bits, take
1456
1456
// 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;
1458
1462
( false , div)
1459
1463
} else if ( pclk_freq / 8 ) >= baud {
1460
1464
// We are close enough to pclk where we can only
1461
1465
// 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;
1463
1469
1464
1470
// Ensure the the fractional bits (only 3) are
1465
1471
// right-aligned.
You can’t perform that action at this time.
0 commit comments