1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use core::{
sync::atomic::Ordering,
task::{Context, Poll},
};
use crate::gpio::{Event, Flex, GpioBank, Input, InputPin};
impl Flex<'_> {
/// Wait until the pin experiences a particular [`Event`].
///
/// The GPIO driver will disable listening for the event once it occurs,
/// or if the `Future` is dropped - which also means this method is **not**
/// cancellation-safe, it will always wait for a future event.
///
/// Note that calling this function will overwrite previous
/// [`listen`][Self::listen] operations for this pin.
#[inline]
#[instability::unstable]
pub async fn wait_for(&mut self, event: Event) {
// Make sure this pin is not being processed by an interrupt handler. We need to
// always take a critical section even if the pin is not listening, because the
// interrupt handler may be running on another core and the interrupt handler
// may be in the process of processing the pin if the interrupt status is set -
// regardless of the pin actually listening or not.
if self.is_listening() || self.is_interrupt_set() {
self.unlisten_and_clear();
}
// At this point the pin is no longer listening, and not being processed, so we
// can safely do our setup.
// Mark pin as async. The interrupt handler clears this bit before processing a
// pin and unlistens it, so this call will not race with the interrupt
// handler (because it must have finished before `unlisten` above, or the
// handler no longer )
self.pin
.bank()
.async_operations()
.fetch_or(self.pin.mask(), Ordering::Relaxed);
// Start listening for the event. We only need to do this once, as disabling
// the interrupt will signal the future to complete.
self.listen(event);
PinFuture { pin: self }.await
}
/// Wait until the pin is high.
///
/// See [Self::wait_for] for more information.
#[inline]
#[instability::unstable]
pub async fn wait_for_high(&mut self) {
self.wait_for(Event::HighLevel).await
}
/// Wait until the pin is low.
///
/// See [Self::wait_for] for more information.
#[inline]
#[instability::unstable]
pub async fn wait_for_low(&mut self) {
self.wait_for(Event::LowLevel).await
}
/// Wait for the pin to undergo a transition from low to high.
///
/// See [Self::wait_for] for more information.
#[inline]
#[instability::unstable]
pub async fn wait_for_rising_edge(&mut self) {
self.wait_for(Event::RisingEdge).await
}
/// Wait for the pin to undergo a transition from high to low.
///
/// See [Self::wait_for] for more information.
#[inline]
#[instability::unstable]
pub async fn wait_for_falling_edge(&mut self) {
self.wait_for(Event::FallingEdge).await
}
/// Wait for the pin to undergo any transition, i.e low to high OR high
/// to low.
///
/// See [Self::wait_for] for more information.
#[inline]
#[instability::unstable]
pub async fn wait_for_any_edge(&mut self) {
self.wait_for(Event::AnyEdge).await
}
}
impl Input<'_> {
#[procmacros::doc_replace]
/// Wait until the pin experiences a particular [`Event`].
///
/// ## Example
///
/// ```rust, no_run
/// # {before_snippet}
/// use esp_hal::gpio::{Event, Input, InputConfig};
/// let mut input_pin = Input::new(peripherals.GPIO4, InputConfig::default());
///
/// input_pin.wait_for(Event::LowLevel).await;
/// # {after_snippet}
/// ```
///
/// ## Cancellation
///
/// This function is not cancellation-safe.
///
/// - Calling this function will overwrite previous [`listen`][Self::listen] operations for this
/// pin, making it side-effectful.
/// - Dropping the [`Future`] returned by this function will cancel the wait operation. If the
/// event occurs after the future is dropped, a consequent wait operation will ignore the
/// event.
#[inline]
#[instability::unstable]
pub async fn wait_for(&mut self, event: Event) {
self.pin.wait_for(event).await
}
#[procmacros::doc_replace]
/// Wait until the pin is high.
///
/// See [Self::wait_for] for more information.
///
/// ## Example
///
/// ```rust, no_run
/// # {before_snippet}
/// use esp_hal::gpio::{Event, Input, InputConfig};
/// let mut input_pin = Input::new(peripherals.GPIO4, InputConfig::default());
///
/// input_pin.wait_for_high().await;
/// # {after_snippet}
/// ```
#[inline]
pub async fn wait_for_high(&mut self) {
self.pin.wait_for_high().await
}
#[procmacros::doc_replace]
/// Wait until the pin is low.
///
/// See [Self::wait_for] for more information.
///
/// ## Example
///
/// ```rust, no_run
/// # {before_snippet}
/// use esp_hal::gpio::{Event, Input, InputConfig};
/// let mut input_pin = Input::new(peripherals.GPIO4, InputConfig::default());
///
/// input_pin.wait_for_low().await;
/// # {after_snippet}
/// ```
#[inline]
pub async fn wait_for_low(&mut self) {
self.pin.wait_for_low().await
}
#[procmacros::doc_replace]
/// Wait for the pin to undergo a transition from low to high.
///
/// See [Self::wait_for] for more information.
///
/// ## Example
///
/// ```rust, no_run
/// # {before_snippet}
/// use esp_hal::gpio::{Event, Input, InputConfig};
/// let mut input_pin = Input::new(peripherals.GPIO4, InputConfig::default());
///
/// input_pin.wait_for_rising_edge().await;
/// # {after_snippet}
/// ```
#[inline]
pub async fn wait_for_rising_edge(&mut self) {
self.pin.wait_for_rising_edge().await
}
#[procmacros::doc_replace]
/// Wait for the pin to undergo a transition from high to low.
///
/// See [Self::wait_for] for more information.
///
/// ## Example
///
/// ```rust, no_run
/// # {before_snippet}
/// use esp_hal::gpio::{Event, Input, InputConfig};
/// let mut input_pin = Input::new(peripherals.GPIO4, InputConfig::default());
///
/// input_pin.wait_for_falling_edge().await;
/// # {after_snippet}
/// ```
#[inline]
pub async fn wait_for_falling_edge(&mut self) {
self.pin.wait_for_falling_edge().await
}
#[procmacros::doc_replace]
/// Wait for the pin to undergo any transition, i.e low to high OR high
/// to low.
///
/// See [Self::wait_for] for more information.
///
/// ## Example
///
/// ```rust, no_run
/// # {before_snippet}
/// use esp_hal::gpio::{Event, Input, InputConfig};
/// let mut input_pin = Input::new(peripherals.GPIO4, InputConfig::default());
///
/// input_pin.wait_for_any_edge().await;
/// # {after_snippet}
/// ```
#[inline]
pub async fn wait_for_any_edge(&mut self) {
self.pin.wait_for_any_edge().await
}
}
#[must_use = "futures do nothing unless you `.await` or poll them"]
struct PinFuture<'f, 'd> {
pin: &'f mut Flex<'d>,
}
impl PinFuture<'_, '_> {
fn bank(&self) -> GpioBank {
self.pin.pin.bank()
}
fn mask(&self) -> u32 {
self.pin.pin.mask()
}
fn is_done(&self) -> bool {
// Only the interrupt handler should clear the async bit, and only if the
// specific pin is handling an interrupt. This way the user may clear the
// interrupt status without worrying about the async bit being cleared.
self.bank().async_operations().load(Ordering::Acquire) & self.mask() == 0
}
}
impl core::future::Future for PinFuture<'_, '_> {
type Output = ();
fn poll(self: core::pin::Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
self.pin.pin.waker().register(cx.waker());
if self.is_done() {
Poll::Ready(())
} else {
Poll::Pending
}
}
}
impl Drop for PinFuture<'_, '_> {
fn drop(&mut self) {
// If the future has completed, unlistening and removing the async bit will have
// been done by the interrupt handler.
if !self.is_done() {
self.pin.unlisten_and_clear();
// Unmark pin as async so that a future listen call doesn't wake a waker for no
// reason.
self.bank()
.async_operations()
.fetch_and(!self.mask(), Ordering::Relaxed);
}
}
}