forked from M-Labs/zynq-rs
uart: use div_round_closest in baud_rate_gen
This commit is contained in:
parent
7428fec200
commit
15883293ac
|
@ -5,6 +5,10 @@ const BDIV_MIN: u8 = 4;
|
||||||
const BDIV_MAX: u8 = 255;
|
const BDIV_MAX: u8 = 255;
|
||||||
const CD_MAX: u16 = 65535;
|
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
|
/// Algorithm as in the Linux 5.1 driver
|
||||||
pub fn configure(regs: &RegisterBlock, mut clk: u32, baud: u32) {
|
pub fn configure(regs: &RegisterBlock, mut clk: u32, baud: u32) {
|
||||||
if regs.mode.read().clks() {
|
if regs.mode.read().clks() {
|
||||||
|
@ -14,7 +18,7 @@ pub fn configure(regs: &RegisterBlock, mut clk: u32, baud: u32) {
|
||||||
let mut best = None;
|
let mut best = None;
|
||||||
for bdiv in BDIV_MIN..=BDIV_MAX {
|
for bdiv in BDIV_MIN..=BDIV_MAX {
|
||||||
let bdiv: u32 = bdiv.into();
|
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() {
|
if cd < 1 || cd > CD_MAX.into() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue