mod datetime;
#[cfg(feature = "low-power")]
mod low_power;
#[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;
use self::datetime::{day_of_week_from_u8, day_of_week_to_u8};
pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
use crate::pac::rtc::regs::{Dr, Tr};
use crate::time::Hertz;
#[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
),
path = "v2.rs"
)]
#[cfg_attr(any(rtc_v3, rtc_v3u5, rtc_v3l5), path = "v3.rs")]
mod _version;
#[allow(unused_imports)]
pub use _version::*;
use embassy_hal_internal::Peripheral;
use crate::peripherals::RTC;
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RtcError {
InvalidDateTime(DateTimeError),
ReadFailure,
NotRunning,
}
pub struct RtcTimeProvider {
_private: (),
}
impl RtcTimeProvider {
pub fn now(&self) -> Result<DateTime, RtcError> {
self.read(|dr, tr, _| {
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 = day_of_week_from_u8(dr.wdu()).map_err(RtcError::InvalidDateTime)?;
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 + 2000_u16;
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 ss == ss_after {
return f(dr, tr, ss.try_into().unwrap());
} else {
ss = ss_after
}
}
Err(RtcError::ReadFailure)
}
}
pub struct Rtc {
#[cfg(feature = "low-power")]
stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<low_power::RtcInstant>>>,
_private: (),
}
#[non_exhaustive]
#[derive(Copy, Clone, PartialEq)]
pub struct RtcConfig {
pub frequency: Hertz,
}
impl Default for RtcConfig {
fn default() -> Self {
RtcConfig { frequency: Hertz(256) }
}
}
#[derive(Default, Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum RtcCalibrationCyclePeriod {
Seconds8,
Seconds16,
#[default]
Seconds32,
}
impl Rtc {
pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self {
#[cfg(not(any(stm32l0, stm32f3, stm32l1, stm32f0, stm32f2)))]
crate::rcc::enable_and_reset::<RTC>();
let mut this = Self {
#[cfg(feature = "low-power")]
stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)),
_private: (),
};
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);
#[cfg(not(rtc_v2f2))]
{
let now = this.time_provider().read(|_, _, ss| Ok(ss)).unwrap();
while now == this.time_provider().read(|_, _, ss| Ok(ss)).unwrap() {}
}
this
}
fn frequency() -> Hertz {
let freqs = unsafe { crate::rcc::get_freqs() };
freqs.rtc.to_hertz().unwrap()
}
pub const fn time_provider(&self) -> RtcTimeProvider {
RtcTimeProvider { _private: () }
}
pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> {
self.write(true, |rtc| {
let (ht, hu) = byte_to_bcd2(t.hour());
let (mnt, mnu) = byte_to_bcd2(t.minute());
let (st, su) = byte_to_bcd2(t.second());
let (dt, du) = byte_to_bcd2(t.day());
let (mt, mu) = byte_to_bcd2(t.month());
let yr = t.year();
let yr_offset = (yr - 2000_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()));
});
});
Ok(())
}
pub fn now(&self) -> Result<DateTime, RtcError> {
self.time_provider().now()
}
pub fn get_daylight_savings(&self) -> bool {
let cr = RTC::regs().cr().read();
cr.bkp()
}
pub fn set_daylight_savings(&mut self, daylight_savings: bool) {
self.write(true, |rtc| {
rtc.cr().modify(|w| w.set_bkp(daylight_savings));
})
}
pub const BACKUP_REGISTER_COUNT: usize = RTC::BACKUP_REGISTER_COUNT;
pub fn read_backup_register(&self, register: usize) -> Option<u32> {
RTC::read_backup_register(RTC::regs(), register)
}
pub fn write_backup_register(&self, register: usize, value: u32) {
RTC::write_backup_register(RTC::regs(), register, value)
}
}
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))
}
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)
}
trait SealedInstance {
const BACKUP_REGISTER_COUNT: usize;
#[cfg(feature = "low-power")]
#[cfg(not(any(stm32u5, stm32u0)))]
const EXTI_WAKEUP_LINE: usize;
#[cfg(feature = "low-power")]
type WakeupInterrupt: crate::interrupt::typelevel::Interrupt;
fn regs() -> crate::pac::rtc::Rtc {
crate::pac::RTC
}
fn read_backup_register(rtc: crate::pac::rtc::Rtc, register: usize) -> Option<u32>;
fn write_backup_register(rtc: crate::pac::rtc::Rtc, register: usize, value: u32);
}