diff options
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/rtc/low_power.rs | 94 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 16 | ||||
| -rw-r--r-- | embassy-stm32/src/time_driver.rs | 23 |
3 files changed, 42 insertions, 91 deletions
diff --git a/embassy-stm32/src/rtc/low_power.rs b/embassy-stm32/src/rtc/low_power.rs index f049d6b12..cd5cea081 100644 --- a/embassy-stm32/src/rtc/low_power.rs +++ b/embassy-stm32/src/rtc/low_power.rs | |||
| @@ -1,64 +1,12 @@ | |||
| 1 | #[cfg(feature = "time")] | 1 | use chrono::{DateTime, NaiveDateTime, TimeDelta, Utc}; |
| 2 | use embassy_time::{Duration, TICK_HZ}; | 2 | use embassy_time::{Duration, Instant, TICK_HZ}; |
| 3 | 3 | ||
| 4 | use super::{DateTimeError, Rtc, RtcError, bcd2_to_byte}; | 4 | use super::Rtc; |
| 5 | use crate::interrupt::typelevel::Interrupt; | 5 | use crate::interrupt::typelevel::Interrupt; |
| 6 | use crate::pac::rtc::vals::Wucksel; | 6 | use crate::pac::rtc::vals::Wucksel; |
| 7 | use crate::peripherals::RTC; | 7 | use crate::peripherals::RTC; |
| 8 | use crate::rtc::{RtcTimeProvider, SealedInstance}; | 8 | use crate::rtc::{RtcTimeProvider, SealedInstance}; |
| 9 | 9 | ||
| 10 | /// Represents an instant in time that can be substracted to compute a duration | ||
| 11 | pub(super) struct RtcInstant { | ||
| 12 | /// 0..59 | ||
| 13 | second: u8, | ||
| 14 | /// 0..256 | ||
| 15 | subsecond: u16, | ||
| 16 | } | ||
| 17 | |||
| 18 | impl RtcInstant { | ||
| 19 | #[cfg(not(rtc_v2_f2))] | ||
| 20 | const fn from(second: u8, subsecond: u16) -> Result<Self, DateTimeError> { | ||
| 21 | if second > 59 { | ||
| 22 | Err(DateTimeError::InvalidSecond) | ||
| 23 | } else { | ||
| 24 | Ok(Self { second, subsecond }) | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | #[cfg(feature = "defmt")] | ||
| 30 | impl defmt::Format for RtcInstant { | ||
| 31 | fn format(&self, fmt: defmt::Formatter) { | ||
| 32 | defmt::write!( | ||
| 33 | fmt, | ||
| 34 | "{}:{}", | ||
| 35 | self.second, | ||
| 36 | RTC::regs().prer().read().prediv_s() - self.subsecond, | ||
| 37 | ) | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | #[cfg(feature = "time")] | ||
| 42 | impl core::ops::Sub for RtcInstant { | ||
| 43 | type Output = embassy_time::Duration; | ||
| 44 | |||
| 45 | fn sub(self, rhs: Self) -> Self::Output { | ||
| 46 | let second = if self.second < rhs.second { | ||
| 47 | self.second + 60 | ||
| 48 | } else { | ||
| 49 | self.second | ||
| 50 | }; | ||
| 51 | |||
| 52 | let psc = RTC::regs().prer().read().prediv_s() as u32; | ||
| 53 | |||
| 54 | let self_ticks = second as u32 * (psc + 1) + (psc - self.subsecond as u32); | ||
| 55 | let other_ticks = rhs.second as u32 * (psc + 1) + (psc - rhs.subsecond as u32); | ||
| 56 | let rtc_ticks = self_ticks - other_ticks; | ||
| 57 | |||
| 58 | Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / (psc + 1)) as u64) | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | fn wucksel_compute_min(val: u32) -> (Wucksel, u32) { | 10 | fn wucksel_compute_min(val: u32) -> (Wucksel, u32) { |
| 63 | *[ | 11 | *[ |
| 64 | (Wucksel::DIV2, 2), | 12 | (Wucksel::DIV2, 2), |
| @@ -72,22 +20,15 @@ fn wucksel_compute_min(val: u32) -> (Wucksel, u32) { | |||
| 72 | } | 20 | } |
| 73 | 21 | ||
| 74 | impl Rtc { | 22 | impl Rtc { |
| 75 | /// Return the current instant. | 23 | pub(super) fn calc_epoch(&self) -> DateTime<Utc> { |
| 76 | fn instant(&self) -> Result<RtcInstant, RtcError> { | 24 | let now: NaiveDateTime = RtcTimeProvider::new().now().unwrap().into(); |
| 77 | RtcTimeProvider::new().read(|_, tr, ss| { | ||
| 78 | let second = bcd2_to_byte((tr.st(), tr.su())); | ||
| 79 | 25 | ||
| 80 | RtcInstant::from(second, ss).map_err(RtcError::InvalidDateTime) | 26 | now.and_utc() - TimeDelta::microseconds(Instant::now().as_micros().try_into().unwrap()) |
| 81 | }) | ||
| 82 | } | 27 | } |
| 83 | 28 | ||
| 84 | /// start the wakeup alarm and with a duration that is as close to but less than | 29 | /// start the wakeup alarm and with a duration that is as close to but less than |
| 85 | /// the requested duration, and record the instant the wakeup alarm was started | 30 | /// the requested duration, and record the instant the wakeup alarm was started |
| 86 | pub(crate) fn start_wakeup_alarm( | 31 | pub(crate) fn start_wakeup_alarm(&mut self, requested_duration: embassy_time::Duration) { |
| 87 | &mut self, | ||
| 88 | requested_duration: embassy_time::Duration, | ||
| 89 | cs: critical_section::CriticalSection, | ||
| 90 | ) { | ||
| 91 | // Panic if the rcc mod knows we're not using low-power rtc | 32 | // Panic if the rcc mod knows we're not using low-power rtc |
| 92 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] | 33 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] |
| 93 | unsafe { crate::rcc::get_freqs() }.rtc.to_hertz().unwrap(); | 34 | unsafe { crate::rcc::get_freqs() }.rtc.to_hertz().unwrap(); |
| @@ -122,27 +63,19 @@ impl Rtc { | |||
| 122 | regs.cr().modify(|w| w.set_wutie(true)); | 63 | regs.cr().modify(|w| w.set_wutie(true)); |
| 123 | }); | 64 | }); |
| 124 | 65 | ||
| 125 | let instant = self.instant().unwrap(); | ||
| 126 | trace!( | 66 | trace!( |
| 127 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", | 67 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {})", |
| 128 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), | 68 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), |
| 129 | prescaler as u32, | 69 | prescaler as u32, |
| 130 | rtc_ticks, | 70 | rtc_ticks, |
| 131 | instant, | ||
| 132 | ); | 71 | ); |
| 133 | |||
| 134 | assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none()) | ||
| 135 | } | 72 | } |
| 136 | 73 | ||
| 137 | /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm` | 74 | /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm` |
| 138 | /// was called, otherwise none | 75 | /// was called, otherwise none |
| 139 | pub(crate) fn stop_wakeup_alarm( | 76 | pub(crate) fn stop_wakeup_alarm(&mut self) -> embassy_time::Instant { |
| 140 | &mut self, | ||
| 141 | cs: critical_section::CriticalSection, | ||
| 142 | ) -> Option<embassy_time::Duration> { | ||
| 143 | let instant = self.instant().unwrap(); | ||
| 144 | if RTC::regs().cr().read().wute() { | 77 | if RTC::regs().cr().read().wute() { |
| 145 | trace!("rtc: stop wakeup alarm at {}", instant); | 78 | trace!("rtc: stop wakeup alarm"); |
| 146 | 79 | ||
| 147 | self.write(false, |regs| { | 80 | self.write(false, |regs| { |
| 148 | regs.cr().modify(|w| w.set_wutie(false)); | 81 | regs.cr().modify(|w| w.set_wutie(false)); |
| @@ -166,10 +99,13 @@ impl Rtc { | |||
| 166 | }); | 99 | }); |
| 167 | } | 100 | } |
| 168 | 101 | ||
| 169 | self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time) | 102 | let datetime: NaiveDateTime = RtcTimeProvider::new().now().expect("failed to read now").into(); |
| 103 | let offset = datetime.and_utc() - self.epoch; | ||
| 104 | |||
| 105 | Instant::from_micros(offset.num_microseconds().unwrap().try_into().unwrap()) | ||
| 170 | } | 106 | } |
| 171 | 107 | ||
| 172 | pub(crate) fn enable_wakeup_line(&self) { | 108 | pub(super) fn enable_wakeup_line(&mut self) { |
| 173 | <RTC as crate::rtc::SealedInstance>::WakeupInterrupt::unpend(); | 109 | <RTC as crate::rtc::SealedInstance>::WakeupInterrupt::unpend(); |
| 174 | unsafe { <RTC as crate::rtc::SealedInstance>::WakeupInterrupt::enable() }; | 110 | unsafe { <RTC as crate::rtc::SealedInstance>::WakeupInterrupt::enable() }; |
| 175 | 111 | ||
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index e88bd7ab2..94ba13ae1 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -5,7 +5,7 @@ mod datetime; | |||
| 5 | mod low_power; | 5 | mod low_power; |
| 6 | 6 | ||
| 7 | #[cfg(feature = "low-power")] | 7 | #[cfg(feature = "low-power")] |
| 8 | use core::cell::{Cell, RefCell, RefMut}; | 8 | use core::cell::{RefCell, RefMut}; |
| 9 | #[cfg(feature = "low-power")] | 9 | #[cfg(feature = "low-power")] |
| 10 | use core::ops; | 10 | use core::ops; |
| 11 | 11 | ||
| @@ -163,7 +163,7 @@ impl<'a> ops::DerefMut for RtcBorrow<'a> { | |||
| 163 | /// RTC driver. | 163 | /// RTC driver. |
| 164 | pub struct Rtc { | 164 | pub struct Rtc { |
| 165 | #[cfg(feature = "low-power")] | 165 | #[cfg(feature = "low-power")] |
| 166 | stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<low_power::RtcInstant>>>, | 166 | epoch: chrono::DateTime<chrono::Utc>, |
| 167 | _private: (), | 167 | _private: (), |
| 168 | } | 168 | } |
| 169 | 169 | ||
| @@ -225,7 +225,7 @@ impl Rtc { | |||
| 225 | 225 | ||
| 226 | let mut this = Self { | 226 | let mut this = Self { |
| 227 | #[cfg(feature = "low-power")] | 227 | #[cfg(feature = "low-power")] |
| 228 | stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), | 228 | epoch: chrono::DateTime::from_timestamp_secs(0).unwrap(), |
| 229 | _private: (), | 229 | _private: (), |
| 230 | }; | 230 | }; |
| 231 | 231 | ||
| @@ -243,7 +243,10 @@ impl Rtc { | |||
| 243 | } | 243 | } |
| 244 | 244 | ||
| 245 | #[cfg(feature = "low-power")] | 245 | #[cfg(feature = "low-power")] |
| 246 | this.enable_wakeup_line(); | 246 | { |
| 247 | this.enable_wakeup_line(); | ||
| 248 | this.epoch = this.calc_epoch(); | ||
| 249 | } | ||
| 247 | 250 | ||
| 248 | this | 251 | this |
| 249 | } | 252 | } |
| @@ -293,6 +296,11 @@ impl Rtc { | |||
| 293 | }); | 296 | }); |
| 294 | }); | 297 | }); |
| 295 | 298 | ||
| 299 | #[cfg(feature = "low-power")] | ||
| 300 | { | ||
| 301 | self.epoch = self.calc_epoch(); | ||
| 302 | } | ||
| 303 | |||
| 296 | Ok(()) | 304 | Ok(()) |
| 297 | } | 305 | } |
| 298 | 306 | ||
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 |
