blob: 35af9b1c0b9ae8158a9ff15d0f1995504ff3fcc1 [file] [log] [blame] [edit]
//! Async timer driver. Can be used for (non-busy) sleeping.
use crate::callback::CallbackSubscription;
use crate::callback::Consumer;
use crate::futures;
use crate::result::OtherError;
use crate::result::TockError;
use crate::result::TockResult;
use crate::syscalls;
use core::cell::Cell;
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Sub};
const DRIVER_NUMBER: usize = 0x00000;
mod command_nr {
pub const IS_DRIVER_AVAILABLE: usize = 0;
pub const GET_CLOCK_FREQUENCY: usize = 1;
pub const GET_CLOCK_VALUE: usize = 2;
pub const STOP_ALARM: usize = 3;
pub const SET_ALARM: usize = 4;
}
mod subscribe_nr {
pub const SUBSCRIBE_CALLBACK: usize = 0;
}
pub struct WithCallback<'a, CB> {
callback: CB,
clock_frequency: ClockFrequency,
phantom: PhantomData<&'a mut ()>,
}
struct TimerEventConsumer;
impl<CB: FnMut(ClockValue, Alarm)> Consumer<WithCallback<'_, CB>> for TimerEventConsumer {
fn consume(data: &mut WithCallback<CB>, clock_value: usize, alarm_id: usize, _: usize) {
(data.callback)(
ClockValue {
num_ticks: clock_value as usize,
clock_frequency: data.clock_frequency,
},
Alarm { alarm_id },
);
}
}
impl<'a, CB: FnMut(ClockValue, Alarm)> WithCallback<'a, CB> {
pub fn init(&'a mut self) -> TockResult<Timer<'a>> {
let num_notifications =
syscalls::command(DRIVER_NUMBER, command_nr::IS_DRIVER_AVAILABLE, 0, 0)?;
let clock_frequency =
unsafe { syscalls::raw::command(DRIVER_NUMBER, command_nr::GET_CLOCK_FREQUENCY, 0, 0) };
if clock_frequency == 0 {
return Err(OtherError::TimerDriverErroneousClockFrequency.into());
}
let clock_frequency = ClockFrequency {
hz: clock_frequency as usize,
};
let subscription = syscalls::subscribe::<TimerEventConsumer, _>(
DRIVER_NUMBER,
subscribe_nr::SUBSCRIBE_CALLBACK,
self,
)?;
Ok(Timer {
num_notifications,
clock_frequency,
subscription,
})
}
}
pub struct Timer<'a> {
num_notifications: usize,
clock_frequency: ClockFrequency,
#[allow(dead_code)] // Used in drop
subscription: CallbackSubscription<'a>,
}
impl<'a> Timer<'a> {
pub fn num_notifications(&self) -> usize {
self.num_notifications
}
pub fn clock_frequency(&self) -> ClockFrequency {
self.clock_frequency
}
pub fn get_current_clock(&self) -> TockResult<ClockValue> {
Ok(ClockValue {
num_ticks: unsafe {
syscalls::raw::command(DRIVER_NUMBER, command_nr::GET_CLOCK_VALUE, 0, 0) as usize
},
clock_frequency: self.clock_frequency,
})
}
pub fn stop_alarm(&mut self, alarm: Alarm) -> TockResult<()> {
syscalls::command(DRIVER_NUMBER, command_nr::STOP_ALARM, alarm.alarm_id, 0)?;
Ok(())
}
pub fn set_alarm(&mut self, duration: Duration<isize>) -> TockResult<Alarm> {
let now = self.get_current_clock()?;
let freq = self.clock_frequency.hz();
let duration_ms = duration.ms() as usize;
let ticks = match duration_ms.checked_mul(freq) {
Some(x) => x / 1000,
None => {
// Divide the largest of the two operands by 1000, to improve precision of the
// result.
if duration_ms > freq {
match (duration_ms / 1000).checked_mul(freq) {
Some(y) => y,
None => return Err(OtherError::TimerDriverDurationOutOfRange.into()),
}
} else {
match (freq / 1000).checked_mul(duration_ms) {
Some(y) => y,
None => return Err(OtherError::TimerDriverDurationOutOfRange.into()),
}
}
}
};
let alarm_instant = now.num_ticks() as usize + ticks;
let alarm_id = unsafe {
syscalls::raw::command(DRIVER_NUMBER, command_nr::SET_ALARM, alarm_instant, 0)
};
Ok(Alarm {
alarm_id: alarm_id as usize,
})
}
}
#[derive(Copy, Clone, Debug)]
pub struct ClockFrequency {
hz: usize,
}
impl ClockFrequency {
pub fn hz(self) -> usize {
self.hz
}
}
#[derive(Copy, Clone, Debug)]
pub struct ClockValue {
num_ticks: usize,
clock_frequency: ClockFrequency,
}
impl ClockValue {
pub fn num_ticks(self) -> usize {
self.num_ticks
}
pub fn ms(self) -> usize {
if self.num_ticks < usize::MAX / 1000 {
(1000 * self.num_ticks) / self.clock_frequency.hz()
} else {
1000 * (self.num_ticks / self.clock_frequency.hz())
}
}
pub fn ms_f64(self) -> f64 {
1000.0 * (self.num_ticks as f64) / (self.clock_frequency.hz() as f64)
}
}
pub struct Alarm {
alarm_id: usize,
}
impl Alarm {
pub fn alarm_id(&self) -> usize {
self.alarm_id
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Duration<T> {
ms: T,
}
impl<T> Duration<T> {
pub const fn from_ms(ms: T) -> Duration<T> {
Duration { ms }
}
}
impl<T> Duration<T>
where
T: Copy,
{
pub fn ms(&self) -> T {
self.ms
}
}
impl<T> Sub for Duration<T>
where
T: Sub<Output = T>,
{
type Output = Duration<T>;
fn sub(self, other: Duration<T>) -> Duration<T> {
Duration {
ms: self.ms - other.ms,
}
}
}
#[derive(Copy, Clone, Debug)]
pub struct Timestamp<T> {
ms: T,
}
impl<T> Timestamp<T> {
pub const fn from_ms(ms: T) -> Timestamp<T> {
Timestamp { ms }
}
}
impl<T> Timestamp<T>
where
T: Copy,
{
pub fn ms(&self) -> T {
self.ms
}
}
impl Timestamp<usize> {
pub fn from_clock_value(value: ClockValue) -> Timestamp<usize> {
Timestamp { ms: value.ms() }
}
}
impl Timestamp<f64> {
pub fn from_clock_value(value: ClockValue) -> Timestamp<f64> {
Timestamp { ms: value.ms_f64() }
}
}
impl<T> Sub for Timestamp<T>
where
T: Sub<Output = T>,
{
type Output = Duration<T>;
fn sub(self, other: Timestamp<T>) -> Duration<T> {
Duration::from_ms(self.ms - other.ms)
}
}
impl<T> Add<Duration<T>> for Timestamp<T>
where
T: Copy + Add<Output = T>,
{
type Output = Timestamp<T>;
fn add(self, duration: Duration<T>) -> Timestamp<T> {
Timestamp {
ms: self.ms + duration.ms(),
}
}
}
impl<T> AddAssign<Duration<T>> for Timestamp<T>
where
T: Copy + AddAssign,
{
fn add_assign(&mut self, duration: Duration<T>) {
self.ms += duration.ms();
}
}
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub(crate) struct ActiveTimer {
instant: u32,
set_at: u32,
}
/// Context for the time driver.
/// You can create a context as follows:
/// ```no_run
/// # use libtock::result::TockResult;
/// # async fn doc() -> TockResult<()> {
/// let mut drivers = libtock::retrieve_drivers()?;
/// let mut timer_context = drivers.timer;
/// # Ok(())
/// # }
/// ```
#[non_exhaustive]
pub struct DriverContext {
pub(crate) active_timer: Cell<Option<ActiveTimer>>,
}
impl DriverContext {
/// Create a driver timer from a context.
pub fn create_timer_driver(&mut self) -> TimerDriver {
TimerDriver {
callback: Callback,
context: self,
}
}
pub fn with_callback<CB>(&mut self, callback: CB) -> WithCallback<CB> {
WithCallback {
callback,
clock_frequency: ClockFrequency { hz: 0 },
phantom: PhantomData,
}
}
}
/// Timer driver instance. You can create a TimerDriver from a DriverContext as follows:
/// ```no_run
/// # use libtock::result::TockResult;
/// # async fn doc() -> TockResult<()> {
/// # let mut drivers = libtock::retrieve_drivers()?;
/// # let mut timer_context = drivers.timer;
/// let mut timer_driver = timer_context.create_timer_driver();
/// let timer_driver = timer_driver.activate()?;
/// # Ok(())
/// # }
/// ```
pub struct TimerDriver<'a> {
callback: Callback,
context: &'a DriverContext,
}
struct Callback;
struct ParallelTimerConsumer;
impl<'a> Consumer<Callback> for ParallelTimerConsumer {
fn consume(_: &mut Callback, _: usize, _: usize, _: usize) {}
}
/// Activated time driver. Updates current time in the context and manages
/// active alarms.
/// Example usage (sleep for 1 second):
/// ```no_run
/// # use libtock::result::TockResult;
/// # use libtock::timer::Duration;
/// # async fn doc() -> TockResult<()> {
/// # let mut drivers = libtock::retrieve_drivers()?;
/// # let mut timer_driver = drivers.timer.create_timer_driver();
/// let timer_driver = timer_driver.activate()?;
/// timer_driver.sleep(Duration::from_ms(1000)).await?;
/// # Ok(())
/// # }
/// ```
pub struct ParallelSleepDriver<'a> {
_callback_subscription: CallbackSubscription<'a>,
context: &'a DriverContext,
}
impl<'a> TimerDriver<'a> {
/// Activate the timer driver, will return a ParallelSleepDriver which
/// can used to sleep.
pub fn activate(&'a mut self) -> TockResult<ParallelSleepDriver<'a>> {
let subscription = syscalls::subscribe::<ParallelTimerConsumer, _>(
DRIVER_NUMBER,
subscribe_nr::SUBSCRIBE_CALLBACK,
&mut self.callback,
)?;
let driver = ParallelSleepDriver {
_callback_subscription: subscription,
context: &self.context,
};
Ok(driver)
}
}
impl<'a> ParallelSleepDriver<'a> {
/// Sleep for the given duration
pub async fn sleep(&self, duration: Duration<usize>) -> TockResult<()> {
let now = get_current_ticks()?;
let freq = get_clock_frequency()?;
let alarm_instant = Self::compute_alarm_instant(duration.ms, now, freq)?;
let this_alarm = ActiveTimer {
instant: alarm_instant as u32,
set_at: now as u32,
};
let suspended_timer: Cell<Option<ActiveTimer>> = Cell::new(None);
futures::wait_until(|| {
self.activate_current_timer(this_alarm, &suspended_timer)
.unwrap_or(false)
})
.await;
Ok(())
}
fn activate_timer(&self, timer: ActiveTimer) -> TockResult<()> {
set_alarm_at(timer.instant as usize)?;
let now = get_current_ticks()?;
if !is_over(timer, now as u32) {
self.context.active_timer.set(Some(timer));
} else {
self.wakeup_soon()?;
}
Ok(())
}
fn wakeup_soon(&self) -> TockResult<()> {
self.context.active_timer.set(None);
for i in 0.. {
let now = get_current_ticks()?;
let next_timer = ActiveTimer {
instant: now as u32 + i,
set_at: now as u32,
};
set_alarm_at(next_timer.instant as usize)?;
let now = get_current_ticks()?;
if !is_over(next_timer, now as u32) {
break;
} else {
stop_alarm_at(next_timer.instant as usize)?;
}
}
Ok(())
}
fn compute_alarm_instant(
duration_ms: usize,
num_ticks: usize,
freq: usize,
) -> TockResult<usize> {
let ticks = match duration_ms.checked_mul(freq) {
Some(x) => x / 1000,
None => {
// Divide the largest of the two operands by 1000, to improve precision of the
// result.
if duration_ms > freq {
match (duration_ms / 1000).checked_mul(freq) {
Some(y) => y,
None => {
return Err(TockError::Other(OtherError::TimerDriverDurationOutOfRange))
}
}
} else {
match (freq / 1000).checked_mul(duration_ms) {
Some(y) => y,
None => {
return Err(TockError::Other(OtherError::TimerDriverDurationOutOfRange))
}
}
}
}
};
let alarm_instant = num_ticks + ticks;
Ok(alarm_instant)
}
fn activate_current_timer(
&self,
this_alarm: ActiveTimer,
suspended_timer: &Cell<Option<ActiveTimer>>,
) -> TockResult<bool> {
let now = get_current_ticks()?;
if let Some(active) = self.context.active_timer.get() {
if left_is_later(active, this_alarm) {
suspended_timer.set(Some(active));
self.activate_timer(this_alarm)?;
}
} else {
self.activate_timer(this_alarm)?;
}
if is_over(this_alarm, now as u32) {
if let Some(paused) = suspended_timer.get() {
self.activate_timer(paused)?;
} else {
self.context.active_timer.set(None);
}
Ok(true)
} else {
Ok(false)
}
}
}
fn get_current_ticks() -> TockResult<usize> {
unsafe { Ok(syscalls::raw::command(DRIVER_NUMBER, command_nr::GET_CLOCK_VALUE, 0, 0) as usize) }
}
fn set_alarm_at(instant: usize) -> TockResult<()> {
unsafe {
syscalls::raw::command(DRIVER_NUMBER, command_nr::SET_ALARM, instant, 0);
Ok(())
}
}
fn stop_alarm_at(instant: usize) -> TockResult<()> {
unsafe {
syscalls::raw::command(DRIVER_NUMBER, command_nr::STOP_ALARM, instant, 0);
Ok(())
}
}
fn get_clock_frequency() -> TockResult<usize> {
unsafe {
Ok(syscalls::raw::command(DRIVER_NUMBER, command_nr::GET_CLOCK_FREQUENCY, 0, 0) as usize)
}
}
fn is_over(timer: ActiveTimer, now: u32) -> bool {
now.wrapping_sub(timer.set_at) >= timer.instant.wrapping_sub(timer.set_at)
}
fn left_is_later(alarm_1: ActiveTimer, alarm_2: ActiveTimer) -> bool {
if alarm_1.set_at <= alarm_1.instant && alarm_2.set_at <= alarm_2.instant {
return alarm_1.instant > alarm_2.instant;
}
if alarm_1.set_at <= alarm_1.instant && alarm_2.set_at >= alarm_2.instant {
return false;
}
if alarm_1.set_at >= alarm_1.instant && alarm_2.set_at <= alarm_2.instant {
return true;
}
if alarm_1.set_at >= alarm_1.instant && alarm_2.set_at >= alarm_2.instant {
return alarm_1.instant > alarm_2.instant;
}
false
}
#[cfg(test)]
mod test {
use super::*;
#[test]
pub fn duration_bigger_than_frequency() {
let x = ParallelSleepDriver::compute_alarm_instant(10000, 0, 1000)
.ok()
.unwrap();
assert_eq!(x, 10000);
}
#[test]
pub fn frequency_bigger_than_duration() {
let x = ParallelSleepDriver::compute_alarm_instant(1000, 0, 10000)
.ok()
.unwrap();
assert_eq!(x, 10000);
}
#[test]
pub fn fails_if_duration_is_too_large() {
let x =
ParallelSleepDriver::compute_alarm_instant(core::usize::MAX, 0, core::usize::MAX - 1);
assert!(x.is_err());
}
#[test]
pub fn fails_if_frequency_is_too_large() {
let x =
ParallelSleepDriver::compute_alarm_instant(core::usize::MAX - 1, 0, core::usize::MAX);
assert!(x.is_err());
}
#[test]
pub fn alarm_before_systick_wrap_expired() {
assert_eq!(
super::is_over(
super::ActiveTimer {
instant: 2u32,
set_at: 1u32
},
3u32
),
true
);
}
#[test]
pub fn alarm_before_systick_wrap_not_expired() {
assert_eq!(
super::is_over(
super::ActiveTimer {
instant: 3u32,
set_at: 1u32
},
2u32
),
false
);
}
#[test]
pub fn alarm_after_systick_wrap_expired() {
assert_eq!(
super::is_over(
super::ActiveTimer {
instant: 1u32,
set_at: 3u32
},
2u32
),
true
);
}
#[test]
pub fn alarm_after_systick_wrap_time_before_systick_wrap_not_expired() {
assert_eq!(
super::is_over(
super::ActiveTimer {
instant: 1u32,
set_at: 3u32
},
4u32
),
false
);
}
#[test]
pub fn alarm_after_systick_wrap_time_after_systick_wrap_not_expired() {
assert_eq!(
super::is_over(
super::ActiveTimer {
instant: 1u32,
set_at: 3u32
},
0u32
),
false
);
}
#[test]
pub fn left_later_than_the_other_both_not_wrapped() {
let later = super::ActiveTimer {
instant: 3u32,
set_at: 1u32,
};
let earlier = super::ActiveTimer {
instant: 2u32,
set_at: 1u32,
};
assert_eq!(super::left_is_later(later, earlier), true);
}
#[test]
pub fn right_later_than_the_other_both_not_wrapped() {
let later = super::ActiveTimer {
instant: 2u32,
set_at: 1u32,
};
let earlier = super::ActiveTimer {
instant: 3u32,
set_at: 1u32,
};
assert_eq!(super::left_is_later(later, earlier), false);
}
#[test]
pub fn left_later_left_wrapped() {
let later = super::ActiveTimer {
instant: 1u32,
set_at: 3u32,
};
let earlier = super::ActiveTimer {
instant: 2u32,
set_at: 1u32,
};
assert_eq!(super::left_is_later(later, earlier), true);
}
#[test]
pub fn right_later_right_wrapped() {
let later = super::ActiveTimer {
instant: 3u32,
set_at: 1u32,
};
let earlier = super::ActiveTimer {
instant: 1u32,
set_at: 3u32,
};
assert_eq!(super::left_is_later(later, earlier), false);
}
#[test]
pub fn left_later_both_wrapped() {
let later = super::ActiveTimer {
instant: 2u32,
set_at: 3u32,
};
let earlier = super::ActiveTimer {
instant: 1u32,
set_at: 3u32,
};
assert_eq!(super::left_is_later(later, earlier), true);
}
#[test]
pub fn right_later_both_wrapped() {
let later = super::ActiveTimer {
instant: 1u32,
set_at: 3u32,
};
let earlier = super::ActiveTimer {
instant: 2u32,
set_at: 3u32,
};
assert_eq!(super::left_is_later(later, earlier), false);
}
#[test]
pub fn inequality_is_strict() {
let later = super::ActiveTimer {
instant: 2u32,
set_at: 1u32,
};
let earlier = super::ActiveTimer {
instant: 2u32,
set_at: 1u32,
};
assert_eq!(super::left_is_later(later, earlier), false);
}
#[test]
pub fn inequality_is_strict_wrapped() {
let later = super::ActiveTimer {
instant: 1u32,
set_at: 2u32,
};
let earlier = super::ActiveTimer {
instant: 1u32,
set_at: 2u32,
};
assert_eq!(super::left_is_later(later, earlier), false);
}
}