2021-08-03 22:08:13 +02:00
|
|
|
use core::cell::Cell;
|
|
|
|
use core::convert::TryInto;
|
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
use core::{mem, ptr};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
|
|
|
use atomic_polyfill::{AtomicU32, AtomicU8};
|
2023-05-25 00:29:56 +02:00
|
|
|
use critical_section::CriticalSection;
|
2022-08-22 21:46:09 +02:00
|
|
|
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
|
|
|
use embassy_sync::blocking_mutex::Mutex;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_time::driver::{AlarmHandle, Driver};
|
2022-09-02 00:58:31 +02:00
|
|
|
use embassy_time::TICK_HZ;
|
2021-08-03 22:08:13 +02:00
|
|
|
use stm32_metapac::timer::regs;
|
|
|
|
|
2023-06-08 16:08:40 +02:00
|
|
|
use crate::interrupt::typelevel::Interrupt;
|
2022-02-24 05:57:52 +01:00
|
|
|
use crate::pac::timer::vals;
|
2021-08-03 22:08:13 +02:00
|
|
|
use crate::rcc::sealed::RccPeripheral;
|
2022-06-12 22:15:44 +02:00
|
|
|
use crate::timer::sealed::{Basic16bitInstance as BasicInstance, GeneralPurpose16bitInstance as Instance};
|
|
|
|
use crate::{interrupt, peripherals};
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2022-02-23 11:52:02 +01:00
|
|
|
#[cfg(not(any(time_driver_tim12, time_driver_tim15)))]
|
2021-08-03 22:08:13 +02:00
|
|
|
const ALARM_COUNT: usize = 3;
|
2021-08-04 12:05:22 +02:00
|
|
|
|
2022-02-23 11:52:02 +01:00
|
|
|
#[cfg(any(time_driver_tim12, time_driver_tim15))]
|
|
|
|
const ALARM_COUNT: usize = 1;
|
|
|
|
|
2022-01-24 00:24:23 +01:00
|
|
|
#[cfg(time_driver_tim2)]
|
2021-08-04 12:05:22 +02:00
|
|
|
type T = peripherals::TIM2;
|
2022-01-24 00:24:23 +01:00
|
|
|
#[cfg(time_driver_tim3)]
|
2021-08-03 22:08:13 +02:00
|
|
|
type T = peripherals::TIM3;
|
2022-01-24 00:50:10 +01:00
|
|
|
#[cfg(time_driver_tim4)]
|
|
|
|
type T = peripherals::TIM4;
|
|
|
|
#[cfg(time_driver_tim5)]
|
|
|
|
type T = peripherals::TIM5;
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2022-02-23 11:52:02 +01:00
|
|
|
#[cfg(time_driver_tim12)]
|
|
|
|
type T = peripherals::TIM12;
|
|
|
|
#[cfg(time_driver_tim15)]
|
|
|
|
type T = peripherals::TIM15;
|
|
|
|
|
2022-02-26 01:40:43 +01:00
|
|
|
foreach_interrupt! {
|
2022-02-24 05:57:52 +01:00
|
|
|
(TIM2, timer, $block:ident, UP, $irq:ident) => {
|
|
|
|
#[cfg(time_driver_tim2)]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-24 05:57:52 +01:00
|
|
|
#[interrupt]
|
|
|
|
fn $irq() {
|
|
|
|
DRIVER.on_interrupt()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(TIM3, timer, $block:ident, UP, $irq:ident) => {
|
|
|
|
#[cfg(time_driver_tim3)]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-24 05:57:52 +01:00
|
|
|
#[interrupt]
|
|
|
|
fn $irq() {
|
|
|
|
DRIVER.on_interrupt()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(TIM4, timer, $block:ident, UP, $irq:ident) => {
|
|
|
|
#[cfg(time_driver_tim4)]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-24 05:57:52 +01:00
|
|
|
#[interrupt]
|
|
|
|
fn $irq() {
|
|
|
|
DRIVER.on_interrupt()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(TIM5, timer, $block:ident, UP, $irq:ident) => {
|
|
|
|
#[cfg(time_driver_tim5)]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-24 05:57:52 +01:00
|
|
|
#[interrupt]
|
|
|
|
fn $irq() {
|
|
|
|
DRIVER.on_interrupt()
|
|
|
|
}
|
|
|
|
};
|
2022-02-23 11:52:02 +01:00
|
|
|
(TIM12, timer, $block:ident, UP, $irq:ident) => {
|
|
|
|
#[cfg(time_driver_tim12)]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-23 11:52:02 +01:00
|
|
|
#[interrupt]
|
|
|
|
fn $irq() {
|
|
|
|
DRIVER.on_interrupt()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
(TIM15, timer, $block:ident, UP, $irq:ident) => {
|
|
|
|
#[cfg(time_driver_tim15)]
|
2023-06-08 18:00:19 +02:00
|
|
|
#[cfg(feature = "rt")]
|
2022-02-23 11:52:02 +01:00
|
|
|
#[interrupt]
|
|
|
|
fn $irq() {
|
|
|
|
DRIVER.on_interrupt()
|
|
|
|
}
|
|
|
|
};
|
2022-01-24 00:50:10 +01:00
|
|
|
}
|
2021-08-04 12:05:22 +02:00
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
// Clock timekeeping works with something we call "periods", which are time intervals
|
|
|
|
// of 2^15 ticks. The Clock counter value is 16 bits, so one "overflow cycle" is 2 periods.
|
|
|
|
//
|
|
|
|
// A `period` count is maintained in parallel to the Timer hardware `counter`, like this:
|
|
|
|
// - `period` and `counter` start at 0
|
|
|
|
// - `period` is incremented on overflow (at counter value 0)
|
|
|
|
// - `period` is incremented "midway" between overflows (at counter value 0x8000)
|
|
|
|
//
|
|
|
|
// Therefore, when `period` is even, counter is in 0..0x7FFF. When odd, counter is in 0x8000..0xFFFF
|
|
|
|
// This allows for now() to return the correct value even if it races an overflow.
|
|
|
|
//
|
|
|
|
// To get `now()`, `period` is read first, then `counter` is read. If the counter value matches
|
|
|
|
// the expected range for the `period` parity, we're done. If it doesn't, this means that
|
|
|
|
// a new period start has raced us between reading `period` and `counter`, so we assume the `counter` value
|
|
|
|
// corresponds to the next period.
|
|
|
|
//
|
|
|
|
// `period` is a 32bit integer, so It overflows on 2^32 * 2^15 / 32768 seconds of uptime, which is 136 years.
|
|
|
|
fn calc_now(period: u32, counter: u16) -> u64 {
|
|
|
|
((period as u64) << 15) + ((counter as u32 ^ ((period & 1) << 15)) as u64)
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AlarmState {
|
|
|
|
timestamp: Cell<u64>,
|
|
|
|
|
|
|
|
// This is really a Option<(fn(*mut ()), *mut ())>
|
|
|
|
// but fn pointers aren't allowed in const yet
|
|
|
|
callback: Cell<*const ()>,
|
|
|
|
ctx: Cell<*mut ()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for AlarmState {}
|
|
|
|
|
|
|
|
impl AlarmState {
|
|
|
|
const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
timestamp: Cell::new(u64::MAX),
|
|
|
|
callback: Cell::new(ptr::null()),
|
|
|
|
ctx: Cell::new(ptr::null_mut()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 18:50:05 +02:00
|
|
|
struct RtcDriver {
|
2021-08-03 22:08:13 +02:00
|
|
|
/// Number of 2^15 periods elapsed since boot.
|
|
|
|
period: AtomicU32,
|
|
|
|
alarm_count: AtomicU8,
|
|
|
|
/// Timestamp at which to fire alarm. u64::MAX if no alarm is scheduled.
|
2022-02-11 23:25:30 +01:00
|
|
|
alarms: Mutex<CriticalSectionRawMutex, [AlarmState; ALARM_COUNT]>,
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const ALARM_STATE_NEW: AlarmState = AlarmState::new();
|
2021-08-25 18:50:05 +02:00
|
|
|
|
2022-08-17 23:40:16 +02:00
|
|
|
embassy_time::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver {
|
2021-08-03 22:08:13 +02:00
|
|
|
period: AtomicU32::new(0),
|
|
|
|
alarm_count: AtomicU8::new(0),
|
2022-02-11 23:25:30 +01:00
|
|
|
alarms: Mutex::const_new(CriticalSectionRawMutex::new(), [ALARM_STATE_NEW; ALARM_COUNT]),
|
2021-08-25 18:50:05 +02:00
|
|
|
});
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2021-08-25 18:50:05 +02:00
|
|
|
impl RtcDriver {
|
2021-08-03 22:08:13 +02:00
|
|
|
fn init(&'static self) {
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = T::regs_gp16();
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2022-02-24 05:57:52 +01:00
|
|
|
<T as RccPeripheral>::enable();
|
|
|
|
<T as RccPeripheral>::reset();
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
let timer_freq = T::frequency();
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
critical_section::with(|_| {
|
2021-08-03 22:08:13 +02:00
|
|
|
r.cr1().modify(|w| w.set_cen(false));
|
|
|
|
r.cnt().write(|w| w.set_cnt(0));
|
|
|
|
|
2022-09-02 00:58:31 +02:00
|
|
|
let psc = timer_freq.0 / TICK_HZ as u32 - 1;
|
2021-07-31 22:34:50 +02:00
|
|
|
let psc: u16 = match psc.try_into() {
|
|
|
|
Err(_) => panic!("psc division overflow: {}", psc),
|
|
|
|
Ok(n) => n,
|
|
|
|
};
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
r.psc().write(|w| w.set_psc(psc));
|
|
|
|
r.arr().write(|w| w.set_arr(u16::MAX));
|
|
|
|
|
|
|
|
// Set URS, generate update and clear URS
|
|
|
|
r.cr1().modify(|w| w.set_urs(vals::Urs::COUNTERONLY));
|
|
|
|
r.egr().write(|w| w.set_ug(true));
|
|
|
|
r.cr1().modify(|w| w.set_urs(vals::Urs::ANYEVENT));
|
|
|
|
|
|
|
|
// Mid-way point
|
|
|
|
r.ccr(0).write(|w| w.set_ccr(0x8000));
|
|
|
|
|
2022-03-29 23:27:33 +02:00
|
|
|
// Enable overflow and half-overflow interrupts
|
|
|
|
r.dier().write(|w| {
|
|
|
|
w.set_uie(true);
|
|
|
|
w.set_ccie(0, true);
|
|
|
|
});
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2023-06-01 02:22:46 +02:00
|
|
|
<T as BasicInstance>::Interrupt::unpend();
|
2023-06-19 03:07:26 +02:00
|
|
|
unsafe { <T as BasicInstance>::Interrupt::enable() };
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
r.cr1().modify(|w| w.set_cen(true));
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_interrupt(&self) {
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = T::regs_gp16();
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
// XXX: reduce the size of this critical section ?
|
2023-06-19 03:07:26 +02:00
|
|
|
critical_section::with(|cs| {
|
2021-08-03 22:08:13 +02:00
|
|
|
let sr = r.sr().read();
|
|
|
|
let dier = r.dier().read();
|
|
|
|
|
|
|
|
// Clear all interrupt flags. Bits in SR are "write 0 to clear", so write the bitwise NOT.
|
|
|
|
// Other approaches such as writing all zeros, or RMWing won't work, they can
|
|
|
|
// miss interrupts.
|
|
|
|
r.sr().write_value(regs::SrGp(!sr.0));
|
|
|
|
|
2022-03-29 23:27:33 +02:00
|
|
|
// Overflow
|
2021-08-03 22:08:13 +02:00
|
|
|
if sr.uif() {
|
|
|
|
self.next_period();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Half overflow
|
|
|
|
if sr.ccif(0) {
|
|
|
|
self.next_period();
|
|
|
|
}
|
|
|
|
|
|
|
|
for n in 0..ALARM_COUNT {
|
|
|
|
if sr.ccif(n + 1) && dier.ccie(n + 1) {
|
|
|
|
self.trigger_alarm(n, cs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn next_period(&self) {
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = T::regs_gp16();
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
|
|
|
|
let t = (period as u64) << 15;
|
|
|
|
|
2023-06-19 03:07:26 +02:00
|
|
|
critical_section::with(move |cs| {
|
2021-08-03 22:08:13 +02:00
|
|
|
r.dier().modify(move |w| {
|
|
|
|
for n in 0..ALARM_COUNT {
|
|
|
|
let alarm = &self.alarms.borrow(cs)[n];
|
|
|
|
let at = alarm.timestamp.get();
|
|
|
|
|
|
|
|
if at < t + 0xc000 {
|
|
|
|
// just enable it. `set_alarm` has already set the correct CCR val.
|
|
|
|
w.set_ccie(n + 1, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_alarm<'a>(&'a self, cs: CriticalSection<'a>, alarm: AlarmHandle) -> &'a AlarmState {
|
|
|
|
// safety: we're allowed to assume the AlarmState is created by us, and
|
|
|
|
// we never create one that's out of bounds.
|
|
|
|
unsafe { self.alarms.borrow(cs).get_unchecked(alarm.id() as usize) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trigger_alarm(&self, n: usize, cs: CriticalSection) {
|
|
|
|
let alarm = &self.alarms.borrow(cs)[n];
|
|
|
|
alarm.timestamp.set(u64::MAX);
|
|
|
|
|
|
|
|
// Call after clearing alarm, so the callback can set another alarm.
|
|
|
|
|
|
|
|
// safety:
|
2023-05-08 23:25:01 +02:00
|
|
|
// - we can ignore the possibility of `f` being unset (null) because of the safety contract of `allocate_alarm`.
|
2021-08-03 22:08:13 +02:00
|
|
|
// - other than that we only store valid function pointers into alarm.callback
|
|
|
|
let f: fn(*mut ()) = unsafe { mem::transmute(alarm.callback.get()) };
|
|
|
|
f(alarm.ctx.get());
|
|
|
|
}
|
2021-08-25 18:50:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Driver for RtcDriver {
|
|
|
|
fn now(&self) -> u64 {
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = T::regs_gp16();
|
2021-08-25 18:50:05 +02:00
|
|
|
|
|
|
|
let period = self.period.load(Ordering::Relaxed);
|
|
|
|
compiler_fence(Ordering::Acquire);
|
2023-06-19 03:07:26 +02:00
|
|
|
let counter = r.cnt().read().cnt();
|
2021-08-25 18:50:05 +02:00
|
|
|
calc_now(period, counter)
|
|
|
|
}
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2021-08-25 18:50:05 +02:00
|
|
|
unsafe fn allocate_alarm(&self) -> Option<AlarmHandle> {
|
2022-06-12 22:15:44 +02:00
|
|
|
let id = self.alarm_count.fetch_update(Ordering::AcqRel, Ordering::Acquire, |x| {
|
|
|
|
if x < ALARM_COUNT as u8 {
|
|
|
|
Some(x + 1)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
match id {
|
2021-08-25 18:50:05 +02:00
|
|
|
Ok(id) => Some(AlarmHandle::new(id)),
|
2021-08-03 22:08:13 +02:00
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) {
|
|
|
|
critical_section::with(|cs| {
|
|
|
|
let alarm = self.get_alarm(cs, alarm);
|
|
|
|
|
2021-08-05 19:19:47 +02:00
|
|
|
alarm.callback.set(callback as *const ());
|
2021-08-03 22:08:13 +02:00
|
|
|
alarm.ctx.set(ctx);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-24 08:17:43 +02:00
|
|
|
fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool {
|
2021-08-03 22:08:13 +02:00
|
|
|
critical_section::with(|cs| {
|
2022-02-28 16:20:42 +01:00
|
|
|
let r = T::regs_gp16();
|
2021-08-03 22:08:13 +02:00
|
|
|
|
2022-10-24 10:31:54 +02:00
|
|
|
let n = alarm.id() as usize;
|
2021-08-03 22:08:13 +02:00
|
|
|
let alarm = self.get_alarm(cs, alarm);
|
|
|
|
alarm.timestamp.set(timestamp);
|
|
|
|
|
2022-10-24 10:10:59 +02:00
|
|
|
let t = self.now();
|
|
|
|
if timestamp <= t {
|
|
|
|
// If alarm timestamp has passed the alarm will not fire.
|
|
|
|
// Disarm the alarm and return `false` to indicate that.
|
2023-06-19 03:07:26 +02:00
|
|
|
r.dier().modify(|w| w.set_ccie(n + 1, false));
|
2022-10-24 10:10:59 +02:00
|
|
|
|
|
|
|
alarm.timestamp.set(u64::MAX);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-03 22:08:13 +02:00
|
|
|
let safe_timestamp = timestamp.max(t + 3);
|
|
|
|
|
|
|
|
// Write the CCR value regardless of whether we're going to enable it now or not.
|
|
|
|
// This way, when we enable it later, the right value is already set.
|
2023-06-19 03:07:26 +02:00
|
|
|
r.ccr(n + 1).write(|w| w.set_ccr(safe_timestamp as u16));
|
2021-08-03 22:08:13 +02:00
|
|
|
|
|
|
|
// Enable it if it'll happen soon. Otherwise, `next_period` will enable it.
|
|
|
|
let diff = timestamp - t;
|
2023-06-19 03:07:26 +02:00
|
|
|
r.dier().modify(|w| w.set_ccie(n + 1, diff < 0xc000));
|
2022-10-24 08:17:43 +02:00
|
|
|
|
|
|
|
true
|
2021-08-03 22:08:13 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn init() {
|
2021-08-25 18:50:05 +02:00
|
|
|
DRIVER.init()
|
2021-08-03 22:08:13 +02:00
|
|
|
}
|