Trait embassy_time::driver::Driver
source · pub trait Driver: Send + Sync + 'static {
// Required methods
fn now(&self) -> u64;
unsafe fn allocate_alarm(&self) -> Option<AlarmHandle>;
fn set_alarm_callback(
&self,
alarm: AlarmHandle,
callback: fn(_: *mut ()),
ctx: *mut ()
);
fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool;
}
Expand description
Time driver
Required Methods§
sourcefn now(&self) -> u64
fn now(&self) -> u64
Return the current timestamp in ticks.
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.
sourceunsafe fn allocate_alarm(&self) -> Option<AlarmHandle>
unsafe fn allocate_alarm(&self) -> Option<AlarmHandle>
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.
sourcefn set_alarm_callback(
&self,
alarm: AlarmHandle,
callback: fn(_: *mut ()),
ctx: *mut ()
)
fn set_alarm_callback( &self, alarm: AlarmHandle, callback: fn(_: *mut ()), ctx: *mut () )
Sets the callback function to be called when the alarm triggers. The callback may be called from any context (interrupt or thread mode).
sourcefn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool
fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool
Sets an alarm at the given timestamp. When the current timestamp reaches the alarm timestamp, the provided callback function will be called.
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.
When callback is called, it is guaranteed that now() will return a value greater or equal than timestamp.
Only one alarm can be active at a time for each AlarmHandle. This overwrites any previously-set alarm if any.