diff options
| author | xoviat <[email protected]> | 2025-12-18 15:42:13 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-18 15:42:13 +0000 |
| commit | da16ea77af0fdd875ac568d5e2da1931b8740892 (patch) | |
| tree | 2fa25c7907ba8ef1923c4baeeb93dd88c57bce7e /embassy-stm32/src/time_driver.rs | |
| parent | b5b49cbcf3a991bf6d434b0870da50f3ee722612 (diff) | |
| parent | cec833c5f48ae93d1a47d5b763e613d8407d48a1 (diff) | |
Merge pull request #5101 from xoviat/low-power-rtc
stm32: use datemath to resume time
Diffstat (limited to 'embassy-stm32/src/time_driver.rs')
| -rw-r--r-- | embassy-stm32/src/time_driver.rs | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index ed5d902bd..59ec58575 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs | |||
| @@ -251,8 +251,8 @@ impl RtcDriver { | |||
| 251 | 251 | ||
| 252 | #[cfg(feature = "low-power")] | 252 | #[cfg(feature = "low-power")] |
| 253 | /// Add the given offset to the current time | 253 | /// Add the given offset to the current time |
| 254 | fn add_time(&self, offset: embassy_time::Duration, cs: CriticalSection) { | 254 | fn set_time(&self, instant: u64, cs: CriticalSection) { |
| 255 | let (period, counter) = calc_period_counter(self.now() + offset.as_ticks()); | 255 | let (period, counter) = calc_period_counter(core::cmp::max(self.now(), instant)); |
| 256 | 256 | ||
| 257 | self.period.store(period, Ordering::SeqCst); | 257 | self.period.store(period, Ordering::SeqCst); |
| 258 | regs_gp16().cnt().write(|w| w.set_cnt(counter)); | 258 | regs_gp16().cnt().write(|w| w.set_cnt(counter)); |
| @@ -269,10 +269,17 @@ impl RtcDriver { | |||
| 269 | #[cfg(feature = "low-power")] | 269 | #[cfg(feature = "low-power")] |
| 270 | /// Stop the wakeup alarm, if enabled, and add the appropriate offset | 270 | /// Stop the wakeup alarm, if enabled, and add the appropriate offset |
| 271 | fn stop_wakeup_alarm(&self, cs: CriticalSection) { | 271 | fn stop_wakeup_alarm(&self, cs: CriticalSection) { |
| 272 | if !regs_gp16().cr1().read().cen() | 272 | if !regs_gp16().cr1().read().cen() { |
| 273 | && let Some(offset) = self.rtc.borrow(cs).borrow_mut().as_mut().unwrap().stop_wakeup_alarm(cs) | 273 | self.set_time( |
| 274 | { | 274 | self.rtc |
| 275 | self.add_time(offset, cs); | 275 | .borrow(cs) |
| 276 | .borrow_mut() | ||
| 277 | .as_mut() | ||
| 278 | .unwrap() | ||
| 279 | .stop_wakeup_alarm() | ||
| 280 | .as_ticks(), | ||
| 281 | cs, | ||
| 282 | ); | ||
| 276 | } | 283 | } |
| 277 | } | 284 | } |
| 278 | 285 | ||
| @@ -287,7 +294,7 @@ impl RtcDriver { | |||
| 287 | #[cfg(feature = "low-power")] | 294 | #[cfg(feature = "low-power")] |
| 288 | /// Set the rtc but panic if it's already been set | 295 | /// Set the rtc but panic if it's already been set |
| 289 | pub(crate) fn set_rtc(&self, cs: CriticalSection, mut rtc: Rtc) { | 296 | pub(crate) fn set_rtc(&self, cs: CriticalSection, mut rtc: Rtc) { |
| 290 | rtc.stop_wakeup_alarm(cs); | 297 | rtc.stop_wakeup_alarm(); |
| 291 | 298 | ||
| 292 | assert!(self.rtc.borrow(cs).replace(Some(rtc)).is_none()); | 299 | assert!(self.rtc.borrow(cs).replace(Some(rtc)).is_none()); |
| 293 | } | 300 | } |
| @@ -310,7 +317,7 @@ impl RtcDriver { | |||
| 310 | .borrow_mut() | 317 | .borrow_mut() |
| 311 | .as_mut() | 318 | .as_mut() |
| 312 | .unwrap() | 319 | .unwrap() |
| 313 | .start_wakeup_alarm(time_until_next_alarm, cs); | 320 | .start_wakeup_alarm(time_until_next_alarm); |
| 314 | 321 | ||
| 315 | regs_gp16().cr1().modify(|w| w.set_cen(false)); | 322 | regs_gp16().cr1().modify(|w| w.set_cen(false)); |
| 316 | // save the count for the timer as its lost in STOP2 for stm32wlex | 323 | // save the count for the timer as its lost in STOP2 for stm32wlex |
