From 5e80ef09651c4a40bd2bd1404953aabd50caf3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 20 Dec 2024 14:18:05 +0100 Subject: [PATCH] Remove panicking match from Flex::set_level --- esp-hal/src/gpio/mod.rs | 19 +++----- examples/src/bin/gpio_interrupt.rs | 77 ++++-------------------------- 2 files changed, 14 insertions(+), 82 deletions(-) diff --git a/esp-hal/src/gpio/mod.rs b/esp-hal/src/gpio/mod.rs index dfed16969b..1d47ee8dd6 100644 --- a/esp-hal/src/gpio/mod.rs +++ b/esp-hal/src/gpio/mod.rs @@ -1029,6 +1029,7 @@ macro_rules! gpio { )* impl $crate::gpio::Pin for $crate::gpio::GpioPin<$gpionum> { + #[inline(always)] fn number(&self) -> u8 { $gpionum } @@ -2037,8 +2038,8 @@ where /// Toggle pin output #[inline] pub fn toggle(&mut self) { - let level = !self.output_level(); - self.set_level(level); + let level = self.output_level(); + self.set_level(!level); } /// Configure the [DriveStrength] of the pin @@ -2124,6 +2125,7 @@ pub(crate) mod internal { } impl Pin for AnyPin { + #[inline(always)] fn number(&self) -> u8 { handle_gpio_input!(&self.0, target, { Pin::number(target) }) } @@ -2191,11 +2193,8 @@ pub(crate) mod internal { }) } - fn set_output_high(&mut self, high: bool, _: private::Internal) { - handle_gpio_output!(&mut self.0, target, { - OutputPin::set_output_high(target, high, private::Internal) - }) - } + // We use the default `set_output_high` implementation to avoid matching on pin + // type. We check the pin type to enable output functionality anyway. fn set_drive_strength(&mut self, strength: DriveStrength, _: private::Internal) { handle_gpio_output!(&mut self.0, target, { @@ -2220,12 +2219,6 @@ pub(crate) mod internal { OutputPin::internal_pull_down_in_sleep_mode(target, on, private::Internal) }) } - - fn is_set_high(&self, _: private::Internal) -> bool { - handle_gpio_output!(&self.0, target, { - OutputPin::is_set_high(target, private::Internal) - }) - } } #[cfg(any(xtensa, esp32c2, esp32c3, esp32c6))] diff --git a/examples/src/bin/gpio_interrupt.rs b/examples/src/bin/gpio_interrupt.rs index 1675738fb2..e52615dd69 100644 --- a/examples/src/bin/gpio_interrupt.rs +++ b/examples/src/bin/gpio_interrupt.rs @@ -8,88 +8,27 @@ //! - BUTTON => GPIO0 (ESP32, ESP32-S2, ESP32-S3) / GPIO9 //% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3 - #![no_std] #![no_main] -use core::cell::RefCell; - -use critical_section::Mutex; use esp_backtrace as _; use esp_hal::{ delay::Delay, - entry, - gpio::{Event, Input, Io, Level, Output, Pull}, - interrupt::InterruptConfigurable, - macros::{handler, ram}, + gpio::{Input, Level, Output, Pin, Pull}, }; -static BUTTON: Mutex>> = Mutex::new(RefCell::new(None)); - -#[entry] +#[esp_hal::entry] fn main() -> ! { let peripherals = esp_hal::init(esp_hal::Config::default()); - // Set GPIO2 as an output, and set its state high initially. - let mut io = Io::new(peripherals.IO_MUX); - io.set_interrupt_handler(handler); - let mut led = Output::new(peripherals.GPIO2, Level::Low); - - cfg_if::cfg_if! { - if #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] { - let button = peripherals.GPIO0; - } else { - let button = peripherals.GPIO9; - } - } - - let mut button = Input::new(button, Pull::Up); - - critical_section::with(|cs| { - button.listen(Event::FallingEdge); - BUTTON.borrow_ref_mut(cs).replace(button) - }); - led.set_high(); + // Set LED GPIOs as an output: + let led = Output::new(peripherals.GPIO2, Level::Low); - let delay = Delay::new(); - - loop { - led.toggle(); - delay.delay_millis(500); - } + toggle_pins(led) } -#[handler] -#[ram] -fn handler() { - cfg_if::cfg_if! { - if #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] { - esp_println::println!( - "GPIO Interrupt with priority {}", - esp_hal::xtensa_lx::interrupt::get_level() - ); - } else { - esp_println::println!("GPIO Interrupt"); - } - } - - if critical_section::with(|cs| { - BUTTON - .borrow_ref_mut(cs) - .as_mut() - .unwrap() - .is_interrupt_set() - }) { - esp_println::println!("Button was the source of the interrupt"); - } else { - esp_println::println!("Button was not the source of the interrupt"); +fn toggle_pins(mut pin: Output) -> ! { + loop { + pin.toggle(); } - - critical_section::with(|cs| { - BUTTON - .borrow_ref_mut(cs) - .as_mut() - .unwrap() - .clear_interrupt() - }); }