2022-09-29 07:49:32 +02:00
|
|
|
//! RTC peripheral abstraction
|
|
|
|
mod datetime;
|
|
|
|
|
2023-08-27 03:31:12 +02:00
|
|
|
#[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;
|
|
|
|
|
2023-10-26 02:50:30 +02:00
|
|
|
pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError, RtcInstant};
|
|
|
|
use crate::rtc::datetime::day_of_week_to_u8;
|
2023-08-30 02:41:03 +02:00
|
|
|
use crate::time::Hertz;
|
2022-09-29 07:49:32 +02:00
|
|
|
|
|
|
|
/// refer to AN4759 to compare features of RTC2 and RTC3
|
|
|
|
#[cfg_attr(any(rtc_v1), path = "v1.rs")]
|
|
|
|
#[cfg_attr(
|
|
|
|
any(
|
|
|
|
rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb
|
|
|
|
),
|
2023-04-18 00:02:40 +02:00
|
|
|
path = "v2.rs"
|
2022-09-29 07:49:32 +02:00
|
|
|
)]
|
|
|
|
#[cfg_attr(any(rtc_v3, rtc_v3u5), path = "v3.rs")]
|
2023-04-19 03:35:43 +02:00
|
|
|
mod _version;
|
2023-08-11 02:14:55 +02:00
|
|
|
#[allow(unused_imports)]
|
2023-04-19 03:35:43 +02:00
|
|
|
pub use _version::*;
|
2023-07-28 13:23:22 +02:00
|
|
|
use embassy_hal_internal::Peripheral;
|
2022-09-29 07:49:32 +02:00
|
|
|
|
2023-08-09 02:47:01 +02:00
|
|
|
use crate::peripherals::RTC;
|
|
|
|
use crate::rtc::sealed::Instance;
|
|
|
|
|
2022-09-29 07:49:32 +02:00
|
|
|
/// Errors that can occur on methods on [RtcClock]
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum RtcError {
|
|
|
|
/// An invalid DateTime was given or stored on the hardware.
|
|
|
|
InvalidDateTime(DateTimeError),
|
|
|
|
|
|
|
|
/// The RTC clock is not running
|
|
|
|
NotRunning,
|
|
|
|
}
|
|
|
|
|
2023-09-18 01:47:22 +02:00
|
|
|
pub struct RtcTimeProvider {
|
|
|
|
_private: (),
|
|
|
|
}
|
2023-09-17 19:04:05 +02:00
|
|
|
|
|
|
|
impl RtcTimeProvider {
|
|
|
|
/// Return the current datetime.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
|
|
|
|
pub fn now(&self) -> Result<DateTime, RtcError> {
|
2023-10-03 23:45:05 +02:00
|
|
|
// For RM0433 we use BYPSHAD=1 to work around errata ES0392 2.19.1
|
|
|
|
#[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 minute = bcd2_to_byte((tr.mnt(), tr.mnu()));
|
|
|
|
let hour = bcd2_to_byte((tr.ht(), tr.hu()));
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-10-26 02:50:30 +02:00
|
|
|
return DateTime::from(year, month, day, weekday, hour, minute, second)
|
2023-10-03 23:45:05 +02:00
|
|
|
.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;
|
|
|
|
|
2023-10-26 02:50:30 +02:00
|
|
|
DateTime::from(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime)
|
2023-10-03 23:45:05 +02:00
|
|
|
}
|
2023-09-17 19:04:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 07:49:32 +02:00
|
|
|
/// RTC Abstraction
|
2023-08-09 02:47:01 +02:00
|
|
|
pub struct Rtc {
|
2023-08-27 03:31:12 +02:00
|
|
|
#[cfg(feature = "low-power")]
|
|
|
|
stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<RtcInstant>>>,
|
2023-09-25 23:26:29 +02:00
|
|
|
#[cfg(not(feature = "low-power"))]
|
|
|
|
_private: (),
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
|
2023-09-17 19:04:05 +02:00
|
|
|
#[non_exhaustive]
|
2022-09-29 07:49:32 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq)]
|
|
|
|
pub struct RtcConfig {
|
2023-08-30 02:41:03 +02:00
|
|
|
/// The subsecond counter frequency; default is 256
|
|
|
|
///
|
|
|
|
/// A high counter frequency may impact stop power consumption
|
|
|
|
pub frequency: Hertz,
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RtcConfig {
|
|
|
|
/// LSI with prescalers assuming 32.768 kHz.
|
|
|
|
/// Raw sub-seconds in 1/256.
|
|
|
|
fn default() -> Self {
|
2023-08-30 02:41:03 +02:00
|
|
|
RtcConfig { frequency: Hertz(256) }
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
#[repr(u8)]
|
|
|
|
pub enum RtcCalibrationCyclePeriod {
|
|
|
|
/// 8-second calibration period
|
|
|
|
Seconds8,
|
|
|
|
/// 16-second calibration period
|
|
|
|
Seconds16,
|
|
|
|
/// 32-second calibration period
|
|
|
|
Seconds32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RtcCalibrationCyclePeriod {
|
|
|
|
fn default() -> Self {
|
|
|
|
RtcCalibrationCyclePeriod::Seconds32
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-09 02:47:01 +02:00
|
|
|
impl Rtc {
|
|
|
|
pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self {
|
2023-10-11 03:53:27 +02:00
|
|
|
#[cfg(not(any(stm32l0, stm32f3, stm32l1, stm32f0, stm32f2)))]
|
2023-10-23 23:26:34 +02:00
|
|
|
critical_section::with(|cs| {
|
|
|
|
<RTC as crate::rcc::sealed::RccPeripheral>::enable_and_reset_with_cs(cs);
|
|
|
|
#[cfg(feature = "low-power")]
|
2023-10-26 02:07:31 +02:00
|
|
|
unsafe {
|
|
|
|
crate::rcc::REFCOUNT_STOP2 -= 1
|
|
|
|
};
|
2023-10-23 23:26:34 +02:00
|
|
|
});
|
2023-09-25 23:26:29 +02:00
|
|
|
|
2023-08-30 02:41:03 +02:00
|
|
|
let mut this = Self {
|
|
|
|
#[cfg(feature = "low-power")]
|
2023-08-27 03:31:12 +02:00
|
|
|
stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)),
|
2023-09-25 23:26:29 +02:00
|
|
|
#[cfg(not(feature = "low-power"))]
|
|
|
|
_private: (),
|
2023-08-27 03:31:12 +02:00
|
|
|
};
|
|
|
|
|
2023-09-16 01:41:33 +02:00
|
|
|
let frequency = Self::frequency();
|
|
|
|
let async_psc = ((frequency.0 / rtc_config.frequency.0) - 1) as u8;
|
|
|
|
let sync_psc = (rtc_config.frequency.0 - 1) as u16;
|
|
|
|
|
|
|
|
this.configure(async_psc, sync_psc);
|
|
|
|
|
|
|
|
this
|
|
|
|
}
|
|
|
|
|
|
|
|
fn frequency() -> Hertz {
|
|
|
|
let freqs = unsafe { crate::rcc::get_freqs() };
|
2023-10-11 03:53:27 +02:00
|
|
|
freqs.rtc.unwrap()
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
|
2023-09-17 19:04:05 +02:00
|
|
|
/// Acquire a [`RtcTimeProvider`] instance.
|
2023-09-18 01:47:22 +02:00
|
|
|
pub const fn time_provider(&self) -> RtcTimeProvider {
|
|
|
|
RtcTimeProvider { _private: () }
|
2023-09-17 19:04:05 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 07:49:32 +02:00
|
|
|
/// Set the datetime to a new value.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range.
|
|
|
|
pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> {
|
|
|
|
self::datetime::validate_datetime(&t).map_err(RtcError::InvalidDateTime)?;
|
2023-10-26 02:50:30 +02:00
|
|
|
self.write(true, |rtc| {
|
|
|
|
let (ht, hu) = byte_to_bcd2(t.hour() as u8);
|
|
|
|
let (mnt, mnu) = byte_to_bcd2(t.minute() as u8);
|
|
|
|
let (st, su) = byte_to_bcd2(t.second() as u8);
|
|
|
|
|
|
|
|
let (dt, du) = byte_to_bcd2(t.day() as u8);
|
|
|
|
let (mt, mu) = byte_to_bcd2(t.month() as u8);
|
|
|
|
let yr = t.year() as u16;
|
|
|
|
let yr_offset = (yr - 1970_u16) as u8;
|
|
|
|
let (yt, yu) = byte_to_bcd2(yr_offset);
|
|
|
|
|
|
|
|
use crate::pac::rtc::vals::Ampm;
|
|
|
|
|
|
|
|
rtc.tr().write(|w| {
|
|
|
|
w.set_ht(ht);
|
|
|
|
w.set_hu(hu);
|
|
|
|
w.set_mnt(mnt);
|
|
|
|
w.set_mnu(mnu);
|
|
|
|
w.set_st(st);
|
|
|
|
w.set_su(su);
|
|
|
|
w.set_pm(Ampm::AM);
|
|
|
|
});
|
|
|
|
|
|
|
|
rtc.dr().write(|w| {
|
|
|
|
w.set_dt(dt);
|
|
|
|
w.set_du(du);
|
|
|
|
w.set_mt(mt > 0);
|
|
|
|
w.set_mu(mu);
|
|
|
|
w.set_yt(yt);
|
|
|
|
w.set_yu(yu);
|
|
|
|
w.set_wdu(day_of_week_to_u8(t.day_of_week()));
|
|
|
|
});
|
|
|
|
});
|
2022-09-29 07:49:32 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-10-26 02:50:30 +02:00
|
|
|
#[cfg(not(rtc_v2f2))]
|
2023-08-27 23:06:33 +02:00
|
|
|
/// Return the current instant.
|
2023-10-26 02:50:30 +02:00
|
|
|
pub fn instant(&self) -> Result<RtcInstant, RtcError> {
|
2023-08-27 23:06:33 +02:00
|
|
|
let r = RTC::regs();
|
|
|
|
let tr = r.tr().read();
|
|
|
|
let subsecond = r.ssr().read().ss();
|
|
|
|
let second = bcd2_to_byte((tr.st(), tr.su()));
|
|
|
|
|
|
|
|
// Unlock the registers
|
2023-08-28 04:15:57 +02:00
|
|
|
r.dr().read();
|
2023-08-27 23:06:33 +02:00
|
|
|
|
2023-10-26 02:50:30 +02:00
|
|
|
RtcInstant::from(second, subsecond.try_into().unwrap())
|
2023-08-27 23:06:33 +02:00
|
|
|
}
|
|
|
|
|
2022-09-29 07:49:32 +02:00
|
|
|
/// Return the current datetime.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
|
|
|
|
pub fn now(&self) -> Result<DateTime, RtcError> {
|
2023-09-18 01:47:22 +02:00
|
|
|
self.time_provider().now()
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if daylight savings time is active.
|
|
|
|
pub fn get_daylight_savings(&self) -> bool {
|
2023-08-09 02:47:01 +02:00
|
|
|
let cr = RTC::regs().cr().read();
|
2022-09-29 07:49:32 +02:00
|
|
|
cr.bkp()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable/disable daylight savings time.
|
|
|
|
pub fn set_daylight_savings(&mut self, daylight_savings: bool) {
|
|
|
|
self.write(true, |rtc| {
|
2023-06-19 03:07:26 +02:00
|
|
|
rtc.cr().modify(|w| w.set_bkp(daylight_savings));
|
2022-09-29 07:49:32 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-09 02:47:01 +02:00
|
|
|
pub const BACKUP_REGISTER_COUNT: usize = RTC::BACKUP_REGISTER_COUNT;
|
2022-09-29 07:49:32 +02:00
|
|
|
|
|
|
|
/// Read content of the backup register.
|
|
|
|
///
|
|
|
|
/// The registers retain their values during wakes from standby mode or system resets. They also
|
|
|
|
/// retain their value when Vdd is switched off as long as V_BAT is powered.
|
|
|
|
pub fn read_backup_register(&self, register: usize) -> Option<u32> {
|
2023-08-09 02:47:01 +02:00
|
|
|
RTC::read_backup_register(&RTC::regs(), register)
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set content of the backup register.
|
|
|
|
///
|
|
|
|
/// The registers retain their values during wakes from standby mode or system resets. They also
|
|
|
|
/// retain their value when Vdd is switched off as long as V_BAT is powered.
|
|
|
|
pub fn write_backup_register(&self, register: usize, value: u32) {
|
2023-08-09 02:47:01 +02:00
|
|
|
RTC::write_backup_register(&RTC::regs(), register, value)
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn byte_to_bcd2(byte: u8) -> (u8, u8) {
|
|
|
|
let mut bcd_high: u8 = 0;
|
|
|
|
let mut value = byte;
|
|
|
|
|
|
|
|
while value >= 10 {
|
|
|
|
bcd_high += 1;
|
|
|
|
value -= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
(bcd_high, ((bcd_high << 4) | value) as u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn bcd2_to_byte(bcd: (u8, u8)) -> u8 {
|
|
|
|
let value = bcd.1 | bcd.0 << 4;
|
|
|
|
|
|
|
|
let tmp = ((value & 0xF0) >> 0x4) * 10;
|
|
|
|
|
|
|
|
tmp + (value & 0x0F)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
2023-04-19 03:35:43 +02:00
|
|
|
use crate::pac::rtc::Rtc;
|
|
|
|
|
2022-09-29 07:49:32 +02:00
|
|
|
pub trait Instance {
|
2023-04-19 03:35:43 +02:00
|
|
|
const BACKUP_REGISTER_COUNT: usize;
|
2022-09-29 07:49:32 +02:00
|
|
|
|
2023-09-16 01:41:33 +02:00
|
|
|
#[cfg(feature = "low-power")]
|
|
|
|
const EXTI_WAKEUP_LINE: usize;
|
|
|
|
|
|
|
|
#[cfg(feature = "low-power")]
|
|
|
|
type WakeupInterrupt: crate::interrupt::typelevel::Interrupt;
|
|
|
|
|
2023-04-19 03:35:43 +02:00
|
|
|
fn regs() -> Rtc {
|
|
|
|
crate::pac::RTC
|
|
|
|
}
|
2022-09-29 07:49:32 +02:00
|
|
|
|
2023-04-19 03:35:43 +02:00
|
|
|
/// Read content of the backup register.
|
|
|
|
///
|
|
|
|
/// The registers retain their values during wakes from standby mode or system resets. They also
|
|
|
|
/// retain their value when Vdd is switched off as long as V_BAT is powered.
|
|
|
|
fn read_backup_register(rtc: &Rtc, register: usize) -> Option<u32>;
|
|
|
|
|
|
|
|
/// Set content of the backup register.
|
|
|
|
///
|
|
|
|
/// The registers retain their values during wakes from standby mode or system resets. They also
|
|
|
|
/// retain their value when Vdd is switched off as long as V_BAT is powered.
|
|
|
|
fn write_backup_register(rtc: &Rtc, register: usize, value: u32);
|
|
|
|
|
|
|
|
// fn apply_config(&mut self, rtc_config: RtcConfig);
|
2022-09-29 07:49:32 +02:00
|
|
|
}
|
|
|
|
}
|