2021-08-24 22:46:07 +02:00
|
|
|
//! Time driver interface
|
|
|
|
//!
|
2022-08-17 23:40:16 +02:00
|
|
|
//! This module defines the interface a driver needs to implement to power the `embassy_time` module.
|
2021-08-24 22:46:07 +02:00
|
|
|
//!
|
|
|
|
//! # Implementing a driver
|
|
|
|
//!
|
|
|
|
//! - Define a struct `MyDriver`
|
|
|
|
//! - Implement [`Driver`] for it
|
2022-08-22 09:11:24 +02:00
|
|
|
//! - Register it as the global driver with [`time_driver_impl`](crate::time_driver_impl).
|
2022-08-17 23:40:16 +02:00
|
|
|
//! - Enable the Cargo features `embassy-executor/time` and one of `embassy-time/tick-*` corresponding to the
|
2021-08-24 22:46:07 +02:00
|
|
|
//! tick rate of your driver.
|
|
|
|
//!
|
|
|
|
//! If you wish to make the tick rate configurable by the end user, you should do so by exposing your own
|
2022-08-17 23:40:16 +02:00
|
|
|
//! Cargo features and having each enable the corresponding `embassy-time/tick-*`.
|
2021-08-24 22:46:07 +02:00
|
|
|
//!
|
|
|
|
//! # Linkage details
|
|
|
|
//!
|
|
|
|
//! Instead of the usual "trait + generic params" approach, calls from embassy to the driver are done via `extern` functions.
|
|
|
|
//!
|
|
|
|
//! `embassy` internally defines the driver functions as `extern "Rust" { fn _embassy_time_now() -> u64; }` and calls them.
|
|
|
|
//! The driver crate defines the functions as `#[no_mangle] fn _embassy_time_now() -> u64`. The linker will resolve the
|
|
|
|
//! calls from the `embassy` crate to call into the driver crate.
|
|
|
|
//!
|
|
|
|
//! If there is none or multiple drivers in the crate tree, linking will fail.
|
|
|
|
//!
|
|
|
|
//! This method has a few key advantages for something as foundational as timekeeping:
|
|
|
|
//!
|
|
|
|
//! - The time driver is available everywhere easily, without having to thread the implementation
|
2021-08-25 18:50:05 +02:00
|
|
|
//! through generic parameters. This is especially helpful for libraries.
|
2021-08-24 22:46:07 +02:00
|
|
|
//! - It means comparing `Instant`s will always make sense: if there were multiple drivers
|
|
|
|
//! active, one could compare an `Instant` from driver A to an `Instant` from driver B, which
|
|
|
|
//! would yield incorrect results.
|
|
|
|
//!
|
2021-08-25 18:50:05 +02:00
|
|
|
//! # Example
|
|
|
|
//!
|
|
|
|
//! ```
|
2022-08-17 23:40:16 +02:00
|
|
|
//! use embassy_time::driver::{Driver, AlarmHandle};
|
2021-08-25 18:50:05 +02:00
|
|
|
//!
|
2023-07-05 14:07:05 +02:00
|
|
|
//! struct MyDriver{} // not public!
|
2022-08-17 23:40:16 +02:00
|
|
|
//! embassy_time::time_driver_impl!(static DRIVER: MyDriver = MyDriver{});
|
2021-08-25 18:50:05 +02:00
|
|
|
//!
|
|
|
|
//! impl Driver for MyDriver {
|
|
|
|
//! fn now(&self) -> u64 {
|
|
|
|
//! todo!()
|
|
|
|
//! }
|
|
|
|
//! unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
|
|
|
|
//! todo!()
|
|
|
|
//! }
|
|
|
|
//! fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) {
|
|
|
|
//! todo!()
|
|
|
|
//! }
|
2023-05-29 19:46:28 +02:00
|
|
|
//! fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool {
|
2021-08-25 18:50:05 +02:00
|
|
|
//! todo!()
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! ```
|
2021-08-24 22:46:07 +02:00
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
/// Alarm handle, assigned by the driver.
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub struct AlarmHandle {
|
|
|
|
id: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AlarmHandle {
|
|
|
|
/// Create an AlarmHandle
|
|
|
|
///
|
|
|
|
/// Safety: May only be called by the current global Driver impl.
|
|
|
|
/// The impl is allowed to rely on the fact that all `AlarmHandle` instances
|
|
|
|
/// are created by itself in unsafe code (e.g. indexing operations)
|
|
|
|
pub unsafe fn new(id: u8) -> Self {
|
|
|
|
Self { id }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the ID of the AlarmHandle.
|
|
|
|
pub fn id(&self) -> u8 {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Time driver
|
2021-08-25 18:50:05 +02:00
|
|
|
pub trait Driver: Send + Sync + 'static {
|
2021-08-03 22:08:13 +02:00
|
|
|
/// Return the current timestamp in ticks.
|
2022-02-23 05:14:32 +01:00
|
|
|
///
|
|
|
|
/// Implementations MUST ensure that:
|
|
|
|
/// - This is guaranteed to be monotonic, i.e. a call to now() will always return
|
|
|
|
/// a greater or equal value than earler calls. Time can't "roll backwards".
|
|
|
|
/// - It "never" overflows. It must not overflow in a sufficiently long time frame, say
|
|
|
|
/// in 10_000 years (Human civilization is likely to already have self-destructed
|
|
|
|
/// 10_000 years from now.). This means if your hardware only has 16bit/32bit timers
|
|
|
|
/// you MUST extend them to 64-bit, for example by counting overflows in software,
|
|
|
|
/// or chaining multiple timers together.
|
2021-08-25 18:50:05 +02:00
|
|
|
fn now(&self) -> u64;
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
/// Try allocating an alarm handle. Returns None if no alarms left.
|
|
|
|
/// Initially the alarm has no callback set, and a null `ctx` pointer.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// It is UB to make the alarm fire before setting a callback.
|
2021-08-25 18:50:05 +02:00
|
|
|
unsafe fn allocate_alarm(&self) -> Option<AlarmHandle>;
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
/// Sets the callback function to be called when the alarm triggers.
|
|
|
|
/// The callback may be called from any context (interrupt or thread mode).
|
2021-08-25 18:50:05 +02:00
|
|
|
fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ());
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Sets an alarm at the given timestamp. When the current timestamp reaches the alarm
|
2022-04-21 18:27:00 +02:00
|
|
|
/// timestamp, the provided callback function will be called.
|
2021-08-03 22:08:13 +02:00
|
|
|
///
|
2022-10-24 08:17:43 +02:00
|
|
|
/// The `Driver` implementation should guarantee that the alarm callback is never called synchronously from `set_alarm`.
|
|
|
|
/// Rather - if `timestamp` is already in the past - `false` should be returned and alarm should not be set,
|
|
|
|
/// or alternatively, the driver should return `true` and arrange to call the alarm callback as soon as possible, but not synchronously.
|
2021-08-26 00:20:52 +02:00
|
|
|
///
|
2021-08-03 22:08:13 +02:00
|
|
|
/// When callback is called, it is guaranteed that now() will return a value greater or equal than timestamp.
|
|
|
|
///
|
2021-08-26 00:20:52 +02:00
|
|
|
/// Only one alarm can be active at a time for each AlarmHandle. This overwrites any previously-set alarm if any.
|
2022-10-24 08:17:43 +02:00
|
|
|
fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool;
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "Rust" {
|
|
|
|
fn _embassy_time_now() -> u64;
|
|
|
|
fn _embassy_time_allocate_alarm() -> Option<AlarmHandle>;
|
|
|
|
fn _embassy_time_set_alarm_callback(alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ());
|
2022-10-24 08:17:43 +02:00
|
|
|
fn _embassy_time_set_alarm(alarm: AlarmHandle, timestamp: u64) -> bool;
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 23:40:16 +02:00
|
|
|
/// See [`Driver::now`]
|
|
|
|
pub fn now() -> u64 {
|
2021-08-03 22:08:13 +02:00
|
|
|
unsafe { _embassy_time_now() }
|
|
|
|
}
|
2022-08-17 23:40:16 +02:00
|
|
|
|
|
|
|
/// See [`Driver::allocate_alarm`]
|
|
|
|
///
|
2021-08-03 22:08:13 +02:00
|
|
|
/// Safety: it is UB to make the alarm fire before setting a callback.
|
2022-08-17 23:40:16 +02:00
|
|
|
pub unsafe fn allocate_alarm() -> Option<AlarmHandle> {
|
2021-08-03 22:08:13 +02:00
|
|
|
_embassy_time_allocate_alarm()
|
|
|
|
}
|
2022-08-17 23:40:16 +02:00
|
|
|
|
|
|
|
/// See [`Driver::set_alarm_callback`]
|
|
|
|
pub fn set_alarm_callback(alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) {
|
2021-08-03 22:08:13 +02:00
|
|
|
unsafe { _embassy_time_set_alarm_callback(alarm, callback, ctx) }
|
|
|
|
}
|
2022-08-17 23:40:16 +02:00
|
|
|
|
|
|
|
/// See [`Driver::set_alarm`]
|
2022-10-24 08:17:43 +02:00
|
|
|
pub fn set_alarm(alarm: AlarmHandle, timestamp: u64) -> bool {
|
2021-08-03 22:08:13 +02:00
|
|
|
unsafe { _embassy_time_set_alarm(alarm, timestamp) }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Set the time Driver implementation.
|
|
|
|
///
|
2021-08-25 18:50:05 +02:00
|
|
|
/// See the module documentation for an example.
|
2021-08-03 22:08:13 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! time_driver_impl {
|
2021-08-25 18:50:05 +02:00
|
|
|
(static $name:ident: $t: ty = $val:expr) => {
|
|
|
|
static $name: $t = $val;
|
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
#[no_mangle]
|
|
|
|
fn _embassy_time_now() -> u64 {
|
2022-08-17 23:40:16 +02:00
|
|
|
<$t as $crate::driver::Driver>::now(&$name)
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
2021-08-25 18:50:05 +02:00
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
#[no_mangle]
|
2022-08-17 23:40:16 +02:00
|
|
|
unsafe fn _embassy_time_allocate_alarm() -> Option<$crate::driver::AlarmHandle> {
|
|
|
|
<$t as $crate::driver::Driver>::allocate_alarm(&$name)
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
2021-08-25 18:50:05 +02:00
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
#[no_mangle]
|
2022-08-17 23:40:16 +02:00
|
|
|
fn _embassy_time_set_alarm_callback(alarm: $crate::driver::AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) {
|
|
|
|
<$t as $crate::driver::Driver>::set_alarm_callback(&$name, alarm, callback, ctx)
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
2021-08-25 18:50:05 +02:00
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
#[no_mangle]
|
2022-10-24 08:17:43 +02:00
|
|
|
fn _embassy_time_set_alarm(alarm: $crate::driver::AlarmHandle, timestamp: u64) -> bool {
|
2022-08-17 23:40:16 +02:00
|
|
|
<$t as $crate::driver::Driver>::set_alarm(&$name, alarm, timestamp)
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|