mirror of
https://github.com/embassy-rs/embassy.git
synced 2024-11-21 22:32:29 +00:00
Add clock::Monotonic trait.
This commit is contained in:
parent
afcf725519
commit
05ca563e7d
@ -1,8 +1,8 @@
|
||||
use core::cell::Cell;
|
||||
use core::ops::Deref;
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
use defmt::trace;
|
||||
use embassy::clock::Monotonic;
|
||||
|
||||
use crate::interrupt;
|
||||
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) {
|
||||
self.rtc.intenclr.write(|w| w.compare1().clear());
|
||||
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| {
|
||||
self.alarm.borrow(cs).set((at, f));
|
||||
self.alarm.borrow(cs).set((timestamp, callback));
|
||||
|
||||
let t = self.now();
|
||||
if at <= t {
|
||||
if timestamp <= t {
|
||||
self.trigger_alarm();
|
||||
return;
|
||||
}
|
||||
|
||||
let diff = at - t;
|
||||
let diff = timestamp - t;
|
||||
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());
|
||||
|
||||
// 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.
|
||||
|
||||
let t = self.now();
|
||||
if at <= t {
|
||||
if timestamp <= t {
|
||||
self.trigger_alarm();
|
||||
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));
|
||||
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)
|
||||
}
|
||||
|
||||
pub fn clear_alarm(&self) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -12,3 +12,4 @@ defmt = "0.1.0"
|
||||
cortex-m = "0.6.3"
|
||||
futures = { version = "0.3.5", default-features = false, features = [ "async-await" ] }
|
||||
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
21
embassy/src/clock.rs
Normal 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);
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
#![feature(generic_associated_types)]
|
||||
#![feature(const_fn)]
|
||||
|
||||
pub mod clock;
|
||||
pub mod flash;
|
||||
pub mod util;
|
||||
pub mod io;
|
||||
pub mod util;
|
||||
|
@ -8,6 +8,7 @@ use example_common::*;
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
use cortex_m_rt::entry;
|
||||
use embassy::clock::Monotonic;
|
||||
use embassy_nrf::rtc;
|
||||
use futures_intrusive::timer::{Clock, LocalTimer, LocalTimerService};
|
||||
use nrf52840_hal::clocks;
|
||||
|
@ -8,6 +8,7 @@ use example_common::*;
|
||||
|
||||
use core::mem::MaybeUninit;
|
||||
use cortex_m_rt::entry;
|
||||
use embassy::clock::Monotonic;
|
||||
use embassy_nrf::rtc;
|
||||
use nrf52840_hal::clocks;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user