Skip to content

Commit c90fc2a

Browse files
bors[bot]Dirbaio
andauthored
Merge #346
346: async: add digital::Wait. r=ryankurte a=Dirbaio Add `digital::Wait` trait. This is the previously proposed `WaitForX` traits, unified in a single one supporting both edge-triggered and level-triggered waits. It is possible to software-emulate edge-triggered out of level-triggered hardware and vice-versa, so requiring support for both shouldn't make it unimplementable for any MCU. It is a good thing to require both: for example, stm32 EXTI is edge-triggered, but drivers usually want level-triggered. It'd be bad if a HAL decided "the hardware only supports edge-triggered, so I'm only going to impl edge-triggered!". Impl for nRF's GPIOTE here: https://github.com/embassy-rs/embassy/blob/master/embassy-nrf/src/gpiote.rs Impl for STM32's EXTI here: https://github.com/embassy-rs/embassy/blob/master/embassy-stm32/src/exti.rs (impls still not unified, but unifying shouldn't be an issue). Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents 61285e4 + 9886bff commit c90fc2a

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

embedded-hal-async/src/digital.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Asynchronous digital I/O
2+
//!
3+
//! # Example
4+
//!
5+
//! ```rust
6+
//! # use embedded_hal_async::digital::Wait;
7+
//! /// Asynchronously wait until the `ready_pin` becomes high.
8+
//! async fn wait_until_ready<P>(ready_pin: &mut P)
9+
//! where
10+
//! P: Wait,
11+
//! {
12+
//! ready_pin
13+
//! .wait_for_high()
14+
//! .await
15+
//! .expect("failed to await input pin")
16+
//! }
17+
//! ```
18+
19+
use core::future::Future;
20+
21+
/// Asynchronously wait for GPIO pin state.
22+
pub trait Wait: embedded_hal::digital::ErrorType {
23+
/// The future returned by the `wait_for_high` function.
24+
type WaitForHighFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
25+
where
26+
Self: 'a;
27+
28+
/// Wait until the pin is high. If it is already high, return immediately.
29+
///
30+
/// # Note for implementers
31+
/// The pin may have switched back to low before the task was run after
32+
/// being woken. The future should still resolve in that case.
33+
fn wait_for_high<'a>(&'a mut self) -> Self::WaitForHighFuture<'a>;
34+
35+
/// The future returned by `wait_for_low`.
36+
type WaitForLowFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
37+
where
38+
Self: 'a;
39+
40+
/// Wait until the pin is low. If it is already low, return immediately.
41+
///
42+
/// # Note for implementers
43+
/// The pin may have switched back to high before the task was run after
44+
/// being woken. The future should still resolve in that case.
45+
fn wait_for_low<'a>(&'a mut self) -> Self::WaitForLowFuture<'a>;
46+
47+
/// The future returned from `wait_for_rising_edge`.
48+
type WaitForRisingEdgeFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
49+
where
50+
Self: 'a;
51+
52+
/// Wait for the pin to undergo a transition from low to high.
53+
///
54+
/// If the pin is already low, this does *not* return immediately, it'll wait for the
55+
/// pin to go high and then low again.
56+
fn wait_for_rising_edge<'a>(&'a mut self) -> Self::WaitForRisingEdgeFuture<'a>;
57+
58+
/// The future returned from `wait_for_falling_edge`.
59+
type WaitForFallingEdgeFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
60+
where
61+
Self: 'a;
62+
63+
/// Wait for the pin to undergo a transition from high to low.
64+
///
65+
/// If the pin is already low, this does *not* return immediately, it'll wait for the
66+
/// pin to go high and then low again.
67+
fn wait_for_falling_edge<'a>(&'a mut self) -> Self::WaitForFallingEdgeFuture<'a>;
68+
69+
/// The future returned from `wait_for_any_edge`.
70+
type WaitForAnyEdgeFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
71+
where
72+
Self: 'a;
73+
74+
/// Wait for the pin to undergo any transition, i.e low to high OR high to low.
75+
fn wait_for_any_edge<'a>(&'a mut self) -> Self::WaitForAnyEdgeFuture<'a>;
76+
}

embedded-hal-async/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
#![feature(generic_associated_types)]
1313

1414
pub mod delay;
15+
pub mod digital;
1516
pub mod i2c;

0 commit comments

Comments
 (0)