Add clock::Monotonic trait.

This commit is contained in:
Dario Nieuwenhuis 2020-09-24 23:26:24 +02:00
parent afcf725519
commit 05ca563e7d
6 changed files with 44 additions and 17 deletions

View File

@ -1,8 +1,8 @@
use core::cell::Cell; use core::cell::Cell;
use core::ops::Deref; use core::ops::Deref;
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
use defmt::trace; use defmt::trace;
use embassy::clock::Monotonic;
use crate::interrupt; use crate::interrupt;
use crate::interrupt::Mutex; use crate::interrupt::Mutex;
@ -121,12 +121,6 @@ impl<T: Instance> RTC<T> {
}) })
} }
pub fn now(&self) -> u64 {
let counter = self.rtc.counter.read().bits();
let period = self.period.load(Ordering::Relaxed);
calc_now(period, counter)
}
fn trigger_alarm(&self) { fn trigger_alarm(&self) {
self.rtc.intenclr.write(|w| w.compare1().clear()); self.rtc.intenclr.write(|w| w.compare1().clear());
interrupt::free(|cs| { interrupt::free(|cs| {
@ -139,19 +133,19 @@ impl<T: Instance> RTC<T> {
}); });
} }
fn do_set_alarm(&self, at: u64, f: Option<fn()>) { fn do_set_alarm(&self, timestamp: u64, callback: Option<fn()>) {
interrupt::free(|cs| { interrupt::free(|cs| {
self.alarm.borrow(cs).set((at, f)); self.alarm.borrow(cs).set((timestamp, callback));
let t = self.now(); let t = self.now();
if at <= t { if timestamp <= t {
self.trigger_alarm(); self.trigger_alarm();
return; return;
} }
let diff = at - t; let diff = timestamp - t;
if diff < 0xc00000 { if diff < 0xc00000 {
self.rtc.cc[1].write(|w| unsafe { w.bits(at as u32 & 0xFFFFFF) }); self.rtc.cc[1].write(|w| unsafe { w.bits(timestamp as u32 & 0xFFFFFF) });
self.rtc.intenset.write(|w| w.compare1().set()); self.rtc.intenset.write(|w| w.compare1().set());
// We may have been preempted for arbitrary time between checking if `at` is in the past // We may have been preempted for arbitrary time between checking if `at` is in the past
@ -159,7 +153,7 @@ impl<T: Instance> RTC<T> {
// So, we check again just in case. // So, we check again just in case.
let t = self.now(); let t = self.now();
if at <= t { if timestamp <= t {
self.trigger_alarm(); self.trigger_alarm();
return; return;
} }
@ -168,12 +162,20 @@ impl<T: Instance> RTC<T> {
} }
}) })
} }
pub fn set_alarm(&self, at: u64, f: fn()) {
self.do_set_alarm(at, Some(f));
} }
pub fn clear_alarm(&self) { impl<T: Instance> Monotonic for RTC<T> {
fn now(&self) -> u64 {
let counter = self.rtc.counter.read().bits();
let period = self.period.load(Ordering::Relaxed);
calc_now(period, counter)
}
fn set_alarm(&self, timestamp: u64, callback: fn()) {
self.do_set_alarm(timestamp, Some(callback));
}
fn clear_alarm(&self) {
self.do_set_alarm(u64::MAX, None); self.do_set_alarm(u64::MAX, None);
} }
} }

View File

@ -12,3 +12,4 @@ defmt = "0.1.0"
cortex-m = "0.6.3" cortex-m = "0.6.3"
futures = { version = "0.3.5", default-features = false, features = [ "async-await" ] } futures = { version = "0.3.5", default-features = false, features = [ "async-await" ] }
pin-project = { version = "0.4.23", default-features = false } pin-project = { version = "0.4.23", default-features = false }
futures-intrusive = { version = "0.3.1", default-features = false }

21
embassy/src/clock.rs Normal file
View File

@ -0,0 +1,21 @@
/// Monotonic clock with support for setting an alarm.
///
/// The clock uses a "tick" time unit, whose length is an implementation-dependent constant.
pub trait Monotonic {
/// Returns the current timestamp in ticks.
/// This is guaranteed to be monotonic, i.e. a call to now() will always return
/// a greater or equal value than earler calls.
fn now(&self) -> u64;
/// Sets an alarm at the given timestamp. When the clock reaches that
/// timestamp, the provided callback funcion will be called.
///
/// 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. This overwrites any previously-set alarm if any.
fn set_alarm(&self, timestamp: u64, callback: fn());
/// Clears the previously-set alarm.
/// If no alarm was set, this is a noop.
fn clear_alarm(&self);
}

View File

@ -3,6 +3,7 @@
#![feature(generic_associated_types)] #![feature(generic_associated_types)]
#![feature(const_fn)] #![feature(const_fn)]
pub mod clock;
pub mod flash; pub mod flash;
pub mod util;
pub mod io; pub mod io;
pub mod util;

View File

@ -8,6 +8,7 @@ use example_common::*;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embassy::clock::Monotonic;
use embassy_nrf::rtc; use embassy_nrf::rtc;
use futures_intrusive::timer::{Clock, LocalTimer, LocalTimerService}; use futures_intrusive::timer::{Clock, LocalTimer, LocalTimerService};
use nrf52840_hal::clocks; use nrf52840_hal::clocks;

View File

@ -8,6 +8,7 @@ use example_common::*;
use core::mem::MaybeUninit; use core::mem::MaybeUninit;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embassy::clock::Monotonic;
use embassy_nrf::rtc; use embassy_nrf::rtc;
use nrf52840_hal::clocks; use nrf52840_hal::clocks;