From 26fa532fa34f9b7a261a78001a804280a009b37f Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Thu, 2 Dec 2021 21:33:21 +0000 Subject: [PATCH 1/2] Refactor Uart initialization --- boards/pico/examples/pico_i2c_pio.rs | 13 ++++++------- rp2040-hal/examples/adc.rs | 13 ++++++------- rp2040-hal/examples/rom_funcs.rs | 13 ++++++------- rp2040-hal/examples/uart.rs | 13 ++++++------- rp2040-hal/src/uart.rs | 26 ++++++++++++++++++-------- 5 files changed, 42 insertions(+), 36 deletions(-) diff --git a/boards/pico/examples/pico_i2c_pio.rs b/boards/pico/examples/pico_i2c_pio.rs index 43db613..8b2f2e9 100644 --- a/boards/pico/examples/pico_i2c_pio.rs +++ b/boards/pico/examples/pico_i2c_pio.rs @@ -94,13 +94,12 @@ fn main() -> ! { &mut pac.RESETS, ); - let mut uart = hal::uart::UartPeripheral::<_, _>::enable( - pac.UART0, - &mut pac.RESETS, - hal::uart::common_configs::_115200_8_N_1, - clocks.peripheral_clock.into(), - ) - .unwrap(); + let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS) + .enable( + hal::uart::common_configs::_115200_8_N_1, + clocks.peripheral_clock.into(), + ) + .unwrap(); // UART TX (characters sent from RP2040) on pin 1 (GPIO0) let _tx_pin = pins.gpio0.into_mode::(); diff --git a/rp2040-hal/examples/adc.rs b/rp2040-hal/examples/adc.rs index 5a826f2..13000a6 100644 --- a/rp2040-hal/examples/adc.rs +++ b/rp2040-hal/examples/adc.rs @@ -85,13 +85,12 @@ fn main() -> ! { ); // Create a UART driver - let mut uart = hal::uart::UartPeripheral::<_, _>::enable( - pac.UART0, - &mut pac.RESETS, - hal::uart::common_configs::_9600_8_N_1, - clocks.peripheral_clock.into(), - ) - .unwrap(); + let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS) + .enable( + hal::uart::common_configs::_9600_8_N_1, + clocks.peripheral_clock.into(), + ) + .unwrap(); // UART TX (characters sent from pico) on pin 1 (GPIO0) and RX (on pin 2 (GPIO1) let _tx_pin = pins.gpio0.into_mode::(); diff --git a/rp2040-hal/examples/rom_funcs.rs b/rp2040-hal/examples/rom_funcs.rs index c9a6ebe..98ea40a 100644 --- a/rp2040-hal/examples/rom_funcs.rs +++ b/rp2040-hal/examples/rom_funcs.rs @@ -80,13 +80,12 @@ fn main() -> ! { &mut pac.RESETS, ); - let mut uart = hal::uart::UartPeripheral::<_, _>::enable( - pac.UART0, - &mut pac.RESETS, - hal::uart::common_configs::_9600_8_N_1, - clocks.peripheral_clock.into(), - ) - .unwrap(); + let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS) + .enable( + hal::uart::common_configs::_9600_8_N_1, + clocks.peripheral_clock.into(), + ) + .unwrap(); // UART TX (characters sent from RP2040) on pin 1 (GPIO0) let _tx_pin = pins.gpio0.into_mode::(); diff --git a/rp2040-hal/examples/uart.rs b/rp2040-hal/examples/uart.rs index 542c661..2bfba06 100644 --- a/rp2040-hal/examples/uart.rs +++ b/rp2040-hal/examples/uart.rs @@ -82,13 +82,12 @@ fn main() -> ! { &mut pac.RESETS, ); - let mut uart = hal::uart::UartPeripheral::<_, _>::enable( - pac.UART0, - &mut pac.RESETS, - hal::uart::common_configs::_9600_8_N_1, - clocks.peripheral_clock.into(), - ) - .unwrap(); + let mut uart = hal::uart::UartPeripheral::<_, _>::new(pac.UART0, &mut pac.RESETS) + .enable( + hal::uart::common_configs::_9600_8_N_1, + clocks.peripheral_clock.into(), + ) + .unwrap(); // UART TX (characters sent from RP2040) on pin 1 (GPIO0) let _tx_pin = pins.gpio0.into_mode::(); diff --git a/rp2040-hal/src/uart.rs b/rp2040-hal/src/uart.rs index 0bd3f49..efbfc9b 100644 --- a/rp2040-hal/src/uart.rs +++ b/rp2040-hal/src/uart.rs @@ -17,12 +17,12 @@ //! let mut clocks = init_clocks_and_plls(XOSC_CRYSTAL_FREQ, peripherals.XOSC, peripherals.CLOCKS, peripherals.PLL_SYS, peripherals.PLL_USB, &mut peripherals.RESETS, &mut watchdog).ok().unwrap(); //! //! // Need to perform clock init before using UART or it will freeze. -//! let uart = UartPeripheral::<_, _>::enable( -//! peripherals.UART0, -//! &mut peripherals.RESETS, +//! let uart = UartPeripheral::<_, _>::new(peripherals.UART0, &mut peripherals.RESETS) +//! .enable( //! uart::common_configs::_9600_8_N_1, //! clocks.peripheral_clock.into(), -//! ).unwrap(); +//! ) +//! .unwrap(); //! //! // Set up UART on GP0 and GP1 (Pico pins 1 and 2) //! let _tx_pin = pins.gpio0.into_mode::(); @@ -222,15 +222,25 @@ impl UartPeripheral { } impl UartPeripheral { + /// Creates an UartPeripheral in Disabled state. + pub fn new(device: D, resets: &mut pac::RESETS) -> UartPeripheral { + device.reset_bring_up(resets); + + UartPeripheral { + device, + config: common_configs::_9600_8_N_1, // placeholder + effective_baudrate: Baud(0), + _state: Disabled, + } + } + /// Enables the provided UART device with the given configuration. pub fn enable( - mut device: D, - resets: &mut pac::RESETS, + self, config: UartConfig, frequency: Hertz, ) -> Result, Error> { - device.reset_bring_up(resets); - + let mut device = self.free(); let effective_baudrate = configure_baudrate(&mut device, &config.baudrate, &frequency)?; device.uartlcr_h.write(|w| { From b380b3ddf69a78269ca6409e6d376a2c7b5a37b6 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sun, 5 Dec 2021 07:05:12 +0000 Subject: [PATCH 2/2] Reset UART in new() --- rp2040-hal/src/uart.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/rp2040-hal/src/uart.rs b/rp2040-hal/src/uart.rs index efbfc9b..e2c5224 100644 --- a/rp2040-hal/src/uart.rs +++ b/rp2040-hal/src/uart.rs @@ -224,6 +224,7 @@ impl UartPeripheral { impl UartPeripheral { /// Creates an UartPeripheral in Disabled state. pub fn new(device: D, resets: &mut pac::RESETS) -> UartPeripheral { + device.reset_bring_down(resets); device.reset_bring_up(resets); UartPeripheral {