From 7172dfd08379a3732372ada61e25d1c5678fa59f Mon Sep 17 00:00:00 2001 From: Andres Hurtado Lopez Date: Sun, 26 Feb 2023 19:14:25 -0500 Subject: [PATCH] RP-PICO UART adding set_baudrate: refactoring of methods --- embassy-rp/src/uart/mod.rs | 51 +++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index 7540052b..f9cce5c6 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs @@ -324,25 +324,6 @@ 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( tx: Option>, rx: Option>, @@ -369,11 +350,7 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { pin.pad_ctrl().write(|w| w.set_ie(true)); } - let (baud_ibrd, baud_fbrd) = Uart::::baudrate_calculations(config.baudrate); - - // 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)); + Uart::::set_baudrate_inner(config.baudrate); let (pen, eps) = match config.parity { Parity::ParityNone => (false, false), @@ -408,20 +385,38 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { } } + + /// sets baudrate on runtime pub fn set_baudrate(&mut self, baudrate: u32) { - + Self::set_baudrate_inner(baudrate); + } + + + fn set_baudrate_inner(baudrate: u32) { let r = T::regs(); - let (baud_ibrd, baud_fbrd) = Self::baudrate_calculations(baudrate); - + 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; + } + 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> {