stm32: complete stop impl.

This commit is contained in:
xoviat
2023-08-26 20:31:12 -05:00
parent 2897670f24
commit 1e430f7413
4 changed files with 107 additions and 164 deletions

View File

@ -1,6 +1,14 @@
//! RTC peripheral abstraction
mod datetime;
#[cfg(feature = "low-power")]
use core::cell::Cell;
#[cfg(feature = "low-power")]
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
#[cfg(feature = "low-power")]
use embassy_sync::blocking_mutex::Mutex;
pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
/// refer to AN4759 to compare features of RTC2 and RTC3
@ -30,9 +38,73 @@ pub enum RtcError {
NotRunning,
}
#[cfg(feature = "low-power")]
/// Represents an instant in time that can be substracted to compute a duration
struct RtcInstant {
second: u8,
subsecond: u16,
}
#[cfg(feature = "low-power")]
impl RtcInstant {
pub fn now() -> Self {
let tr = RTC::regs().tr().read();
let tr2 = RTC::regs().tr().read();
let ssr = RTC::regs().ssr().read().ss();
let ssr2 = RTC::regs().ssr().read().ss();
let st = bcd2_to_byte((tr.st(), tr.su()));
let st2 = bcd2_to_byte((tr2.st(), tr2.su()));
assert!(st == st2);
assert!(ssr == ssr2);
let _ = RTC::regs().dr().read();
let subsecond = ssr;
let second = st;
// trace!("rtc: instant now: st, ssr: {}, {}", st, ssr);
Self { second, subsecond }
}
}
#[cfg(feature = "low-power")]
impl core::ops::Sub for RtcInstant {
type Output = embassy_time::Duration;
fn sub(self, rhs: Self) -> Self::Output {
use embassy_time::{Duration, TICK_HZ};
let second = if self.second < rhs.second {
self.second + 60
} else {
self.second
};
// TODO: read prescaler
let self_ticks = second as u32 * 256 + (255 - self.subsecond as u32);
let other_ticks = rhs.second as u32 * 256 + (255 - rhs.subsecond as u32);
let rtc_ticks = self_ticks - other_ticks;
// trace!(
// "rtc: instant sub: self, other, rtc ticks: {}, {}, {}",
// self_ticks,
// other_ticks,
// rtc_ticks
// );
Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / 256u32) as u64)
}
}
/// RTC Abstraction
pub struct Rtc {
rtc_config: RtcConfig,
#[cfg(feature = "low-power")]
stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<RtcInstant>>>,
}
#[derive(Copy, Clone, Debug, PartialEq)]
@ -108,8 +180,15 @@ impl Rtc {
pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self {
RTC::enable_peripheral_clk();
#[cfg(not(feature = "low-power"))]
let mut rtc_struct = Self { rtc_config };
#[cfg(feature = "low-power")]
let mut rtc_struct = Self {
rtc_config,
stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)),
};
Self::enable();
rtc_struct.configure(rtc_config);

View File

@ -1,71 +1,12 @@
use stm32_metapac::rtc::vals::{Init, Osel, Pol};
#[cfg(feature = "low-power")]
use super::RtcInstant;
use super::{sealed, RtcClockSource, RtcConfig};
use crate::pac::rtc::Rtc;
use crate::peripherals::RTC;
use crate::rtc::sealed::Instance;
#[cfg(all(feature = "time", any(stm32wb, stm32f4)))]
pub struct RtcInstant {
ssr: u16,
st: u8,
}
#[cfg(all(feature = "time", any(stm32wb, stm32f4)))]
impl RtcInstant {
pub fn now() -> Self {
// TODO: read value twice
use crate::rtc::bcd2_to_byte;
let tr = RTC::regs().tr().read();
let tr2 = RTC::regs().tr().read();
let ssr = RTC::regs().ssr().read().ss();
let ssr2 = RTC::regs().ssr().read().ss();
let st = bcd2_to_byte((tr.st(), tr.su()));
let st2 = bcd2_to_byte((tr2.st(), tr2.su()));
assert!(st == st2);
assert!(ssr == ssr2);
let _ = RTC::regs().dr().read();
trace!("rtc: instant now: st, ssr: {}, {}", st, ssr);
Self { ssr, st }
}
}
#[cfg(all(feature = "time", any(stm32wb, stm32f4)))]
impl core::ops::Sub for RtcInstant {
type Output = embassy_time::Duration;
fn sub(self, rhs: Self) -> Self::Output {
use embassy_time::{Duration, TICK_HZ};
let st = if self.st < rhs.st { self.st + 60 } else { self.st };
// TODO: read prescaler
let self_ticks = st as u32 * 256 + (255 - self.ssr as u32);
let other_ticks = rhs.st as u32 * 256 + (255 - rhs.ssr as u32);
let rtc_ticks = self_ticks - other_ticks;
trace!(
"rtc: instant sub: self, other, rtc ticks: {}, {}, {}",
self_ticks,
other_ticks,
rtc_ticks
);
Duration::from_ticks(
((((st as u32 * 256 + (255u32 - self.ssr as u32)) - (rhs.st as u32 * 256 + (255u32 - rhs.ssr as u32)))
* TICK_HZ as u32) as u32
/ 256u32) as u64,
)
}
}
#[allow(dead_code)]
#[derive(Clone, Copy, Debug)]
pub(crate) enum WakeupPrescaler {
@ -165,16 +106,11 @@ impl super::Rtc {
#[allow(dead_code)]
#[cfg(all(feature = "time", any(stm32wb, stm32f4)))]
/// start the wakeup alarm and return the actual duration of the alarm
/// the actual duration will be the closest value possible that is less
/// than the requested duration.
///
/// note: this api is exposed for testing purposes until low power is implemented.
/// it is not intended to be public
pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) -> RtcInstant {
/// start the wakeup alarm and wtih a duration that is as close to but less than
/// the requested duration, and record the instant the wakeup alarm was started
pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) {
use embassy_time::{Duration, TICK_HZ};
use crate::interrupt::typelevel::Interrupt;
use crate::rcc::get_freqs;
let rtc_hz = unsafe { get_freqs() }.rtc.unwrap().0 as u64;
@ -206,47 +142,34 @@ impl super::Rtc {
trace!("rtc: start wakeup alarm for {} ms", duration.as_millis());
// self.write(false, |regs| {
// regs.cr().modify(|w| w.set_wutie(false));
//
// regs.isr().modify(|w| w.set_wutf(false));
//
// regs.cr().modify(|w| w.set_wutie(true));
// });
//
// trace!("rtc: clear wuf...");
// crate::pac::PWR.cr1().modify(|w| w.set_cwuf(true));
// while crate::pac::PWR.csr1().read().wuf() {}
// trace!("rtc: clear wuf...done");
//
// crate::pac::PWR
// .cr1()
// .modify(|w| w.set_pdds(crate::pac::pwr::vals::Pdds::STANDBY_MODE));
//
// // crate::pac::PWR.cr1().modify(|w| w.set_lpds(true));
//
// // crate::pac::EXTI.pr(0).modify(|w| w.set_line(22, true));
// crate::interrupt::typelevel::RTC_WKUP::unpend();
RtcInstant::now()
critical_section::with(|cs| assert!(self.stop_time.borrow(cs).replace(Some(RtcInstant::now())).is_none()))
}
#[allow(dead_code)]
#[cfg(all(feature = "time", any(stm32wb, stm32f4)))]
/// stop the wakeup alarm and return the time remaining
///
/// note: this api is exposed for testing purposes until low power is implemented.
/// it is not intended to be public
pub(crate) fn stop_wakeup_alarm(&self) -> RtcInstant {
/// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm`
/// was called, otherwise none
pub(crate) fn stop_wakeup_alarm(&self) -> Option<embassy_time::Duration> {
use crate::interrupt::typelevel::Interrupt;
trace!("rtc: stop wakeup alarm...");
self.write(false, |regs| {
regs.cr().modify(|w| w.set_wutie(false));
regs.cr().modify(|w| w.set_wute(false));
regs.isr().modify(|w| w.set_wutf(false));
crate::pac::EXTI.pr(0).modify(|w| w.set_line(22, true));
crate::interrupt::typelevel::RTC_WKUP::unpend();
});
RtcInstant::now()
critical_section::with(|cs| {
if let Some(stop_time) = self.stop_time.borrow(cs).take() {
Some(RtcInstant::now() - stop_time)
} else {
None
}
})
}
#[allow(dead_code)]