stm32: update stm32-metapac.
This commit is contained in:
@ -154,29 +154,27 @@ pub(super) fn write_date_time(rtc: &Rtc, t: DateTime) {
|
||||
let yr_offset = (yr - 1970_u16) as u8;
|
||||
let (yt, yu) = byte_to_bcd2(yr_offset);
|
||||
|
||||
unsafe {
|
||||
use crate::pac::rtc::vals::Ampm;
|
||||
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.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));
|
||||
});
|
||||
}
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
||||
pub(super) fn datetime(
|
||||
|
@ -113,7 +113,7 @@ impl Default for RtcCalibrationCyclePeriod {
|
||||
|
||||
impl<'d, T: Instance> Rtc<'d, T> {
|
||||
pub fn new(_rtc: impl Peripheral<P = T> + 'd, rtc_config: RtcConfig) -> Self {
|
||||
unsafe { T::enable_peripheral_clk() };
|
||||
T::enable_peripheral_clk();
|
||||
|
||||
let mut rtc_struct = Self {
|
||||
phantom: PhantomData,
|
||||
@ -144,34 +144,32 @@ impl<'d, T: Instance> Rtc<'d, T> {
|
||||
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
|
||||
pub fn now(&self) -> Result<DateTime, RtcError> {
|
||||
let r = T::regs();
|
||||
unsafe {
|
||||
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 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;
|
||||
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;
|
||||
|
||||
self::datetime::datetime(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime)
|
||||
}
|
||||
self::datetime::datetime(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime)
|
||||
}
|
||||
|
||||
/// Check if daylight savings time is active.
|
||||
pub fn get_daylight_savings(&self) -> bool {
|
||||
let cr = unsafe { T::regs().cr().read() };
|
||||
let cr = T::regs().cr().read();
|
||||
cr.bkp()
|
||||
}
|
||||
|
||||
/// Enable/disable daylight savings time.
|
||||
pub fn set_daylight_savings(&mut self, daylight_savings: bool) {
|
||||
self.write(true, |rtc| {
|
||||
unsafe { rtc.cr().modify(|w| w.set_bkp(daylight_savings)) };
|
||||
rtc.cr().modify(|w| w.set_bkp(daylight_savings));
|
||||
})
|
||||
}
|
||||
|
||||
@ -228,7 +226,7 @@ pub(crate) mod sealed {
|
||||
crate::pac::RTC
|
||||
}
|
||||
|
||||
unsafe fn enable_peripheral_clk() {}
|
||||
fn enable_peripheral_clk() {}
|
||||
|
||||
/// Read content of the backup register.
|
||||
///
|
||||
|
@ -8,74 +8,72 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
/// It this changes the RTC clock source the time will be reset
|
||||
pub(super) fn apply_config(&mut self, rtc_config: RtcConfig) {
|
||||
// Unlock the backup domain
|
||||
unsafe {
|
||||
let clock_config = rtc_config.clock_config as u8;
|
||||
let clock_config = rtc_config.clock_config as u8;
|
||||
|
||||
#[cfg(not(rtc_v2wb))]
|
||||
use stm32_metapac::rcc::vals::Rtcsel;
|
||||
#[cfg(not(rtc_v2wb))]
|
||||
use stm32_metapac::rcc::vals::Rtcsel;
|
||||
|
||||
#[cfg(any(rtc_v2f2, rtc_v2f3, rtc_v2l1))]
|
||||
let cr = crate::pac::PWR.cr();
|
||||
#[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l4, rtc_v2wb))]
|
||||
let cr = crate::pac::PWR.cr1();
|
||||
#[cfg(any(rtc_v2f2, rtc_v2f3, rtc_v2l1))]
|
||||
let cr = crate::pac::PWR.cr();
|
||||
#[cfg(any(rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l4, rtc_v2wb))]
|
||||
let cr = crate::pac::PWR.cr1();
|
||||
|
||||
// TODO: Missing from PAC for l0 and f0?
|
||||
#[cfg(not(any(rtc_v2f0, rtc_v2l0)))]
|
||||
{
|
||||
cr.modify(|w| w.set_dbp(true));
|
||||
while !cr.read().dbp() {}
|
||||
}
|
||||
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
let reg = crate::pac::RCC.bdcr().read();
|
||||
#[cfg(any(rtc_v2l0, rtc_v2l1))]
|
||||
let reg = crate::pac::RCC.csr().read();
|
||||
|
||||
#[cfg(any(rtc_v2h7, rtc_v2l4, rtc_v2wb))]
|
||||
assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet.");
|
||||
|
||||
#[cfg(rtc_v2wb)]
|
||||
let rtcsel = reg.rtcsel();
|
||||
#[cfg(not(rtc_v2wb))]
|
||||
let rtcsel = reg.rtcsel().0;
|
||||
|
||||
if !reg.rtcen() || rtcsel != clock_config {
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true));
|
||||
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
let cr = crate::pac::RCC.bdcr();
|
||||
#[cfg(any(rtc_v2l0, rtc_v2l1))]
|
||||
let cr = crate::pac::RCC.csr();
|
||||
|
||||
cr.modify(|w| {
|
||||
// Reset
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
w.set_bdrst(false);
|
||||
|
||||
// Select RTC source
|
||||
#[cfg(not(rtc_v2wb))]
|
||||
w.set_rtcsel(Rtcsel(clock_config));
|
||||
#[cfg(rtc_v2wb)]
|
||||
w.set_rtcsel(clock_config);
|
||||
w.set_rtcen(true);
|
||||
|
||||
// Restore bcdr
|
||||
#[cfg(any(rtc_v2l4, rtc_v2wb))]
|
||||
w.set_lscosel(reg.lscosel());
|
||||
#[cfg(any(rtc_v2l4, rtc_v2wb))]
|
||||
w.set_lscoen(reg.lscoen());
|
||||
|
||||
w.set_lseon(reg.lseon());
|
||||
|
||||
#[cfg(any(rtc_v2f0, rtc_v2f7, rtc_v2h7, rtc_v2l4, rtc_v2wb))]
|
||||
w.set_lsedrv(reg.lsedrv());
|
||||
w.set_lsebyp(reg.lsebyp());
|
||||
});
|
||||
}
|
||||
// TODO: Missing from PAC for l0 and f0?
|
||||
#[cfg(not(any(rtc_v2f0, rtc_v2l0)))]
|
||||
{
|
||||
cr.modify(|w| w.set_dbp(true));
|
||||
while !cr.read().dbp() {}
|
||||
}
|
||||
|
||||
self.write(true, |rtc| unsafe {
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
let reg = crate::pac::RCC.bdcr().read();
|
||||
#[cfg(any(rtc_v2l0, rtc_v2l1))]
|
||||
let reg = crate::pac::RCC.csr().read();
|
||||
|
||||
#[cfg(any(rtc_v2h7, rtc_v2l4, rtc_v2wb))]
|
||||
assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet.");
|
||||
|
||||
#[cfg(rtc_v2wb)]
|
||||
let rtcsel = reg.rtcsel();
|
||||
#[cfg(not(rtc_v2wb))]
|
||||
let rtcsel = reg.rtcsel().0;
|
||||
|
||||
if !reg.rtcen() || rtcsel != clock_config {
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true));
|
||||
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
let cr = crate::pac::RCC.bdcr();
|
||||
#[cfg(any(rtc_v2l0, rtc_v2l1))]
|
||||
let cr = crate::pac::RCC.csr();
|
||||
|
||||
cr.modify(|w| {
|
||||
// Reset
|
||||
#[cfg(not(any(rtc_v2l0, rtc_v2l1)))]
|
||||
w.set_bdrst(false);
|
||||
|
||||
// Select RTC source
|
||||
#[cfg(not(rtc_v2wb))]
|
||||
w.set_rtcsel(Rtcsel(clock_config));
|
||||
#[cfg(rtc_v2wb)]
|
||||
w.set_rtcsel(clock_config);
|
||||
w.set_rtcen(true);
|
||||
|
||||
// Restore bcdr
|
||||
#[cfg(any(rtc_v2l4, rtc_v2wb))]
|
||||
w.set_lscosel(reg.lscosel());
|
||||
#[cfg(any(rtc_v2l4, rtc_v2wb))]
|
||||
w.set_lscoen(reg.lscoen());
|
||||
|
||||
w.set_lseon(reg.lseon());
|
||||
|
||||
#[cfg(any(rtc_v2f0, rtc_v2f7, rtc_v2h7, rtc_v2l4, rtc_v2wb))]
|
||||
w.set_lsedrv(reg.lsedrv());
|
||||
w.set_lsebyp(reg.lsebyp());
|
||||
});
|
||||
}
|
||||
|
||||
self.write(true, |rtc| {
|
||||
rtc.cr().modify(|w| {
|
||||
#[cfg(rtc_v2f2)]
|
||||
w.set_fmt(false);
|
||||
@ -117,47 +115,45 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
clock_drift = clock_drift / RTC_CALR_RESOLUTION_PPM;
|
||||
|
||||
self.write(false, |rtc| {
|
||||
unsafe {
|
||||
rtc.calr().write(|w| {
|
||||
match period {
|
||||
super::RtcCalibrationCyclePeriod::Seconds8 => {
|
||||
w.set_calw8(stm32_metapac::rtc::vals::Calw8::EIGHT_SECOND);
|
||||
}
|
||||
super::RtcCalibrationCyclePeriod::Seconds16 => {
|
||||
w.set_calw16(stm32_metapac::rtc::vals::Calw16::SIXTEEN_SECOND);
|
||||
}
|
||||
super::RtcCalibrationCyclePeriod::Seconds32 => {
|
||||
// Set neither `calw8` nor `calw16` to use 32 seconds
|
||||
}
|
||||
rtc.calr().write(|w| {
|
||||
match period {
|
||||
super::RtcCalibrationCyclePeriod::Seconds8 => {
|
||||
w.set_calw8(stm32_metapac::rtc::vals::Calw8::EIGHT_SECOND);
|
||||
}
|
||||
|
||||
// Extra pulses during calibration cycle period: CALP * 512 - CALM
|
||||
//
|
||||
// CALP sets whether pulses are added or omitted.
|
||||
//
|
||||
// CALM contains how many pulses (out of 512) are masked in a
|
||||
// given calibration cycle period.
|
||||
if clock_drift > 0.0 {
|
||||
// Maximum (about 512.2) rounds to 512.
|
||||
clock_drift += 0.5;
|
||||
|
||||
// When the offset is positive (0 to 512), the opposite of
|
||||
// the offset (512 - offset) is masked, i.e. for the
|
||||
// maximum offset (512), 0 pulses are masked.
|
||||
w.set_calp(stm32_metapac::rtc::vals::Calp::INCREASEFREQ);
|
||||
w.set_calm(512 - clock_drift as u16);
|
||||
} else {
|
||||
// Minimum (about -510.7) rounds to -511.
|
||||
clock_drift -= 0.5;
|
||||
|
||||
// When the offset is negative or zero (-511 to 0),
|
||||
// the absolute offset is masked, i.e. for the minimum
|
||||
// offset (-511), 511 pulses are masked.
|
||||
w.set_calp(stm32_metapac::rtc::vals::Calp::NOCHANGE);
|
||||
w.set_calm((clock_drift * -1.0) as u16);
|
||||
super::RtcCalibrationCyclePeriod::Seconds16 => {
|
||||
w.set_calw16(stm32_metapac::rtc::vals::Calw16::SIXTEEN_SECOND);
|
||||
}
|
||||
});
|
||||
}
|
||||
super::RtcCalibrationCyclePeriod::Seconds32 => {
|
||||
// Set neither `calw8` nor `calw16` to use 32 seconds
|
||||
}
|
||||
}
|
||||
|
||||
// Extra pulses during calibration cycle period: CALP * 512 - CALM
|
||||
//
|
||||
// CALP sets whether pulses are added or omitted.
|
||||
//
|
||||
// CALM contains how many pulses (out of 512) are masked in a
|
||||
// given calibration cycle period.
|
||||
if clock_drift > 0.0 {
|
||||
// Maximum (about 512.2) rounds to 512.
|
||||
clock_drift += 0.5;
|
||||
|
||||
// When the offset is positive (0 to 512), the opposite of
|
||||
// the offset (512 - offset) is masked, i.e. for the
|
||||
// maximum offset (512), 0 pulses are masked.
|
||||
w.set_calp(stm32_metapac::rtc::vals::Calp::INCREASEFREQ);
|
||||
w.set_calm(512 - clock_drift as u16);
|
||||
} else {
|
||||
// Minimum (about -510.7) rounds to -511.
|
||||
clock_drift -= 0.5;
|
||||
|
||||
// When the offset is negative or zero (-511 to 0),
|
||||
// the absolute offset is masked, i.e. for the minimum
|
||||
// offset (-511), 511 pulses are masked.
|
||||
w.set_calp(stm32_metapac::rtc::vals::Calp::NOCHANGE);
|
||||
w.set_calm((clock_drift * -1.0) as u16);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -168,31 +164,27 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
let r = T::regs();
|
||||
// Disable write protection.
|
||||
// This is safe, as we're only writin the correct and expected values.
|
||||
unsafe {
|
||||
r.wpr().write(|w| w.set_key(0xca));
|
||||
r.wpr().write(|w| w.set_key(0x53));
|
||||
r.wpr().write(|w| w.set_key(0xca));
|
||||
r.wpr().write(|w| w.set_key(0x53));
|
||||
|
||||
// true if initf bit indicates RTC peripheral is in init mode
|
||||
if init_mode && !r.isr().read().initf() {
|
||||
// to update calendar date/time, time format, and prescaler configuration, RTC must be in init mode
|
||||
r.isr().modify(|w| w.set_init(Init::INITMODE));
|
||||
// wait till init state entered
|
||||
// ~2 RTCCLK cycles
|
||||
while !r.isr().read().initf() {}
|
||||
}
|
||||
// true if initf bit indicates RTC peripheral is in init mode
|
||||
if init_mode && !r.isr().read().initf() {
|
||||
// to update calendar date/time, time format, and prescaler configuration, RTC must be in init mode
|
||||
r.isr().modify(|w| w.set_init(Init::INITMODE));
|
||||
// wait till init state entered
|
||||
// ~2 RTCCLK cycles
|
||||
while !r.isr().read().initf() {}
|
||||
}
|
||||
|
||||
let result = f(&r);
|
||||
|
||||
unsafe {
|
||||
if init_mode {
|
||||
r.isr().modify(|w| w.set_init(Init::FREERUNNINGMODE)); // Exits init mode
|
||||
}
|
||||
|
||||
// Re-enable write protection.
|
||||
// This is safe, as the field accepts the full range of 8-bit values.
|
||||
r.wpr().write(|w| w.set_key(0xff));
|
||||
if init_mode {
|
||||
r.isr().modify(|w| w.set_init(Init::FREERUNNINGMODE)); // Exits init mode
|
||||
}
|
||||
|
||||
// Re-enable write protection.
|
||||
// This is safe, as the field accepts the full range of 8-bit values.
|
||||
r.wpr().write(|w| w.set_key(0xff));
|
||||
result
|
||||
}
|
||||
}
|
||||
@ -200,7 +192,7 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
impl sealed::Instance for crate::peripherals::RTC {
|
||||
const BACKUP_REGISTER_COUNT: usize = 20;
|
||||
|
||||
unsafe fn enable_peripheral_clk() {
|
||||
fn enable_peripheral_clk() {
|
||||
#[cfg(any(rtc_v2l4, rtc_v2wb))]
|
||||
{
|
||||
// enable peripheral clock for communication
|
||||
@ -213,7 +205,7 @@ impl sealed::Instance for crate::peripherals::RTC {
|
||||
|
||||
fn read_backup_register(rtc: &Rtc, register: usize) -> Option<u32> {
|
||||
if register < Self::BACKUP_REGISTER_COUNT {
|
||||
Some(unsafe { rtc.bkpr(register).read().bkp() })
|
||||
Some(rtc.bkpr(register).read().bkp())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@ -221,7 +213,7 @@ impl sealed::Instance for crate::peripherals::RTC {
|
||||
|
||||
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)) }
|
||||
rtc.bkpr(register).write(|w| w.set_bkp(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,70 +8,66 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
/// It this changes the RTC clock source the time will be reset
|
||||
pub(super) fn apply_config(&mut self, rtc_config: RtcConfig) {
|
||||
// Unlock the backup domain
|
||||
unsafe {
|
||||
#[cfg(not(any(rtc_v3u5, rcc_wl5, rcc_wle)))]
|
||||
{
|
||||
crate::pac::PWR.cr1().modify(|w| w.set_dbp(true));
|
||||
while !crate::pac::PWR.cr1().read().dbp() {}
|
||||
}
|
||||
#[cfg(any(rcc_wl5, rcc_wle))]
|
||||
{
|
||||
use crate::pac::pwr::vals::Dbp;
|
||||
#[cfg(not(any(rtc_v3u5, rcc_wl5, rcc_wle)))]
|
||||
{
|
||||
crate::pac::PWR.cr1().modify(|w| w.set_dbp(true));
|
||||
while !crate::pac::PWR.cr1().read().dbp() {}
|
||||
}
|
||||
#[cfg(any(rcc_wl5, rcc_wle))]
|
||||
{
|
||||
use crate::pac::pwr::vals::Dbp;
|
||||
|
||||
crate::pac::PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED));
|
||||
while crate::pac::PWR.cr1().read().dbp() != Dbp::ENABLED {}
|
||||
}
|
||||
crate::pac::PWR.cr1().modify(|w| w.set_dbp(Dbp::ENABLED));
|
||||
while crate::pac::PWR.cr1().read().dbp() != Dbp::ENABLED {}
|
||||
}
|
||||
|
||||
let reg = crate::pac::RCC.bdcr().read();
|
||||
assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet.");
|
||||
let reg = crate::pac::RCC.bdcr().read();
|
||||
assert!(!reg.lsecsson(), "RTC is not compatible with LSE CSS, yet.");
|
||||
|
||||
let config_rtcsel = rtc_config.clock_config as u8;
|
||||
#[cfg(not(any(rcc_wl5, rcc_wle)))]
|
||||
let config_rtcsel = crate::pac::rcc::vals::Rtcsel(config_rtcsel);
|
||||
let config_rtcsel = rtc_config.clock_config as u8;
|
||||
#[cfg(not(any(rcc_wl5, rcc_wle)))]
|
||||
let config_rtcsel = crate::pac::rcc::vals::Rtcsel(config_rtcsel);
|
||||
|
||||
if !reg.rtcen() || reg.rtcsel() != config_rtcsel {
|
||||
crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true));
|
||||
if !reg.rtcen() || reg.rtcsel() != config_rtcsel {
|
||||
crate::pac::RCC.bdcr().modify(|w| w.set_bdrst(true));
|
||||
|
||||
crate::pac::RCC.bdcr().modify(|w| {
|
||||
// Reset
|
||||
w.set_bdrst(false);
|
||||
crate::pac::RCC.bdcr().modify(|w| {
|
||||
// Reset
|
||||
w.set_bdrst(false);
|
||||
|
||||
// Select RTC source
|
||||
w.set_rtcsel(config_rtcsel);
|
||||
// Select RTC source
|
||||
w.set_rtcsel(config_rtcsel);
|
||||
|
||||
w.set_rtcen(true);
|
||||
w.set_rtcen(true);
|
||||
|
||||
// Restore bcdr
|
||||
w.set_lscosel(reg.lscosel());
|
||||
w.set_lscoen(reg.lscoen());
|
||||
// Restore bcdr
|
||||
w.set_lscosel(reg.lscosel());
|
||||
w.set_lscoen(reg.lscoen());
|
||||
|
||||
w.set_lseon(reg.lseon());
|
||||
w.set_lsedrv(reg.lsedrv());
|
||||
w.set_lsebyp(reg.lsebyp());
|
||||
});
|
||||
}
|
||||
w.set_lseon(reg.lseon());
|
||||
w.set_lsedrv(reg.lsedrv());
|
||||
w.set_lsebyp(reg.lsebyp());
|
||||
});
|
||||
}
|
||||
|
||||
self.write(true, |rtc| {
|
||||
unsafe {
|
||||
rtc.cr().modify(|w| {
|
||||
w.set_fmt(Fmt::TWENTYFOURHOUR);
|
||||
w.set_osel(Osel::DISABLED);
|
||||
w.set_pol(Pol::HIGH);
|
||||
});
|
||||
rtc.cr().modify(|w| {
|
||||
w.set_fmt(Fmt::TWENTYFOURHOUR);
|
||||
w.set_osel(Osel::DISABLED);
|
||||
w.set_pol(Pol::HIGH);
|
||||
});
|
||||
|
||||
rtc.prer().modify(|w| {
|
||||
w.set_prediv_s(rtc_config.sync_prescaler);
|
||||
w.set_prediv_a(rtc_config.async_prescaler);
|
||||
});
|
||||
rtc.prer().modify(|w| {
|
||||
w.set_prediv_s(rtc_config.sync_prescaler);
|
||||
w.set_prediv_a(rtc_config.async_prescaler);
|
||||
});
|
||||
|
||||
// TODO: configuration for output pins
|
||||
rtc.cr().modify(|w| {
|
||||
w.set_out2en(false);
|
||||
w.set_tampalrm_type(TampalrmType::PUSHPULL);
|
||||
w.set_tampalrm_pu(TampalrmPu::NOPULLUP);
|
||||
});
|
||||
}
|
||||
// TODO: configuration for output pins
|
||||
rtc.cr().modify(|w| {
|
||||
w.set_out2en(false);
|
||||
w.set_tampalrm_type(TampalrmType::PUSHPULL);
|
||||
w.set_tampalrm_pu(TampalrmPu::NOPULLUP);
|
||||
});
|
||||
});
|
||||
|
||||
self.rtc_config = rtc_config;
|
||||
@ -99,47 +95,45 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
clock_drift = clock_drift / Self::RTC_CALR_RESOLUTION_PPM;
|
||||
|
||||
self.write(false, |rtc| {
|
||||
unsafe {
|
||||
rtc.calr().write(|w| {
|
||||
match period {
|
||||
RtcCalibrationCyclePeriod::Seconds8 => {
|
||||
w.set_calw8(Calw8::EIGHTSECONDS);
|
||||
}
|
||||
RtcCalibrationCyclePeriod::Seconds16 => {
|
||||
w.set_calw16(Calw16::SIXTEENSECONDS);
|
||||
}
|
||||
RtcCalibrationCyclePeriod::Seconds32 => {
|
||||
// Set neither `calw8` nor `calw16` to use 32 seconds
|
||||
}
|
||||
rtc.calr().write(|w| {
|
||||
match period {
|
||||
RtcCalibrationCyclePeriod::Seconds8 => {
|
||||
w.set_calw8(Calw8::EIGHTSECONDS);
|
||||
}
|
||||
|
||||
// Extra pulses during calibration cycle period: CALP * 512 - CALM
|
||||
//
|
||||
// CALP sets whether pulses are added or omitted.
|
||||
//
|
||||
// CALM contains how many pulses (out of 512) are masked in a
|
||||
// given calibration cycle period.
|
||||
if clock_drift > 0.0 {
|
||||
// Maximum (about 512.2) rounds to 512.
|
||||
clock_drift += 0.5;
|
||||
|
||||
// When the offset is positive (0 to 512), the opposite of
|
||||
// the offset (512 - offset) is masked, i.e. for the
|
||||
// maximum offset (512), 0 pulses are masked.
|
||||
w.set_calp(Calp::INCREASEFREQ);
|
||||
w.set_calm(512 - clock_drift as u16);
|
||||
} else {
|
||||
// Minimum (about -510.7) rounds to -511.
|
||||
clock_drift -= 0.5;
|
||||
|
||||
// When the offset is negative or zero (-511 to 0),
|
||||
// the absolute offset is masked, i.e. for the minimum
|
||||
// offset (-511), 511 pulses are masked.
|
||||
w.set_calp(Calp::NOCHANGE);
|
||||
w.set_calm((clock_drift * -1.0) as u16);
|
||||
RtcCalibrationCyclePeriod::Seconds16 => {
|
||||
w.set_calw16(Calw16::SIXTEENSECONDS);
|
||||
}
|
||||
});
|
||||
}
|
||||
RtcCalibrationCyclePeriod::Seconds32 => {
|
||||
// Set neither `calw8` nor `calw16` to use 32 seconds
|
||||
}
|
||||
}
|
||||
|
||||
// Extra pulses during calibration cycle period: CALP * 512 - CALM
|
||||
//
|
||||
// CALP sets whether pulses are added or omitted.
|
||||
//
|
||||
// CALM contains how many pulses (out of 512) are masked in a
|
||||
// given calibration cycle period.
|
||||
if clock_drift > 0.0 {
|
||||
// Maximum (about 512.2) rounds to 512.
|
||||
clock_drift += 0.5;
|
||||
|
||||
// When the offset is positive (0 to 512), the opposite of
|
||||
// the offset (512 - offset) is masked, i.e. for the
|
||||
// maximum offset (512), 0 pulses are masked.
|
||||
w.set_calp(Calp::INCREASEFREQ);
|
||||
w.set_calm(512 - clock_drift as u16);
|
||||
} else {
|
||||
// Minimum (about -510.7) rounds to -511.
|
||||
clock_drift -= 0.5;
|
||||
|
||||
// When the offset is negative or zero (-511 to 0),
|
||||
// the absolute offset is masked, i.e. for the minimum
|
||||
// offset (-511), 511 pulses are masked.
|
||||
w.set_calp(Calp::NOCHANGE);
|
||||
w.set_calm((clock_drift * -1.0) as u16);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
@ -150,29 +144,26 @@ impl<'d, T: Instance> super::Rtc<'d, T> {
|
||||
let r = T::regs();
|
||||
// Disable write protection.
|
||||
// This is safe, as we're only writin the correct and expected values.
|
||||
unsafe {
|
||||
r.wpr().write(|w| w.set_key(Key::DEACTIVATE1));
|
||||
r.wpr().write(|w| w.set_key(Key::DEACTIVATE2));
|
||||
r.wpr().write(|w| w.set_key(Key::DEACTIVATE1));
|
||||
r.wpr().write(|w| w.set_key(Key::DEACTIVATE2));
|
||||
|
||||
if init_mode && !r.icsr().read().initf() {
|
||||
r.icsr().modify(|w| w.set_init(Init::INITMODE));
|
||||
// wait till init state entered
|
||||
// ~2 RTCCLK cycles
|
||||
while !r.icsr().read().initf() {}
|
||||
}
|
||||
if init_mode && !r.icsr().read().initf() {
|
||||
r.icsr().modify(|w| w.set_init(Init::INITMODE));
|
||||
// wait till init state entered
|
||||
// ~2 RTCCLK cycles
|
||||
while !r.icsr().read().initf() {}
|
||||
}
|
||||
|
||||
let result = f(&r);
|
||||
|
||||
unsafe {
|
||||
if init_mode {
|
||||
r.icsr().modify(|w| w.set_init(Init::FREERUNNINGMODE)); // Exits init mode
|
||||
}
|
||||
|
||||
// Re-enable write protection.
|
||||
// This is safe, as the field accepts the full range of 8-bit values.
|
||||
r.wpr().write(|w| w.set_key(Key::ACTIVATE));
|
||||
if init_mode {
|
||||
r.icsr().modify(|w| w.set_init(Init::FREERUNNINGMODE)); // Exits init mode
|
||||
}
|
||||
|
||||
// Re-enable write protection.
|
||||
// This is safe, as the field accepts the full range of 8-bit values.
|
||||
r.wpr().write(|w| w.set_key(Key::ACTIVATE));
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
@ -192,7 +183,7 @@ impl sealed::Instance for crate::peripherals::RTC {
|
||||
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)) }
|
||||
//self.rtc.bkpr()[register].write(|w| w.bits(value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user