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/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 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..f750955 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(); //! @@ -44,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; diff --git a/rp2040-hal/src/uart/utils.rs b/rp2040-hal/src/uart/utils.rs index 508207d..f2eb9e7 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 const 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