Add support for multi alarm to RTC.
This commit is contained in:
parent
a734a9ff46
commit
cf1d604749
4 changed files with 75 additions and 30 deletions
|
@ -3,7 +3,7 @@ use core::ops::Deref;
|
||||||
use core::sync::atomic::{AtomicU32, Ordering};
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
use crate::interrupt;
|
use crate::interrupt;
|
||||||
use crate::interrupt::Mutex;
|
use crate::interrupt::{CriticalSection, Mutex};
|
||||||
use crate::pac::{rtc0, Interrupt, RTC0, RTC1};
|
use crate::pac::{rtc0, Interrupt, RTC0, RTC1};
|
||||||
|
|
||||||
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
|
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
|
||||||
|
@ -34,6 +34,22 @@ mod test {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct AlarmState {
|
||||||
|
timestamp: Cell<u64>,
|
||||||
|
callback: Cell<Option<fn()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AlarmState {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
timestamp: Cell::new(u64::MAX),
|
||||||
|
callback: Cell::new(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const ALARM_COUNT: usize = 3;
|
||||||
|
|
||||||
pub struct RTC<T> {
|
pub struct RTC<T> {
|
||||||
rtc: T,
|
rtc: T,
|
||||||
|
|
||||||
|
@ -50,7 +66,7 @@ pub struct RTC<T> {
|
||||||
period: AtomicU32,
|
period: AtomicU32,
|
||||||
|
|
||||||
/// Timestamp at which to fire alarm. u64::MAX if no alarm is scheduled.
|
/// Timestamp at which to fire alarm. u64::MAX if no alarm is scheduled.
|
||||||
alarm: Mutex<Cell<(u64, Option<fn()>)>>,
|
alarms: Mutex<[AlarmState; ALARM_COUNT]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl<T> Send for RTC<T> {}
|
unsafe impl<T> Send for RTC<T> {}
|
||||||
|
@ -61,7 +77,7 @@ impl<T: Instance> RTC<T> {
|
||||||
Self {
|
Self {
|
||||||
rtc,
|
rtc,
|
||||||
period: AtomicU32::new(0),
|
period: AtomicU32::new(0),
|
||||||
alarm: Mutex::new(Cell::new((u64::MAX, None))),
|
alarms: Mutex::new([AlarmState::new(), AlarmState::new(), AlarmState::new()]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,9 +117,13 @@ impl<T: Instance> RTC<T> {
|
||||||
self.next_period();
|
self.next_period();
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.rtc.events_compare[1].read().bits() == 1 {
|
for n in 0..ALARM_COUNT {
|
||||||
self.rtc.events_compare[1].write(|w| w);
|
if self.rtc.events_compare[n + 1].read().bits() == 1 {
|
||||||
self.trigger_alarm();
|
self.rtc.events_compare[n + 1].write(|w| w);
|
||||||
|
interrupt::free(|cs| {
|
||||||
|
self.trigger_alarm(n, cs);
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,35 +132,43 @@ impl<T: Instance> RTC<T> {
|
||||||
let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
|
let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
|
||||||
let t = (period as u64) << 23;
|
let t = (period as u64) << 23;
|
||||||
|
|
||||||
let (at, _) = self.alarm.borrow(cs).get();
|
for alarm in self.alarms.borrow(cs) {
|
||||||
|
let at = alarm.timestamp.get();
|
||||||
|
|
||||||
let diff = at - t;
|
let diff = at - 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(at as u32 & 0xFFFFFF) });
|
||||||
self.rtc.intenset.write(|w| w.compare1().set());
|
self.rtc.intenset.write(|w| w.compare1().set());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn trigger_alarm(&self) {
|
fn trigger_alarm(&self, n: usize, cs: &CriticalSection) {
|
||||||
self.rtc.intenclr.write(|w| w.compare1().clear());
|
self.rtc.intenclr.write(|w| w.compare1().clear());
|
||||||
interrupt::free(|cs| {
|
|
||||||
let alarm = self.alarm.borrow(cs);
|
|
||||||
let (_, f) = alarm.get();
|
|
||||||
alarm.set((u64::MAX, None));
|
|
||||||
|
|
||||||
// Call after clearing alarm, so the callback can set another alarm.
|
let alarm = &self.alarms.borrow(cs)[n];
|
||||||
f.map(|f| f())
|
alarm.timestamp.set(u64::MAX);
|
||||||
});
|
|
||||||
|
// Call after clearing alarm, so the callback can set another alarm.
|
||||||
|
alarm.callback.get().map(|f| f());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_set_alarm(&self, timestamp: u64, callback: Option<fn()>) {
|
fn set_alarm_callback(&self, n: usize, callback: fn()) {
|
||||||
interrupt::free(|cs| {
|
interrupt::free(|cs| {
|
||||||
self.alarm.borrow(cs).set((timestamp, callback));
|
let alarm = &self.alarms.borrow(cs)[n];
|
||||||
|
alarm.callback.set(Some(callback));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_alarm(&self, n: usize, timestamp: u64) {
|
||||||
|
interrupt::free(|cs| {
|
||||||
|
let alarm = &self.alarms.borrow(cs)[n];
|
||||||
|
alarm.timestamp.set(timestamp);
|
||||||
|
|
||||||
let t = self.now();
|
let t = self.now();
|
||||||
if timestamp <= t {
|
if timestamp <= t {
|
||||||
self.trigger_alarm();
|
self.trigger_alarm(n, cs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +183,7 @@ impl<T: Instance> RTC<T> {
|
||||||
|
|
||||||
let t = self.now();
|
let t = self.now();
|
||||||
if timestamp <= t {
|
if timestamp <= t {
|
||||||
self.trigger_alarm();
|
self.trigger_alarm(n, cs);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -165,21 +193,32 @@ impl<T: Instance> RTC<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn alarm0(&'static self) -> Alarm<T> {
|
pub fn alarm0(&'static self) -> Alarm<T> {
|
||||||
Alarm { rtc: self }
|
Alarm { n: 0, rtc: self }
|
||||||
|
}
|
||||||
|
pub fn alarm1(&'static self) -> Alarm<T> {
|
||||||
|
Alarm { n: 1, rtc: self }
|
||||||
|
}
|
||||||
|
pub fn alarm2(&'static self) -> Alarm<T> {
|
||||||
|
Alarm { n: 2, rtc: self }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Alarm<T: Instance> {
|
pub struct Alarm<T: Instance> {
|
||||||
|
n: usize,
|
||||||
rtc: &'static RTC<T>,
|
rtc: &'static RTC<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Instance> embassy::time::Alarm for Alarm<T> {
|
impl<T: Instance> embassy::time::Alarm for Alarm<T> {
|
||||||
fn set(&self, timestamp: u64, callback: fn()) {
|
fn set_callback(&self, callback: fn()) {
|
||||||
self.rtc.do_set_alarm(timestamp, Some(callback));
|
self.rtc.set_alarm_callback(self.n, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set(&self, timestamp: u64) {
|
||||||
|
self.rtc.set_alarm(self.n, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clear(&self) {
|
fn clear(&self) {
|
||||||
self.rtc.do_set_alarm(u64::MAX, None);
|
self.rtc.set_alarm(self.n, u64::MAX);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,7 @@ pub struct Executor<M, A: Alarm> {
|
||||||
|
|
||||||
impl<M: Model, A: Alarm> Executor<M, A> {
|
impl<M: Model, A: Alarm> Executor<M, A> {
|
||||||
pub fn new(alarm: A) -> Self {
|
pub fn new(alarm: A) -> Self {
|
||||||
|
alarm.set_callback(M::signal);
|
||||||
Self {
|
Self {
|
||||||
inner: se::Executor::new(M::signal),
|
inner: se::Executor::new(M::signal),
|
||||||
alarm,
|
alarm,
|
||||||
|
@ -53,7 +54,7 @@ impl<M: Model, A: Alarm> Executor<M, A> {
|
||||||
match self.timer.next_expiration() {
|
match self.timer.next_expiration() {
|
||||||
// If this is in the past, set_alarm will immediately trigger the alarm,
|
// If this is in the past, set_alarm will immediately trigger the alarm,
|
||||||
// which will make the wfe immediately return so we do another loop iteration.
|
// which will make the wfe immediately return so we do another loop iteration.
|
||||||
Some(at) => self.alarm.set(at, M::signal),
|
Some(at) => self.alarm.set(at),
|
||||||
None => self.alarm.clear(),
|
None => self.alarm.clear(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -273,13 +273,17 @@ impl Future for Timer {
|
||||||
|
|
||||||
/// Trait to register a callback at a given timestamp.
|
/// Trait to register a callback at a given timestamp.
|
||||||
pub trait Alarm {
|
pub trait Alarm {
|
||||||
|
/// Sets the callback function to be called when the alarm triggers.
|
||||||
|
/// The callback may be called from any context (interrupt or thread mode).
|
||||||
|
fn set_callback(&self, callback: fn());
|
||||||
|
|
||||||
/// Sets an alarm at the given timestamp. When the clock reaches that
|
/// Sets an alarm at the given timestamp. When the clock reaches that
|
||||||
/// timestamp, the provided callback funcion will be called.
|
/// 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.
|
/// 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.
|
/// Only one alarm can be active at a time. This overwrites any previously-set alarm if any.
|
||||||
fn set(&self, timestamp: u64, callback: fn());
|
fn set(&self, timestamp: u64);
|
||||||
|
|
||||||
/// Clears the previously-set alarm.
|
/// Clears the previously-set alarm.
|
||||||
/// If no alarm was set, this is a noop.
|
/// If no alarm was set, this is a noop.
|
||||||
|
|
|
@ -35,7 +35,8 @@ fn main() -> ! {
|
||||||
|
|
||||||
rtc.start();
|
rtc.start();
|
||||||
|
|
||||||
alarm.set(53719, || info!("ALARM TRIGGERED"));
|
alarm.set_callback(|| info!("ALARM TRIGGERED"));
|
||||||
|
alarm.set(53719);
|
||||||
|
|
||||||
info!("initialized!");
|
info!("initialized!");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue