diff --git a/esp-hal/Cargo.toml b/esp-hal/Cargo.toml index 7071de3d89..90d4a6f2b5 100644 --- a/esp-hal/Cargo.toml +++ b/esp-hal/Cargo.toml @@ -27,7 +27,7 @@ digest = { version = "0.10.7", default-features = false, optio document-features = "0.2.10" embassy-embedded-hal = "0.2.0" embassy-futures = "0.1.1" -embassy-sync = "0.6.0" +embassy-sync = "0.6.1" embassy-usb-driver = { version = "0.1.0", optional = true } embassy-usb-synopsys-otg = { version = "0.1.0", optional = true } embedded-can = "0.4.1" diff --git a/esp-hal/src/asynch.rs b/esp-hal/src/asynch.rs new file mode 100644 index 0000000000..f0620f7b64 --- /dev/null +++ b/esp-hal/src/asynch.rs @@ -0,0 +1,30 @@ +//! Asynchronous utilities. +use core::task::Waker; + +use embassy_sync::waitqueue::GenericAtomicWaker; + +use crate::sync::RawMutex; + +/// Utility struct to register and wake a waker. +pub struct AtomicWaker { + waker: GenericAtomicWaker, +} + +impl AtomicWaker { + /// Create a new `AtomicWaker`. + #[allow(clippy::new_without_default)] + pub const fn new() -> Self { + Self { + waker: GenericAtomicWaker::new(RawMutex::new()), + } + } + + delegate::delegate! { + to self.waker { + /// Register a waker. Overwrites the previous waker, if any. + pub fn register(&self, w: &Waker); + /// Wake the registered waker, if any. + pub fn wake(&self); + } + } +} diff --git a/esp-hal/src/lib.rs b/esp-hal/src/lib.rs index 7b2676790a..ea2e912752 100644 --- a/esp-hal/src/lib.rs +++ b/esp-hal/src/lib.rs @@ -145,6 +145,8 @@ // MUST be the first module mod fmt; +pub mod asynch; + #[cfg(riscv)] pub use esp_riscv_rt::{self, entry, riscv}; #[cfg(xtensa)] @@ -572,48 +574,3 @@ pub fn init(config: Config) -> Peripherals { peripherals } - -/// Asynchronous utilities. -pub mod asynch { - use core::{cell::Cell, task::Waker}; - - use embassy_sync::blocking_mutex::Mutex; - - use crate::sync::RawMutex; - - /// TODO: this just exists to test RawMutex, otherwise it should be replaced - /// by embassy_sync::waitqueue::atomic_waker::GenericAtomicWaker. - pub struct AtomicWaker { - waker: Mutex>>, - } - - impl AtomicWaker { - /// Create a new `AtomicWaker`. - #[allow(clippy::new_without_default)] - pub const fn new() -> Self { - Self { - waker: Mutex::const_new(RawMutex::new(), Cell::new(None)), - } - } - - /// Register a waker. Overwrites the previous waker, if any. - pub fn register(&self, w: &Waker) { - self.waker.lock(|cell| { - cell.set(match cell.replace(None) { - Some(w2) if (w2.will_wake(w)) => Some(w2), - _ => Some(w.clone()), - }) - }) - } - - /// Wake the registered waker, if any. - pub fn wake(&self) { - self.waker.lock(|cell| { - if let Some(w) = cell.replace(None) { - w.wake_by_ref(); - cell.set(Some(w)); - } - }) - } - } -}