diff options
| author | xoviat <[email protected]> | 2023-11-08 19:04:20 -0600 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-11-08 19:04:20 -0600 |
| commit | 4b4c28d8752aab0a96fc244f9eaabe256e7dbeb2 (patch) | |
| tree | ac83d2715dfa266ef1f965a606daff03981bffb4 /embassy-stm32/src/rtc | |
| parent | 3bccb672316415d54baf306a1084003b8796e94a (diff) | |
stm32: add low power for g4
Diffstat (limited to 'embassy-stm32/src/rtc')
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 162 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 138 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v3.rs | 8 |
3 files changed, 169 insertions, 139 deletions
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index e94a58575..3f03d83a4 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -33,6 +33,60 @@ use embassy_hal_internal::Peripheral; | |||
| 33 | use crate::peripherals::RTC; | 33 | use crate::peripherals::RTC; |
| 34 | use crate::rtc::sealed::Instance; | 34 | use crate::rtc::sealed::Instance; |
| 35 | 35 | ||
| 36 | #[repr(u8)] | ||
| 37 | #[derive(Clone, Copy, Debug)] | ||
| 38 | pub(crate) enum WakeupPrescaler { | ||
| 39 | Div2 = 2, | ||
| 40 | Div4 = 4, | ||
| 41 | Div8 = 8, | ||
| 42 | Div16 = 16, | ||
| 43 | } | ||
| 44 | |||
| 45 | #[cfg(any(stm32wb, stm32f4, stm32l0, stm32g4))] | ||
| 46 | impl From<WakeupPrescaler> for crate::pac::rtc::vals::Wucksel { | ||
| 47 | fn from(val: WakeupPrescaler) -> Self { | ||
| 48 | use crate::pac::rtc::vals::Wucksel; | ||
| 49 | |||
| 50 | match val { | ||
| 51 | WakeupPrescaler::Div2 => Wucksel::DIV2, | ||
| 52 | WakeupPrescaler::Div4 => Wucksel::DIV4, | ||
| 53 | WakeupPrescaler::Div8 => Wucksel::DIV8, | ||
| 54 | WakeupPrescaler::Div16 => Wucksel::DIV16, | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | #[cfg(any(stm32wb, stm32f4, stm32l0, stm32g4))] | ||
| 60 | impl From<crate::pac::rtc::vals::Wucksel> for WakeupPrescaler { | ||
| 61 | fn from(val: crate::pac::rtc::vals::Wucksel) -> Self { | ||
| 62 | use crate::pac::rtc::vals::Wucksel; | ||
| 63 | |||
| 64 | match val { | ||
| 65 | Wucksel::DIV2 => WakeupPrescaler::Div2, | ||
| 66 | Wucksel::DIV4 => WakeupPrescaler::Div4, | ||
| 67 | Wucksel::DIV8 => WakeupPrescaler::Div8, | ||
| 68 | Wucksel::DIV16 => WakeupPrescaler::Div16, | ||
| 69 | _ => unreachable!(), | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | #[cfg(feature = "low-power")] | ||
| 75 | impl WakeupPrescaler { | ||
| 76 | pub fn compute_min(val: u32) -> Self { | ||
| 77 | *[ | ||
| 78 | WakeupPrescaler::Div2, | ||
| 79 | WakeupPrescaler::Div4, | ||
| 80 | WakeupPrescaler::Div8, | ||
| 81 | WakeupPrescaler::Div16, | ||
| 82 | ] | ||
| 83 | .iter() | ||
| 84 | .skip_while(|psc| **psc as u32 <= val) | ||
| 85 | .next() | ||
| 86 | .unwrap_or(&WakeupPrescaler::Div16) | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 36 | /// Errors that can occur on methods on [RtcClock] | 90 | /// Errors that can occur on methods on [RtcClock] |
| 37 | #[non_exhaustive] | 91 | #[non_exhaustive] |
| 38 | #[derive(Clone, Debug, PartialEq, Eq)] | 92 | #[derive(Clone, Debug, PartialEq, Eq)] |
| @@ -277,6 +331,114 @@ impl Rtc { | |||
| 277 | pub fn write_backup_register(&self, register: usize, value: u32) { | 331 | pub fn write_backup_register(&self, register: usize, value: u32) { |
| 278 | RTC::write_backup_register(&RTC::regs(), register, value) | 332 | RTC::write_backup_register(&RTC::regs(), register, value) |
| 279 | } | 333 | } |
| 334 | |||
| 335 | #[cfg(feature = "low-power")] | ||
| 336 | /// start the wakeup alarm and wtih a duration that is as close to but less than | ||
| 337 | /// the requested duration, and record the instant the wakeup alarm was started | ||
| 338 | pub(crate) fn start_wakeup_alarm( | ||
| 339 | &self, | ||
| 340 | requested_duration: embassy_time::Duration, | ||
| 341 | cs: critical_section::CriticalSection, | ||
| 342 | ) { | ||
| 343 | use embassy_time::{Duration, TICK_HZ}; | ||
| 344 | |||
| 345 | #[cfg(any(rtc_v3, rtc_v3u5))] | ||
| 346 | use crate::pac::rtc::vals::Calrf; | ||
| 347 | |||
| 348 | // Panic if the rcc mod knows we're not using low-power rtc | ||
| 349 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] | ||
| 350 | unsafe { crate::rcc::get_freqs() }.rtc.unwrap(); | ||
| 351 | |||
| 352 | let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64); | ||
| 353 | let rtc_hz = Self::frequency().0 as u64; | ||
| 354 | let rtc_ticks = requested_duration * rtc_hz / TICK_HZ; | ||
| 355 | let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32); | ||
| 356 | |||
| 357 | // adjust the rtc ticks to the prescaler and subtract one rtc tick | ||
| 358 | let rtc_ticks = rtc_ticks / prescaler as u64; | ||
| 359 | let rtc_ticks = rtc_ticks.clamp(0, (u16::MAX - 1) as u64).saturating_sub(1) as u16; | ||
| 360 | |||
| 361 | self.write(false, |regs| { | ||
| 362 | regs.cr().modify(|w| w.set_wute(false)); | ||
| 363 | |||
| 364 | #[cfg(any( | ||
| 365 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb | ||
| 366 | ))] | ||
| 367 | { | ||
| 368 | regs.isr().modify(|w| w.set_wutf(false)); | ||
| 369 | while !regs.isr().read().wutwf() {} | ||
| 370 | } | ||
| 371 | |||
| 372 | #[cfg(any(rtc_v3, rtc_v3u5))] | ||
| 373 | { | ||
| 374 | regs.scr().write(|w| w.set_cwutf(Calrf::CLEAR)); | ||
| 375 | while !regs.icsr().read().wutwf() {} | ||
| 376 | } | ||
| 377 | |||
| 378 | regs.cr().modify(|w| w.set_wucksel(prescaler.into())); | ||
| 379 | regs.wutr().write(|w| w.set_wut(rtc_ticks)); | ||
| 380 | regs.cr().modify(|w| w.set_wute(true)); | ||
| 381 | regs.cr().modify(|w| w.set_wutie(true)); | ||
| 382 | }); | ||
| 383 | |||
| 384 | let instant = self.instant().unwrap(); | ||
| 385 | trace!( | ||
| 386 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", | ||
| 387 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), | ||
| 388 | prescaler as u32, | ||
| 389 | rtc_ticks, | ||
| 390 | instant, | ||
| 391 | ); | ||
| 392 | |||
| 393 | assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none()) | ||
| 394 | } | ||
| 395 | |||
| 396 | #[cfg(feature = "low-power")] | ||
| 397 | /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm` | ||
| 398 | /// was called, otherwise none | ||
| 399 | pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option<embassy_time::Duration> { | ||
| 400 | use crate::interrupt::typelevel::Interrupt; | ||
| 401 | #[cfg(any(rtc_v3, rtc_v3u5))] | ||
| 402 | use crate::pac::rtc::vals::Calrf; | ||
| 403 | |||
| 404 | let instant = self.instant().unwrap(); | ||
| 405 | if RTC::regs().cr().read().wute() { | ||
| 406 | trace!("rtc: stop wakeup alarm at {}", instant); | ||
| 407 | |||
| 408 | self.write(false, |regs| { | ||
| 409 | regs.cr().modify(|w| w.set_wutie(false)); | ||
| 410 | regs.cr().modify(|w| w.set_wute(false)); | ||
| 411 | |||
| 412 | #[cfg(any( | ||
| 413 | rtc_v2f0, rtc_v2f2, rtc_v2f3, rtc_v2f4, rtc_v2f7, rtc_v2h7, rtc_v2l0, rtc_v2l1, rtc_v2l4, rtc_v2wb | ||
| 414 | ))] | ||
| 415 | regs.isr().modify(|w| w.set_wutf(false)); | ||
| 416 | |||
| 417 | #[cfg(any(rtc_v3, rtc_v3u5))] | ||
| 418 | regs.scr().write(|w| w.set_cwutf(Calrf::CLEAR)); | ||
| 419 | |||
| 420 | crate::pac::EXTI | ||
| 421 | .pr(0) | ||
| 422 | .modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | ||
| 423 | |||
| 424 | <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend(); | ||
| 425 | }); | ||
| 426 | } | ||
| 427 | |||
| 428 | self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time) | ||
| 429 | } | ||
| 430 | |||
| 431 | #[cfg(feature = "low-power")] | ||
| 432 | pub(crate) fn enable_wakeup_line(&self) { | ||
| 433 | use crate::interrupt::typelevel::Interrupt; | ||
| 434 | use crate::pac::EXTI; | ||
| 435 | |||
| 436 | <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend(); | ||
| 437 | unsafe { <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::enable() }; | ||
| 438 | |||
| 439 | EXTI.rtsr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | ||
| 440 | EXTI.imr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | ||
| 441 | } | ||
| 280 | } | 442 | } |
| 281 | 443 | ||
| 282 | pub(crate) fn byte_to_bcd2(byte: u8) -> (u8, u8) { | 444 | pub(crate) fn byte_to_bcd2(byte: u8) -> (u8, u8) { |
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index 006fd63f4..91f08fae4 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs | |||
| @@ -6,145 +6,7 @@ use crate::peripherals::RTC; | |||
| 6 | use crate::rtc::sealed::Instance; | 6 | use crate::rtc::sealed::Instance; |
| 7 | 7 | ||
| 8 | #[allow(dead_code)] | 8 | #[allow(dead_code)] |
| 9 | #[repr(u8)] | ||
| 10 | #[derive(Clone, Copy, Debug)] | ||
| 11 | pub(crate) enum WakeupPrescaler { | ||
| 12 | Div2 = 2, | ||
| 13 | Div4 = 4, | ||
| 14 | Div8 = 8, | ||
| 15 | Div16 = 16, | ||
| 16 | } | ||
| 17 | |||
| 18 | #[cfg(any(stm32wb, stm32f4, stm32l0))] | ||
| 19 | impl From<WakeupPrescaler> for crate::pac::rtc::vals::Wucksel { | ||
| 20 | fn from(val: WakeupPrescaler) -> Self { | ||
| 21 | use crate::pac::rtc::vals::Wucksel; | ||
| 22 | |||
| 23 | match val { | ||
| 24 | WakeupPrescaler::Div2 => Wucksel::DIV2, | ||
| 25 | WakeupPrescaler::Div4 => Wucksel::DIV4, | ||
| 26 | WakeupPrescaler::Div8 => Wucksel::DIV8, | ||
| 27 | WakeupPrescaler::Div16 => Wucksel::DIV16, | ||
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | #[cfg(any(stm32wb, stm32f4, stm32l0))] | ||
| 33 | impl From<crate::pac::rtc::vals::Wucksel> for WakeupPrescaler { | ||
| 34 | fn from(val: crate::pac::rtc::vals::Wucksel) -> Self { | ||
| 35 | use crate::pac::rtc::vals::Wucksel; | ||
| 36 | |||
| 37 | match val { | ||
| 38 | Wucksel::DIV2 => WakeupPrescaler::Div2, | ||
| 39 | Wucksel::DIV4 => WakeupPrescaler::Div4, | ||
| 40 | Wucksel::DIV8 => WakeupPrescaler::Div8, | ||
| 41 | Wucksel::DIV16 => WakeupPrescaler::Div16, | ||
| 42 | _ => unreachable!(), | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | #[allow(dead_code)] | ||
| 48 | impl WakeupPrescaler { | ||
| 49 | pub fn compute_min(val: u32) -> Self { | ||
| 50 | *[ | ||
| 51 | WakeupPrescaler::Div2, | ||
| 52 | WakeupPrescaler::Div4, | ||
| 53 | WakeupPrescaler::Div8, | ||
| 54 | WakeupPrescaler::Div16, | ||
| 55 | ] | ||
| 56 | .iter() | ||
| 57 | .skip_while(|psc| **psc as u32 <= val) | ||
| 58 | .next() | ||
| 59 | .unwrap_or(&WakeupPrescaler::Div16) | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | impl super::Rtc { | 9 | impl super::Rtc { |
| 64 | #[cfg(feature = "low-power")] | ||
| 65 | /// start the wakeup alarm and wtih a duration that is as close to but less than | ||
| 66 | /// the requested duration, and record the instant the wakeup alarm was started | ||
| 67 | pub(crate) fn start_wakeup_alarm( | ||
| 68 | &self, | ||
| 69 | requested_duration: embassy_time::Duration, | ||
| 70 | cs: critical_section::CriticalSection, | ||
| 71 | ) { | ||
| 72 | use embassy_time::{Duration, TICK_HZ}; | ||
| 73 | |||
| 74 | // Panic if the rcc mod knows we're not using low-power rtc | ||
| 75 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] | ||
| 76 | unsafe { crate::rcc::get_freqs() }.rtc.unwrap(); | ||
| 77 | |||
| 78 | let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64); | ||
| 79 | let rtc_hz = Self::frequency().0 as u64; | ||
| 80 | let rtc_ticks = requested_duration * rtc_hz / TICK_HZ; | ||
| 81 | let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32); | ||
| 82 | |||
| 83 | // adjust the rtc ticks to the prescaler and subtract one rtc tick | ||
| 84 | let rtc_ticks = rtc_ticks / prescaler as u64; | ||
| 85 | let rtc_ticks = rtc_ticks.clamp(0, (u16::MAX - 1) as u64).saturating_sub(1) as u16; | ||
| 86 | |||
| 87 | self.write(false, |regs| { | ||
| 88 | regs.cr().modify(|w| w.set_wute(false)); | ||
| 89 | regs.isr().modify(|w| w.set_wutf(false)); | ||
| 90 | while !regs.isr().read().wutwf() {} | ||
| 91 | |||
| 92 | regs.cr().modify(|w| w.set_wucksel(prescaler.into())); | ||
| 93 | regs.wutr().write(|w| w.set_wut(rtc_ticks)); | ||
| 94 | regs.cr().modify(|w| w.set_wute(true)); | ||
| 95 | regs.cr().modify(|w| w.set_wutie(true)); | ||
| 96 | }); | ||
| 97 | |||
| 98 | let instant = self.instant().unwrap(); | ||
| 99 | trace!( | ||
| 100 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", | ||
| 101 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), | ||
| 102 | prescaler as u32, | ||
| 103 | rtc_ticks, | ||
| 104 | instant, | ||
| 105 | ); | ||
| 106 | |||
| 107 | assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none()) | ||
| 108 | } | ||
| 109 | |||
| 110 | #[cfg(feature = "low-power")] | ||
| 111 | /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm` | ||
| 112 | /// was called, otherwise none | ||
| 113 | pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option<embassy_time::Duration> { | ||
| 114 | use crate::interrupt::typelevel::Interrupt; | ||
| 115 | |||
| 116 | let instant = self.instant().unwrap(); | ||
| 117 | if RTC::regs().cr().read().wute() { | ||
| 118 | trace!("rtc: stop wakeup alarm at {}", instant); | ||
| 119 | |||
| 120 | self.write(false, |regs| { | ||
| 121 | regs.cr().modify(|w| w.set_wutie(false)); | ||
| 122 | regs.cr().modify(|w| w.set_wute(false)); | ||
| 123 | regs.isr().modify(|w| w.set_wutf(false)); | ||
| 124 | |||
| 125 | crate::pac::EXTI | ||
| 126 | .pr(0) | ||
| 127 | .modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | ||
| 128 | |||
| 129 | <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend(); | ||
| 130 | }); | ||
| 131 | } | ||
| 132 | |||
| 133 | self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time) | ||
| 134 | } | ||
| 135 | |||
| 136 | #[cfg(feature = "low-power")] | ||
| 137 | pub(crate) fn enable_wakeup_line(&self) { | ||
| 138 | use crate::interrupt::typelevel::Interrupt; | ||
| 139 | use crate::pac::EXTI; | ||
| 140 | |||
| 141 | <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend(); | ||
| 142 | unsafe { <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::enable() }; | ||
| 143 | |||
| 144 | EXTI.rtsr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | ||
| 145 | EXTI.imr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | ||
| 146 | } | ||
| 147 | |||
| 148 | /// Applies the RTC config | 10 | /// Applies the RTC config |
| 149 | /// It this changes the RTC clock source the time will be reset | 11 | /// It this changes the RTC clock source the time will be reset |
| 150 | pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) { | 12 | pub(super) fn configure(&mut self, async_psc: u8, sync_psc: u16) { |
diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs index 7bf757e7d..d2d0d9309 100644 --- a/embassy-stm32/src/rtc/v3.rs +++ b/embassy-stm32/src/rtc/v3.rs | |||
| @@ -95,7 +95,7 @@ impl super::Rtc { | |||
| 95 | }) | 95 | }) |
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | pub(super) fn write<F, R>(&mut self, init_mode: bool, f: F) -> R | 98 | pub(super) fn write<F, R>(&self, init_mode: bool, f: F) -> R |
| 99 | where | 99 | where |
| 100 | F: FnOnce(&crate::pac::rtc::Rtc) -> R, | 100 | F: FnOnce(&crate::pac::rtc::Rtc) -> R, |
| 101 | { | 101 | { |
| @@ -129,6 +129,12 @@ impl super::Rtc { | |||
| 129 | impl sealed::Instance for crate::peripherals::RTC { | 129 | impl sealed::Instance for crate::peripherals::RTC { |
| 130 | const BACKUP_REGISTER_COUNT: usize = 32; | 130 | const BACKUP_REGISTER_COUNT: usize = 32; |
| 131 | 131 | ||
| 132 | #[cfg(all(feature = "low-power", stm32g4))] | ||
| 133 | const EXTI_WAKEUP_LINE: usize = 20; | ||
| 134 | |||
| 135 | #[cfg(all(feature = "low-power", stm32g4))] | ||
| 136 | type WakeupInterrupt = crate::interrupt::typelevel::RTC_WKUP; | ||
| 137 | |||
| 132 | fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { | 138 | fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { |
| 133 | #[allow(clippy::if_same_then_else)] | 139 | #[allow(clippy::if_same_then_else)] |
| 134 | if register < Self::BACKUP_REGISTER_COUNT { | 140 | if register < Self::BACKUP_REGISTER_COUNT { |
