aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Beaumont <[email protected]>2021-03-18 18:48:24 +0100
committerMichael Beaumont <[email protected]>2021-03-18 19:58:32 +0100
commit7b0bed2c5ec66cfac4c601d933800b66b6b72723 (patch)
treef57702e4d0f725de0d8086d26e53c9068b3e755d
parent1908141c867e72c19bddad13388e455bfb2f03bd (diff)
Add WaitFor{Low,High} for stm32l0 exti pins
-rw-r--r--embassy-stm32l0/Cargo.toml1
-rw-r--r--embassy-stm32l0/src/exti.rs62
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]
19embassy = { version = "0.1.0", path = "../embassy" } 19embassy = { version = "0.1.0", path = "../embassy" }
20defmt = { version = "0.2.0", optional = true } 20defmt = { version = "0.2.0", optional = true }
21futures = { version = "0.3.5", default-features = false, features = [ "cfg-target-has-atomic", "unstable" ] }
21log = { version = "0.4.11", optional = true } 22log = { version = "0.4.11", optional = true }
22cortex-m-rt = "0.6.13" 23cortex-m-rt = "0.6.13"
23cortex-m = "0.7.1" 24cortex-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;
2use core::mem; 2use core::mem;
3use core::pin::Pin; 3use core::pin::Pin;
4 4
5use embassy::traits::gpio::{WaitForAnyEdge, WaitForFallingEdge, WaitForRisingEdge}; 5use embassy::traits::gpio::{
6 WaitForAnyEdge, WaitForFallingEdge, WaitForHigh, WaitForLow, WaitForRisingEdge,
7};
6use embassy::util::InterruptFuture; 8use embassy::util::InterruptFuture;
7 9
8use crate::hal::{ 10use crate::hal::{
@@ -12,6 +14,7 @@ use crate::hal::{
12}; 14};
13use crate::interrupt; 15use crate::interrupt;
14use crate::pac::EXTI; 16use crate::pac::EXTI;
17use embedded_hal::digital::v2::InputPin;
15 18
16pub struct ExtiPin<T: PinWithInterrupt> { 19pub 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
57impl<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
54impl<T: PinWithInterrupt + 'static> WaitForRisingEdge for ExtiPin<T> { 98impl<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
122impl<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
130impl<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
78mod private { 138mod private {
79 pub trait Sealed {} 139 pub trait Sealed {}
80} 140}