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/v2.rs | |
| parent | 3bccb672316415d54baf306a1084003b8796e94a (diff) | |
stm32: add low power for g4
Diffstat (limited to 'embassy-stm32/src/rtc/v2.rs')
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 138 |
1 files changed, 0 insertions, 138 deletions
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) { |
