diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-01-11 22:47:05 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2024-01-11 23:01:24 +0100 |
| commit | f0606da9adc8032cc92c06c0661b385742459fc8 (patch) | |
| tree | ccff465740429fa86f3455312ae02eab1170f8d4 /embassy-time/src | |
| parent | b3ab2d91f7ed48e6a377661e7e504cb0ae0091a3 (diff) | |
time: split queue driver too, don't reexport drivers.
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/driver_mock.rs | 4 | ||||
| -rw-r--r-- | embassy-time/src/driver_std.rs | 5 | ||||
| -rw-r--r-- | embassy-time/src/driver_wasm.rs | 5 | ||||
| -rw-r--r-- | embassy-time/src/instant.rs | 6 | ||||
| -rw-r--r-- | embassy-time/src/lib.rs | 5 | ||||
| -rw-r--r-- | embassy-time/src/queue.rs | 60 | ||||
| -rw-r--r-- | embassy-time/src/queue_generic.rs | 10 | ||||
| -rw-r--r-- | embassy-time/src/timer.rs | 16 |
8 files changed, 20 insertions, 91 deletions
diff --git a/embassy-time/src/driver_mock.rs b/embassy-time/src/driver_mock.rs index 7abc2bd70..8587f9172 100644 --- a/embassy-time/src/driver_mock.rs +++ b/embassy-time/src/driver_mock.rs | |||
| @@ -1,8 +1,8 @@ | |||
| 1 | use core::cell::RefCell; | 1 | use core::cell::RefCell; |
| 2 | 2 | ||
| 3 | use critical_section::Mutex as CsMutex; | 3 | use critical_section::Mutex as CsMutex; |
| 4 | use embassy_time_driver::{AlarmHandle, Driver}; | ||
| 4 | 5 | ||
| 5 | use crate::driver::{AlarmHandle, Driver}; | ||
| 6 | use crate::{Duration, Instant}; | 6 | use crate::{Duration, Instant}; |
| 7 | 7 | ||
| 8 | /// A mock driver that can be manually advanced. | 8 | /// A mock driver that can be manually advanced. |
| @@ -28,7 +28,7 @@ use crate::{Duration, Instant}; | |||
| 28 | /// ``` | 28 | /// ``` |
| 29 | pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>); | 29 | pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>); |
| 30 | 30 | ||
| 31 | crate::driver::time_driver_impl!(static DRIVER: MockDriver = MockDriver::new()); | 31 | embassy_time_driver::time_driver_impl!(static DRIVER: MockDriver = MockDriver::new()); |
| 32 | 32 | ||
| 33 | impl MockDriver { | 33 | impl MockDriver { |
| 34 | /// Creates a new mock driver. | 34 | /// Creates a new mock driver. |
diff --git a/embassy-time/src/driver_std.rs b/embassy-time/src/driver_std.rs index 3b5524f9e..d182f8331 100644 --- a/embassy-time/src/driver_std.rs +++ b/embassy-time/src/driver_std.rs | |||
| @@ -6,8 +6,7 @@ use std::time::{Duration as StdDuration, Instant as StdInstant}; | |||
| 6 | use std::{mem, ptr, thread}; | 6 | use std::{mem, ptr, thread}; |
| 7 | 7 | ||
| 8 | use critical_section::Mutex as CsMutex; | 8 | use critical_section::Mutex as CsMutex; |
| 9 | 9 | use embassy_time_driver::{AlarmHandle, Driver}; | |
| 10 | use crate::driver::{AlarmHandle, Driver}; | ||
| 11 | 10 | ||
| 12 | const ALARM_COUNT: usize = 4; | 11 | const ALARM_COUNT: usize = 4; |
| 13 | 12 | ||
| @@ -45,7 +44,7 @@ struct TimeDriver { | |||
| 45 | } | 44 | } |
| 46 | 45 | ||
| 47 | const ALARM_NEW: AlarmState = AlarmState::new(); | 46 | const ALARM_NEW: AlarmState = AlarmState::new(); |
| 48 | crate::driver::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver { | 47 | embassy_time_driver::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver { |
| 49 | alarm_count: AtomicU8::new(0), | 48 | alarm_count: AtomicU8::new(0), |
| 50 | 49 | ||
| 51 | once: Once::new(), | 50 | once: Once::new(), |
diff --git a/embassy-time/src/driver_wasm.rs b/embassy-time/src/driver_wasm.rs index d75856c26..ad884f060 100644 --- a/embassy-time/src/driver_wasm.rs +++ b/embassy-time/src/driver_wasm.rs | |||
| @@ -4,11 +4,10 @@ use std::mem::MaybeUninit; | |||
| 4 | use std::ptr; | 4 | use std::ptr; |
| 5 | use std::sync::{Mutex, Once}; | 5 | use std::sync::{Mutex, Once}; |
| 6 | 6 | ||
| 7 | use embassy_time_driver::{AlarmHandle, Driver}; | ||
| 7 | use wasm_bindgen::prelude::*; | 8 | use wasm_bindgen::prelude::*; |
| 8 | use wasm_timer::Instant as StdInstant; | 9 | use wasm_timer::Instant as StdInstant; |
| 9 | 10 | ||
| 10 | use crate::driver::{AlarmHandle, Driver}; | ||
| 11 | |||
| 12 | const ALARM_COUNT: usize = 4; | 11 | const ALARM_COUNT: usize = 4; |
| 13 | 12 | ||
| 14 | struct AlarmState { | 13 | struct AlarmState { |
| @@ -42,7 +41,7 @@ struct TimeDriver { | |||
| 42 | } | 41 | } |
| 43 | 42 | ||
| 44 | const ALARM_NEW: AlarmState = AlarmState::new(); | 43 | const ALARM_NEW: AlarmState = AlarmState::new(); |
| 45 | crate::driver::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver { | 44 | embassy_time_driver::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver { |
| 46 | alarm_count: AtomicU8::new(0), | 45 | alarm_count: AtomicU8::new(0), |
| 47 | once: Once::new(), | 46 | once: Once::new(), |
| 48 | alarms: UninitCell::uninit(), | 47 | alarms: UninitCell::uninit(), |
diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs index 5571cdd15..909f1b173 100644 --- a/embassy-time/src/instant.rs +++ b/embassy-time/src/instant.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | use core::fmt; | 1 | use core::fmt; |
| 2 | use core::ops::{Add, AddAssign, Sub, SubAssign}; | 2 | use core::ops::{Add, AddAssign, Sub, SubAssign}; |
| 3 | 3 | ||
| 4 | use super::{driver, Duration, GCD_1K, GCD_1M, TICK_HZ}; | 4 | use super::{Duration, GCD_1K, GCD_1M, TICK_HZ}; |
| 5 | 5 | ||
| 6 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | 6 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -18,7 +18,9 @@ impl Instant { | |||
| 18 | 18 | ||
| 19 | /// Returns an Instant representing the current time. | 19 | /// Returns an Instant representing the current time. |
| 20 | pub fn now() -> Instant { | 20 | pub fn now() -> Instant { |
| 21 | Instant { ticks: driver::now() } | 21 | Instant { |
| 22 | ticks: embassy_time_driver::now(), | ||
| 23 | } | ||
| 22 | } | 24 | } |
| 23 | 25 | ||
| 24 | /// Create an Instant from a tick count since system boot. | 26 | /// Create an Instant from a tick count since system boot. |
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs index 3f8c09f1a..d27eb92f6 100644 --- a/embassy-time/src/lib.rs +++ b/embassy-time/src/lib.rs | |||
| @@ -10,12 +10,9 @@ | |||
| 10 | // This mod MUST go first, so that the others see its macros. | 10 | // This mod MUST go first, so that the others see its macros. |
| 11 | pub(crate) mod fmt; | 11 | pub(crate) mod fmt; |
| 12 | 12 | ||
| 13 | pub use embassy_time_driver as driver; | ||
| 14 | |||
| 15 | mod delay; | 13 | mod delay; |
| 16 | mod duration; | 14 | mod duration; |
| 17 | mod instant; | 15 | mod instant; |
| 18 | pub mod queue; | ||
| 19 | mod timer; | 16 | mod timer; |
| 20 | 17 | ||
| 21 | #[cfg(feature = "mock-driver")] | 18 | #[cfg(feature = "mock-driver")] |
| @@ -32,8 +29,8 @@ mod driver_wasm; | |||
| 32 | mod queue_generic; | 29 | mod queue_generic; |
| 33 | 30 | ||
| 34 | pub use delay::{block_for, Delay}; | 31 | pub use delay::{block_for, Delay}; |
| 35 | pub use driver::TICK_HZ; | ||
| 36 | pub use duration::Duration; | 32 | pub use duration::Duration; |
| 33 | pub use embassy_time_driver::TICK_HZ; | ||
| 37 | pub use instant::Instant; | 34 | pub use instant::Instant; |
| 38 | pub use timer::{with_timeout, Ticker, TimeoutError, Timer}; | 35 | pub use timer::{with_timeout, Ticker, TimeoutError, Timer}; |
| 39 | 36 | ||
diff --git a/embassy-time/src/queue.rs b/embassy-time/src/queue.rs deleted file mode 100644 index d65197c54..000000000 --- a/embassy-time/src/queue.rs +++ /dev/null | |||
| @@ -1,60 +0,0 @@ | |||
| 1 | //! Timer queue implementation | ||
| 2 | //! | ||
| 3 | //! This module defines the interface a timer queue needs to implement to power the `embassy_time` module. | ||
| 4 | //! | ||
| 5 | //! # Implementing a timer queue | ||
| 6 | //! | ||
| 7 | //! - Define a struct `MyTimerQueue` | ||
| 8 | //! - Implement [`TimerQueue`] for it | ||
| 9 | //! - Register it as the global timer queue with [`timer_queue_impl`](crate::timer_queue_impl). | ||
| 10 | //! | ||
| 11 | //! # Linkage details | ||
| 12 | //! | ||
| 13 | //! Check the documentation of the [`driver`](crate::driver) module for more information. | ||
| 14 | //! | ||
| 15 | //! Similarly to driver, if there is none or multiple timer queues in the crate tree, linking will fail. | ||
| 16 | //! | ||
| 17 | //! # Example | ||
| 18 | //! | ||
| 19 | //! ``` | ||
| 20 | //! use core::task::Waker; | ||
| 21 | //! | ||
| 22 | //! use embassy_time::Instant; | ||
| 23 | //! use embassy_time::queue::{TimerQueue}; | ||
| 24 | //! | ||
| 25 | //! struct MyTimerQueue{}; // not public! | ||
| 26 | //! | ||
| 27 | //! impl TimerQueue for MyTimerQueue { | ||
| 28 | //! fn schedule_wake(&'static self, at: Instant, waker: &Waker) { | ||
| 29 | //! todo!() | ||
| 30 | //! } | ||
| 31 | //! } | ||
| 32 | //! ``` | ||
| 33 | //! ```ignore | ||
| 34 | //! embassy_time::timer_queue_impl!(static QUEUE: MyTimerQueue = MyTimerQueue{}); | ||
| 35 | //! ``` | ||
| 36 | use core::task::Waker; | ||
| 37 | |||
| 38 | use crate::Instant; | ||
| 39 | |||
| 40 | /// Timer queue | ||
| 41 | pub trait TimerQueue { | ||
| 42 | /// Schedules a waker in the queue to be awoken at moment `at`. | ||
| 43 | /// If this moment is in the past, the waker might be awoken immediately. | ||
| 44 | fn schedule_wake(&'static self, at: Instant, waker: &Waker); | ||
| 45 | } | ||
| 46 | |||
| 47 | /// Set the TimerQueue implementation. | ||
| 48 | /// | ||
| 49 | /// See the module documentation for an example. | ||
| 50 | #[macro_export] | ||
| 51 | macro_rules! timer_queue_impl { | ||
| 52 | (static $name:ident: $t: ty = $val:expr) => { | ||
| 53 | static $name: $t = $val; | ||
| 54 | |||
| 55 | #[no_mangle] | ||
| 56 | fn _embassy_time_schedule_wake(at: $crate::Instant, waker: &core::task::Waker) { | ||
| 57 | <$t as $crate::queue::TimerQueue>::schedule_wake(&$name, at, waker); | ||
| 58 | } | ||
| 59 | }; | ||
| 60 | } | ||
diff --git a/embassy-time/src/queue_generic.rs b/embassy-time/src/queue_generic.rs index 829368ffc..cf7a986d5 100644 --- a/embassy-time/src/queue_generic.rs +++ b/embassy-time/src/queue_generic.rs | |||
| @@ -3,10 +3,10 @@ use core::cmp::{min, Ordering}; | |||
| 3 | use core::task::Waker; | 3 | use core::task::Waker; |
| 4 | 4 | ||
| 5 | use critical_section::Mutex; | 5 | use critical_section::Mutex; |
| 6 | use embassy_time_driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle}; | ||
| 7 | use embassy_time_queue_driver::TimerQueue; | ||
| 6 | use heapless::Vec; | 8 | use heapless::Vec; |
| 7 | 9 | ||
| 8 | use crate::driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle}; | ||
| 9 | use crate::queue::TimerQueue; | ||
| 10 | use crate::Instant; | 10 | use crate::Instant; |
| 11 | 11 | ||
| 12 | #[cfg(feature = "generic-queue-8")] | 12 | #[cfg(feature = "generic-queue-8")] |
| @@ -167,12 +167,12 @@ impl Queue { | |||
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | impl TimerQueue for Queue { | 169 | impl TimerQueue for Queue { |
| 170 | fn schedule_wake(&'static self, at: Instant, waker: &Waker) { | 170 | fn schedule_wake(&'static self, at: u64, waker: &Waker) { |
| 171 | Queue::schedule_wake(self, at, waker); | 171 | Queue::schedule_wake(self, Instant::from_ticks(at), waker); |
| 172 | } | 172 | } |
| 173 | } | 173 | } |
| 174 | 174 | ||
| 175 | crate::timer_queue_impl!(static QUEUE: Queue = Queue::new()); | 175 | embassy_time_queue_driver::timer_queue_impl!(static QUEUE: Queue = Queue::new()); |
| 176 | 176 | ||
| 177 | #[cfg(test)] | 177 | #[cfg(test)] |
| 178 | #[cfg(feature = "mock-driver")] | 178 | #[cfg(feature = "mock-driver")] |
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 2705ba03f..565a65cb8 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | use core::future::{poll_fn, Future}; | 1 | use core::future::{poll_fn, Future}; |
| 2 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll, Waker}; | 3 | use core::task::{Context, Poll}; |
| 4 | 4 | ||
| 5 | use futures_util::future::{select, Either}; | 5 | use futures_util::future::{select, Either}; |
| 6 | use futures_util::stream::FusedStream; | 6 | use futures_util::stream::FusedStream; |
| @@ -116,7 +116,7 @@ impl Future for Timer { | |||
| 116 | if self.yielded_once && self.expires_at <= Instant::now() { | 116 | if self.yielded_once && self.expires_at <= Instant::now() { |
| 117 | Poll::Ready(()) | 117 | Poll::Ready(()) |
| 118 | } else { | 118 | } else { |
| 119 | schedule_wake(self.expires_at, cx.waker()); | 119 | embassy_time_queue_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker()); |
| 120 | self.yielded_once = true; | 120 | self.yielded_once = true; |
| 121 | Poll::Pending | 121 | Poll::Pending |
| 122 | } | 122 | } |
| @@ -185,7 +185,7 @@ impl Ticker { | |||
| 185 | self.expires_at += dur; | 185 | self.expires_at += dur; |
| 186 | Poll::Ready(()) | 186 | Poll::Ready(()) |
| 187 | } else { | 187 | } else { |
| 188 | schedule_wake(self.expires_at, cx.waker()); | 188 | embassy_time_queue_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker()); |
| 189 | Poll::Pending | 189 | Poll::Pending |
| 190 | } | 190 | } |
| 191 | }) | 191 | }) |
| @@ -202,7 +202,7 @@ impl Stream for Ticker { | |||
| 202 | self.expires_at += dur; | 202 | self.expires_at += dur; |
| 203 | Poll::Ready(Some(())) | 203 | Poll::Ready(Some(())) |
| 204 | } else { | 204 | } else { |
| 205 | schedule_wake(self.expires_at, cx.waker()); | 205 | embassy_time_queue_driver::schedule_wake(self.expires_at.as_ticks(), cx.waker()); |
| 206 | Poll::Pending | 206 | Poll::Pending |
| 207 | } | 207 | } |
| 208 | } | 208 | } |
| @@ -214,11 +214,3 @@ impl FusedStream for Ticker { | |||
| 214 | false | 214 | false |
| 215 | } | 215 | } |
| 216 | } | 216 | } |
| 217 | |||
| 218 | extern "Rust" { | ||
| 219 | fn _embassy_time_schedule_wake(at: Instant, waker: &Waker); | ||
| 220 | } | ||
| 221 | |||
| 222 | fn schedule_wake(at: Instant, waker: &Waker) { | ||
| 223 | unsafe { _embassy_time_schedule_wake(at, waker) } | ||
| 224 | } | ||
