RP-PICO UART adding set_baudrate

This commit is contained in:
Andres Hurtado Lopez 2023-02-26 18:40:23 -05:00
parent bc71230cd0
commit 8fb380b180

View File

@ -324,6 +324,25 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> {
} }
} }
fn baudrate_calculations(baudrate: u32) -> (u32, u32) {
let clk_base = crate::clocks::clk_peri_freq();
let baud_rate_div = (8 * clk_base) / baudrate;
let mut baud_ibrd = baud_rate_div >> 7;
let mut baud_fbrd = ((baud_rate_div & 0x7f) + 1) / 2;
if baud_ibrd == 0 {
baud_ibrd = 1;
baud_fbrd = 0;
} else if baud_ibrd >= 65535 {
baud_ibrd = 65535;
baud_fbrd = 0;
}
(baud_ibrd, baud_fbrd)
}
fn init( fn init(
tx: Option<PeripheralRef<'_, AnyPin>>, tx: Option<PeripheralRef<'_, AnyPin>>,
rx: Option<PeripheralRef<'_, AnyPin>>, rx: Option<PeripheralRef<'_, AnyPin>>,
@ -350,19 +369,7 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> {
pin.pad_ctrl().write(|w| w.set_ie(true)); pin.pad_ctrl().write(|w| w.set_ie(true));
} }
let clk_base = crate::clocks::clk_peri_freq(); let (baud_ibrd, baud_fbrd) = Uart::<T,M>::baudrate_calculations(config.baudrate);
let baud_rate_div = (8 * clk_base) / config.baudrate;
let mut baud_ibrd = baud_rate_div >> 7;
let mut baud_fbrd = ((baud_rate_div & 0x7f) + 1) / 2;
if baud_ibrd == 0 {
baud_ibrd = 1;
baud_fbrd = 0;
} else if baud_ibrd >= 65535 {
baud_ibrd = 65535;
baud_fbrd = 0;
}
// Load PL011's baud divisor registers // Load PL011's baud divisor registers
r.uartibrd().write_value(pac::uart::regs::Uartibrd(baud_ibrd)); r.uartibrd().write_value(pac::uart::regs::Uartibrd(baud_ibrd));
@ -400,6 +407,21 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> {
}); });
} }
} }
pub fn set_baudrate(&mut self, baudrate: u32) {
let r = T::regs();
let (baud_ibrd, baud_fbrd) = Self::baudrate_calculations(baudrate);
unsafe {
// Load PL011's baud divisor registers
r.uartibrd().write_value(pac::uart::regs::Uartibrd(baud_ibrd));
r.uartfbrd().write_value(pac::uart::regs::Uartfbrd(baud_fbrd));
}
}
} }
impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { impl<'d, T: Instance, M: Mode> Uart<'d, T, M> {