diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index c2f07a066..962927fb1 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs @@ -13,9 +13,9 @@ pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; path = "v2.rs" )] #[cfg_attr(any(rtc_v3, rtc_v3u5), path = "v3.rs")] -mod versions; +mod _version; +pub use _version::*; use embassy_hal_common::Peripheral; -pub use versions::*; /// Errors that can occur on methods on [RtcClock] #[derive(Clone, Debug, PartialEq, Eq)] @@ -113,7 +113,7 @@ impl Default for RtcCalibrationCyclePeriod { impl<'d, T: Instance> Rtc<'d, T> { pub fn new(_rtc: impl Peripheral

+ 'd, rtc_config: RtcConfig) -> Self { - unsafe { enable_peripheral_clk() }; + unsafe { T::enable_peripheral_clk() }; let mut rtc_struct = Self { phantom: PhantomData, @@ -179,14 +179,14 @@ impl<'d, T: Instance> Rtc<'d, T> { self.rtc_config } - pub const BACKUP_REGISTER_COUNT: usize = BACKUP_REGISTER_COUNT; + pub const BACKUP_REGISTER_COUNT: usize = T::BACKUP_REGISTER_COUNT; /// 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 { - read_backup_register(&T::regs(), register) + T::read_backup_register(&T::regs(), register) } /// Set content of the backup register. @@ -194,7 +194,7 @@ impl<'d, T: Instance> Rtc<'d, T> { /// 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) { - write_backup_register(&T::regs(), register, value) + T::write_backup_register(&T::regs(), register, value) } } @@ -219,17 +219,31 @@ pub(crate) fn bcd2_to_byte(bcd: (u8, u8)) -> u8 { } pub(crate) mod sealed { + use crate::pac::rtc::Rtc; + pub trait Instance { - fn regs() -> crate::pac::rtc::Rtc; + const BACKUP_REGISTER_COUNT: usize; + + fn regs() -> Rtc { + crate::pac::RTC + } + + unsafe fn enable_peripheral_clk() {} + + /// 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; + + /// 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); } } pub trait Instance: sealed::Instance + 'static {} - -impl sealed::Instance for crate::peripherals::RTC { - fn regs() -> crate::pac::rtc::Rtc { - crate::pac::RTC - } -} - -impl Instance for crate::peripherals::RTC {} diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index 8ab06a59a..adaafe67a 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs @@ -1,6 +1,6 @@ use stm32_metapac::rtc::vals::{Init, Osel, Pol}; -use super::{Instance, RtcConfig}; +use super::{sealed, Instance, RtcConfig}; use crate::pac::rtc::Rtc; impl<'d, T: Instance> super::Rtc<'d, T> { @@ -197,37 +197,33 @@ impl<'d, T: Instance> super::Rtc<'d, T> { } } -/// 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(rtc: &Rtc, register: usize) -> Option { - if register < BACKUP_REGISTER_COUNT { - Some(unsafe { rtc.bkpr(register).read().bkp() }) - } else { - None +impl sealed::Instance for crate::peripherals::RTC { + const BACKUP_REGISTER_COUNT: usize = 20; + + unsafe fn enable_peripheral_clk() { + #[cfg(any(rtc_v2l4, rtc_v2wb))] + { + // enable peripheral clock for communication + crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true)); + + // read to allow the pwr clock to enable + crate::pac::PWR.cr1().read(); + } + } + + fn read_backup_register(rtc: &Rtc, register: usize) -> Option { + if register < Self::BACKUP_REGISTER_COUNT { + Some(unsafe { rtc.bkpr(register).read().bkp() }) + } else { + None + } + } + + fn write_backup_register(rtc: &Rtc, register: usize, value: u32) { + if register < Self::BACKUP_REGISTER_COUNT { + unsafe { rtc.bkpr(register).write(|w| w.set_bkp(value)) } + } } } -/// 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(rtc: &Rtc, register: usize, value: u32) { - if register < BACKUP_REGISTER_COUNT { - unsafe { rtc.bkpr(register).write(|w| w.set_bkp(value)) } - } -} - -pub(crate) unsafe fn enable_peripheral_clk() { - #[cfg(any(rtc_v2l4, rtc_v2wb))] - { - // enable peripheral clock for communication - crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true)); - - // read to allow the pwr clock to enable - crate::pac::PWR.cr1().read(); - } -} - -pub const BACKUP_REGISTER_COUNT: usize = 20; +impl Instance for crate::peripherals::RTC {} diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs index c2b3c88c2..19c52ee02 100644 --- a/embassy-stm32/src/rtc/v3.rs +++ b/embassy-stm32/src/rtc/v3.rs @@ -1,6 +1,6 @@ use stm32_metapac::rtc::vals::{Calp, Calw16, Calw8, Fmt, Init, Key, Osel, Pol, TampalrmPu, TampalrmType}; -use super::{Instance, RtcCalibrationCyclePeriod, RtcConfig}; +use super::{sealed, Instance, RtcCalibrationCyclePeriod, RtcConfig}; use crate::pac::rtc::Rtc; impl<'d, T: Instance> super::Rtc<'d, T> { @@ -182,32 +182,24 @@ impl<'d, T: Instance> super::Rtc<'d, T> { } } -pub(super) unsafe fn enable_peripheral_clk() { - // Nothing to do -} +impl sealed::Instance for crate::peripherals::RTC { + const BACKUP_REGISTER_COUNT: usize = 32; -pub const BACKUP_REGISTER_COUNT: usize = 32; + fn read_backup_register(_rtc: &Rtc, register: usize) -> Option { + if register < Self::BACKUP_REGISTER_COUNT { + //Some(rtc.bkpr()[register].read().bits()) + None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC + } else { + None + } + } -/// 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(_rtc: &Rtc, register: usize) -> Option { - if register < BACKUP_REGISTER_COUNT { - //Some(rtc.bkpr()[register].read().bits()) - None // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC - } else { - None + fn write_backup_register(_rtc: &Rtc, register: usize, _value: u32) { + if register < Self::BACKUP_REGISTER_COUNT { + // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC + //unsafe { self.rtc.bkpr()[register].write(|w| w.bits(value)) } + } } } -/// 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(_rtc: &Rtc, register: usize, _value: u32) { - if register < BACKUP_REGISTER_COUNT { - // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC - //unsafe { self.rtc.bkpr()[register].write(|w| w.bits(value)) } - } -} +impl Instance for crate::peripherals::RTC {}