From 0a86dad34ccc99c339138997035bae90257ad2c1 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 15 Oct 2022 11:53:16 +0000 Subject: [PATCH 1/5] Implement UartConfig::new constructor method --- rp2040-hal/src/uart/utils.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/uart/utils.rs b/rp2040-hal/src/uart/utils.rs index 508207d..094096d 100644 --- a/rp2040-hal/src/uart/utils.rs +++ b/rp2040-hal/src/uart/utils.rs @@ -1,8 +1,7 @@ -use fugit::HertzU32; - use crate::pac::{uart0::RegisterBlock, UART0, UART1}; use crate::resets::SubsystemReset; use core::ops::Deref; +use fugit::HertzU32; /// Error type for UART operations. #[derive(Debug)] @@ -84,6 +83,23 @@ pub struct UartConfig { pub parity: Option, } +impl UartConfig { + /// Create a new instance of UartConfig + pub fn new( + baudrate: HertzU32, + data_bits: DataBits, + parity: Option, + stop_bits: StopBits, + ) -> UartConfig { + UartConfig { + baudrate, + data_bits, + stop_bits, + parity, + } + } +} + /// Rx/Tx FIFO Watermark /// /// Determine the FIFO level that trigger DMA/Interrupt From b81ad6f599b9b0985e74117ad9388f3d35182bd7 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 15 Oct 2022 12:50:30 +0000 Subject: [PATCH 2/5] Use UartConfig::new constructor instead of common_configs in examples --- boards/rp-pico/examples/pico_i2c_pio.rs | 5 ++++- boards/rp-pico/examples/pico_uart_irq_buffer.rs | 8 +++++++- boards/rp-pico/examples/pico_uart_irq_echo.rs | 8 +++++++- rp2040-hal/examples/adc.rs | 6 +++++- rp2040-hal/examples/rom_funcs.rs | 6 +++++- rp2040-hal/examples/uart.rs | 6 +++++- rp2040-hal/src/uart/mod.rs | 5 +++-- 7 files changed, 36 insertions(+), 8 deletions(-) diff --git a/boards/rp-pico/examples/pico_i2c_pio.rs b/boards/rp-pico/examples/pico_i2c_pio.rs index dad70a8..0ee03b0 100644 --- a/boards/rp-pico/examples/pico_i2c_pio.rs +++ b/boards/rp-pico/examples/pico_i2c_pio.rs @@ -38,6 +38,9 @@ use rp_pico::hal::pac; // higher-level drivers. use rp_pico::hal; +// UART related types +use hal::uart::{DataBits, StopBits, UartConfig}; + /// Prints the temperature received from the sensor fn print_temperature(serial: &mut impl FmtWrite, temp: [u8; 2]) { let temp_i16 = i16::from_be_bytes(temp) >> 5; @@ -97,7 +100,7 @@ fn main() -> ! { let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) .enable( - hal::uart::common_configs::_115200_8_N_1, + UartConfig::new(115_200.Hz(), DataBits::Eight, None, StopBits::One), clocks.peripheral_clock.freq(), ) .unwrap(); diff --git a/boards/rp-pico/examples/pico_uart_irq_buffer.rs b/boards/rp-pico/examples/pico_uart_irq_buffer.rs index f8114c9..685f9f8 100644 --- a/boards/rp-pico/examples/pico_uart_irq_buffer.rs +++ b/boards/rp-pico/examples/pico_uart_irq_buffer.rs @@ -30,6 +30,9 @@ use rp2040_hal::Clock; // The macro for our start-up function use rp_pico::entry; +// Time handling traits +use fugit::RateExtU32; + // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; @@ -52,6 +55,9 @@ use heapless::spsc::Queue; /// Import the GPIO pins we use use hal::gpio::pin::bank0::{Gpio0, Gpio1}; +// UART related types +use hal::uart::{DataBits, StopBits, UartConfig}; + /// Alias the type for our UART pins to make things clearer. type UartPins = ( hal::gpio::Pin>, @@ -134,7 +140,7 @@ fn main() -> ! { // Make a UART on the given pins let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) .enable( - hal::uart::common_configs::_9600_8_N_1, + UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One), clocks.peripheral_clock.freq(), ) .unwrap(); diff --git a/boards/rp-pico/examples/pico_uart_irq_echo.rs b/boards/rp-pico/examples/pico_uart_irq_echo.rs index 8de2206..6835d22 100644 --- a/boards/rp-pico/examples/pico_uart_irq_echo.rs +++ b/boards/rp-pico/examples/pico_uart_irq_echo.rs @@ -28,6 +28,9 @@ use rp2040_hal::Clock; // The macro for our start-up function use rp_pico::entry; +// Time handling traits +use fugit::RateExtU32; + // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use panic_halt as _; @@ -49,6 +52,9 @@ use critical_section::Mutex; /// Import the GPIO pins we use use hal::gpio::pin::bank0::{Gpio0, Gpio1}; +// UART related types +use hal::uart::{DataBits, StopBits, UartConfig}; + /// Alias the type for our UART pins to make things clearer. type UartPins = ( hal::gpio::Pin>, @@ -118,7 +124,7 @@ fn main() -> ! { // Make a UART on the given pins let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) .enable( - hal::uart::common_configs::_9600_8_N_1, + UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One), clocks.peripheral_clock.freq(), ) .unwrap(); diff --git a/rp2040-hal/examples/adc.rs b/rp2040-hal/examples/adc.rs index a9ddae4..38530eb 100644 --- a/rp2040-hal/examples/adc.rs +++ b/rp2040-hal/examples/adc.rs @@ -20,8 +20,12 @@ use rp2040_hal as hal; // Some traits we need use core::fmt::Write; use embedded_hal::adc::OneShot; +use fugit::RateExtU32; use rp2040_hal::Clock; +// UART related types +use hal::uart::{DataBits, StopBits, UartConfig}; + // A shorter alias for the Peripheral Access Crate, which provides low-level // register access use hal::pac; @@ -91,7 +95,7 @@ fn main() -> ! { // Create a UART driver let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) .enable( - hal::uart::common_configs::_9600_8_N_1, + UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One), clocks.peripheral_clock.freq(), ) .unwrap(); diff --git a/rp2040-hal/examples/rom_funcs.rs b/rp2040-hal/examples/rom_funcs.rs index c801aba..281025d 100644 --- a/rp2040-hal/examples/rom_funcs.rs +++ b/rp2040-hal/examples/rom_funcs.rs @@ -22,8 +22,12 @@ use hal::pac; // Some traits we need use core::fmt::Write; +use fugit::RateExtU32; use hal::Clock; +// UART related types +use hal::uart::{DataBits, StopBits, UartConfig}; + /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. /// Note: This boot block is not necessary when using a rp-hal based BSP @@ -88,7 +92,7 @@ fn main() -> ! { ); let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) .enable( - hal::uart::common_configs::_9600_8_N_1, + UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One), clocks.peripheral_clock.freq(), ) .unwrap(); diff --git a/rp2040-hal/examples/uart.rs b/rp2040-hal/examples/uart.rs index 56de75e..ec4d010 100644 --- a/rp2040-hal/examples/uart.rs +++ b/rp2040-hal/examples/uart.rs @@ -24,8 +24,12 @@ use hal::pac; // Some traits we need use core::fmt::Write; +use fugit::RateExtU32; use rp2040_hal::clocks::Clock; +// UART related types +use hal::uart::{DataBits, StopBits, UartConfig}; + /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. /// Note: This boot block is not necessary when using a rp-hal based BSP @@ -88,7 +92,7 @@ fn main() -> ! { ); let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS) .enable( - hal::uart::common_configs::_9600_8_N_1, + UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One), clocks.peripheral_clock.freq(), ) .unwrap(); diff --git a/rp2040-hal/src/uart/mod.rs b/rp2040-hal/src/uart/mod.rs index d0ddc57..fb3aa45 100644 --- a/rp2040-hal/src/uart/mod.rs +++ b/rp2040-hal/src/uart/mod.rs @@ -6,7 +6,8 @@ //! //! See [examples/uart.rs](https://github.com/rp-rs/rp-hal/tree/main/rp2040-hal/examples/uart.rs) for a more complete example //! ```no_run -//! use rp2040_hal::{Clock, clocks::init_clocks_and_plls, gpio::{Pins, FunctionUart}, pac, sio::Sio, uart::{self, UartPeripheral}, watchdog::Watchdog}; +//! use rp2040_hal::{Clock, clocks::init_clocks_and_plls, gpio::{Pins, FunctionUart}, pac, sio::Sio, uart::{self, DataBits, StopBits, UartConfig, UartPeripheral}, watchdog::Watchdog}; +//! use fugit::RateExtU32; //! //! const XOSC_CRYSTAL_FREQ: u32 = 12_000_000; // Typically found in BSP crates //! @@ -24,7 +25,7 @@ //! // Need to perform clock init before using UART or it will freeze. //! let uart = UartPeripheral::new(peripherals.UART0, pins, &mut peripherals.RESETS) //! .enable( -//! uart::common_configs::_9600_8_N_1, +//! UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One), //! clocks.peripheral_clock.freq(), //! ).unwrap(); //! From fb7fba03ad86e94aa74a5cf8e55665ef0a983c07 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 15 Oct 2022 12:55:46 +0000 Subject: [PATCH 3/5] Add deprecation attribute to common_configs --- rp2040-hal/src/uart/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rp2040-hal/src/uart/mod.rs b/rp2040-hal/src/uart/mod.rs index fb3aa45..f750955 100644 --- a/rp2040-hal/src/uart/mod.rs +++ b/rp2040-hal/src/uart/mod.rs @@ -45,4 +45,5 @@ pub use self::utils::*; pub use self::writer::Writer; /// Common configurations for UART. +#[deprecated(note = "Use UartConfig::new(...) instead.")] pub mod common_configs; From e9428c3f57a14f9aa6d0bf3097bb308e42e86205 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 15 Oct 2022 13:02:06 +0000 Subject: [PATCH 4/5] Add CHANGELOG entries --- rp2040-hal/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rp2040-hal/CHANGELOG.md b/rp2040-hal/CHANGELOG.md index d94944a..1ab97bf 100644 --- a/rp2040-hal/CHANGELOG.md +++ b/rp2040-hal/CHANGELOG.md @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Update embedded-hal alpha support to version 1.0.0-alpha.9 - @jannic (Non-blocking traits were moved to embedded-hal-nb, which is not yet supported) +- Implement UartConfig::new constructor method - @jannic +- Deprecate uart::common_configs - @jannic ## [0.6.0] - 2022-08-26 From d8a0c6461842bcee3389c3e8e98a3a1a038d2868 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 15 Oct 2022 17:34:13 +0000 Subject: [PATCH 5/5] Make UartConfig::new(...) const --- rp2040-hal/src/uart/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rp2040-hal/src/uart/utils.rs b/rp2040-hal/src/uart/utils.rs index 094096d..f2eb9e7 100644 --- a/rp2040-hal/src/uart/utils.rs +++ b/rp2040-hal/src/uart/utils.rs @@ -85,7 +85,7 @@ pub struct UartConfig { impl UartConfig { /// Create a new instance of UartConfig - pub fn new( + pub const fn new( baudrate: HertzU32, data_bits: DataBits, parity: Option,