diff --git a/esp-hal-embassy/Cargo.toml b/esp-hal-embassy/Cargo.toml index dc33695eb0..834dae8c65 100644 --- a/esp-hal-embassy/Cargo.toml +++ b/esp-hal-embassy/Cargo.toml @@ -19,7 +19,7 @@ embassy-executor = { version = "0.6.3", optional = true } embassy-sync = { version = "0.6.1" } embassy-time = { version = "0.3.0" } embassy-time-driver = { version = "0.1.0", features = [ "tick-hz-1_000_000" ] } -embassy-time-queue-driver = { version = "0.1.0", features = ["generic-queue-const-generic"] } +embassy-time-queue-driver = { version = "0.1.0" } esp-config = { version = "0.2.0", path = "../esp-config" } esp-hal = { version = "0.22.0", path = "../esp-hal" } log = { version = "0.4.22", optional = true } diff --git a/esp-hal-embassy/src/timer_queue.rs b/esp-hal-embassy/src/timer_queue.rs index 7f6071e3ef..fadf417608 100644 --- a/esp-hal-embassy/src/timer_queue.rs +++ b/esp-hal-embassy/src/timer_queue.rs @@ -107,12 +107,39 @@ mod adapter { #[cfg(generic_timers)] mod adapter { - use core::task::Waker; + use core::{cell::RefCell, task::Waker}; - pub(super) type RawQueue = embassy_time_queue_driver::queue_generic::RefCellQueue< + type Q = embassy_time_queue_driver::queue_generic::ConstGenericQueue< { esp_config::esp_config_int!(usize, "ESP_HAL_EMBASSY_GENERIC_QUEUE_SIZE") }, >; + /// A simple wrapper around a `Queue` to provide interior mutability. + pub struct RefCellQueue { + inner: RefCell, + } + + impl RefCellQueue { + /// Creates a new timer queue. + pub const fn new() -> Self { + Self { + inner: RefCell::new(Q::new()), + } + } + + /// Schedules a task to run at a specific time, and returns whether any + /// changes were made. + pub fn schedule_wake(&self, at: u64, waker: &core::task::Waker) -> bool { + self.inner.borrow_mut().schedule_wake(at, waker) + } + + /// Dequeues expired timers and returns the next alarm time. + pub fn next_expiration(&self, now: u64) -> u64 { + self.inner.borrow_mut().next_expiration(now) + } + } + + pub(super) type RawQueue = RefCellQueue; + pub(super) fn dequeue(q: &RawQueue, now: u64) -> u64 { q.next_expiration(now) }