2020-09-24 17:59:20 +00:00
|
|
|
use core::cell::Cell;
|
|
|
|
use core::ops::Deref;
|
|
|
|
use core::sync::atomic::{AtomicU32, Ordering};
|
|
|
|
|
2020-09-25 21:38:42 +00:00
|
|
|
use embassy::time::Clock;
|
|
|
|
|
2020-09-24 17:59:20 +00:00
|
|
|
use crate::interrupt;
|
2020-09-25 21:25:49 +00:00
|
|
|
use crate::interrupt::{CriticalSection, Mutex};
|
2020-09-24 17:59:20 +00:00
|
|
|
use crate::pac::{rtc0, Interrupt, RTC0, RTC1};
|
|
|
|
|
|
|
|
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
|
|
|
|
use crate::pac::RTC2;
|
|
|
|
|
|
|
|
fn calc_now(period: u32, counter: u32) -> u64 {
|
|
|
|
let shift = ((period & 1) << 23) + 0x400000;
|
|
|
|
let counter_shifted = (counter + shift) & 0xFFFFFF;
|
|
|
|
((period as u64) << 23) + counter_shifted as u64 - 0x400000
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:35:25 +00:00
|
|
|
fn compare_n(n: usize) -> u32 {
|
|
|
|
1 << (n + 16)
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:59:20 +00:00
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_calc_now() {
|
|
|
|
assert_eq!(calc_now(0, 0x000000), 0x0_000000);
|
|
|
|
assert_eq!(calc_now(0, 0x000001), 0x0_000001);
|
|
|
|
assert_eq!(calc_now(0, 0x7FFFFF), 0x0_7FFFFF);
|
|
|
|
assert_eq!(calc_now(1, 0x7FFFFF), 0x0_7FFFFF);
|
|
|
|
assert_eq!(calc_now(0, 0x800000), 0x0_800000);
|
|
|
|
assert_eq!(calc_now(1, 0x800000), 0x0_800000);
|
|
|
|
assert_eq!(calc_now(1, 0x800001), 0x0_800001);
|
|
|
|
assert_eq!(calc_now(1, 0xFFFFFF), 0x0_FFFFFF);
|
|
|
|
assert_eq!(calc_now(2, 0xFFFFFF), 0x0_FFFFFF);
|
|
|
|
assert_eq!(calc_now(1, 0x000000), 0x1_000000);
|
|
|
|
assert_eq!(calc_now(2, 0x000000), 0x1_000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:25:49 +00:00
|
|
|
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;
|
|
|
|
|
2020-09-24 17:59:20 +00:00
|
|
|
pub struct RTC<T> {
|
|
|
|
rtc: T,
|
|
|
|
|
|
|
|
/// Number of 2^23 periods elapsed since boot.
|
|
|
|
///
|
|
|
|
/// This is incremented by 1
|
|
|
|
/// - on overflow (counter value 0)
|
|
|
|
/// - on "midway" between overflows (at counter value 0x800000)
|
|
|
|
///
|
|
|
|
/// Therefore: When even, counter is in 0..0x7fffff. When odd, counter is in 0x800000..0xFFFFFF
|
|
|
|
/// This allows for now() to return the correct value even if it races an overflow.
|
|
|
|
///
|
|
|
|
/// It overflows on 2^32 * 2^23 / 32768 seconds of uptime, which is 34865 years.
|
|
|
|
period: AtomicU32,
|
|
|
|
|
|
|
|
/// Timestamp at which to fire alarm. u64::MAX if no alarm is scheduled.
|
2020-09-25 21:25:49 +00:00
|
|
|
alarms: Mutex<[AlarmState; ALARM_COUNT]>,
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 20:41:52 +00:00
|
|
|
unsafe impl<T> Send for RTC<T> {}
|
|
|
|
unsafe impl<T> Sync for RTC<T> {}
|
|
|
|
|
2020-09-24 17:59:20 +00:00
|
|
|
impl<T: Instance> RTC<T> {
|
|
|
|
pub fn new(rtc: T) -> Self {
|
|
|
|
Self {
|
|
|
|
rtc,
|
|
|
|
period: AtomicU32::new(0),
|
2020-09-25 21:25:49 +00:00
|
|
|
alarms: Mutex::new([AlarmState::new(), AlarmState::new(), AlarmState::new()]),
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn start(&'static self) {
|
2020-09-25 22:35:25 +00:00
|
|
|
self.rtc.cc[3].write(|w| unsafe { w.bits(0x800000) });
|
2020-09-24 17:59:20 +00:00
|
|
|
|
|
|
|
self.rtc.intenset.write(|w| {
|
|
|
|
let w = w.ovrflw().set();
|
2020-09-25 22:35:25 +00:00
|
|
|
let w = w.compare3().set();
|
2020-09-24 17:59:20 +00:00
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
self.rtc.tasks_clear.write(|w| w.tasks_clear().set_bit());
|
|
|
|
self.rtc.tasks_start.write(|w| w.tasks_start().set_bit());
|
|
|
|
|
|
|
|
// Wait for clear
|
|
|
|
while self.rtc.counter.read().bits() != 0 {}
|
|
|
|
|
|
|
|
T::set_rtc_instance(self);
|
|
|
|
interrupt::enable(T::INTERRUPT);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_interrupt(&self) {
|
|
|
|
if self.rtc.events_ovrflw.read().bits() == 1 {
|
|
|
|
self.rtc.events_ovrflw.write(|w| w);
|
|
|
|
self.next_period();
|
|
|
|
}
|
|
|
|
|
2020-09-25 22:35:25 +00:00
|
|
|
if self.rtc.events_compare[3].read().bits() == 1 {
|
|
|
|
self.rtc.events_compare[3].write(|w| w);
|
2020-09-24 17:59:20 +00:00
|
|
|
self.next_period();
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:25:49 +00:00
|
|
|
for n in 0..ALARM_COUNT {
|
2020-09-25 22:35:25 +00:00
|
|
|
if self.rtc.events_compare[n].read().bits() == 1 {
|
|
|
|
self.rtc.events_compare[n].write(|w| w);
|
2020-09-25 21:25:49 +00:00
|
|
|
interrupt::free(|cs| {
|
|
|
|
self.trigger_alarm(n, cs);
|
|
|
|
})
|
|
|
|
}
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_period(&self) {
|
|
|
|
interrupt::free(|cs| {
|
|
|
|
let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
|
|
|
|
let t = (period as u64) << 23;
|
|
|
|
|
2020-09-25 22:35:25 +00:00
|
|
|
for n in 0..ALARM_COUNT {
|
|
|
|
let alarm = &self.alarms.borrow(cs)[n];
|
2020-09-25 21:25:49 +00:00
|
|
|
let at = alarm.timestamp.get();
|
2020-09-24 17:59:20 +00:00
|
|
|
|
2020-09-25 21:25:49 +00:00
|
|
|
let diff = at - t;
|
|
|
|
if diff < 0xc00000 {
|
2020-09-25 22:35:25 +00:00
|
|
|
self.rtc.cc[n].write(|w| unsafe { w.bits(at as u32 & 0xFFFFFF) });
|
|
|
|
self.rtc.intenset.write(|w| unsafe { w.bits(compare_n(n)) });
|
2020-09-25 21:25:49 +00:00
|
|
|
}
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:25:49 +00:00
|
|
|
fn trigger_alarm(&self, n: usize, cs: &CriticalSection) {
|
2020-09-25 22:35:25 +00:00
|
|
|
self.rtc.intenclr.write(|w| unsafe { w.bits(compare_n(n)) });
|
2020-09-24 17:59:20 +00:00
|
|
|
|
2020-09-25 21:25:49 +00:00
|
|
|
let alarm = &self.alarms.borrow(cs)[n];
|
|
|
|
alarm.timestamp.set(u64::MAX);
|
|
|
|
|
|
|
|
// Call after clearing alarm, so the callback can set another alarm.
|
|
|
|
alarm.callback.get().map(|f| f());
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 21:25:49 +00:00
|
|
|
fn set_alarm_callback(&self, n: usize, callback: fn()) {
|
2020-09-24 17:59:20 +00:00
|
|
|
interrupt::free(|cs| {
|
2020-09-25 21:25:49 +00:00
|
|
|
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);
|
2020-09-24 20:41:52 +00:00
|
|
|
|
2020-09-24 17:59:20 +00:00
|
|
|
let t = self.now();
|
2020-09-24 21:26:24 +00:00
|
|
|
if timestamp <= t {
|
2020-09-25 21:25:49 +00:00
|
|
|
self.trigger_alarm(n, cs);
|
2020-09-24 17:59:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:26:24 +00:00
|
|
|
let diff = timestamp - t;
|
2020-09-24 17:59:20 +00:00
|
|
|
if diff < 0xc00000 {
|
2020-09-25 22:35:25 +00:00
|
|
|
self.rtc.cc[n].write(|w| unsafe { w.bits(timestamp as u32 & 0xFFFFFF) });
|
|
|
|
self.rtc.intenset.write(|w| unsafe { w.bits(compare_n(n)) });
|
2020-09-24 17:59:20 +00:00
|
|
|
|
|
|
|
// We may have been preempted for arbitrary time between checking if `at` is in the past
|
|
|
|
// and setting the cc. In that case, we don't know if the cc has triggered.
|
|
|
|
// So, we check again just in case.
|
|
|
|
|
|
|
|
let t = self.now();
|
2020-09-24 21:26:24 +00:00
|
|
|
if timestamp <= t {
|
2020-09-25 21:25:49 +00:00
|
|
|
self.trigger_alarm(n, cs);
|
2020-09-24 17:59:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-25 22:35:25 +00:00
|
|
|
self.rtc.intenclr.write(|w| unsafe { w.bits(compare_n(n)) });
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-09-24 21:26:24 +00:00
|
|
|
|
2020-09-25 01:25:06 +00:00
|
|
|
pub fn alarm0(&'static self) -> Alarm<T> {
|
2020-09-25 21:25:49 +00:00
|
|
|
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 }
|
2020-09-24 21:26:24 +00:00
|
|
|
}
|
2020-09-25 01:25:06 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 21:38:42 +00:00
|
|
|
impl<T: Instance> embassy::time::Clock 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 01:25:06 +00:00
|
|
|
pub struct Alarm<T: Instance> {
|
2020-09-25 21:25:49 +00:00
|
|
|
n: usize,
|
2020-09-25 01:25:06 +00:00
|
|
|
rtc: &'static RTC<T>,
|
|
|
|
}
|
2020-09-24 17:59:20 +00:00
|
|
|
|
2020-09-25 01:25:06 +00:00
|
|
|
impl<T: Instance> embassy::time::Alarm for Alarm<T> {
|
2020-09-25 21:25:49 +00:00
|
|
|
fn set_callback(&self, callback: fn()) {
|
|
|
|
self.rtc.set_alarm_callback(self.n, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set(&self, timestamp: u64) {
|
|
|
|
self.rtc.set_alarm(self.n, timestamp);
|
2020-09-24 20:41:52 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 01:25:06 +00:00
|
|
|
fn clear(&self) {
|
2020-09-25 21:25:49 +00:00
|
|
|
self.rtc.set_alarm(self.n, u64::MAX);
|
2020-09-24 17:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implemented by all RTC instances.
|
2020-09-25 01:25:06 +00:00
|
|
|
pub trait Instance: Deref<Target = rtc0::RegisterBlock> + Sized + 'static {
|
2020-09-24 17:59:20 +00:00
|
|
|
/// The interrupt associated with this RTC instance.
|
|
|
|
const INTERRUPT: Interrupt;
|
|
|
|
|
|
|
|
fn set_rtc_instance(rtc: &'static RTC<Self>);
|
|
|
|
fn get_rtc_instance() -> &'static RTC<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_instance {
|
|
|
|
($name:ident, $static_name:ident) => {
|
|
|
|
static mut $static_name: Option<&'static RTC<$name>> = None;
|
|
|
|
|
|
|
|
impl Instance for $name {
|
|
|
|
const INTERRUPT: Interrupt = Interrupt::$name;
|
|
|
|
fn set_rtc_instance(rtc: &'static RTC<Self>) {
|
|
|
|
unsafe { $static_name = Some(rtc) }
|
|
|
|
}
|
|
|
|
fn get_rtc_instance() -> &'static RTC<Self> {
|
|
|
|
unsafe { $static_name.unwrap() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[interrupt]
|
|
|
|
fn $name() {
|
|
|
|
$name::get_rtc_instance().on_interrupt();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_instance!(RTC0, RTC0_INSTANCE);
|
|
|
|
impl_instance!(RTC1, RTC1_INSTANCE);
|
|
|
|
|
|
|
|
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
|
|
|
|
impl_instance!(RTC2, RTC2_INSTANCE);
|