From b97b6d409c1b042b5d5f1b17dd2c8dfec50acdfc Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 14 Nov 2025 14:53:09 -0600 Subject: low_power: cleanup add_time --- embassy-stm32/src/time_driver.rs | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index 7db51d72e..bc34892ee 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs @@ -196,6 +196,11 @@ fn calc_now(period: u32, counter: u16) -> u64 { ((period as u64) << 15) + ((counter as u32 ^ ((period & 1) << 15)) as u64) } +#[cfg(feature = "low-power")] +fn calc_period_counter(ticks: u64) -> (u32, u16) { + (2 * (ticks >> 16) as u32 + (ticks as u16 >= 0x8000) as u32, ticks as u16) +} + struct AlarmState { timestamp: Cell, } @@ -358,34 +363,10 @@ impl RtcDriver { #[cfg(feature = "low-power")] /// Add the given offset to the current time fn add_time(&self, offset: embassy_time::Duration, cs: CriticalSection) { - let offset = offset.as_ticks(); - let cnt = regs_gp16().cnt().read().cnt() as u32; - let period = self.period.load(Ordering::SeqCst); - - // Correct the race, if it exists - let period = if period & 1 == 1 && cnt < u16::MAX as u32 / 2 { - period + 1 - } else { - period - }; - - // Normalize to the full overflow - let period = (period / 2) * 2; - - // Add the offset - let period = period + 2 * (offset / u16::MAX as u64) as u32; - let cnt = cnt + (offset % u16::MAX as u64) as u32; - - let (cnt, period) = if cnt > u16::MAX as u32 { - (cnt - u16::MAX as u32, period + 2) - } else { - (cnt, period) - }; - - let period = if cnt > u16::MAX as u32 / 2 { period + 1 } else { period }; + let (period, counter) = calc_period_counter(self.now() + offset.as_ticks()); self.period.store(period, Ordering::SeqCst); - regs_gp16().cnt().write(|w| w.set_cnt(cnt as u16)); + regs_gp16().cnt().write(|w| w.set_cnt(counter)); // Now, recompute alarm let alarm = self.alarm.borrow(cs); -- cgit From af9bfe52cb1949c0f484cbab3701a6072fcd8496 Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 14 Nov 2025 15:17:25 -0600 Subject: changelog --- embassy-stm32/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/embassy-stm32/CHANGELOG.md b/embassy-stm32/CHANGELOG.md index 8bd930e79..cb846588e 100644 --- a/embassy-stm32/CHANGELOG.md +++ b/embassy-stm32/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased - ReleaseDate +- chore: cleanup low-power add time - fix: flash erase on dual-bank STM32Gxxx - feat: Add support for STM32N657X0 - feat: timer: Add 32-bit timer support to SimplePwm waveform_up method following waveform pattern ([#4717](https://github.com/embassy-rs/embassy/pull/4717)) -- cgit From c49398adfcb7d6c874f37ec00eba89e416712c50 Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 14 Nov 2025 15:44:30 -0600 Subject: low_power remove wucksel enum --- embassy-stm32/src/rtc/low_power.rs | 69 +++++++------------------------------- 1 file changed, 13 insertions(+), 56 deletions(-) diff --git a/embassy-stm32/src/rtc/low_power.rs b/embassy-stm32/src/rtc/low_power.rs index e5bf30927..264b0b795 100644 --- a/embassy-stm32/src/rtc/low_power.rs +++ b/embassy-stm32/src/rtc/low_power.rs @@ -1,3 +1,4 @@ +use crate::pac::rtc::vals::Wucksel; #[cfg(feature = "time")] use embassy_time::{Duration, TICK_HZ}; @@ -58,60 +59,16 @@ impl core::ops::Sub for RtcInstant { } } -#[repr(u8)] -#[derive(Clone, Copy, Debug)] -pub(crate) enum WakeupPrescaler { - Div2 = 2, - Div4 = 4, - Div8 = 8, - Div16 = 16, -} - -#[cfg(any( - stm32f4, stm32l0, stm32g4, stm32l4, stm32l5, stm32wb, stm32h5, stm32g0, stm32u5, stm32u0, stm32wba, stm32wlex -))] -impl From for crate::pac::rtc::vals::Wucksel { - fn from(val: WakeupPrescaler) -> Self { - use crate::pac::rtc::vals::Wucksel; - - match val { - WakeupPrescaler::Div2 => Wucksel::DIV2, - WakeupPrescaler::Div4 => Wucksel::DIV4, - WakeupPrescaler::Div8 => Wucksel::DIV8, - WakeupPrescaler::Div16 => Wucksel::DIV16, - } - } -} - -#[cfg(any( - stm32f4, stm32l0, stm32g4, stm32l4, stm32l5, stm32wb, stm32h5, stm32g0, stm32u5, stm32u0, stm32wba, stm32wlex -))] -impl From for WakeupPrescaler { - fn from(val: crate::pac::rtc::vals::Wucksel) -> Self { - use crate::pac::rtc::vals::Wucksel; - - match val { - Wucksel::DIV2 => WakeupPrescaler::Div2, - Wucksel::DIV4 => WakeupPrescaler::Div4, - Wucksel::DIV8 => WakeupPrescaler::Div8, - Wucksel::DIV16 => WakeupPrescaler::Div16, - _ => unreachable!(), - } - } -} - -impl WakeupPrescaler { - pub fn compute_min(val: u32) -> Self { - *[ - WakeupPrescaler::Div2, - WakeupPrescaler::Div4, - WakeupPrescaler::Div8, - WakeupPrescaler::Div16, - ] - .iter() - .find(|psc| **psc as u32 > val) - .unwrap_or(&WakeupPrescaler::Div16) - } +fn wucksel_compute_min(val: u32) -> (Wucksel, u32) { + *[ + (Wucksel::DIV2, 2), + (Wucksel::DIV4, 4), + (Wucksel::DIV8, 8), + (Wucksel::DIV16, 16), + ] + .iter() + .find(|(_, psc)| *psc as u32 > val) + .unwrap_or(&(Wucksel::DIV16, 16)) } impl Rtc { @@ -138,7 +95,7 @@ impl Rtc { let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64); let rtc_hz = Self::frequency().0 as u64; let rtc_ticks = requested_duration * rtc_hz / TICK_HZ; - let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32); + let (wucksel, prescaler) = wucksel_compute_min((rtc_ticks / u16::MAX as u64) as u32); // adjust the rtc ticks to the prescaler and subtract one rtc tick let rtc_ticks = rtc_ticks / prescaler as u64; @@ -159,7 +116,7 @@ impl Rtc { while !regs.icsr().read().wutwf() {} } - regs.cr().modify(|w| w.set_wucksel(prescaler.into())); + regs.cr().modify(|w| w.set_wucksel(wucksel)); regs.wutr().write(|w| w.set_wut(rtc_ticks)); regs.cr().modify(|w| w.set_wute(true)); regs.cr().modify(|w| w.set_wutie(true)); -- cgit From 4a919328dea682f29ae469cc7b0d47116647e74b Mon Sep 17 00:00:00 2001 From: xoviat Date: Fri, 14 Nov 2025 15:51:05 -0600 Subject: fmt --- embassy-stm32/src/rtc/low_power.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embassy-stm32/src/rtc/low_power.rs b/embassy-stm32/src/rtc/low_power.rs index 264b0b795..f049d6b12 100644 --- a/embassy-stm32/src/rtc/low_power.rs +++ b/embassy-stm32/src/rtc/low_power.rs @@ -1,9 +1,9 @@ -use crate::pac::rtc::vals::Wucksel; #[cfg(feature = "time")] use embassy_time::{Duration, TICK_HZ}; use super::{DateTimeError, Rtc, RtcError, bcd2_to_byte}; use crate::interrupt::typelevel::Interrupt; +use crate::pac::rtc::vals::Wucksel; use crate::peripherals::RTC; use crate::rtc::{RtcTimeProvider, SealedInstance}; -- cgit