Skip to content

Commit

Permalink
Simplify GPIO interrupt handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ committed Mar 5, 2024
1 parent 525c4bd commit 83bb743
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 49 deletions.
61 changes: 21 additions & 40 deletions esp-hal/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
//!
//! [embedded-hal]: https://docs.rs/embedded-hal/latest/embedded_hal/

#[cfg(feature = "async")]
use core::cell::RefCell;
use core::{convert::Infallible, marker::PhantomData};
use core::{cell::Cell, convert::Infallible, marker::PhantomData};

#[cfg(feature = "async")]
use critical_section::Mutex;
use procmacros::interrupt;

#[cfg(any(adc, dac))]
pub(crate) use crate::analog;
Expand All @@ -43,9 +41,8 @@ pub type NoPinType = Gpio0<Unknown>;
/// Convenience constant for `Option::None` pin
pub const NO_PIN: Option<NoPinType> = None;

#[cfg(feature = "async")]
static USER_INTERRUPT_HANDLER: Mutex<RefCell<Option<unsafe extern "C" fn()>>> =
Mutex::new(RefCell::new(None));
static USER_INTERRUPT_HANDLER: Mutex<Cell<Option<unsafe extern "C" fn()>>> =
Mutex::new(Cell::new(None));

#[derive(Copy, Clone)]
pub enum Event {
Expand Down Expand Up @@ -1872,22 +1869,26 @@ impl IO {
/// important to not reset the interrupt status when mixing sync and
/// async (i.e. using async wait) interrupt handling.
pub fn set_interrupt_handler(&mut self, handler: unsafe extern "C" fn() -> ()) {
critical_section::with(|_cs| {
#[cfg(feature = "async")]
USER_INTERRUPT_HANDLER.replace(_cs, Some(handler));

#[cfg(not(feature = "async"))]
{
let mut gpio = unsafe { crate::peripherals::GPIO::steal() };
#[cfg(not(esp32p4))]
gpio.bind_gpio_interrupt(handler);
#[cfg(esp32p4)]
gpio.bind_gpio_int0_interrupt(handler);
}
critical_section::with(|cs| {
USER_INTERRUPT_HANDLER.borrow(cs).set(Some(handler));
});
}
}

#[interrupt]
unsafe fn GPIO() {
if let Some(user_handler) =
critical_section::with(|cs| USER_INTERRUPT_HANDLER.borrow(cs).get().clone())
{
unsafe {
user_handler();
}
}

#[cfg(feature = "async")]
asynch::handle_gpio_interrupt();
}

pub trait GpioProperties {
type Bank: BankGpioRegisterAccess;
type InterruptStatus: InterruptStatusRegisterAccess;
Expand Down Expand Up @@ -3181,27 +3182,7 @@ mod asynch {
});
}

#[cfg(not(any(esp32p4)))]
#[interrupt]
unsafe fn GPIO() {
handle_gpio_interrupt();

if let Some(user_handler) =
critical_section::with(|cs| USER_INTERRUPT_HANDLER.borrow_ref(cs).clone())
{
unsafe {
user_handler();
}
}
}

#[cfg(esp32p4)]
#[interrupt]
unsafe fn GPIO_INT0() {
handle_gpio_interrupt();
}

fn handle_gpio_interrupt() {
pub(super) fn handle_gpio_interrupt() {
let intrs_bank0 = InterruptStatusRegisterAccessBank0::interrupt_status_read();

#[cfg(any(esp32, esp32s2, esp32s3, esp32p4))]
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/soc/esp32p4/peripherals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ crate::peripherals! {
ECC <= ECC,
ECDSA <= ECDSA,
EFUSE <= EFUSE,
GPIO <= GPIO (GPIO_INT0,GPIO_INT1,GPIO_INT2,GPIO_INT3),
GPIO <= GPIO (GPIO,GPIO_INT1,GPIO_INT2,GPIO_INT3),
GPIO_SD <= GPIO_SD,
H264 <= H264,
H264_DMA <= H264_DMA,
Expand Down
16 changes: 8 additions & 8 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ psram-2m = ["esp-hal/psram-2m"]
debug = true

[patch.crates-io]
esp32 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32s2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32s3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32c2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32c3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32c6 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32p4 = { git = "https://github.com/esp-rs/esp-pacs", rev = "35de99f1843abe2b7eaaa2c9246bd49f82f18d0f" }
esp32 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32s2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32s3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32c2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32c3 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32c6 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32h2 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }
esp32p4 = { git = "https://github.com/esp-rs/esp-pacs", rev = "e394c25fea52d7796955da4192e30e5ba5178e3c" }

0 comments on commit 83bb743

Please sign in to comment.