stm32/rtc: impl. functions on trait

This commit is contained in:
xoviat 2023-04-18 20:35:43 -05:00
parent 4de4039417
commit e24421a393
3 changed files with 74 additions and 72 deletions

View File

@ -13,9 +13,9 @@ pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
path = "v2.rs" path = "v2.rs"
)] )]
#[cfg_attr(any(rtc_v3, rtc_v3u5), path = "v3.rs")] #[cfg_attr(any(rtc_v3, rtc_v3u5), path = "v3.rs")]
mod versions; mod _version;
pub use _version::*;
use embassy_hal_common::Peripheral; use embassy_hal_common::Peripheral;
pub use versions::*;
/// Errors that can occur on methods on [RtcClock] /// Errors that can occur on methods on [RtcClock]
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
@ -113,7 +113,7 @@ impl Default for RtcCalibrationCyclePeriod {
impl<'d, T: Instance> Rtc<'d, T> { impl<'d, T: Instance> Rtc<'d, T> {
pub fn new(_rtc: impl Peripheral<P = T> + 'd, rtc_config: RtcConfig) -> Self { pub fn new(_rtc: impl Peripheral<P = T> + 'd, rtc_config: RtcConfig) -> Self {
unsafe { enable_peripheral_clk() }; unsafe { T::enable_peripheral_clk() };
let mut rtc_struct = Self { let mut rtc_struct = Self {
phantom: PhantomData, phantom: PhantomData,
@ -179,14 +179,14 @@ impl<'d, T: Instance> Rtc<'d, T> {
self.rtc_config 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. /// Read content of the backup register.
/// ///
/// The registers retain their values during wakes from standby mode or system resets. They also /// 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. /// 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> { pub fn read_backup_register(&self, register: usize) -> Option<u32> {
read_backup_register(&T::regs(), register) T::read_backup_register(&T::regs(), register)
} }
/// Set content of the backup 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 /// 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. /// 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) { 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 { pub(crate) mod sealed {
use crate::pac::rtc::Rtc;
pub trait Instance { 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<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);
} }
} }
pub trait Instance: sealed::Instance + 'static {} 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 {}

View File

@ -1,6 +1,6 @@
use stm32_metapac::rtc::vals::{Init, Osel, Pol}; use stm32_metapac::rtc::vals::{Init, Osel, Pol};
use super::{Instance, RtcConfig}; use super::{sealed, Instance, RtcConfig};
use crate::pac::rtc::Rtc; use crate::pac::rtc::Rtc;
impl<'d, T: Instance> super::Rtc<'d, T> { 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. impl sealed::Instance for crate::peripherals::RTC {
/// const BACKUP_REGISTER_COUNT: usize = 20;
/// 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. unsafe fn enable_peripheral_clk() {
pub fn read_backup_register(rtc: &Rtc, register: usize) -> Option<u32> { #[cfg(any(rtc_v2l4, rtc_v2wb))]
if register < BACKUP_REGISTER_COUNT { {
Some(unsafe { rtc.bkpr(register).read().bkp() }) // enable peripheral clock for communication
} else { crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true));
None
// read to allow the pwr clock to enable
crate::pac::PWR.cr1().read();
}
}
fn read_backup_register(rtc: &Rtc, register: usize) -> Option<u32> {
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. impl Instance for crate::peripherals::RTC {}
///
/// 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;

View File

@ -1,6 +1,6 @@
use stm32_metapac::rtc::vals::{Calp, Calw16, Calw8, Fmt, Init, Key, Osel, Pol, TampalrmPu, TampalrmType}; 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; use crate::pac::rtc::Rtc;
impl<'d, T: Instance> super::Rtc<'d, T> { 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() { impl sealed::Instance for crate::peripherals::RTC {
// Nothing to do const BACKUP_REGISTER_COUNT: usize = 32;
}
pub const BACKUP_REGISTER_COUNT: usize = 32; fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> {
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. fn write_backup_register(_rtc: &Rtc, register: usize, _value: u32) {
/// if register < Self::BACKUP_REGISTER_COUNT {
/// The registers retain their values during wakes from standby mode or system resets. They also // RTC3 backup registers come from the TAMP peripe=heral, not RTC. Not() even in the L412 PAC
/// retain their value when Vdd is switched off as long as V_BAT is powered. //unsafe { self.rtc.bkpr()[register].write(|w| w.bits(value)) }
pub fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { }
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
} }
} }
/// Set content of the backup register. impl Instance for crate::peripherals::RTC {}
///
/// 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)) }
}
}