diff options
| author | Michael Beaumont <[email protected]> | 2021-03-09 14:23:02 +0100 |
|---|---|---|
| committer | Michael Beaumont <[email protected]> | 2021-03-09 14:23:02 +0100 |
| commit | 6278ecf4b0c312894821c2e4b4f5a1b6ea5688d4 (patch) | |
| tree | 55fcae522b9a70a1c372a9f674472d821ad017bc | |
| parent | b490c8a48d718c94c70109e5dfc4f5d2f5a2e035 (diff) | |
Use a critical section to listen on GPIO pins
| -rw-r--r-- | embassy-stm32l0/src/exti.rs | 50 | ||||
| -rw-r--r-- | embassy-stm32l0/src/lib.rs | 6 |
2 files changed, 27 insertions, 29 deletions
diff --git a/embassy-stm32l0/src/exti.rs b/embassy-stm32l0/src/exti.rs index 84c38ccc7..ca699a086 100644 --- a/embassy-stm32l0/src/exti.rs +++ b/embassy-stm32l0/src/exti.rs | |||
| @@ -39,26 +39,32 @@ impl<'a> ExtiManager { | |||
| 39 | pub struct ExtiPin<T, I> { | 39 | pub struct ExtiPin<T, I> { |
| 40 | pin: T, | 40 | pin: T, |
| 41 | interrupt: I, | 41 | interrupt: I, |
| 42 | mgr: &'static mut ExtiManager, | 42 | mgr: &'static ExtiManager, |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitForRisingEdge | 45 | impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> ExtiPin<T, I> { |
| 46 | for ExtiPin<T, I> | 46 | fn wait_for_edge<'a>( |
| 47 | { | 47 | self: Pin<&'a mut Self>, |
| 48 | type Future<'a> = impl Future<Output = ()> + 'a; | 48 | edge: TriggerEdge, |
| 49 | 49 | ) -> impl Future<Output = ()> + 'a { | |
| 50 | fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 50 | let line = self.pin.line(); |
| 51 | let s = unsafe { self.get_unchecked_mut() }; | 51 | let s = unsafe { self.get_unchecked_mut() }; |
| 52 | 52 | ||
| 53 | let line = s.pin.line(); | ||
| 54 | Exti::unpend(line); | 53 | Exti::unpend(line); |
| 55 | 54 | ||
| 56 | async move { | 55 | async move { |
| 57 | let exti: EXTI = unsafe { mem::transmute(()) }; | 56 | let exti: EXTI = unsafe { mem::transmute(()) }; |
| 58 | let mut exti = Exti::new(exti); | 57 | let mut exti = Exti::new(exti); |
| 58 | |||
| 59 | let fut = InterruptFuture::new(&mut s.interrupt); | 59 | let fut = InterruptFuture::new(&mut s.interrupt); |
| 60 | 60 | ||
| 61 | exti.listen_gpio(&mut s.mgr.syscfg, s.pin.port(), line, TriggerEdge::Rising); | 61 | let port = s.pin.port(); |
| 62 | let syscfg = &s.mgr.syscfg as *const _ as *mut SYSCFG; | ||
| 63 | cortex_m::interrupt::free(|_| { | ||
| 64 | let syscfg = unsafe { &mut *syscfg }; | ||
| 65 | exti.listen_gpio(syscfg, port, line, edge); | ||
| 66 | }); | ||
| 67 | |||
| 62 | fut.await; | 68 | fut.await; |
| 63 | 69 | ||
| 64 | Exti::unpend(line); | 70 | Exti::unpend(line); |
| @@ -66,27 +72,23 @@ impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitF | |||
| 66 | } | 72 | } |
| 67 | } | 73 | } |
| 68 | 74 | ||
| 69 | impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitForFallingEdge | 75 | impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitForRisingEdge |
| 70 | for ExtiPin<T, I> | 76 | for ExtiPin<T, I> |
| 71 | { | 77 | { |
| 72 | type Future<'a> = impl Future<Output = ()> + 'a; | 78 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 73 | 79 | ||
| 74 | fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | 80 | fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { |
| 75 | let s = unsafe { self.get_unchecked_mut() }; | 81 | self.wait_for_edge(TriggerEdge::Rising) |
| 76 | 82 | } | |
| 77 | let line = s.pin.line(); | 83 | } |
| 78 | Exti::unpend(line); | ||
| 79 | |||
| 80 | async move { | ||
| 81 | let exti: EXTI = unsafe { mem::transmute(()) }; | ||
| 82 | let mut exti = Exti::new(exti); | ||
| 83 | let fut = InterruptFuture::new(&mut s.interrupt); | ||
| 84 | 84 | ||
| 85 | exti.listen_gpio(&mut s.mgr.syscfg, s.pin.port(), line, TriggerEdge::Falling); | 85 | impl<T: PinWithInterrupt<Interrupt = I> + 'static, I: Interrupt + 'static> WaitForFallingEdge |
| 86 | fut.await; | 86 | for ExtiPin<T, I> |
| 87 | { | ||
| 88 | type Future<'a> = impl Future<Output = ()> + 'a; | ||
| 87 | 89 | ||
| 88 | Exti::unpend(line); | 90 | fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { |
| 89 | } | 91 | self.wait_for_edge(TriggerEdge::Falling) |
| 90 | } | 92 | } |
| 91 | } | 93 | } |
| 92 | 94 | ||
diff --git a/embassy-stm32l0/src/lib.rs b/embassy-stm32l0/src/lib.rs index c4fa15c54..62f0f0375 100644 --- a/embassy-stm32l0/src/lib.rs +++ b/embassy-stm32l0/src/lib.rs | |||
| @@ -4,11 +4,7 @@ | |||
| 4 | #![feature(type_alias_impl_trait)] | 4 | #![feature(type_alias_impl_trait)] |
| 5 | #![allow(incomplete_features)] | 5 | #![allow(incomplete_features)] |
| 6 | 6 | ||
| 7 | #[cfg(not(any( | 7 | #[cfg(not(any(feature = "stm32l0x1", feature = "stm32l0x2", feature = "stm32l0x3",)))] |
| 8 | feature = "stm32l0x1", | ||
| 9 | feature = "stm32l0x2", | ||
| 10 | feature = "stm32l0x3", | ||
| 11 | )))] | ||
| 12 | compile_error!( | 8 | compile_error!( |
| 13 | "No chip feature activated. You must activate exactly one of the following features: " | 9 | "No chip feature activated. You must activate exactly one of the following features: " |
| 14 | ); | 10 | ); |
