Skip to content

Commit

Permalink
Remove panicking match from Flex::set_level
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Dec 20, 2024
1 parent d66e153 commit 009ddbb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 76 deletions.
11 changes: 4 additions & 7 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2037,8 +2037,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
Expand Down Expand Up @@ -2191,11 +2191,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, {
Expand Down
77 changes: 8 additions & 69 deletions examples/src/bin/gpio_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RefCell<Option<Input>>> = 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()
});
}

0 comments on commit 009ddbb

Please sign in to comment.