uart: use div_round_closest in baud_rate_gen

This commit is contained in:
Astro 2019-05-23 15:37:07 +02:00
parent 7428fec200
commit 15883293ac

View File

@ -5,6 +5,10 @@ const BDIV_MIN: u8 = 4;
const BDIV_MAX: u8 = 255;
const CD_MAX: u16 = 65535;
fn div_round_closest(q: u32, d: u32) -> u32 {
(q + (d / 2)) / d
}
/// Algorithm as in the Linux 5.1 driver
pub fn configure(regs: &RegisterBlock, mut clk: u32, baud: u32) {
if regs.mode.read().clks() {
@ -14,7 +18,7 @@ pub fn configure(regs: &RegisterBlock, mut clk: u32, baud: u32) {
let mut best = None;
for bdiv in BDIV_MIN..=BDIV_MAX {
let bdiv: u32 = bdiv.into();
let cd = clk / (baud * (bdiv + 1));
let cd = div_round_closest(clk, baud * (bdiv + 1));
if cd < 1 || cd > CD_MAX.into() {
continue;
}