From 8fb380b18052c2393ae1dc3466bb87e9402181d8 Mon Sep 17 00:00:00 2001 From: Andres Hurtado Lopez Date: Sun, 26 Feb 2023 18:40:23 -0500 Subject: [PATCH 1/4] RP-PICO UART adding set_baudrate --- embassy-rp/src/uart/mod.rs | 50 +++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index bbbf97c0..7540052b 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs @@ -296,7 +296,7 @@ impl<'d, T: Instance> Uart<'d, T, Async> { Some(rx_dma.map_into()), config, ) - } + } } impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { @@ -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( tx: Option>, rx: Option>, @@ -350,19 +369,7 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { pin.pad_ctrl().write(|w| w.set_ie(true)); } - let clk_base = crate::clocks::clk_peri_freq(); - - 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; - } + 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)); @@ -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> { From 7172dfd08379a3732372ada61e25d1c5678fa59f Mon Sep 17 00:00:00 2001 From: Andres Hurtado Lopez Date: Sun, 26 Feb 2023 19:14:25 -0500 Subject: [PATCH 2/4] 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> { From 482ba835c4eec6d3bb006eea3a715360c170e418 Mon Sep 17 00:00:00 2001 From: Andres Hurtado Lopez Date: Sun, 26 Feb 2023 19:20:08 -0500 Subject: [PATCH 3/4] RP-PICO UART adding set_baudrate: Changing static call from specific type to a Self (requires adding lifetime specifier) --- embassy-rp/src/uart/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index f9cce5c6..6b3e2406 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs @@ -299,7 +299,7 @@ impl<'d, T: Instance> Uart<'d, T, Async> { } } -impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { +impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { fn new_inner( _uart: impl Peripheral

+ 'd, mut tx: PeripheralRef<'d, AnyPin>, @@ -350,7 +350,7 @@ impl<'d, T: Instance, M: Mode> Uart<'d, T, M> { pin.pad_ctrl().write(|w| w.set_ie(true)); } - Uart::::set_baudrate_inner(config.baudrate); + Self::set_baudrate_inner(config.baudrate); let (pen, eps) = match config.parity { Parity::ParityNone => (false, false), From 2331d58aa667d31ce74a2e10582a93b710c2aef7 Mon Sep 17 00:00:00 2001 From: Andres Hurtado Lopez Date: Sun, 26 Feb 2023 21:23:51 -0500 Subject: [PATCH 4/4] RP-PICO UART adding set_baudrate: missing to run rust-fmt --- embassy-rp/src/uart/mod.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/embassy-rp/src/uart/mod.rs b/embassy-rp/src/uart/mod.rs index 6b3e2406..42b3671a 100644 --- a/embassy-rp/src/uart/mod.rs +++ b/embassy-rp/src/uart/mod.rs @@ -296,7 +296,7 @@ impl<'d, T: Instance> Uart<'d, T, Async> { Some(rx_dma.map_into()), config, ) - } + } } impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { @@ -350,7 +350,7 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { pin.pad_ctrl().write(|w| w.set_ie(true)); } - Self::set_baudrate_inner(config.baudrate); + Self::set_baudrate_inner(config.baudrate); let (pen, eps) = match config.parity { Parity::ParityNone => (false, false), @@ -385,22 +385,20 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { } } - /// sets baudrate on runtime pub fn set_baudrate(&mut self, baudrate: u32) { - Self::set_baudrate_inner(baudrate); + Self::set_baudrate_inner(baudrate); } - fn set_baudrate_inner(baudrate: u32) { - let r = T::regs(); - + let r = T::regs(); + 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; @@ -408,15 +406,13 @@ impl<'d, T: Instance + 'd, M: Mode> Uart<'d, T, M> { baud_ibrd = 65535; baud_fbrd = 0; } - + unsafe { - - // Load PL011's baud divisor registers + // 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> {