From c1e52a70bf6549ad68e663402263423e7701e6d8 Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Date: Wed, 29 May 2024 11:37:57 +0200 Subject: [PATCH] Add UartTx/Rx constructors --- esp-hal/src/uart.rs | 86 ++++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 33 deletions(-) diff --git a/esp-hal/src/uart.rs b/esp-hal/src/uart.rs index 0780028e6d7..950e77991e8 100644 --- a/esp-hal/src/uart.rs +++ b/esp-hal/src/uart.rs @@ -344,6 +344,30 @@ where } } +impl<'d, T> UartTx<'d, T, Blocking> +where + T: Instance + 'd, +{ + /// Create a new UART TX instance in [`Blocking`] mode. + /// + /// Use [crate::gpio::NO_PIN] for the `rts` parameter if RTS is not used. + pub fn new( + _uart: impl Peripheral

+ 'd, + tx: impl Peripheral

+ 'd, + rts: Option + 'd>, + ) -> Self { + crate::into_ref!(tx); + tx.set_to_push_pull_output(crate::private::Internal); + tx.connect_peripheral_to_output(T::tx_signal(), crate::private::Internal); + if let Some(rts) = rts { + crate::into_ref!(rts); + rts.set_to_push_pull_output(crate::private::Internal); + rts.connect_peripheral_to_output(T::rts_signal(), crate::private::Internal); + } + Self::new_inner() + } +} + impl<'d, T, M> UartRx<'d, T, M> where T: Instance, @@ -402,6 +426,30 @@ where } } +impl<'d, T> UartRx<'d, T, Blocking> +where + T: Instance + 'd, +{ + /// Create a new UART RX instance in [`Blocking`] mode. + /// + /// Use [crate::gpio::NO_PIN] for the `cts` parameter if CTS is not used. + pub fn new( + _uart: impl Peripheral

+ 'd, + rx: impl Peripheral

+ 'd, + cts: Option + 'd>, + ) -> Self { + crate::into_ref!(rx); + rx.set_to_input(crate::private::Internal); + rx.connect_input_to_peripheral(T::rx_signal(), crate::private::Internal); + if let Some(cts) = cts { + crate::into_ref!(cts); + cts.set_to_input(crate::private::Internal); + cts.connect_input_to_peripheral(T::cts_signal(), crate::private::Internal); + } + Self::new_inner() + } +} + impl<'d, T> Uart<'d, T, Blocking> where T: Instance + 'd, @@ -438,49 +486,21 @@ where self } - /// Configure TX and RX pins - pub fn with_pins( - self, + /// Create a new UART instance with defaults in [`Blocking`] mode. + pub fn new( + uart: impl Peripheral

+ 'd, + clocks: &Clocks, tx: impl Peripheral

+ 'd, rx: impl Peripheral

+ 'd, - cts: impl Peripheral

+ 'd, - rts: impl Peripheral

+ 'd, ) -> Self { crate::into_ref!(tx); crate::into_ref!(rx); - crate::into_ref!(cts); - crate::into_ref!(rts); - - tx.set_to_push_pull_output(crate::private::Internal); - tx.connect_peripheral_to_output(T::tx_signal(), crate::private::Internal); - - rx.set_to_input(crate::private::Internal); - rx.connect_input_to_peripheral(T::rx_signal(), crate::private::Internal); - - cts.set_to_input(crate::private::Internal); - cts.connect_input_to_peripheral(T::cts_signal(), crate::private::Internal); - - rts.set_to_push_pull_output(crate::private::Internal); - rts.connect_peripheral_to_output(T::rts_signal(), crate::private::Internal); - self - } - - /// Configure TX pin - pub fn with_tx(self, tx: impl Peripheral

+ 'd) -> Self { - crate::into_ref!(tx); tx.set_to_push_pull_output(crate::private::Internal); tx.connect_peripheral_to_output(T::tx_signal(), crate::private::Internal); - self - } - - /// Configure RX pin - pub fn with_rx(self, rx: impl Peripheral

+ 'd) -> Self { - crate::into_ref!(rx); rx.set_to_input(crate::private::Internal); rx.connect_input_to_peripheral(T::rx_signal(), crate::private::Internal); - - self + Self::new_inner(uart, clocks) } /// Configure CTS pin