diff --git a/embassy-stm32f4/src/exti.rs b/embassy-stm32f4/src/exti.rs index bf1dd3a7..11704e6d 100644 --- a/embassy-stm32f4/src/exti.rs +++ b/embassy-stm32f4/src/exti.rs @@ -1,49 +1,39 @@ +use core::cell::UnsafeCell; use core::future::Future; use core::mem; use core::pin::Pin; +use cortex_m; use embassy::interrupt::Interrupt; use embassy::traits::gpio::{WaitForFallingEdge, WaitForRisingEdge}; use embassy::util::InterruptFuture; use crate::hal::gpio; -use crate::hal::gpio::{Edge, ExtiPin as HalExtiPin}; +use crate::hal::gpio::Edge; use crate::hal::syscfg::SysCfg; use crate::pac::EXTI; use embedded_hal::digital::v2 as digital; use crate::interrupt; -pub struct ExtiManager { - syscfg: SysCfg, -} - -impl<'a> ExtiManager { - pub fn new(_exti: EXTI, syscfg: SysCfg) -> Self { - Self { syscfg } - } - - pub fn new_pin(&'static self, mut pin: T, interrupt: T::Interrupt) -> ExtiPin - where - T: HalExtiPin + WithInterrupt, - { - pin.make_interrupt_source(&mut self.syscfg); - - ExtiPin { - pin, - interrupt, - _mgr: self, - } - } -} - -pub struct ExtiPin { +pub struct ExtiPin { pin: T, interrupt: T::Interrupt, - _mgr: &'static ExtiManager, } -impl digital::OutputPin for ExtiPin { +impl ExtiPin { + fn new(mut pin: T, interrupt: T::Interrupt) -> Self { + let mut syscfg: SysCfg = unsafe { mem::transmute(()) }; + + cortex_m::interrupt::free(|_| { + pin.make_interrupt_source(&mut syscfg); + }); + + Self { pin, interrupt } + } +} + +impl digital::OutputPin for ExtiPin { type Error = T::Error; fn set_low(&mut self) -> Result<(), Self::Error> { @@ -55,7 +45,7 @@ impl digital::OutputPin for } } -impl digital::StatefulOutputPin +impl digital::StatefulOutputPin for ExtiPin { fn is_set_low(&self) -> Result { @@ -67,7 +57,7 @@ impl digital::Statef } } -impl digital::ToggleableOutputPin +impl digital::ToggleableOutputPin for ExtiPin { type Error = T::Error; @@ -77,7 +67,7 @@ impl digital::Togg } } -impl digital::InputPin for ExtiPin { +impl digital::InputPin for ExtiPin { type Error = T::Error; fn is_high(&self) -> Result { @@ -100,7 +90,7 @@ impl digital::InputPin for Ex EXTI15_10_IRQn EXTI15_10_IRQHandler Handler for pins connected to line 10 to 15 */ -impl WaitForRisingEdge for ExtiPin { +impl WaitForRisingEdge for ExtiPin { type Future<'a> = impl Future + 'a; fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { @@ -109,10 +99,12 @@ impl WaitForRisingEdge for ExtiPin { s.pin.clear_interrupt_pending_bit(); async move { let fut = InterruptFuture::new(&mut s.interrupt); - let mut exti: EXTI = unsafe { mem::transmute(()) }; + cortex_m::interrupt::free(|_| { + let mut exti: EXTI = unsafe { mem::transmute(()) }; - s.pin.trigger_on_edge(&mut exti, Edge::RISING); - s.pin.enable_interrupt(&mut exti); + s.pin.trigger_on_edge(&mut exti, Edge::RISING); + s.pin.enable_interrupt(&mut exti); + }); fut.await; s.pin.clear_interrupt_pending_bit(); @@ -120,7 +112,7 @@ impl WaitForRisingEdge for ExtiPin { } } -impl WaitForFallingEdge for ExtiPin { +impl WaitForFallingEdge for ExtiPin { type Future<'a> = impl Future + 'a; fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { @@ -129,10 +121,12 @@ impl WaitForFallingEdge for ExtiPin s.pin.clear_interrupt_pending_bit(); async move { let fut = InterruptFuture::new(&mut s.interrupt); - let mut exti: EXTI = unsafe { mem::transmute(()) }; + cortex_m::interrupt::free(|_| { + let mut exti: EXTI = unsafe { mem::transmute(()) }; - s.pin.trigger_on_edge(&mut exti, Edge::FALLING); - s.pin.enable_interrupt(&mut exti); + s.pin.trigger_on_edge(&mut exti, Edge::FALLING); + s.pin.enable_interrupt(&mut exti); + }); fut.await; s.pin.clear_interrupt_pending_bit();