diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-03-18 20:35:28 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-03-18 20:35:28 +0100 |
| commit | c4406a3f2231f9267be53e2e65b4d3ce8b59dfb6 (patch) | |
| tree | 8c6011a216a3a7da03fae86c2cd5d3f8cf87bbcf | |
| parent | 6eb0522102c0a808b245b16e7498527464355c04 (diff) | |
| parent | 7b0bed2c5ec66cfac4c601d933800b66b6b72723 (diff) | |
Merge pull request #84 from michaelbeaumont/stm32l0_waitforhighlow
Add WaitFor{Low,High} for stm32l0 exti pins
| -rw-r--r-- | embassy-stm32l0/Cargo.toml | 1 | ||||
| -rw-r--r-- | embassy-stm32l0/src/exti.rs | 62 |
2 files changed, 62 insertions, 1 deletions
diff --git a/embassy-stm32l0/Cargo.toml b/embassy-stm32l0/Cargo.toml index 70aa431c9..c74d14076 100644 --- a/embassy-stm32l0/Cargo.toml +++ b/embassy-stm32l0/Cargo.toml | |||
| @@ -18,6 +18,7 @@ stm32l0x3 = ["stm32l0xx-hal/stm32l0x3"] | |||
| 18 | [dependencies] | 18 | [dependencies] |
| 19 | embassy = { version = "0.1.0", path = "../embassy" } | 19 | embassy = { version = "0.1.0", path = "../embassy" } |
| 20 | defmt = { version = "0.2.0", optional = true } | 20 | defmt = { version = "0.2.0", optional = true } |
| 21 | futures = { version = "0.3.5", default-features = false, features = [ "cfg-target-has-atomic", "unstable" ] } | ||
| 21 | log = { version = "0.4.11", optional = true } | 22 | log = { version = "0.4.11", optional = true } |
| 22 | cortex-m-rt = "0.6.13" | 23 | cortex-m-rt = "0.6.13" |
| 23 | cortex-m = "0.7.1" | 24 | cortex-m = "0.7.1" |
diff --git a/embassy-stm32l0/src/exti.rs b/embassy-stm32l0/src/exti.rs index 32c56c490..f50c3ae8e 100644 --- a/embassy-stm32l0/src/exti.rs +++ b/embassy-stm32l0/src/exti.rs | |||
| @@ -2,7 +2,9 @@ use core::future::Future; | |||
| 2 | use core::mem; | 2 | use core::mem; |
| 3 | use core::pin::Pin; | 3 | use core::pin::Pin; |
| 4 | 4 | ||
| 5 | use embassy::traits::gpio::{WaitForAnyEdge, WaitForFallingEdge, WaitForRisingEdge}; | 5 | use embassy::traits::gpio::{ |
| 6 | WaitForAnyEdge, WaitForFallingEdge, WaitForHigh, WaitForLow, WaitForRisingEdge, | ||
| 7 | }; | ||
| 6 | use embassy::util::InterruptFuture; | 8 | use embassy::util::InterruptFuture; |
| 7 | 9 | ||
| 8 | use crate::hal::{ | 10 | use crate::hal::{ |
| @@ -12,6 +14,7 @@ use crate::hal::{ | |||
| 12 | }; | 14 | }; |
| 13 | use crate::interrupt; | 15 | use crate::interrupt; |
| 14 | use crate::pac::EXTI; | 16 | use crate::pac::EXTI; |
| 17 | use embedded_hal::digital::v2::InputPin; | ||
| 15 | 18 | ||
| 16 | pub struct ExtiPin<T: PinWithInterrupt> { | 19 | pub struct ExtiPin<T: PinWithInterrupt> { |
| 17 | pin: T, | 20 | pin: T, |
| @@ -51,6 +54,47 @@ impl<T: PinWithInterrupt + 'static> ExtiPin<T> { | |||
| 51 | } | 54 | } |
| 52 | } | 55 | } |
| 53 | 56 | ||
| 57 | impl<T: InputPin + PinWithInterrupt + 'static> ExtiPin<T> { | ||
| 58 | fn wait_for_state<'a>(self: Pin<&'a mut Self>, state: bool) -> impl Future<Output = ()> + 'a { | ||
| 59 | let line = self.pin.line(); | ||
| 60 | let s = unsafe { self.get_unchecked_mut() }; | ||
| 61 | |||
| 62 | Exti::unpend(line); | ||
| 63 | |||
| 64 | async move { | ||
| 65 | let exti: EXTI = unsafe { mem::transmute(()) }; | ||
| 66 | let mut exti = Exti::new(exti); | ||
| 67 | |||
| 68 | let fut = InterruptFuture::new(&mut s.interrupt); | ||
| 69 | |||
| 70 | let port = s.pin.port(); | ||
| 71 | cortex_m::interrupt::free(|_| { | ||
| 72 | let mut syscfg: SYSCFG = unsafe { mem::transmute(()) }; | ||
| 73 | let edge = if state { | ||
| 74 | TriggerEdge::Rising | ||
| 75 | } else { | ||
| 76 | TriggerEdge::Falling | ||
| 77 | }; | ||
| 78 | exti.listen_gpio(&mut syscfg, port, line, edge); | ||
| 79 | }); | ||
| 80 | |||
| 81 | let pin_has_state = if state { | ||
| 82 | s.pin.is_high() | ||
| 83 | } else { | ||
| 84 | s.pin.is_low() | ||
| 85 | } | ||
| 86 | .unwrap_or(false); | ||
| 87 | if pin_has_state { | ||
| 88 | return (); | ||
| 89 | } | ||
| 90 | |||
| 91 | fut.await; | ||
| 92 | |||
| 93 | Exti::unpend(line); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 54 | impl<T: PinWithInterrupt + 'static> WaitForRisingEdge for ExtiPin<T> { | 98 | impl<T: PinWithInterrupt + 'static> WaitForRisingEdge for ExtiPin<T> { |
| 55 | type Future<'a> = impl Future<Output = ()> + 'a; | 99 | type Future<'a> = impl Future<Output = ()> + 'a; |
| 56 | 100 | ||
| @@ -75,6 +119,22 @@ impl<T: PinWithInterrupt + 'static> WaitForAnyEdge for ExtiPin<T> { | |||
| 75 | } | 119 | } |
| 76 | } | 120 | } |
| 77 | 121 | ||
| 122 | impl<T: InputPin + PinWithInterrupt + 'static> WaitForHigh for ExtiPin<T> { | ||
| 123 | type Future<'a> = impl Future<Output = ()> + 'a; | ||
| 124 | |||
| 125 | fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | ||
| 126 | self.wait_for_state(true) | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | impl<T: InputPin + PinWithInterrupt + 'static> WaitForLow for ExtiPin<T> { | ||
| 131 | type Future<'a> = impl Future<Output = ()> + 'a; | ||
| 132 | |||
| 133 | fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a> { | ||
| 134 | self.wait_for_state(false) | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 78 | mod private { | 138 | mod private { |
| 79 | pub trait Sealed {} | 139 | pub trait Sealed {} |
| 80 | } | 140 | } |
