From afc918f8808ea7ea53da75aeb5a3f39d862ac02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 08:54:01 +0200 Subject: [PATCH 01/19] Reuse enable_iomux_clk_gate --- esp-hal/src/gpio/mod.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index 210b827fd41..5cd2bfc55b9 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -1490,10 +1490,7 @@ macro_rules! rtc_pins { let rtcio = unsafe{ &*RTC_IO::ptr() }; - #[cfg(esp32s3)] - unsafe { $crate::peripherals::SENS::steal() }.sar_peri_clk_gate_conf().modify(|_,w| w.iomux_clk_en().set_bit()); - #[cfg(esp32s2)] - unsafe { $crate::peripherals::SENS::steal() }.sar_io_mux_conf().modify(|_,w| w.iomux_clk_gate_en().set_bit()); + $crate::gpio::enable_iomux_clk_gate(); // disable input paste::paste!{ @@ -1633,13 +1630,18 @@ macro_rules! rtc_pins { #[doc(hidden)] pub fn enable_iomux_clk_gate() { - #[cfg(esp32s2)] - { - use crate::peripherals::SENS; - let sensors = unsafe { &*SENS::ptr() }; - sensors - .sar_io_mux_conf() - .modify(|_, w| w.iomux_clk_gate_en().set_bit()); + cfg_if::cfg_if! { + if #[cfg(esp32s2)] { + let sensors = unsafe { &*crate::peripherals::SENS::ptr() }; + sensors + .sar_io_mux_conf() + .modify(|_, w| w.iomux_clk_gate_en().set_bit()); + } else if #[cfg(esp32s3)] { + let sensors = unsafe { &*crate::peripherals::SENS::ptr() }; + sensors + .sar_peri_clk_gate_conf() + .modify(|_,w| w.iomux_clk_en().set_bit()); + } } } @@ -1659,6 +1661,7 @@ macro_rules! analog { use $crate::peripherals::RTC_IO; let rtcio = unsafe{ &*RTC_IO::ptr() }; + #[cfg(esp32s2)] $crate::gpio::enable_iomux_clk_gate(); match pin { @@ -1697,7 +1700,7 @@ macro_rules! analog { } } )+ - _ => unreachable!(), + _ => unreachable!(), } } } From 9b7871c261581b6de59d6519724070a1023f0816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 08:59:22 +0200 Subject: [PATCH 02/19] Remove public functions --- esp-hal/CHANGELOG.md | 1 + esp-hal/src/gpio/mod.rs | 61 ++--------------------------------------- 2 files changed, 4 insertions(+), 58 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 6d32881603b..bc9bd7c36c5 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed the `GpioN` type aliasses. Use `GpioPin` instead. (#2073) - Removed `Peripherals::take`. Use `esp_hal::init` to obtain `Peripherals` (#1999) - Removed `AnyInputOnlyPin` in favour of `AnyPin`. (#2071) +- Removed the following functions from `GpioPin`: `is_high`, `is_low`, `set_high`, `set_low`, `set_state`, `is_set_high`, `is_set_low`, `toggle`. ## [0.20.1] - 2024-08-30 diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index 5cd2bfc55b9..d0f053050f4 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -695,18 +695,6 @@ where Self::new() } - /// Is the input pin high? - #[inline] - pub fn is_high(&self) -> bool { - ::Bank::read_input() & (1 << (GPIONUM % 32)) != 0 - } - - /// Is the input pin low? - #[inline] - pub fn is_low(&self) -> bool { - !self.is_high() - } - fn write_out_en(&self, enable: bool) { if enable { ::Bank::write_out_en_set(1 << (GPIONUM % 32)); @@ -791,7 +779,7 @@ where } fn is_input_high(&self, _: private::Internal) -> bool { - self.is_high() + ::Bank::read_input() & (1 << (GPIONUM % 32)) != 0 } fn connect_input_to_peripheral(&mut self, signal: InputSignal, _: private::Internal) { @@ -985,49 +973,6 @@ where .clear_bit() }); } - - /// Drives the pin high. - #[inline] - pub fn set_high(&mut self) { - ::Bank::write_output_set(1 << (GPIONUM % 32)); - } - - /// Drives the pin low. - #[inline] - pub fn set_low(&mut self) { - ::Bank::write_output_clear(1 << (GPIONUM % 32)); - } - - /// Drives the pin high or low depending on the provided value. - #[inline] - pub fn set_state(&mut self, state: bool) { - match state { - true => self.set_high(), - false => self.set_low(), - } - } - - /// Is the pin in drive high mode? - #[inline] - pub fn is_set_high(&self) -> bool { - ::Bank::read_output() & (1 << (GPIONUM % 32)) != 0 - } - - /// Is the pin in drive low mode? - #[inline] - pub fn is_set_low(&self) -> bool { - !self.is_set_high() - } - - /// Toggle pin output. - #[inline] - pub fn toggle(&mut self) { - if self.is_set_high() { - self.set_low(); - } else { - self.set_high(); - } - } } impl OutputPin for GpioPin @@ -1048,9 +993,9 @@ where fn set_output_high(&mut self, high: bool, _: private::Internal) { if high { - self.set_high() + ::Bank::write_output_set(1 << (GPIONUM % 32)); } else { - self.set_low() + ::Bank::write_output_clear(1 << (GPIONUM % 32)); } } From bef07a6897a76d97818e5b8f48ee4477e8e5dace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 09:07:05 +0200 Subject: [PATCH 03/19] Remove set_to_input --- esp-hal/src/gpio/any_pin.rs | 1 - esp-hal/src/gpio/dummy_pin.rs | 2 -- esp-hal/src/gpio/mod.rs | 13 -------- esp-hal/src/i2s.rs | 2 +- esp-hal/src/lcd_cam/cam.rs | 60 +++++++++++++++++------------------ esp-hal/src/parl_io.rs | 8 ++--- esp-hal/src/rmt.rs | 4 +-- esp-hal/src/spi/master.rs | 4 +-- esp-hal/src/spi/slave.rs | 6 ++-- esp-hal/src/twai/mod.rs | 2 +- esp-hal/src/uart.rs | 16 +++++----- 11 files changed, 51 insertions(+), 67 deletions(-) diff --git a/esp-hal/src/gpio/any_pin.rs b/esp-hal/src/gpio/any_pin.rs index f222039147c..c5ccd425283 100644 --- a/esp-hal/src/gpio/any_pin.rs +++ b/esp-hal/src/gpio/any_pin.rs @@ -148,7 +148,6 @@ impl<'d> InputPin for AnyPin<'d> { delegate::delegate! { to self.pin { fn init_input(&self, pull_down: bool, pull_up: bool, _internal: private::Internal); - fn set_to_input(&mut self, _internal: private::Internal); fn enable_input(&mut self, on: bool, _internal: private::Internal); fn enable_input_in_sleep_mode(&mut self, on: bool, _internal: private::Internal); fn is_input_high(&self, _internal: private::Internal) -> bool; diff --git a/esp-hal/src/gpio/dummy_pin.rs b/esp-hal/src/gpio/dummy_pin.rs index 5d319808afd..0b79017aa4e 100644 --- a/esp-hal/src/gpio/dummy_pin.rs +++ b/esp-hal/src/gpio/dummy_pin.rs @@ -113,8 +113,6 @@ impl OutputPin for DummyPin { impl InputPin for DummyPin { fn init_input(&self, _pull_down: bool, _pull_up: bool, _: private::Internal) {} - fn set_to_input(&mut self, _: private::Internal) {} - fn enable_input(&mut self, _on: bool, _: private::Internal) {} fn enable_input_in_sleep_mode(&mut self, _on: bool, _: private::Internal) {} diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index d0f053050f4..de7a43fee35 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -327,9 +327,6 @@ pub trait InputPin: Pin { /// Init as input with the given pull-up/pull-down fn init_input(&self, pull_down: bool, pull_up: bool, _: private::Internal); - /// Set the pin to input mode without internal pull-up / pull-down resistors - fn set_to_input(&mut self, _: private::Internal); - /// Enable input for the pin fn enable_input(&mut self, on: bool, _: private::Internal); @@ -766,10 +763,6 @@ where }); } - fn set_to_input(&mut self, _: private::Internal) { - self.init_input(false, false, private::Internal); - } - fn enable_input(&mut self, on: bool, _: private::Internal) { get_io_mux_reg(GPIONUM).modify(|_, w| w.fun_ie().bit(on)); } @@ -2380,12 +2373,6 @@ pub(crate) mod internal { }) } - fn set_to_input(&mut self, _: private::Internal) { - handle_gpio_input!(self, target, { - InputPin::set_to_input(target, private::Internal); - }); - } - fn enable_input(&mut self, on: bool, _: private::Internal) { handle_gpio_input!(self, target, { InputPin::enable_input(target, on, private::Internal) diff --git a/esp-hal/src/i2s.rs b/esp-hal/src/i2s.rs index 16089349b6a..8c6b1bddd9f 100644 --- a/esp-hal/src/i2s.rs +++ b/esp-hal/src/i2s.rs @@ -999,7 +999,7 @@ mod private { P: InputPin, { into_ref!(pin); - pin.set_to_input(crate::private::Internal); + pin.init_input(false, false, crate::private::Internal); pin.connect_input_to_peripheral(T::din_signal(), crate::private::Internal); self } diff --git a/esp-hal/src/lcd_cam/cam.rs b/esp-hal/src/lcd_cam/cam.rs index 6d59b76a33f..a8b7c5e95c4 100644 --- a/esp-hal/src/lcd_cam/cam.rs +++ b/esp-hal/src/lcd_cam/cam.rs @@ -311,7 +311,7 @@ impl<'d, CH: DmaChannel> Camera<'d, CH> { pub fn with_pixel_clock(self, pclk: impl Peripheral

+ 'd) -> Self { crate::into_ref!(pclk); - pclk.set_to_input(crate::private::Internal); + pclk.init_input(false, false, crate::private::Internal); pclk.connect_input_to_peripheral(InputSignal::CAM_PCLK, crate::private::Internal); self @@ -327,9 +327,9 @@ impl<'d, CH: DmaChannel> Camera<'d, CH> { crate::into_ref!(vsync); crate::into_ref!(h_enable); - vsync.set_to_input(crate::private::Internal); + vsync.init_input(false, false, crate::private::Internal); vsync.connect_input_to_peripheral(InputSignal::CAM_V_SYNC, crate::private::Internal); - h_enable.set_to_input(crate::private::Internal); + h_enable.init_input(false, false, crate::private::Internal); h_enable.connect_input_to_peripheral(InputSignal::CAM_H_ENABLE, crate::private::Internal); self.lcd_cam @@ -351,11 +351,11 @@ impl<'d, CH: DmaChannel> Camera<'d, CH> { crate::into_ref!(hsync); crate::into_ref!(h_enable); - vsync.set_to_input(crate::private::Internal); + vsync.init_input(false, false, crate::private::Internal); vsync.connect_input_to_peripheral(InputSignal::CAM_V_SYNC, crate::private::Internal); - hsync.set_to_input(crate::private::Internal); + hsync.init_input(false, false, crate::private::Internal); hsync.connect_input_to_peripheral(InputSignal::CAM_H_SYNC, crate::private::Internal); - h_enable.set_to_input(crate::private::Internal); + h_enable.init_input(false, false, crate::private::Internal); h_enable.connect_input_to_peripheral(InputSignal::CAM_H_ENABLE, crate::private::Internal); self.lcd_cam @@ -475,21 +475,21 @@ impl RxEightBits { crate::into_ref!(pin_6); crate::into_ref!(pin_7); - pin_0.set_to_input(crate::private::Internal); + pin_0.init_input(false, false, crate::private::Internal); pin_0.connect_input_to_peripheral(InputSignal::CAM_DATA_0, crate::private::Internal); - pin_1.set_to_input(crate::private::Internal); + pin_1.init_input(false, false, crate::private::Internal); pin_1.connect_input_to_peripheral(InputSignal::CAM_DATA_1, crate::private::Internal); - pin_2.set_to_input(crate::private::Internal); + pin_2.init_input(false, false, crate::private::Internal); pin_2.connect_input_to_peripheral(InputSignal::CAM_DATA_2, crate::private::Internal); - pin_3.set_to_input(crate::private::Internal); + pin_3.init_input(false, false, crate::private::Internal); pin_3.connect_input_to_peripheral(InputSignal::CAM_DATA_3, crate::private::Internal); - pin_4.set_to_input(crate::private::Internal); + pin_4.init_input(false, false, crate::private::Internal); pin_4.connect_input_to_peripheral(InputSignal::CAM_DATA_4, crate::private::Internal); - pin_5.set_to_input(crate::private::Internal); + pin_5.init_input(false, false, crate::private::Internal); pin_5.connect_input_to_peripheral(InputSignal::CAM_DATA_5, crate::private::Internal); - pin_6.set_to_input(crate::private::Internal); + pin_6.init_input(false, false, crate::private::Internal); pin_6.connect_input_to_peripheral(InputSignal::CAM_DATA_6, crate::private::Internal); - pin_7.set_to_input(crate::private::Internal); + pin_7.init_input(false, false, crate::private::Internal); pin_7.connect_input_to_peripheral(InputSignal::CAM_DATA_7, crate::private::Internal); Self { _pins: () } @@ -563,37 +563,37 @@ impl RxSixteenBits { crate::into_ref!(pin_14); crate::into_ref!(pin_15); - pin_0.set_to_input(crate::private::Internal); + pin_0.init_input(false, false, crate::private::Internal); pin_0.connect_input_to_peripheral(InputSignal::CAM_DATA_0, crate::private::Internal); - pin_1.set_to_input(crate::private::Internal); + pin_1.init_input(false, false, crate::private::Internal); pin_1.connect_input_to_peripheral(InputSignal::CAM_DATA_1, crate::private::Internal); - pin_2.set_to_input(crate::private::Internal); + pin_2.init_input(false, false, crate::private::Internal); pin_2.connect_input_to_peripheral(InputSignal::CAM_DATA_2, crate::private::Internal); - pin_3.set_to_input(crate::private::Internal); + pin_3.init_input(false, false, crate::private::Internal); pin_3.connect_input_to_peripheral(InputSignal::CAM_DATA_3, crate::private::Internal); - pin_4.set_to_input(crate::private::Internal); + pin_4.init_input(false, false, crate::private::Internal); pin_4.connect_input_to_peripheral(InputSignal::CAM_DATA_4, crate::private::Internal); - pin_5.set_to_input(crate::private::Internal); + pin_5.init_input(false, false, crate::private::Internal); pin_5.connect_input_to_peripheral(InputSignal::CAM_DATA_5, crate::private::Internal); - pin_6.set_to_input(crate::private::Internal); + pin_6.init_input(false, false, crate::private::Internal); pin_6.connect_input_to_peripheral(InputSignal::CAM_DATA_6, crate::private::Internal); - pin_7.set_to_input(crate::private::Internal); + pin_7.init_input(false, false, crate::private::Internal); pin_7.connect_input_to_peripheral(InputSignal::CAM_DATA_7, crate::private::Internal); - pin_8.set_to_input(crate::private::Internal); + pin_8.init_input(false, false, crate::private::Internal); pin_8.connect_input_to_peripheral(InputSignal::CAM_DATA_8, crate::private::Internal); - pin_9.set_to_input(crate::private::Internal); + pin_9.init_input(false, false, crate::private::Internal); pin_9.connect_input_to_peripheral(InputSignal::CAM_DATA_9, crate::private::Internal); - pin_10.set_to_input(crate::private::Internal); + pin_10.init_input(false, false, crate::private::Internal); pin_10.connect_input_to_peripheral(InputSignal::CAM_DATA_10, crate::private::Internal); - pin_11.set_to_input(crate::private::Internal); + pin_11.init_input(false, false, crate::private::Internal); pin_11.connect_input_to_peripheral(InputSignal::CAM_DATA_11, crate::private::Internal); - pin_12.set_to_input(crate::private::Internal); + pin_12.init_input(false, false, crate::private::Internal); pin_12.connect_input_to_peripheral(InputSignal::CAM_DATA_12, crate::private::Internal); - pin_13.set_to_input(crate::private::Internal); + pin_13.init_input(false, false, crate::private::Internal); pin_13.connect_input_to_peripheral(InputSignal::CAM_DATA_13, crate::private::Internal); - pin_14.set_to_input(crate::private::Internal); + pin_14.init_input(false, false, crate::private::Internal); pin_14.connect_input_to_peripheral(InputSignal::CAM_DATA_14, crate::private::Internal); - pin_15.set_to_input(crate::private::Internal); + pin_15.init_input(false, false, crate::private::Internal); pin_15.connect_input_to_peripheral(InputSignal::CAM_DATA_15, crate::private::Internal); Self { _pins: () } diff --git a/esp-hal/src/parl_io.rs b/esp-hal/src/parl_io.rs index 05756df85b7..97fb8a0ad7c 100644 --- a/esp-hal/src/parl_io.rs +++ b/esp-hal/src/parl_io.rs @@ -332,7 +332,7 @@ where pcr.parl_clk_tx_conf() .modify(|_, w| unsafe { w.parl_clk_tx_sel().bits(3).parl_clk_tx_div_num().bits(0) }); // PAD_CLK_TX, no divider - self.pin.set_to_input(crate::private::Internal); + self.pin.init_input(false, false, crate::private::Internal); self.pin.connect_input_to_peripheral( crate::gpio::InputSignal::PARL_TX_CLK, crate::private::Internal, @@ -367,7 +367,7 @@ where pcr.parl_clk_rx_conf() .modify(|_, w| unsafe { w.parl_clk_rx_sel().bits(3).parl_clk_rx_div_num().bits(0) }); // PAD_CLK_TX, no divider - self.pin.set_to_input(crate::private::Internal); + self.pin.init_input(false, false, crate::private::Internal); self.pin.connect_input_to_peripheral( crate::gpio::InputSignal::PARL_RX_CLK, crate::private::Internal, @@ -634,7 +634,7 @@ where { fn configure(&mut self) -> Result<(), Error> { self.rx_pins.configure()?; - self.valid_pin.set_to_input(crate::private::Internal); + self.valid_pin.init_input(false, false, crate::private::Internal); self.valid_pin .connect_input_to_peripheral(Instance::rx_valid_pin_signal(), crate::private::Internal); Instance::set_rx_sw_en(false); @@ -738,7 +738,7 @@ macro_rules! rx_pins { { fn configure(&mut self) -> Result<(), Error> { $( - self.[< pin_ $pin:lower >].set_to_input($crate::private::Internal); + self.[< pin_ $pin:lower >].init_input(false, false, $crate::private::Internal); self.[< pin_ $pin:lower >].connect_input_to_peripheral(crate::gpio::InputSignal::$signal, $crate::private::Internal); )+ diff --git a/esp-hal/src/rmt.rs b/esp-hal/src/rmt.rs index e505d7d1688..7ea901626c9 100644 --- a/esp-hal/src/rmt.rs +++ b/esp-hal/src/rmt.rs @@ -387,7 +387,7 @@ where } crate::into_ref!(pin); - pin.set_to_input(crate::private::Internal); + pin.init_input(false, false, crate::private::Internal); pin.connect_input_to_peripheral(T::input_signal(), crate::private::Internal); T::set_divider(config.clk_divider); T::set_carrier( @@ -433,7 +433,7 @@ where } crate::into_ref!(pin); - pin.set_to_input(crate::private::Internal); + pin.init_input(false, false, crate::private::Internal); pin.connect_input_to_peripheral(T::input_signal(), crate::private::Internal); T::set_divider(config.clk_divider); T::set_carrier( diff --git a/esp-hal/src/spi/master.rs b/esp-hal/src/spi/master.rs index c0c0287f06a..da82422798b 100644 --- a/esp-hal/src/spi/master.rs +++ b/esp-hal/src/spi/master.rs @@ -542,7 +542,7 @@ where /// Sets the specified pin to input and connects it to the SPI MISO signal. pub fn with_miso(self, miso: impl Peripheral

+ 'd) -> Self { crate::into_ref!(miso); - miso.set_to_input(private::Internal); + miso.init_input(false, false, private::Internal); miso.connect_input_to_peripheral(self.spi.miso_signal(), private::Internal); self @@ -593,7 +593,7 @@ where if let Some(miso) = miso { crate::into_ref!(miso); - miso.set_to_input(private::Internal); + miso.init_input(false, false, private::Internal); miso.connect_input_to_peripheral(self.spi.miso_signal(), private::Internal); } diff --git a/esp-hal/src/spi/slave.rs b/esp-hal/src/spi/slave.rs index 2641615de9a..cc9bef4ff66 100644 --- a/esp-hal/src/spi/slave.rs +++ b/esp-hal/src/spi/slave.rs @@ -115,16 +115,16 @@ where mode: SpiMode, ) -> Spi<'d, T, FullDuplexMode> { crate::into_ref!(spi, sck, mosi, miso, cs); - sck.set_to_input(private::Internal); + sck.init_input(false, false, private::Internal); sck.connect_input_to_peripheral(spi.sclk_signal(), private::Internal); - mosi.set_to_input(private::Internal); + mosi.init_input(false, false, private::Internal); mosi.connect_input_to_peripheral(spi.mosi_signal(), private::Internal); miso.set_to_push_pull_output(private::Internal); miso.connect_peripheral_to_output(spi.miso_signal(), private::Internal); - cs.set_to_input(private::Internal); + cs.init_input(false, false, private::Internal); cs.connect_input_to_peripheral(spi.cs_signal(), private::Internal); Self::new_internal(spi, mode) diff --git a/esp-hal/src/twai/mod.rs b/esp-hal/src/twai/mod.rs index 75a6dcd54f0..930cd512b0d 100644 --- a/esp-hal/src/twai/mod.rs +++ b/esp-hal/src/twai/mod.rs @@ -741,7 +741,7 @@ where } tx_pin.set_to_push_pull_output(crate::private::Internal); tx_pin.connect_peripheral_to_output(T::OUTPUT_SIGNAL, crate::private::Internal); - rx_pin.set_to_input(crate::private::Internal); + rx_pin.init_input(false, false, crate::private::Internal); rx_pin.connect_input_to_peripheral(T::INPUT_SIGNAL, crate::private::Internal); // Set the operating mode based on provided option diff --git a/esp-hal/src/uart.rs b/esp-hal/src/uart.rs index a9c64c6f841..d70c6ddc62c 100644 --- a/esp-hal/src/uart.rs +++ b/esp-hal/src/uart.rs @@ -613,7 +613,7 @@ where /// Configure CTS pin pub fn with_cts(self, cts: impl Peripheral

+ 'd) -> Self { crate::into_ref!(cts); - cts.set_to_input(Internal); + cts.init_input(false, false, Internal); cts.connect_input_to_peripheral(T::cts_signal(), Internal); self @@ -791,7 +791,7 @@ where rx: impl Peripheral

+ 'd, ) -> Result { crate::into_ref!(rx); - rx.set_to_input(Internal); + rx.init_input(false, false, Internal); rx.connect_input_to_peripheral(T::rx_signal(), Internal); let (_, uart_rx) = Uart::<'d, T, Blocking>::new_with_config_inner(uart, config)?.split(); @@ -817,7 +817,7 @@ where tx.set_to_push_pull_output(Internal); tx.connect_peripheral_to_output(T::tx_signal(), Internal); - rx.set_to_input(Internal); + rx.init_input(false, false, Internal); rx.connect_input_to_peripheral(T::rx_signal(), Internal); Self::new_with_config_inner(uart, config) } @@ -833,7 +833,7 @@ where tx.set_to_push_pull_output(Internal); tx.connect_peripheral_to_output(T::tx_signal(), Internal); - rx.set_to_input(Internal); + rx.init_input(false, false, Internal); rx.connect_input_to_peripheral(T::rx_signal(), Internal); Self::new_inner(uart) } @@ -848,7 +848,7 @@ where tx.set_to_push_pull_output(Internal); tx.connect_peripheral_to_output(T::tx_signal(), Internal); - rx.set_to_input(Internal); + rx.init_input(false, false, Internal); rx.connect_input_to_peripheral(T::rx_signal(), Internal); Self::new_inner(uart) } @@ -916,7 +916,7 @@ where /// Configure CTS pin pub fn with_cts(self, cts: impl Peripheral

+ 'd) -> Self { crate::into_ref!(cts); - cts.set_to_input(Internal); + cts.init_input(false, false, Internal); cts.connect_input_to_peripheral(T::cts_signal(), Internal); self @@ -2141,7 +2141,7 @@ mod asynch { tx.set_to_push_pull_output(Internal); tx.connect_peripheral_to_output(T::tx_signal(), Internal); - rx.set_to_input(Internal); + rx.init_input(false, false, Internal); rx.connect_input_to_peripheral(T::rx_signal(), Internal); let mut this = Self::new_with_config_inner(uart, config)?; @@ -2303,7 +2303,7 @@ mod asynch { rx: impl Peripheral

+ 'd, ) -> Result { crate::into_ref!(rx); - rx.set_to_input(Internal); + rx.init_input(false, false, Internal); rx.connect_input_to_peripheral(T::rx_signal(), Internal); let mut uart = Uart::<'d, T, Async>::new_with_config_inner(uart, config)?; From cd8e005d76ba6ebc85c02e4149f499ea554a2eca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 09:31:38 +0200 Subject: [PATCH 04/19] Deduplicate constructor --- esp-hal/src/gpio/mod.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index de7a43fee35..2b8de386a2e 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -1162,12 +1162,7 @@ impl Io { gpio.bind_gpio_interrupt(gpio_interrupt_handler); crate::interrupt::enable(crate::peripherals::Interrupt::GPIO, prio).unwrap(); - let pins = gpio.pins(); - - Io { - _io_mux: io_mux, - pins, - } + Self::new_no_bind_interrupt(gpio, io_mux) } /// Initialize the I/O driver without enabling the GPIO interrupt or @@ -1176,12 +1171,10 @@ impl Io { /// *Note:* You probably don't want to use this, it is intended to be used /// in very specific use cases. Async GPIO functionality will not work /// when instantiating `Io` using this constructor. - pub fn new_no_bind_interrupt(gpio: GPIO, io_mux: IO_MUX) -> Self { - let pins = gpio.pins(); - + pub fn new_no_bind_interrupt(gpio: GPIO, _io_mux: IO_MUX) -> Self { Io { - _io_mux: io_mux, - pins, + _io_mux, + pins: gpio.pins(), } } } From 7aaeb9ab8c2ef9b1c3e568920fc1257dceb2c4c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 09:32:35 +0200 Subject: [PATCH 05/19] Deduplicate is_listening --- esp-hal/src/gpio/mod.rs | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index 2b8de386a2e..ce439a9816e 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -297,7 +297,9 @@ pub trait Pin: private::Sealed { } /// Checks if listening for interrupts is enabled for this Pin - fn is_listening(&self, _: private::Internal) -> bool; + fn is_listening(&self, _: private::Internal) -> bool { + is_listening(self.number(private::Internal)) + } /// Listen for interrupts fn listen_with_options( @@ -2310,12 +2312,6 @@ pub(crate) mod internal { }) } - fn is_listening(&self, _: private::Internal) -> bool { - handle_gpio_input!(self, target, { - Pin::is_listening(target, private::Internal) - }) - } - fn listen_with_options( &mut self, event: Event, @@ -2524,6 +2520,15 @@ pub(crate) mod internal { } } +fn is_listening(pin_num: u8) -> bool { + let bits = unsafe { &*GPIO::PTR } + .pin(pin_num as usize) + .read() + .int_ena() + .bits(); + bits != 0 +} + mod asynch { use core::task::{Context, Poll}; @@ -2631,16 +2636,7 @@ mod asynch { } } - pub(crate) fn is_listening(gpio_num: u8) -> bool { - let bits = unsafe { &*GPIO::PTR } - .pin(gpio_num as usize) - .read() - .int_ena() - .bits(); - bits != 0 - } - - pub(crate) fn set_int_enable( + fn set_int_enable( gpio_num: u8, int_ena: u8, int_type: u8, From 738b4b561879217128dfeb86b8d654d396f136df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 09:32:48 +0200 Subject: [PATCH 06/19] Hide PinFuture better --- esp-hal/src/gpio/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index ce439a9816e..31ff309291b 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -2610,12 +2610,12 @@ mod asynch { } #[must_use = "futures do nothing unless you `.await` or poll them"] - pub struct PinFuture { + struct PinFuture { pin_num: u8, } impl PinFuture { - pub fn new(pin_num: u8) -> Self { + fn new(pin_num: u8) -> Self { Self { pin_num } } } From fd9ef38566def1772a6f9c709e2cbe5b4ffb55da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 09:37:31 +0200 Subject: [PATCH 07/19] Deduplicate set_int_enable --- esp-hal/src/gpio/mod.rs | 54 ++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index 31ff309291b..e92c50c323e 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -877,25 +877,7 @@ where } } - unsafe { - (*GPIO::PTR).pin(GPIONUM as usize).modify(|_, w| { - w.int_ena() - .bits(gpio_intr_enable(int_enable, nmi_enable)) - .int_type() - .bits(event as u8) - .wakeup_enable() - .bit(wake_up_from_light_sleep) - }); - } - } - - fn is_listening(&self, _: private::Internal) -> bool { - let bits = unsafe { &*GPIO::PTR } - .pin(GPIONUM as usize) - .read() - .int_ena() - .bits(); - bits != 0 + set_int_enable(GPIONUM, gpio_intr_enable(int_enable, nmi_enable), event as u8, wake_up_from_light_sleep) } fn unlisten(&mut self, _: private::Internal) { @@ -2529,6 +2511,23 @@ fn is_listening(pin_num: u8) -> bool { bits != 0 } +fn set_int_enable( + gpio_num: u8, + int_ena: u8, + int_type: u8, + wake_up_from_light_sleep: bool, +) { + let gpio = unsafe { &*crate::peripherals::GPIO::PTR }; + gpio.pin(gpio_num as usize).modify(|_, w| unsafe { + w.int_ena() + .bits(int_ena) + .int_type() + .bits(int_type) + .wakeup_enable() + .bit(wake_up_from_light_sleep) + }); +} + mod asynch { use core::task::{Context, Poll}; @@ -2636,23 +2635,6 @@ mod asynch { } } - fn set_int_enable( - gpio_num: u8, - int_ena: u8, - int_type: u8, - wake_up_from_light_sleep: bool, - ) { - let gpio = unsafe { &*crate::peripherals::GPIO::PTR }; - gpio.pin(gpio_num as usize).modify(|_, w| unsafe { - w.int_ena() - .bits(int_ena) - .int_type() - .bits(int_type) - .wakeup_enable() - .bit(wake_up_from_light_sleep) - }); - } - #[ram] pub(super) fn handle_gpio_interrupt() { let intrs_bank0 = InterruptStatusRegisterAccessBank0::interrupt_status_read(); From 632aa0fc44a1714a83c5457a3c8e2271a7f88743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 12:44:03 +0200 Subject: [PATCH 08/19] Align macro indentation --- esp-hal/src/soc/esp32/gpio.rs | 114 ++++++++++++++++---------------- esp-hal/src/soc/esp32s3/gpio.rs | 44 ++++++------ 2 files changed, 79 insertions(+), 79 deletions(-) diff --git a/esp-hal/src/soc/esp32/gpio.rs b/esp-hal/src/soc/esp32/gpio.rs index d2fa84b3b29..10757e09eb1 100644 --- a/esp-hal/src/soc/esp32/gpio.rs +++ b/esp-hal/src/soc/esp32/gpio.rs @@ -804,74 +804,74 @@ crate::gpio::gpio! { } crate::gpio::analog! { - (36, 0, sensor_pads(), sense1_mux_sel, sense1_fun_sel, sense1_fun_ie) - (37, 1, sensor_pads(), sense2_mux_sel, sense2_fun_sel, sense2_fun_ie) - (38, 2, sensor_pads(), sense3_mux_sel, sense3_fun_sel, sense3_fun_ie) - (39, 3, sensor_pads(), sense4_mux_sel, sense4_fun_sel, sense4_fun_ie) - (34, 4, adc_pad(), adc1_mux_sel, adc1_fun_sel, adc1_fun_ie) - (35, 5, adc_pad(), adc2_mux_sel, adc2_fun_sel, adc1_fun_ie) - (25, 6, pad_dac1(), mux_sel, fun_sel, fun_ie, rue, rde) - (26, 7, pad_dac2(), mux_sel, fun_sel, fun_ie, rue, rde) - (33, 8, xtal_32k_pad(), x32n_mux_sel, x32n_fun_sel, x32n_fun_ie, x32n_rue, x32n_rde ) - (32, 9, xtal_32k_pad(), x32p_mux_sel, x32p_fun_sel, x32p_fun_ie, x32p_rue, x32p_rde ) - (4, 10, touch_pad0(), mux_sel, fun_sel, fun_ie, rue, rde ) - (0, 11, touch_pad1(), mux_sel, fun_sel, fun_ie, rue, rde ) - (2, 12, touch_pad2(), mux_sel, fun_sel, fun_ie, rue, rde ) - (15, 13, touch_pad3(), mux_sel, fun_sel, fun_ie, rue, rde ) - (13, 14, touch_pad4(), mux_sel, fun_sel, fun_ie, rue, rde ) - (12, 15, touch_pad5(), mux_sel, fun_sel, fun_ie, rue, rde ) - (14, 16, touch_pad6(), mux_sel, fun_sel, fun_ie, rue, rde ) - (27, 17, touch_pad7(), mux_sel, fun_sel, fun_ie, rue, rde ) + (36, 0, sensor_pads(), sense1_mux_sel, sense1_fun_sel, sense1_fun_ie) + (37, 1, sensor_pads(), sense2_mux_sel, sense2_fun_sel, sense2_fun_ie) + (38, 2, sensor_pads(), sense3_mux_sel, sense3_fun_sel, sense3_fun_ie) + (39, 3, sensor_pads(), sense4_mux_sel, sense4_fun_sel, sense4_fun_ie) + (34, 4, adc_pad(), adc1_mux_sel, adc1_fun_sel, adc1_fun_ie) + (35, 5, adc_pad(), adc2_mux_sel, adc2_fun_sel, adc1_fun_ie) + (25, 6, pad_dac1(), mux_sel, fun_sel, fun_ie, rue, rde) + (26, 7, pad_dac2(), mux_sel, fun_sel, fun_ie, rue, rde) + (33, 8, xtal_32k_pad(), x32n_mux_sel, x32n_fun_sel, x32n_fun_ie, x32n_rue, x32n_rde ) + (32, 9, xtal_32k_pad(), x32p_mux_sel, x32p_fun_sel, x32p_fun_ie, x32p_rue, x32p_rde ) + (4, 10, touch_pad0(), mux_sel, fun_sel, fun_ie, rue, rde ) + (0, 11, touch_pad1(), mux_sel, fun_sel, fun_ie, rue, rde ) + (2, 12, touch_pad2(), mux_sel, fun_sel, fun_ie, rue, rde ) + (15, 13, touch_pad3(), mux_sel, fun_sel, fun_ie, rue, rde ) + (13, 14, touch_pad4(), mux_sel, fun_sel, fun_ie, rue, rde ) + (12, 15, touch_pad5(), mux_sel, fun_sel, fun_ie, rue, rde ) + (14, 16, touch_pad6(), mux_sel, fun_sel, fun_ie, rue, rde ) + (27, 17, touch_pad7(), mux_sel, fun_sel, fun_ie, rue, rde ) } crate::gpio::rtc_pins! { - (36, 0, sensor_pads(), sense1_, sense1_hold_force ) - (37, 1, sensor_pads(), sense2_, sense2_hold_force ) - (38, 2, sensor_pads(), sense3_, sense3_hold_force ) - (39, 3, sensor_pads(), sense4_, sense4_hold_force ) - (34, 4, adc_pad(), adc1_, adc1_hold_force ) - (35, 5, adc_pad(), adc2_, adc2_hold_force ) - (25, 6, pad_dac1(), "", pdac1_hold_force, rue, rde ) - (26, 7, pad_dac2(), "", pdac2_hold_force, rue, rde ) - (33, 8, xtal_32k_pad(), x32n_, x32n_hold_force, x32n_rue, x32n_rde ) - (32, 9, xtal_32k_pad(), x32p_, x32p_hold_force, x32p_rue, x32p_rde ) - (4, 10, touch_pad0(), "", touch_pad0_hold_force, rue, rde ) - (0, 11, touch_pad1(), "", touch_pad1_hold_force, rue, rde ) - (2, 12, touch_pad2(), "", touch_pad2_hold_force, rue, rde ) - (15, 13, touch_pad3(), "", touch_pad3_hold_force, rue, rde ) - (13, 14, touch_pad4(), "", touch_pad4_hold_force, rue, rde ) - (12, 15, touch_pad5(), "", touch_pad5_hold_force, rue, rde ) - (14, 16, touch_pad6(), "", touch_pad6_hold_force, rue, rde ) - (27, 17, touch_pad7(), "", touch_pad7_hold_force, rue, rde ) + (36, 0, sensor_pads(), sense1_, sense1_hold_force ) + (37, 1, sensor_pads(), sense2_, sense2_hold_force ) + (38, 2, sensor_pads(), sense3_, sense3_hold_force ) + (39, 3, sensor_pads(), sense4_, sense4_hold_force ) + (34, 4, adc_pad(), adc1_, adc1_hold_force ) + (35, 5, adc_pad(), adc2_, adc2_hold_force ) + (25, 6, pad_dac1(), "", pdac1_hold_force, rue, rde ) + (26, 7, pad_dac2(), "", pdac2_hold_force, rue, rde ) + (33, 8, xtal_32k_pad(), x32n_, x32n_hold_force, x32n_rue, x32n_rde ) + (32, 9, xtal_32k_pad(), x32p_, x32p_hold_force, x32p_rue, x32p_rde ) + (4, 10, touch_pad0(), "", touch_pad0_hold_force, rue, rde ) + (0, 11, touch_pad1(), "", touch_pad1_hold_force, rue, rde ) + (2, 12, touch_pad2(), "", touch_pad2_hold_force, rue, rde ) + (15, 13, touch_pad3(), "", touch_pad3_hold_force, rue, rde ) + (13, 14, touch_pad4(), "", touch_pad4_hold_force, rue, rde ) + (12, 15, touch_pad5(), "", touch_pad5_hold_force, rue, rde ) + (14, 16, touch_pad6(), "", touch_pad6_hold_force, rue, rde ) + (27, 17, touch_pad7(), "", touch_pad7_hold_force, rue, rde ) } crate::gpio::touch_into! { // (touch_nr, pin_nr, rtc_pin, touch_comb_reg_nr, normal_pin) - (0, 4, 10, sar_touch_thres1, touch_out_th0, true ) - (1, 0, 11, sar_touch_thres1, touch_out_th1, true ) - (2, 2, 12, sar_touch_thres2, touch_out_th2, true ) - (3, 15, 13, sar_touch_thres2, touch_out_th3, true ) - (4, 13, 14, sar_touch_thres3, touch_out_th4, true ) - (5, 12, 15, sar_touch_thres3, touch_out_th5, true ) - (6, 14, 16, sar_touch_thres4, touch_out_th6, true ) - (7, 27, 17, sar_touch_thres4, touch_out_th7, true ) - --- - (8, 33, 8, sar_touch_thres5, touch_out_th8, false ) - (9, 32, 9, sar_touch_thres5, touch_out_th9, false ) + (0, 4, 10, sar_touch_thres1, touch_out_th0, true ) + (1, 0, 11, sar_touch_thres1, touch_out_th1, true ) + (2, 2, 12, sar_touch_thres2, touch_out_th2, true ) + (3, 15, 13, sar_touch_thres2, touch_out_th3, true ) + (4, 13, 14, sar_touch_thres3, touch_out_th4, true ) + (5, 12, 15, sar_touch_thres3, touch_out_th5, true ) + (6, 14, 16, sar_touch_thres4, touch_out_th6, true ) + (7, 27, 17, sar_touch_thres4, touch_out_th7, true ) + --- + (8, 33, 8, sar_touch_thres5, touch_out_th8, false ) + (9, 32, 9, sar_touch_thres5, touch_out_th9, false ) } crate::gpio::touch_common! { // (touch_nr, pin_nr, touch_out_reg, touch_thres_reg ) - (0, 4, sar_touch_out1, touch_meas_out0, sar_touch_thres1, touch_out_th0) - (1, 0, sar_touch_out1, touch_meas_out1, sar_touch_thres1, touch_out_th1) - (2, 2, sar_touch_out2, touch_meas_out2, sar_touch_thres2, touch_out_th2) - (3, 15, sar_touch_out2, touch_meas_out3, sar_touch_thres2, touch_out_th3) - (4, 13, sar_touch_out3, touch_meas_out4, sar_touch_thres3, touch_out_th4) - (5, 12, sar_touch_out3, touch_meas_out5, sar_touch_thres3, touch_out_th5) - (6, 14, sar_touch_out4, touch_meas_out6, sar_touch_thres4, touch_out_th6) - (7, 27, sar_touch_out4, touch_meas_out7, sar_touch_thres4, touch_out_th7) - (8, 33, sar_touch_out5, touch_meas_out8, sar_touch_thres5, touch_out_th8) - (9, 32, sar_touch_out5, touch_meas_out9, sar_touch_thres5, touch_out_th9) + (0, 4, sar_touch_out1, touch_meas_out0, sar_touch_thres1, touch_out_th0) + (1, 0, sar_touch_out1, touch_meas_out1, sar_touch_thres1, touch_out_th1) + (2, 2, sar_touch_out2, touch_meas_out2, sar_touch_thres2, touch_out_th2) + (3, 15, sar_touch_out2, touch_meas_out3, sar_touch_thres2, touch_out_th3) + (4, 13, sar_touch_out3, touch_meas_out4, sar_touch_thres3, touch_out_th4) + (5, 12, sar_touch_out3, touch_meas_out5, sar_touch_thres3, touch_out_th5) + (6, 14, sar_touch_out4, touch_meas_out6, sar_touch_thres4, touch_out_th6) + (7, 27, sar_touch_out4, touch_meas_out7, sar_touch_thres4, touch_out_th7) + (8, 33, sar_touch_out5, touch_meas_out8, sar_touch_thres5, touch_out_th8) + (9, 32, sar_touch_out5, touch_meas_out9, sar_touch_thres5, touch_out_th9) } impl InterruptStatusRegisterAccess for InterruptStatusRegisterAccessBank0 { diff --git a/esp-hal/src/soc/esp32s3/gpio.rs b/esp-hal/src/soc/esp32s3/gpio.rs index 9cf339f1bd6..9c4598bfe8c 100644 --- a/esp-hal/src/soc/esp32s3/gpio.rs +++ b/esp-hal/src/soc/esp32s3/gpio.rs @@ -405,28 +405,28 @@ crate::gpio::gpio! { } crate::gpio::analog! { - ( 0, 0, touch_pad(0), mux_sel, fun_sel, fun_ie, rue, rde) - ( 1, 1, touch_pad(1), mux_sel, fun_sel, fun_ie, rue, rde) - ( 2, 2, touch_pad(2), mux_sel, fun_sel, fun_ie, rue, rde) - ( 3, 3, touch_pad(3), mux_sel, fun_sel, fun_ie, rue, rde) - ( 4, 4, touch_pad(4), mux_sel, fun_sel, fun_ie, rue, rde) - ( 5, 5, touch_pad(5), mux_sel, fun_sel, fun_ie, rue, rde) - ( 6, 6, touch_pad(6), mux_sel, fun_sel, fun_ie, rue, rde) - ( 7, 7, touch_pad(7), mux_sel, fun_sel, fun_ie, rue, rde) - ( 8, 8, touch_pad(8), mux_sel, fun_sel, fun_ie, rue, rde) - ( 9, 9, touch_pad(9), mux_sel, fun_sel, fun_ie, rue, rde) - (10, 10, touch_pad(10), mux_sel, fun_sel, fun_ie, rue, rde) - (11, 11, touch_pad(11), mux_sel, fun_sel, fun_ie, rue, rde) - (12, 12, touch_pad(12), mux_sel, fun_sel, fun_ie, rue, rde) - (13, 13, touch_pad(13), mux_sel, fun_sel, fun_ie, rue, rde) - (14, 14, touch_pad(14), mux_sel, fun_sel, fun_ie, rue, rde) - (15, 15, xtal_32p_pad(), x32p_mux_sel, x32p_fun_sel, x32p_fun_ie, x32p_rue, x32p_rde) - (16, 16, xtal_32n_pad(), x32n_mux_sel, x32n_fun_sel, x32n_fun_ie, x32n_rue, x32n_rde) - (17, 17, pad_dac1(), pdac1_mux_sel,pdac1_fun_sel,pdac1_fun_ie, pdac1_rue, pdac1_rde) - (18, 18, pad_dac2(), pdac2_mux_sel,pdac2_fun_sel,pdac2_fun_ie, pdac2_rue, pdac2_rde) - (19, 19, rtc_pad19(), mux_sel, fun_sel, fun_ie, rue, rde) - (20, 20, rtc_pad20(), mux_sel, fun_sel, fun_ie, rue, rde) - (21, 21, rtc_pad21(), mux_sel, fun_sel, fun_ie, rue, rde) + ( 0, 0, touch_pad(0), mux_sel, fun_sel, fun_ie, rue, rde) + ( 1, 1, touch_pad(1), mux_sel, fun_sel, fun_ie, rue, rde) + ( 2, 2, touch_pad(2), mux_sel, fun_sel, fun_ie, rue, rde) + ( 3, 3, touch_pad(3), mux_sel, fun_sel, fun_ie, rue, rde) + ( 4, 4, touch_pad(4), mux_sel, fun_sel, fun_ie, rue, rde) + ( 5, 5, touch_pad(5), mux_sel, fun_sel, fun_ie, rue, rde) + ( 6, 6, touch_pad(6), mux_sel, fun_sel, fun_ie, rue, rde) + ( 7, 7, touch_pad(7), mux_sel, fun_sel, fun_ie, rue, rde) + ( 8, 8, touch_pad(8), mux_sel, fun_sel, fun_ie, rue, rde) + ( 9, 9, touch_pad(9), mux_sel, fun_sel, fun_ie, rue, rde) + (10, 10, touch_pad(10), mux_sel, fun_sel, fun_ie, rue, rde) + (11, 11, touch_pad(11), mux_sel, fun_sel, fun_ie, rue, rde) + (12, 12, touch_pad(12), mux_sel, fun_sel, fun_ie, rue, rde) + (13, 13, touch_pad(13), mux_sel, fun_sel, fun_ie, rue, rde) + (14, 14, touch_pad(14), mux_sel, fun_sel, fun_ie, rue, rde) + (15, 15, xtal_32p_pad(), x32p_mux_sel, x32p_fun_sel, x32p_fun_ie, x32p_rue, x32p_rde) + (16, 16, xtal_32n_pad(), x32n_mux_sel, x32n_fun_sel, x32n_fun_ie, x32n_rue, x32n_rde) + (17, 17, pad_dac1(), pdac1_mux_sel,pdac1_fun_sel,pdac1_fun_ie, pdac1_rue, pdac1_rde) + (18, 18, pad_dac2(), pdac2_mux_sel,pdac2_fun_sel,pdac2_fun_ie, pdac2_rue, pdac2_rde) + (19, 19, rtc_pad19(), mux_sel, fun_sel, fun_ie, rue, rde) + (20, 20, rtc_pad20(), mux_sel, fun_sel, fun_ie, rue, rde) + (21, 21, rtc_pad21(), mux_sel, fun_sel, fun_ie, rue, rde) } crate::gpio::rtc_pins! { From 5fbaaebb332a6e4d77a36ac339b90f01ad5e8c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 13:04:29 +0200 Subject: [PATCH 09/19] Typo --- esp-hal/CHANGELOG.md | 1 + esp-hal/src/touch.rs | 32 ++++++++++++++++---------------- examples/src/bin/i2s_read.rs | 2 +- examples/src/bin/touch.rs | 6 +++--- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index bc9bd7c36c5..0933a7bccee 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - You can now create an `AnyPin` out of an `ErasedPin`. (#2072) - `Input`, `Output`, `OutputOpenDrain` and `Flex` are now type-erased by default. Use the new `new_typed` constructor to keep using the ZST pin types. (#2075) - To avoid confusion with the `Rtc::current_time` wall clock time APIs, we've renamed `esp_hal::time::current_time` to `esp_hal::time::now`. (#2091) +- Renamed `touch::Continous` to `touch::Continuous`. (#?) ### Fixed - SHA driver can now be safely used in multiple contexts concurrently (#2049) diff --git a/esp-hal/src/touch.rs b/esp-hal/src/touch.rs index e81034c5e04..3005d2e2984 100644 --- a/esp-hal/src/touch.rs +++ b/esp-hal/src/touch.rs @@ -13,7 +13,7 @@ //! # use esp_hal::gpio::Io; //! let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); //! let touch_pin0 = io.pins.gpio2; -//! let touch = Touch::continous_mode(peripherals.TOUCH, None); +//! let touch = Touch::continuous_mode(peripherals.TOUCH, None); //! let mut touchpad = TouchPad::new(touch_pin0, &touch); //! // ... give the peripheral some time for the measurement //! let touch_val = touchpad.read(); @@ -50,16 +50,16 @@ pub trait TouchMode: Sealed {} #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct OneShot; -/// Marker struct for the touch peripherals continous reading mode. In the +/// Marker struct for the touch peripherals continuous reading mode. In the /// technical reference manual, this is referred to as "start FSM via timer". #[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] -pub struct Continous; +pub struct Continuous; impl TouchMode for OneShot {} -impl TouchMode for Continous {} +impl TouchMode for Continuous {} impl Sealed for OneShot {} -impl Sealed for Continous {} +impl Sealed for Continuous {} /// Touchpad threshold type. #[derive(Debug, Copy, Clone)] @@ -81,7 +81,7 @@ pub struct TouchConfig { /// Duration of a single measurement (in cycles of the 8 MHz touch clock). /// Defaults to `0x7fff` pub measurement_duration: Option, - /// Sleep cycles for the touch timer in [`Continous`]-mode. Defaults to + /// Sleep cycles for the touch timer in [`Continuous`]-mode. Defaults to /// `0x100` pub sleep_cycles: Option, } @@ -147,8 +147,8 @@ impl<'d, TOUCHMODE: TouchMode, MODE: Mode> Touch<'d, TOUCHMODE, MODE> { }); } - /// Common parts of the continous mode initialization. - fn initialize_common_continoous(config: Option) { + /// Common parts of the continuous mode initialization. + fn initialize_common_continuous(config: Option) { let rtccntl = unsafe { &*RTC_CNTL::ptr() }; let sens = unsafe { &*SENS::ptr() }; @@ -241,8 +241,8 @@ impl<'d> Touch<'d, OneShot, Blocking> { } } } -impl<'d> Touch<'d, Continous, Blocking> { - /// Initializes the touch peripheral in continous mode and returns this +impl<'d> Touch<'d, Continuous, Blocking> { + /// Initializes the touch peripheral in continuous mode and returns this /// marker struct. Optionally accepts configuration options. /// /// ## Example @@ -254,16 +254,16 @@ impl<'d> Touch<'d, Continous, Blocking> { /// measurement_duration: Some(0x3000), /// ..Default::default() /// }); - /// let touch = Touch::continous_mode(peripherals.TOUCH, touch_cfg); + /// let touch = Touch::continuous_mode(peripherals.TOUCH, touch_cfg); /// # } /// ``` - pub fn continous_mode( + pub fn continuous_mode( touch_peripheral: impl Peripheral

+ 'd, config: Option, ) -> Self { crate::into_ref!(touch_peripheral); - Self::initialize_common_continoous(config); + Self::initialize_common_continuous(config); Self { _inner: touch_peripheral, @@ -272,8 +272,8 @@ impl<'d> Touch<'d, Continous, Blocking> { } } } -impl<'d> Touch<'d, Continous, Async> { - /// Initializes the touch peripheral in continous async mode and returns +impl<'d> Touch<'d, Continuous, Async> { + /// Initializes the touch peripheral in continuous async mode and returns /// this marker struct. /// /// ## Warning: @@ -307,7 +307,7 @@ impl<'d> Touch<'d, Continous, Async> { ) -> Self { crate::into_ref!(touch_peripheral); - Self::initialize_common_continoous(config); + Self::initialize_common_continuous(config); rtc.set_interrupt_handler(asynch::handle_touch_interrupt); diff --git a/examples/src/bin/i2s_read.rs b/examples/src/bin/i2s_read.rs index 3c6b18ee3e4..4d3a6483248 100644 --- a/examples/src/bin/i2s_read.rs +++ b/examples/src/bin/i2s_read.rs @@ -1,4 +1,4 @@ -//! This shows how to continously receive data via I2S. +//! This shows how to continuously receive data via I2S. //! //! Without an additional I2S source device you can connect 3V3 or GND to DIN //! to read 0 or 0xFF or connect DIN to WS to read two different values. diff --git a/examples/src/bin/touch.rs b/examples/src/bin/touch.rs index 0f34e6d74ce..22a324c28e7 100644 --- a/examples/src/bin/touch.rs +++ b/examples/src/bin/touch.rs @@ -21,12 +21,12 @@ use esp_hal::{ macros::ram, prelude::*, rtc_cntl::Rtc, - touch::{Continous, Touch, TouchConfig, TouchPad}, + touch::{Continuous, Touch, TouchConfig, TouchPad}, Blocking, }; use esp_println::println; -static TOUCH1: Mutex, Continous, Blocking>>>> = +static TOUCH1: Mutex, Continuous, Blocking>>>> = Mutex::new(RefCell::new(None)); #[handler] @@ -62,7 +62,7 @@ fn main() -> ! { ..Default::default() }); - let touch = Touch::continous_mode(peripherals.TOUCH, touch_cfg); + let touch = Touch::continuous_mode(peripherals.TOUCH, touch_cfg); let mut touch0 = TouchPad::new(touch_pin0, &touch); let mut touch1 = TouchPad::new(touch_pin1, &touch); From f4a7b983d175a64d103f19b2afdc7ecf6d3808fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 13:28:56 +0200 Subject: [PATCH 10/19] Slightly simplify the touch_into macro --- esp-hal/src/gpio/mod.rs | 112 ++++++++++++++-------------------- esp-hal/src/soc/esp32/gpio.rs | 2 +- 2 files changed, 47 insertions(+), 67 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index e92c50c323e..dd52b8c046c 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -1660,93 +1660,73 @@ macro_rules! analog { #[doc(hidden)] #[macro_export] macro_rules! touch_into { + (@pin_specific $touch_num:expr, true) => { + paste::paste! { + unsafe { &*RTC_IO::ptr() }.[< touch_pad $touch_num >]().write(|w| unsafe { + w + .xpd().set_bit() + // clear input_enable + .fun_ie().clear_bit() + // Connect pin to analog / RTC module instead of standard GPIO + .mux_sel().set_bit() + // Disable pull-up and pull-down resistors on the pin + .rue().clear_bit() + .rde().clear_bit() + .tie_opt().clear_bit() + // Select function "RTC function 1" (GPIO) for analog use + .fun_sel().bits(0b00) + }); + } + }; + + (@pin_specific $touch_num:expr, false) => { + paste::paste! { + unsafe { &*RTC_IO::ptr() }.[< touch_pad $touch_num >]().write(|w| + w + .xpd().set_bit() + .tie_opt().clear_bit() + ); + } + }; + ( - $( ( $touch_num:expr, $pin_num:expr, $rtc_pin:expr, $touch_thres_reg:expr, $touch_thres_field:expr , true ) )+ - --- - $( ( $touch_numx:expr, $pin_numx:expr, $rtc_pinx:expr, $touch_thres_regx:expr , $touch_thres_fieldx:expr , false ) )* + $( ( $touch_num:expr, $pin_num:expr, $rtc_pin:expr, $touch_thres_reg:expr, $touch_thres_field:expr , $needs_extra_setup:tt ))+ ) => { pub(crate) fn internal_into_touch(pin: u8) { - use $crate::peripherals::{GPIO, RTC_IO, SENS}; - - let rtcio = unsafe { &*RTC_IO::ptr() }; - let sens = unsafe { &*SENS::ptr() }; - let gpio = unsafe { &*GPIO::ptr() }; - match pin { $( $pin_num => { - paste::paste! { - // Pad to normal mode (not open-drain) - gpio.pin($rtc_pin).write(|w| w.pad_driver().clear_bit()); + use $crate::peripherals::{GPIO, RTC_IO, SENS}; - // clear output - rtcio.enable_w1tc().write(|w| unsafe { w.enable_w1tc().bits(1 << $rtc_pin) }); - sens - . $touch_thres_reg () - .write(|w| unsafe { - w. $touch_thres_field ().bits( - 0b0 // Default: 0 for esp32 gets overridden later anyway. - ) - }); + let gpio = unsafe { &*GPIO::ptr() }; + let rtcio = unsafe { &*RTC_IO::ptr() }; + let sens = unsafe { &*SENS::ptr() }; - rtcio.[< touch_pad $touch_num >]().write(|w| unsafe { - w - .xpd().set_bit() - // clear input_enable - .fun_ie().clear_bit() - // Connect pin to analog / RTC module instead of standard GPIO - .mux_sel().set_bit() - // Disable pull-up and pull-down resistors on the pin - .rue().clear_bit() - .rde().clear_bit() - .tie_opt().clear_bit() - // Select function "RTC function 1" (GPIO) for analog use - .fun_sel().bits(0b00) - }); + // Pad to normal mode (not open-drain) + gpio.pin($rtc_pin).write(|w| w.pad_driver().clear_bit()); - sens.sar_touch_enable().modify(|r, w| unsafe { - w - // enable the pin - .touch_pad_worken().bits( - r.touch_pad_worken().bits() | ( 1 << [< $touch_num >] ) - ) - }); - } - } - )+ - - $( - $pin_numx => { + // clear output + rtcio.enable_w1tc().write(|w| unsafe { w.enable_w1tc().bits(1 << $rtc_pin) }); paste::paste! { - // Pad to normal mode (not open-drain) - gpio.pin($rtc_pinx).write(|w| w.pad_driver().clear_bit()); - - // clear output - rtcio.enable_w1tc().write(|w| unsafe { w.enable_w1tc().bits(1 << $rtc_pinx) }); - sens - . $touch_thres_regx () + sens . $touch_thres_reg () .write(|w| unsafe { - w. $touch_thres_fieldx ().bits( + w. $touch_thres_field ().bits( 0b0 // Default: 0 for esp32 gets overridden later anyway. ) }); - rtcio.[< touch_pad $touch_numx >]().write(|w| - w - .xpd().set_bit() - .tie_opt().clear_bit() - ); + $crate::touch_into!( @pin_specific $touch_num, $needs_extra_setup ); + // enable the pin sens.sar_touch_enable().modify(|r, w| unsafe { - w - // enable the pin - .touch_pad_worken().bits( - r.touch_pad_worken().bits() | ( 1 << [< $touch_numx >] ) + w.touch_pad_worken().bits( + r.touch_pad_worken().bits() | ( 1 << [< $touch_num >] ) ) }); } } - )* + )+ + _ => unreachable!(), } } diff --git a/esp-hal/src/soc/esp32/gpio.rs b/esp-hal/src/soc/esp32/gpio.rs index 10757e09eb1..8b62dba181b 100644 --- a/esp-hal/src/soc/esp32/gpio.rs +++ b/esp-hal/src/soc/esp32/gpio.rs @@ -855,7 +855,7 @@ crate::gpio::touch_into! { (5, 12, 15, sar_touch_thres3, touch_out_th5, true ) (6, 14, 16, sar_touch_thres4, touch_out_th6, true ) (7, 27, 17, sar_touch_thres4, touch_out_th7, true ) - --- + // --- (8, 33, 8, sar_touch_thres5, touch_out_th8, false ) (9, 32, 9, sar_touch_thres5, touch_out_th9, false ) } From 82fabdffbf77831ae48cfd103e673863d2a20c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 13:55:44 +0200 Subject: [PATCH 11/19] Implement the AnalogPin trait directly --- esp-hal/src/gpio/mod.rs | 120 +++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 68 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index dd52b8c046c..e7151b6d725 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -1089,17 +1089,6 @@ where } } -#[cfg(any(adc, dac))] -impl AnalogPin for GpioPin -where - Self: GpioProperties, -{ - /// Configures the pin for analog mode. - fn set_analog(&self, _: private::Internal) { - crate::soc::gpio::internal_into_analog(GPIONUM); - } -} - #[cfg(touch)] impl TouchPin for GpioPin where @@ -1541,8 +1530,6 @@ macro_rules! rtc_pins { ( $( $pin_num:expr )+ ) => { $( $crate::gpio::rtc_pins!($pin_num); )+ }; } -// Following code enables `into_analog` - #[doc(hidden)] pub fn enable_iomux_clk_gate() { cfg_if::cfg_if! { @@ -1572,52 +1559,52 @@ macro_rules! analog { ) )+ ) => { - pub(crate) fn internal_into_analog(pin: u8) { - use $crate::peripherals::RTC_IO; + $( + #[cfg(any(adc, dac))] + impl $crate::gpio::AnalogPin for GpioPin<$pin_num> + where + Self: $crate::gpio::GpioProperties, + { + /// Configures the pin for analog mode. + fn set_analog(&self, _: $crate::private::Internal) { + let rtcio = unsafe{ &*$crate::peripherals::RTC_IO::ptr() }; - let rtcio = unsafe{ &*RTC_IO::ptr() }; - #[cfg(esp32s2)] - $crate::gpio::enable_iomux_clk_gate(); + #[cfg(esp32s2)] + $crate::gpio::enable_iomux_clk_gate(); - match pin { - $( - $pin_num => { - // We need `paste` (and a [< >] in it) to rewrite the token stream to - // handle indexed pins. - paste::paste! { - // disable input - rtcio.$pin_reg.modify(|_,w| w.$fun_ie().bit([< false >])); + // We need `paste` (and a [< >] in it) to rewrite the token stream to + // handle indexed pins. + paste::paste! { + // disable input + rtcio.$pin_reg.modify(|_,w| w.$fun_ie().bit([< false >])); - // disable output - rtcio.enable_w1tc().write(|w| unsafe { w.enable_w1tc().bits(1 << $rtc_pin) }); + // disable output + rtcio.enable_w1tc().write(|w| unsafe { w.enable_w1tc().bits(1 << $rtc_pin) }); - // disable open drain - rtcio.pin($rtc_pin).modify(|_,w| w.pad_driver().bit(false)); + // disable open drain + rtcio.pin($rtc_pin).modify(|_,w| w.pad_driver().bit(false)); - rtcio.$pin_reg.modify(|_,w| { - w.$fun_ie().clear_bit(); + rtcio.$pin_reg.modify(|_,w| { + w.$fun_ie().clear_bit(); - // Connect pin to analog / RTC module instead of standard GPIO - w.$mux_sel().set_bit(); + // Connect pin to analog / RTC module instead of standard GPIO + w.$mux_sel().set_bit(); - // Select function "RTC function 1" (GPIO) for analog use - unsafe { w.$fun_sel().bits(0b00) } - }); + // Select function "RTC function 1" (GPIO) for analog use + unsafe { w.$fun_sel().bits(0b00) }; // Disable pull-up and pull-down resistors on the pin, if it has them $( - rtcio.$pin_reg.modify(|_,w| { - w - .$rue().bit(false) - .$rde().bit(false) - }); + w.$rue().bit(false); + w.$rde().bit(false); )? - } + + w + }); } - )+ - _ => unreachable!(), + } } - } + )+ } } @@ -1628,30 +1615,27 @@ macro_rules! analog { ( $($pin_num:literal)+ ) => { - pub(crate) fn internal_into_analog(pin: u8) { - use $crate::peripherals::IO_MUX; - use $crate::peripherals::GPIO; - - let io_mux = unsafe{ &*IO_MUX::PTR }; - let gpio = unsafe{ &*GPIO::PTR }; - - match pin { - $( - $pin_num => { - io_mux.gpio($pin_num).modify(|_,w| unsafe { - w.mcu_sel().bits(1) - .fun_ie().clear_bit() - .fun_wpu().clear_bit() - .fun_wpd().clear_bit() - }); + $( + #[cfg(any(adc, dac))] + impl $crate::gpio::AnalogPin for GpioPin<$pin_num> + where + Self: $crate::gpio::GpioProperties, + { + /// Configures the pin for analog mode. + fn set_analog(&self, _: $crate::private::Internal) { + use $crate::peripherals::{GPIO}; + + get_io_mux_reg($pin_num).modify(|_,w| unsafe { + w.mcu_sel().bits(1) + .fun_ie().clear_bit() + .fun_wpu().clear_bit() + .fun_wpd().clear_bit() + }); - gpio.enable_w1tc().write(|w| unsafe { w.bits(1 << $pin_num) }); - } - )+ - _ => unreachable!() + unsafe{ &*GPIO::PTR }.enable_w1tc().write(|w| unsafe { w.bits(1 << $pin_num) }); + } } - - } + )+ } } From 78003f72546bbcffb8550911d84d16c14bb5037f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 14:23:14 +0200 Subject: [PATCH 12/19] Provide default impls for simple forwarding methods --- esp-hal/src/gpio/any_pin.rs | 21 ------------- esp-hal/src/gpio/mod.rs | 59 ++++++++++++------------------------- 2 files changed, 19 insertions(+), 61 deletions(-) diff --git a/esp-hal/src/gpio/any_pin.rs b/esp-hal/src/gpio/any_pin.rs index c5ccd425283..93c59da4d93 100644 --- a/esp-hal/src/gpio/any_pin.rs +++ b/esp-hal/src/gpio/any_pin.rs @@ -78,7 +78,6 @@ impl<'d> Pin for AnyPin<'d> { fn unlisten(&mut self, _internal: private::Internal); fn is_interrupt_set(&self, _internal: private::Internal) -> bool; fn clear_interrupt(&mut self, _internal: private::Internal); - fn wakeup_enable(&mut self, enable: bool, event: WakeEvent, _internal: private::Internal); } } } @@ -102,17 +101,6 @@ impl<'d> OutputPin for AnyPin<'d> { } } - fn connect_peripheral_to_output(&mut self, signal: OutputSignal, _internal: private::Internal) { - self.pin.connect_peripheral_to_output_with_options( - signal, - self.is_inverted, - false, - false, - self.is_inverted, - private::Internal, - ); - } - fn connect_peripheral_to_output_with_options( &mut self, signal: OutputSignal, @@ -155,15 +143,6 @@ impl<'d> InputPin for AnyPin<'d> { } } - fn connect_input_to_peripheral(&mut self, signal: InputSignal, _internal: private::Internal) { - self.pin.connect_input_to_peripheral_with_options( - signal, - self.is_inverted, - self.is_inverted, - private::Internal, - ); - } - fn connect_input_to_peripheral_with_options( &mut self, signal: InputSignal, diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index e7151b6d725..fe259d318db 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -321,7 +321,9 @@ pub trait Pin: private::Sealed { fn clear_interrupt(&mut self, _: private::Internal); /// Enable this pin as a wake up source - fn wakeup_enable(&mut self, enable: bool, event: WakeEvent, _: private::Internal); + fn wakeup_enable(&mut self, enable: bool, event: WakeEvent, _: private::Internal) { + self.listen_with_options(event.into(), false, false, enable, private::Internal); + } } /// Trait implemented by pins which can be used as inputs @@ -339,7 +341,9 @@ pub trait InputPin: Pin { fn is_input_high(&self, _: private::Internal) -> bool; /// Connect the pin to a peripheral input signal - fn connect_input_to_peripheral(&mut self, signal: InputSignal, _: private::Internal); + fn connect_input_to_peripheral(&mut self, signal: InputSignal, _: private::Internal) { + self.connect_input_to_peripheral_with_options(signal, false, false, private::Internal); + } /// Connect the pin to a peripheral input signal. /// @@ -397,7 +401,16 @@ pub trait OutputPin: Pin { fn internal_pull_down(&mut self, on: bool, _: private::Internal); /// Connect the pin to a peripheral output signal - fn connect_peripheral_to_output(&mut self, signal: OutputSignal, _: private::Internal); + fn connect_peripheral_to_output(&mut self, signal: OutputSignal, _: private::Internal) { + self.connect_peripheral_to_output_with_options( + signal, + false, + false, + false, + false, + private::Internal, + ) + } /// Connect the pin to a peripheral output signal. /// @@ -777,10 +790,6 @@ where ::Bank::read_input() & (1 << (GPIONUM % 32)) != 0 } - fn connect_input_to_peripheral(&mut self, signal: InputSignal, _: private::Internal) { - self.connect_input_to_peripheral_with_options(signal, false, false, private::Internal); - } - fn connect_input_to_peripheral_with_options( &mut self, signal: InputSignal, @@ -895,10 +904,6 @@ where fn clear_interrupt(&mut self, _: private::Internal) { ::Bank::write_interrupt_status_clear(1 << (GPIONUM % 32)); } - - fn wakeup_enable(&mut self, enable: bool, event: WakeEvent, _: private::Internal) { - self.listen_with_options(event.into(), false, false, enable, private::Internal); - } } impl crate::peripheral::Peripheral for GpioPin @@ -989,9 +994,11 @@ where fn internal_pull_up_in_sleep_mode(&mut self, on: bool, _: private::Internal) { get_io_mux_reg(GPIONUM).modify(|_, w| w.mcu_wpu().bit(on)); } + fn internal_pull_down_in_sleep_mode(&mut self, on: bool, _: private::Internal) { get_io_mux_reg(GPIONUM).modify(|_, w| w.mcu_wpd().bit(on)); } + fn enable_output_in_sleep_mode(&mut self, on: bool, _: private::Internal) { get_io_mux_reg(GPIONUM).modify(|_, w| w.mcu_oe().bit(on)); } @@ -1002,6 +1009,7 @@ where get_io_mux_reg(GPIONUM).modify(|_, w| w.fun_wpu().bit(on)); } + fn internal_pull_down(&mut self, on: bool, _: private::Internal) { #[cfg(esp32)] crate::soc::gpio::errata36(GPIONUM, None, Some(on)); @@ -1009,17 +1017,6 @@ where get_io_mux_reg(GPIONUM).modify(|_, w| w.fun_wpd().bit(on)); } - fn connect_peripheral_to_output(&mut self, signal: OutputSignal, _: private::Internal) { - self.connect_peripheral_to_output_with_options( - signal, - false, - false, - false, - false, - private::Internal, - ) - } - fn connect_peripheral_to_output_with_options( &mut self, signal: OutputSignal, @@ -2293,12 +2290,6 @@ pub(crate) mod internal { Pin::clear_interrupt(target, private::Internal) }) } - - fn wakeup_enable(&mut self, enable: bool, event: WakeEvent, _: private::Internal) { - handle_gpio_input!(self, target, { - Pin::wakeup_enable(target, enable, event, private::Internal) - }) - } } impl InputPin for ErasedPin { @@ -2326,12 +2317,6 @@ pub(crate) mod internal { }) } - fn connect_input_to_peripheral(&mut self, signal: InputSignal, _: private::Internal) { - handle_gpio_input!(self, target, { - InputPin::connect_input_to_peripheral(target, signal, private::Internal) - }); - } - fn connect_input_to_peripheral_with_options( &mut self, signal: InputSignal, @@ -2424,12 +2409,6 @@ pub(crate) mod internal { }); } - fn connect_peripheral_to_output(&mut self, signal: OutputSignal, _: private::Internal) { - handle_gpio_output!(self, target, { - OutputPin::connect_peripheral_to_output(target, signal, private::Internal) - }); - } - fn connect_peripheral_to_output_with_options( &mut self, signal: OutputSignal, From 6a67e4b203d7dc484721a5fb6c7bfb033ab8a24a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 15:25:07 +0200 Subject: [PATCH 13/19] Newtype ErasedPin --- esp-hal/src/gpio/mod.rs | 96 +++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index fe259d318db..99da4f27cec 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -886,7 +886,12 @@ where } } - set_int_enable(GPIONUM, gpio_intr_enable(int_enable, nmi_enable), event as u8, wake_up_from_light_sleep) + set_int_enable( + GPIONUM, + gpio_intr_enable(int_enable, nmi_enable), + event as u8, + wake_up_from_light_sleep, + ) } fn unlisten(&mut self, _: private::Internal) { @@ -1265,7 +1270,7 @@ macro_rules! gpio { $crate::pin_types!($type); fn degrade_pin(&self, _: $crate::private::Internal) -> ErasedPin { - $crate::gpio::ErasedPin::[< Gpio $gpionum >](unsafe { Self::steal() }) + ErasedPin($crate::gpio::ErasedPinInner::[< Gpio $gpionum >](unsafe { Self::steal() })) } } @@ -1308,21 +1313,21 @@ macro_rules! gpio { )+ } - #[doc(hidden)] - pub enum ErasedPin { + pub(crate) enum ErasedPinInner { $( [](GpioPin<$gpionum>), )+ } + /// Type-erased GPIO pin + pub struct ErasedPin(pub(crate) ErasedPinInner); + impl ErasedPin { pub(crate) unsafe fn clone_unchecked(&self) -> Self { - match self { - $( - ErasedPin::[](_) => { - ErasedPin::[< Gpio $gpionum >](unsafe { GpioPin::steal() }) - } - )+ + match self.0 { + $(ErasedPinInner::[](_) => { + Self(ErasedPinInner::[< Gpio $gpionum >](unsafe { GpioPin::steal() })) + })+ } } } @@ -1339,10 +1344,10 @@ macro_rules! gpio { #[doc(hidden)] #[macro_export] macro_rules! handle_gpio_output { - ($this:ident, $inner:ident, $code:tt) => { + ($this:expr, $inner:ident, $code:tt) => { match $this { $( - ErasedPin::[]($inner) => if_output_pin!($type, { + ErasedPinInner::[]($inner) => if_output_pin!($type, { $code } else {{ let _ = $inner; @@ -1356,10 +1361,10 @@ macro_rules! gpio { #[doc(hidden)] #[macro_export] macro_rules! handle_gpio_input { - ($this:ident, $inner:ident, $code:tt) => { + ($this:expr, $inner:ident, $code:tt) => { match $this { $( - ErasedPin::[]($inner) => $code + ErasedPinInner::[]($inner) => $code )+ } } @@ -2236,7 +2241,7 @@ pub(crate) mod internal { impl Pin for ErasedPin { fn number(&self, _: private::Internal) -> u8 { - handle_gpio_input!(self, target, { Pin::number(target, private::Internal) }) + handle_gpio_input!(&self.0, target, { Pin::number(target, private::Internal) }) } fn degrade_internal(&self, _: private::Internal) -> ErasedPin { @@ -2244,13 +2249,13 @@ pub(crate) mod internal { } fn sleep_mode(&mut self, on: bool, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { Pin::sleep_mode(target, on, private::Internal) }) } fn set_alternate_function(&mut self, alternate: AlternateFunction, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { Pin::set_alternate_function(target, alternate, private::Internal) }) } @@ -2263,7 +2268,7 @@ pub(crate) mod internal { wake_up_from_light_sleep: bool, _: private::Internal, ) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { Pin::listen_with_options( target, event, @@ -2276,17 +2281,19 @@ pub(crate) mod internal { } fn unlisten(&mut self, _: private::Internal) { - handle_gpio_input!(self, target, { Pin::unlisten(target, private::Internal) }) + handle_gpio_input!(&mut self.0, target, { + Pin::unlisten(target, private::Internal) + }) } fn is_interrupt_set(&self, _: private::Internal) -> bool { - handle_gpio_input!(self, target, { + handle_gpio_input!(&self.0, target, { Pin::is_interrupt_set(target, private::Internal) }) } fn clear_interrupt(&mut self, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { Pin::clear_interrupt(target, private::Internal) }) } @@ -2294,25 +2301,25 @@ pub(crate) mod internal { impl InputPin for ErasedPin { fn init_input(&self, pull_down: bool, pull_up: bool, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&self.0, target, { InputPin::init_input(target, pull_down, pull_up, private::Internal) }) } fn enable_input(&mut self, on: bool, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { InputPin::enable_input(target, on, private::Internal) }); } fn enable_input_in_sleep_mode(&mut self, on: bool, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { InputPin::enable_input_in_sleep_mode(target, on, private::Internal) }); } fn is_input_high(&self, _: private::Internal) -> bool { - handle_gpio_input!(self, target, { + handle_gpio_input!(&self.0, target, { InputPin::is_input_high(target, private::Internal) }) } @@ -2324,7 +2331,7 @@ pub(crate) mod internal { force_via_gpio_mux: bool, _: private::Internal, ) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { InputPin::connect_input_to_peripheral_with_options( target, signal, @@ -2336,7 +2343,7 @@ pub(crate) mod internal { } fn disconnect_input_from_peripheral(&mut self, signal: InputSignal, _: private::Internal) { - handle_gpio_input!(self, target, { + handle_gpio_input!(&mut self.0, target, { InputPin::disconnect_input_from_peripheral(target, signal, private::Internal) }); } @@ -2344,67 +2351,67 @@ pub(crate) mod internal { impl OutputPin for ErasedPin { fn set_to_open_drain_output(&mut self, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::set_to_open_drain_output(target, private::Internal) }); } fn set_to_push_pull_output(&mut self, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::set_to_push_pull_output(target, private::Internal) }); } fn enable_output(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::enable_output(target, on, private::Internal) }); } fn set_output_high(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::set_output_high(target, on, private::Internal) }); } fn set_drive_strength(&mut self, strength: DriveStrength, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::set_drive_strength(target, strength, private::Internal) }); } fn enable_open_drain(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::enable_open_drain(target, on, private::Internal) }); } fn enable_output_in_sleep_mode(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::enable_output_in_sleep_mode(target, on, private::Internal) }); } fn internal_pull_up_in_sleep_mode(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::internal_pull_up_in_sleep_mode(target, on, private::Internal) }); } fn internal_pull_down_in_sleep_mode(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::internal_pull_down_in_sleep_mode(target, on, private::Internal) }); } fn internal_pull_up(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::internal_pull_up(target, on, private::Internal) }); } fn internal_pull_down(&mut self, on: bool, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::internal_pull_down(target, on, private::Internal) }); } @@ -2418,7 +2425,7 @@ pub(crate) mod internal { force_via_gpio_mux: bool, _: private::Internal, ) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::connect_peripheral_to_output_with_options( target, signal, @@ -2432,13 +2439,13 @@ pub(crate) mod internal { } fn disconnect_peripheral_from_output(&mut self, _: private::Internal) { - handle_gpio_output!(self, target, { + handle_gpio_output!(&mut self.0, target, { OutputPin::disconnect_peripheral_from_output(target, private::Internal) }); } fn is_set_high(&self, _: private::Internal) -> bool { - handle_gpio_output!(self, target, { + handle_gpio_output!(&self.0, target, { OutputPin::is_set_high(target, private::Internal) }) } @@ -2454,12 +2461,7 @@ fn is_listening(pin_num: u8) -> bool { bits != 0 } -fn set_int_enable( - gpio_num: u8, - int_ena: u8, - int_type: u8, - wake_up_from_light_sleep: bool, -) { +fn set_int_enable(gpio_num: u8, int_ena: u8, int_type: u8, wake_up_from_light_sleep: bool) { let gpio = unsafe { &*crate::peripherals::GPIO::PTR }; gpio.pin(gpio_num as usize).modify(|_, w| unsafe { w.int_ena() From 5199641bc413028d706e25186b73aa3995744498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 16:49:40 +0200 Subject: [PATCH 14/19] Merge rtc_pin macros --- esp-hal/src/gpio/mod.rs | 60 ++++++++++------------------------------- 1 file changed, 14 insertions(+), 46 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index 99da4f27cec..6c4e773123f 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -1450,7 +1450,7 @@ macro_rules! rtc_pins { }; } -#[cfg(esp32c3)] +#[cfg(any(esp32c2, esp32c3))] #[doc(hidden)] #[macro_export] macro_rules! rtc_pins { @@ -1460,51 +1460,19 @@ macro_rules! rtc_pins { impl $crate::gpio::RtcPin for GpioPin<$pin_num> { unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) { let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() }; - paste::paste! { - rtc_cntl.gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup)); - rtc_cntl.gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level)); - } - } - - fn rtcio_pad_hold(&mut self, enable: bool) { - let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() }; - paste::paste! { - rtc_cntl.pad_hold().modify(|_, w| w.[< gpio_pin $pin_num _hold >]().bit(enable)); - } - } - } - - impl $crate::gpio::RtcPinWithResistors for GpioPin<$pin_num> { - fn rtcio_pullup(&mut self, enable: bool) { - let io_mux = unsafe { &*$crate::peripherals::IO_MUX::ptr() }; - io_mux.gpio($pin_num).modify(|_, w| w.fun_wpu().bit(enable)); - } - - fn rtcio_pulldown(&mut self, enable: bool) { - let io_mux = unsafe { &*$crate::peripherals::IO_MUX::ptr() }; - io_mux.gpio($pin_num).modify(|_, w| w.fun_wpd().bit(enable)); - } - } - - }; - - ( $( $pin_num:expr )+ ) => { $( $crate::gpio::rtc_pins!($pin_num); )+ }; -} - -#[cfg(esp32c2)] -#[doc(hidden)] -#[macro_export] -macro_rules! rtc_pins { - ( - $pin_num:expr - ) => { - impl $crate::gpio::RtcPin for GpioPin<$pin_num> { - unsafe fn apply_wakeup(&mut self, wakeup: bool, level: u8) { - let rtc_cntl = unsafe { &*$crate::peripherals::RTC_CNTL::ptr() }; - paste::paste! { - rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup)); - rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level)); - } + cfg_if::cfg_if! { + if #[cfg(esp32c2)] { + paste::paste! { + rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup)); + rtc_cntl.cntl_gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level)); + } + } else { + paste::paste! { + rtc_cntl.gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _wakeup_enable >]().bit(wakeup)); + rtc_cntl.gpio_wakeup().modify(|_, w| w.[< gpio_pin $pin_num _int_type >]().bits(level)); + } + } + }; } fn rtcio_pad_hold(&mut self, enable: bool) { From 410d0e45bb44418afa373442115f1fcff6ff31fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 16:55:08 +0200 Subject: [PATCH 15/19] Fmt --- esp-hal/src/parl_io.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/esp-hal/src/parl_io.rs b/esp-hal/src/parl_io.rs index 97fb8a0ad7c..75f298c3577 100644 --- a/esp-hal/src/parl_io.rs +++ b/esp-hal/src/parl_io.rs @@ -634,7 +634,8 @@ where { fn configure(&mut self) -> Result<(), Error> { self.rx_pins.configure()?; - self.valid_pin.init_input(false, false, crate::private::Internal); + self.valid_pin + .init_input(false, false, crate::private::Internal); self.valid_pin .connect_input_to_peripheral(Instance::rx_valid_pin_signal(), crate::private::Internal); Instance::set_rx_sw_en(false); From 24f67c9ea42984f599c1bf9f61a24c9d06fe56b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 16:56:34 +0200 Subject: [PATCH 16/19] Changelog --- esp-hal/CHANGELOG.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 0933a7bccee..7fa0d67ffbb 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -17,30 +17,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added sleep and wakeup support for esp32c2 (#1922) ### Changed -- Make saving and restoring SHA digest state an explicit operation (#2049) +- Make saving and restoring SHA digest state an explicit operation (#2049) - `Delay::new()` is now a `const` function (#1999) - You can now create an `AnyPin` out of an `ErasedPin`. (#2072) - `Input`, `Output`, `OutputOpenDrain` and `Flex` are now type-erased by default. Use the new `new_typed` constructor to keep using the ZST pin types. (#2075) - To avoid confusion with the `Rtc::current_time` wall clock time APIs, we've renamed `esp_hal::time::current_time` to `esp_hal::time::now`. (#2091) -- Renamed `touch::Continous` to `touch::Continuous`. (#?) +- Renamed `touch::Continous` to `touch::Continuous`. (#2094) +- The (previously undocumented) `ErasedPin` enum has been replaced with the `ErasedPin` struct. (#2094) ### Fixed -- SHA driver can now be safely used in multiple contexts concurrently (#2049) +- SHA driver can now be safely used in multiple contexts concurrently (#2049) - Fixed an issue with DMA transfers potentially not waking up the correct async task (#2065) - Fixed an issue with LCD_CAM i8080 where it would send double the clocks in 16bit mode (#2085) - Fix i2c embedded-hal transaction (#2028) ### Removed -- Removed `digest::Digest` implementation from SHA (#2049) +- Removed `digest::Digest` implementation from SHA (#2049) - Removed `NoPinType` in favour of `DummyPin`. (#2068) - Removed the `async`, `embedded-hal-02`, `embedded-hal`, `embedded-io`, `embedded-io-async`, and `ufmt` features (#2070) - Removed the `GpioN` type aliasses. Use `GpioPin` instead. (#2073) - Removed `Peripherals::take`. Use `esp_hal::init` to obtain `Peripherals` (#1999) - Removed `AnyInputOnlyPin` in favour of `AnyPin`. (#2071) -- Removed the following functions from `GpioPin`: `is_high`, `is_low`, `set_high`, `set_low`, `set_state`, `is_set_high`, `is_set_low`, `toggle`. +- Removed the following functions from `GpioPin`: `is_high`, `is_low`, `set_high`, `set_low`, `set_state`, `is_set_high`, `is_set_low`, `toggle`. (#2094) ## [0.20.1] - 2024-08-30 From a97780f4412ec08e4653fb6f777f5c23c370a9fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 17:13:04 +0200 Subject: [PATCH 17/19] Fix migration guide --- esp-hal/MIGRATING-0.20.md | 1 + 1 file changed, 1 insertion(+) diff --git a/esp-hal/MIGRATING-0.20.md b/esp-hal/MIGRATING-0.20.md index e48490ae6e5..93f9e74cb03 100644 --- a/esp-hal/MIGRATING-0.20.md +++ b/esp-hal/MIGRATING-0.20.md @@ -50,6 +50,7 @@ However, if you want to, you can keep using their typed form! ```rust let pin = Input::new(io.gpio0); // pin will have the type `Input<'some>` (or `Input<'some, ErasedPin>` if you want to be explicit about it) let pin = Input::new_typed(io.gpio0); // pin will have the type `Input<'some, GpioPin<0>>` +``` ## `esp_hal::time::current_time` rename From d8549a672dc67972f539078f90b4a850b8a84ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 17:18:01 +0200 Subject: [PATCH 18/19] Fix example --- examples/src/bin/rmt_rx.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/src/bin/rmt_rx.rs b/examples/src/bin/rmt_rx.rs index 16fffe80b77..948b6c8c6ef 100644 --- a/examples/src/bin/rmt_rx.rs +++ b/examples/src/bin/rmt_rx.rs @@ -14,7 +14,7 @@ use esp_backtrace as _; use esp_hal::{ delay::Delay, - gpio::Io, + gpio::{Io, Level, Output}, prelude::*, rmt::{PulseCode, Rmt, RxChannel, RxChannelConfig, RxChannelCreator}, }; @@ -27,7 +27,7 @@ fn main() -> ! { let peripherals = esp_hal::init(esp_hal::Config::default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - let mut out = io.pins.gpio5; + let mut out = Output::new(io.pins.gpio5, Level::Low); cfg_if::cfg_if! { if #[cfg(feature = "esp32h2")] { From b33219661e693d0cfb828a6a33eb1229fdd3045c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 5 Sep 2024 18:10:32 +0200 Subject: [PATCH 19/19] Fix ETM --- esp-hal/CHANGELOG.md | 1 + esp-hal/src/gpio/mod.rs | 69 ++++++++++++++++++++++++------------ esp-hal/src/peripheral.rs | 6 ++-- examples/src/bin/etm_gpio.rs | 3 +- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 7fa0d67ffbb..6a50da292fe 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added missing functions to `Flex`: `unlisten`, `is_interrupt_set`, `wakeup_enable`, `wait_for_high`, `wait_for_low`, `wait_for_rising_edge`, `wait_for_falling_edge`, `wait_for_any_edge`. (#2075) - `Flex` now implements `Wait`. (#2075) - Added sleep and wakeup support for esp32c2 (#1922) +- `Input`, `Output`, `OutputOpenDrain` and `Flex` now implement `Peripheral`. (#2094) ### Changed diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index 6c4e773123f..f4db13a6489 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -65,7 +65,7 @@ pub(crate) use crate::rtc_pins; pub use crate::soc::gpio::*; use crate::{ interrupt::InterruptHandler, - peripheral::PeripheralRef, + peripheral::{Peripheral, PeripheralRef}, peripherals::{GPIO, IO_MUX}, private, InterruptConfigurable, @@ -911,7 +911,7 @@ where } } -impl crate::peripheral::Peripheral for GpioPin +impl Peripheral for GpioPin where Self: GpioProperties, { @@ -1744,13 +1744,19 @@ pub struct Output<'d, P = ErasedPin> { pin: Flex<'d, P>, } +impl

private::Sealed for Output<'_, P> {} + +impl<'d, P> Peripheral for Output<'d, P> { + type P = P; + unsafe fn clone_unchecked(&mut self) -> P { + self.pin.clone_unchecked() + } +} + impl<'d> Output<'d> { /// Create GPIO output driver for a [GpioPin] with the provided level #[inline] - pub fn new( - pin: impl crate::peripheral::Peripheral

+ 'd, - initial_output: Level, - ) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, initial_output: Level) -> Self { let pin = Flex::new(pin); Self::new_inner(pin, initial_output) @@ -1763,10 +1769,7 @@ where { /// Create GPIO output driver for a [GpioPin] with the provided level #[inline] - pub fn new_typed( - pin: impl crate::peripheral::Peripheral

+ 'd, - initial_output: Level, - ) -> Self { + pub fn new_typed(pin: impl Peripheral

+ 'd, initial_output: Level) -> Self { let pin = Flex::new_typed(pin); Self::new_inner(pin, initial_output) @@ -1834,14 +1837,20 @@ pub struct Input<'d, P = ErasedPin> { pin: Flex<'d, P>, } +impl

private::Sealed for Input<'_, P> {} + +impl<'d, P> Peripheral for Input<'d, P> { + type P = P; + unsafe fn clone_unchecked(&mut self) -> P { + self.pin.clone_unchecked() + } +} + impl<'d> Input<'d> { /// Create GPIO input driver for a [Pin] with the provided [Pull] /// configuration. #[inline] - pub fn new( - pin: impl crate::peripheral::Peripheral

+ 'd, - pull: Pull, - ) -> Self { + pub fn new(pin: impl Peripheral

+ 'd, pull: Pull) -> Self { let pin = Flex::new(pin); Self::new_inner(pin, pull) @@ -1855,7 +1864,7 @@ where /// Create GPIO input driver for a [Pin] with the provided [Pull] /// configuration. #[inline] - pub fn new_typed(pin: impl crate::peripheral::Peripheral

+ 'd, pull: Pull) -> Self { + pub fn new_typed(pin: impl Peripheral

+ 'd, pull: Pull) -> Self { let pin = Flex::new_typed(pin); Self::new_inner(pin, pull) @@ -1922,12 +1931,21 @@ pub struct OutputOpenDrain<'d, P = ErasedPin> { pin: Flex<'d, P>, } +impl

private::Sealed for OutputOpenDrain<'_, P> {} + +impl<'d, P> Peripheral for OutputOpenDrain<'d, P> { + type P = P; + unsafe fn clone_unchecked(&mut self) -> P { + self.pin.clone_unchecked() + } +} + impl<'d> OutputOpenDrain<'d> { /// Create GPIO open-drain output driver for a [Pin] with the provided /// initial output-level and [Pull] configuration. #[inline] pub fn new( - pin: impl crate::peripheral::Peripheral

+ 'd, + pin: impl Peripheral

+ 'd, initial_output: Level, pull: Pull, ) -> Self { @@ -1944,11 +1962,7 @@ where /// Create GPIO open-drain output driver for a [Pin] with the provided /// initial output-level and [Pull] configuration. #[inline] - pub fn new_typed( - pin: impl crate::peripheral::Peripheral

+ 'd, - initial_output: Level, - pull: Pull, - ) -> Self { + pub fn new_typed(pin: impl Peripheral

+ 'd, initial_output: Level, pull: Pull) -> Self { let pin = Flex::new_typed(pin); Self::new_inner(pin, initial_output, pull) @@ -2046,11 +2060,20 @@ pub struct Flex<'d, P = ErasedPin> { pin: PeripheralRef<'d, P>, } +impl

private::Sealed for Flex<'_, P> {} + +impl<'d, P> Peripheral for Flex<'d, P> { + type P = P; + unsafe fn clone_unchecked(&mut self) -> P { + core::ptr::read(&*self.pin as *const _) + } +} + impl<'d> Flex<'d> { /// Create flexible pin driver for a [Pin]. /// No mode change happens. #[inline] - pub fn new(pin: impl crate::peripheral::Peripheral

+ 'd) -> Self { + pub fn new(pin: impl Peripheral

+ 'd) -> Self { crate::into_ref!(pin); let pin = pin.degrade_internal(private::Internal); Self::new_typed(pin) @@ -2064,7 +2087,7 @@ where /// Create flexible pin driver for a [Pin]. /// No mode change happens. #[inline] - pub fn new_typed(pin: impl crate::peripheral::Peripheral

+ 'd) -> Self { + pub fn new_typed(pin: impl Peripheral

+ 'd) -> Self { crate::into_ref!(pin); Self { pin } } diff --git a/esp-hal/src/peripheral.rs b/esp-hal/src/peripheral.rs index bdba4866b27..3f56ab9a3fc 100644 --- a/esp-hal/src/peripheral.rs +++ b/esp-hal/src/peripheral.rs @@ -194,11 +194,11 @@ pub trait Peripheral: Sized + crate::private::Sealed { } } -impl Peripheral for &mut T +impl Peripheral for &mut T where - T: Peripheral

, + T: Peripheral

, { - type P = T; + type P = P; unsafe fn clone_unchecked(&mut self) -> Self::P { T::clone_unchecked(self) diff --git a/examples/src/bin/etm_gpio.rs b/examples/src/bin/etm_gpio.rs index 25bcf9b9a92..112fa2db789 100644 --- a/examples/src/bin/etm_gpio.rs +++ b/examples/src/bin/etm_gpio.rs @@ -15,6 +15,7 @@ use esp_hal::{ etm::{GpioEtmChannels, GpioEtmInputConfig, GpioEtmOutputConfig}, Io, Level, + Output, Pull, }, prelude::*, @@ -25,7 +26,7 @@ fn main() -> ! { let peripherals = esp_hal::init(esp_hal::Config::default()); let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); - let mut led = io.pins.gpio1; + let mut led = Output::new(io.pins.gpio1, Level::Low); let button = io.pins.gpio9; led.set_high();