Merge pull request #2099 from xoviat/rtc

stm32/rtc: misc and low-power cleanup
This commit is contained in:
xoviat 2023-11-01 22:19:03 +00:00 committed by GitHub
commit 4ef3dc5b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 73 deletions

View File

@ -61,7 +61,7 @@ pub struct Executor {
impl Executor { impl Executor {
/// Create a new Executor. /// Create a new Executor.
pub fn take() -> &'static mut Self { pub fn take() -> &'static mut Self {
unsafe { critical_section::with(|_| unsafe {
assert!(EXECUTOR.is_none()); assert!(EXECUTOR.is_none());
EXECUTOR = Some(Self { EXECUTOR = Some(Self {
@ -72,7 +72,7 @@ impl Executor {
}); });
EXECUTOR.as_mut().unwrap() EXECUTOR.as_mut().unwrap()
} })
} }
unsafe fn on_wakeup_irq(&mut self) { unsafe fn on_wakeup_irq(&mut self) {

View File

@ -18,11 +18,15 @@ pub struct RtcInstant {
} }
impl RtcInstant { impl RtcInstant {
#[allow(dead_code)] #[cfg(not(rtc_v2f2))]
pub(super) fn from(second: u8, subsecond: u16) -> Result<Self, super::RtcError> { pub(super) const fn from(second: u8, subsecond: u16) -> Result<Self, Error> {
if second > 59 {
Err(Error::InvalidSecond)
} else {
Ok(Self { second, subsecond }) Ok(Self { second, subsecond })
} }
} }
}
#[cfg(feature = "defmt")] #[cfg(feature = "defmt")]
impl defmt::Format for RtcInstant { impl defmt::Format for RtcInstant {
@ -226,7 +230,7 @@ impl From<DayOfWeek> for chrono::Weekday {
} }
} }
fn day_of_week_from_u8(v: u8) -> Result<DayOfWeek, Error> { pub(super) const fn day_of_week_from_u8(v: u8) -> Result<DayOfWeek, Error> {
Ok(match v { Ok(match v {
1 => DayOfWeek::Monday, 1 => DayOfWeek::Monday,
2 => DayOfWeek::Tuesday, 2 => DayOfWeek::Tuesday,
@ -239,24 +243,6 @@ fn day_of_week_from_u8(v: u8) -> Result<DayOfWeek, Error> {
}) })
} }
pub(super) fn day_of_week_to_u8(dotw: DayOfWeek) -> u8 { pub(super) const fn day_of_week_to_u8(dotw: DayOfWeek) -> u8 {
dotw as u8 dotw as u8
} }
pub(super) fn validate_datetime(dt: &DateTime) -> Result<(), Error> {
if dt.year > 4095 {
Err(Error::InvalidYear)
} else if dt.month < 1 || dt.month > 12 {
Err(Error::InvalidMonth)
} else if dt.day < 1 || dt.day > 31 {
Err(Error::InvalidDay)
} else if dt.hour > 23 {
Err(Error::InvalidHour)
} else if dt.minute > 59 {
Err(Error::InvalidMinute)
} else if dt.second > 59 {
Err(Error::InvalidSecond)
} else {
Ok(())
}
}

View File

@ -9,8 +9,11 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
#[cfg(feature = "low-power")] #[cfg(feature = "low-power")]
use embassy_sync::blocking_mutex::Mutex; use embassy_sync::blocking_mutex::Mutex;
pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError, RtcInstant}; use self::datetime::day_of_week_to_u8;
use crate::rtc::datetime::day_of_week_to_u8; #[cfg(not(rtc_v2f2))]
use self::datetime::RtcInstant;
pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
use crate::pac::rtc::regs::{Dr, Tr};
use crate::time::Hertz; use crate::time::Hertz;
/// refer to AN4759 to compare features of RTC2 and RTC3 /// refer to AN4759 to compare features of RTC2 and RTC3
@ -31,11 +34,15 @@ use crate::peripherals::RTC;
use crate::rtc::sealed::Instance; use crate::rtc::sealed::Instance;
/// Errors that can occur on methods on [RtcClock] /// Errors that can occur on methods on [RtcClock]
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum RtcError { pub enum RtcError {
/// An invalid DateTime was given or stored on the hardware. /// An invalid DateTime was given or stored on the hardware.
InvalidDateTime(DateTimeError), InvalidDateTime(DateTimeError),
/// The current time could not be read
ReadFailure,
/// The RTC clock is not running /// The RTC clock is not running
NotRunning, NotRunning,
} }
@ -45,24 +52,22 @@ pub struct RtcTimeProvider {
} }
impl RtcTimeProvider { impl RtcTimeProvider {
#[cfg(not(rtc_v2f2))]
pub(crate) fn instant(&self) -> Result<RtcInstant, RtcError> {
self.read(|_, tr, ss| {
let second = bcd2_to_byte((tr.st(), tr.su()));
RtcInstant::from(second, ss).map_err(RtcError::InvalidDateTime)
})
}
/// Return the current datetime. /// Return the current datetime.
/// ///
/// # Errors /// # Errors
/// ///
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`]. /// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
pub fn now(&self) -> Result<DateTime, RtcError> { pub fn now(&self) -> Result<DateTime, RtcError> {
// For RM0433 we use BYPSHAD=1 to work around errata ES0392 2.19.1 self.read(|dr, tr, _| {
#[cfg(rcc_h7rm0433)]
loop {
let r = RTC::regs();
let ss = r.ssr().read().ss();
let dr = r.dr().read();
let tr = r.tr().read();
// If an RTCCLK edge occurs during read we may see inconsistent values
// so read ssr again and see if it has changed. (see RM0433 Rev 7 46.3.9)
let ss_after = r.ssr().read().ss();
if ss == ss_after {
let second = bcd2_to_byte((tr.st(), tr.su())); let second = bcd2_to_byte((tr.st(), tr.su()));
let minute = bcd2_to_byte((tr.mnt(), tr.mnu())); let minute = bcd2_to_byte((tr.mnt(), tr.mnu()));
let hour = bcd2_to_byte((tr.ht(), tr.hu())); let hour = bcd2_to_byte((tr.ht(), tr.hu()));
@ -72,29 +77,34 @@ impl RtcTimeProvider {
let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); let month = bcd2_to_byte((dr.mt() as u8, dr.mu()));
let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16;
return DateTime::from(year, month, day, weekday, hour, minute, second)
.map_err(RtcError::InvalidDateTime);
}
}
#[cfg(not(rcc_h7rm0433))]
{
let r = RTC::regs();
let tr = r.tr().read();
let second = bcd2_to_byte((tr.st(), tr.su()));
let minute = bcd2_to_byte((tr.mnt(), tr.mnu()));
let hour = bcd2_to_byte((tr.ht(), tr.hu()));
// Reading either RTC_SSR or RTC_TR locks the values in the higher-order
// calendar shadow registers until RTC_DR is read.
let dr = r.dr().read();
let weekday = dr.wdu();
let day = bcd2_to_byte((dr.dt(), dr.du()));
let month = bcd2_to_byte((dr.mt() as u8, dr.mu()));
let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16;
DateTime::from(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime) DateTime::from(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime)
})
} }
fn read<R>(&self, mut f: impl FnMut(Dr, Tr, u16) -> Result<R, RtcError>) -> Result<R, RtcError> {
let r = RTC::regs();
#[cfg(not(rtc_v2f2))]
let read_ss = || r.ssr().read().ss();
#[cfg(rtc_v2f2)]
let read_ss = || 0;
let mut ss = read_ss();
for _ in 0..5 {
let tr = r.tr().read();
let dr = r.dr().read();
let ss_after = read_ss();
// If an RTCCLK edge occurs during read we may see inconsistent values
// so read ssr again and see if it has changed. (see RM0433 Rev 7 46.3.9)
if ss == ss_after {
return f(dr, tr, ss.try_into().unwrap());
} else {
ss = ss_after
}
}
return Err(RtcError::ReadFailure);
} }
} }
@ -145,6 +155,7 @@ impl Rtc {
#[cfg(not(any(stm32l0, stm32f3, stm32l1, stm32f0, stm32f2)))] #[cfg(not(any(stm32l0, stm32f3, stm32l1, stm32f0, stm32f2)))]
critical_section::with(|cs| { critical_section::with(|cs| {
<RTC as crate::rcc::sealed::RccPeripheral>::enable_and_reset_with_cs(cs); <RTC as crate::rcc::sealed::RccPeripheral>::enable_and_reset_with_cs(cs);
#[cfg(feature = "low-power")] #[cfg(feature = "low-power")]
unsafe { unsafe {
crate::rcc::REFCOUNT_STOP2 -= 1 crate::rcc::REFCOUNT_STOP2 -= 1
@ -164,6 +175,14 @@ impl Rtc {
this.configure(async_psc, sync_psc); this.configure(async_psc, sync_psc);
// Wait for the clock to update after initialization
#[cfg(not(rtc_v2f2))]
{
let now = this.instant().unwrap();
while this.instant().unwrap().subsecond == now.subsecond {}
}
this this
} }
@ -183,7 +202,6 @@ impl Rtc {
/// ///
/// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range.
pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> { pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> {
self::datetime::validate_datetime(&t).map_err(RtcError::InvalidDateTime)?;
self.write(true, |rtc| { self.write(true, |rtc| {
let (ht, hu) = byte_to_bcd2(t.hour() as u8); let (ht, hu) = byte_to_bcd2(t.hour() as u8);
let (mnt, mnu) = byte_to_bcd2(t.minute() as u8); let (mnt, mnu) = byte_to_bcd2(t.minute() as u8);
@ -223,16 +241,8 @@ impl Rtc {
#[cfg(not(rtc_v2f2))] #[cfg(not(rtc_v2f2))]
/// Return the current instant. /// Return the current instant.
pub fn instant(&self) -> Result<RtcInstant, RtcError> { fn instant(&self) -> Result<RtcInstant, RtcError> {
let r = RTC::regs(); self.time_provider().instant()
let tr = r.tr().read();
let subsecond = r.ssr().read().ss();
let second = bcd2_to_byte((tr.st(), tr.su()));
// Unlock the registers
r.dr().read();
RtcInstant::from(second, subsecond.try_into().unwrap())
} }
/// Return the current datetime. /// Return the current datetime.

View File

@ -150,14 +150,14 @@ impl super::Rtc {
pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) { pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) {
self.write(true, |rtc| { self.write(true, |rtc| {
rtc.cr().modify(|w| { rtc.cr().modify(|w| {
#[cfg(not(rtc_v2f2))]
w.set_bypshad(true);
#[cfg(rtc_v2f2)] #[cfg(rtc_v2f2)]
w.set_fmt(false); w.set_fmt(false);
#[cfg(not(rtc_v2f2))] #[cfg(not(rtc_v2f2))]
w.set_fmt(stm32_metapac::rtc::vals::Fmt::TWENTY_FOUR_HOUR); w.set_fmt(stm32_metapac::rtc::vals::Fmt::TWENTY_FOUR_HOUR);
w.set_osel(Osel::DISABLED); w.set_osel(Osel::DISABLED);
w.set_pol(Pol::HIGH); w.set_pol(Pol::HIGH);
#[cfg(rcc_h7rm0433)]
w.set_bypshad(true);
}); });
rtc.prer().modify(|w| { rtc.prer().modify(|w| {

View File

@ -11,6 +11,7 @@ impl super::Rtc {
pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) { pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) {
self.write(true, |rtc| { self.write(true, |rtc| {
rtc.cr().modify(|w| { rtc.cr().modify(|w| {
w.set_bypshad(true);
w.set_fmt(Fmt::TWENTYFOURHOUR); w.set_fmt(Fmt::TWENTYFOURHOUR);
w.set_osel(Osel::DISABLED); w.set_osel(Osel::DISABLED);
w.set_pol(Pol::HIGH); w.set_pol(Pol::HIGH);