aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/time_driver.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32/src/time_driver.rs')
-rw-r--r--embassy-stm32/src/time_driver.rs23
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