diff options
| author | xoviat <[email protected]> | 2023-09-15 18:41:33 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-09-15 18:41:33 -0500 |
| commit | 6da75ea285482bd950b04ae00dde25303f160705 (patch) | |
| tree | 724ba3dced83647a94aee34f73ddb11bca90b6ae /embassy-stm32/src/rtc | |
| parent | 5a158b94bde3a5fee096be213d2afede06d9f019 (diff) | |
stm32: rtc/low-power cleanup
Diffstat (limited to 'embassy-stm32/src/rtc')
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 34 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 50 |
2 files changed, 44 insertions, 40 deletions
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index a1133a80b..32a5cc123 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -124,9 +124,6 @@ impl Default for RtcCalibrationCyclePeriod { | |||
| 124 | 124 | ||
| 125 | impl Rtc { | 125 | impl Rtc { |
| 126 | pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self { | 126 | pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self { |
| 127 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] | ||
| 128 | use crate::rcc::get_freqs; | ||
| 129 | |||
| 130 | RTC::enable_peripheral_clk(); | 127 | RTC::enable_peripheral_clk(); |
| 131 | BackupDomain::enable_rtc(); | 128 | BackupDomain::enable_rtc(); |
| 132 | 129 | ||
| @@ -135,26 +132,29 @@ impl Rtc { | |||
| 135 | stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), | 132 | stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), |
| 136 | }; | 133 | }; |
| 137 | 134 | ||
| 135 | let frequency = Self::frequency(); | ||
| 136 | let async_psc = ((frequency.0 / rtc_config.frequency.0) - 1) as u8; | ||
| 137 | let sync_psc = (rtc_config.frequency.0 - 1) as u16; | ||
| 138 | |||
| 139 | this.configure(async_psc, sync_psc); | ||
| 140 | |||
| 141 | this | ||
| 142 | } | ||
| 143 | |||
| 144 | fn frequency() -> Hertz { | ||
| 138 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] | 145 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] |
| 139 | let freqs = unsafe { get_freqs() }; | 146 | let freqs = unsafe { crate::rcc::get_freqs() }; |
| 140 | 147 | ||
| 141 | // Load the clock frequency from the rcc mod, if supported | 148 | // Load the clock frequency from the rcc mod, if supported |
| 142 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] | 149 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] |
| 143 | let frequency = match freqs.rtc { | 150 | match freqs.rtc { |
| 144 | Some(hertz) => hertz, | 151 | Some(hertz) => hertz, |
| 145 | None => freqs.rtc_hse.unwrap(), | 152 | None => freqs.rtc_hse.unwrap(), |
| 146 | }; | 153 | } |
| 147 | 154 | ||
| 148 | // Assume the default value, if not supported | 155 | // Assume the default value, if not supported |
| 149 | #[cfg(not(any(rcc_wb, rcc_f4, rcc_f410)))] | 156 | #[cfg(not(any(rcc_wb, rcc_f4, rcc_f410)))] |
| 150 | let frequency = Hertz(32_768); | 157 | Hertz(32_768) |
| 151 | |||
| 152 | let async_psc = ((frequency.0 / rtc_config.frequency.0) - 1) as u8; | ||
| 153 | let sync_psc = (rtc_config.frequency.0 - 1) as u16; | ||
| 154 | |||
| 155 | this.configure(async_psc, sync_psc); | ||
| 156 | |||
| 157 | this | ||
| 158 | } | 158 | } |
| 159 | 159 | ||
| 160 | /// Set the datetime to a new value. | 160 | /// Set the datetime to a new value. |
| @@ -264,6 +264,12 @@ pub(crate) mod sealed { | |||
| 264 | pub trait Instance { | 264 | pub trait Instance { |
| 265 | const BACKUP_REGISTER_COUNT: usize; | 265 | const BACKUP_REGISTER_COUNT: usize; |
| 266 | 266 | ||
| 267 | #[cfg(feature = "low-power")] | ||
| 268 | const EXTI_WAKEUP_LINE: usize; | ||
| 269 | |||
| 270 | #[cfg(feature = "low-power")] | ||
| 271 | type WakeupInterrupt: crate::interrupt::typelevel::Interrupt; | ||
| 272 | |||
| 267 | fn regs() -> Rtc { | 273 | fn regs() -> Rtc { |
| 268 | crate::pac::RTC | 274 | crate::pac::RTC |
| 269 | } | 275 | } |
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index 726886f10..aa3c31ee1 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs | |||
| @@ -67,15 +67,10 @@ impl super::Rtc { | |||
| 67 | pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) { | 67 | pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) { |
| 68 | use embassy_time::{Duration, TICK_HZ}; | 68 | use embassy_time::{Duration, TICK_HZ}; |
| 69 | 69 | ||
| 70 | #[cfg(not(stm32l0))] | 70 | #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] |
| 71 | use crate::rcc::get_freqs; | 71 | unsafe { crate::rcc::get_freqs() }.rtc.unwrap(); |
| 72 | |||
| 73 | #[cfg(not(stm32l0))] | ||
| 74 | let rtc_hz = unsafe { get_freqs() }.rtc.unwrap().0 as u64; | ||
| 75 | |||
| 76 | #[cfg(stm32l0)] | ||
| 77 | let rtc_hz = 32_768u64; | ||
| 78 | 72 | ||
| 73 | let rtc_hz = Self::frequency().0 as u64; | ||
| 79 | let rtc_ticks = requested_duration.as_ticks() * rtc_hz / TICK_HZ; | 74 | let rtc_ticks = requested_duration.as_ticks() * rtc_hz / TICK_HZ; |
| 80 | let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32); | 75 | let prescaler = WakeupPrescaler::compute_min((rtc_ticks / u16::MAX as u64) as u32); |
| 81 | 76 | ||
| @@ -112,17 +107,14 @@ impl super::Rtc { | |||
| 112 | 107 | ||
| 113 | #[cfg(feature = "low-power")] | 108 | #[cfg(feature = "low-power")] |
| 114 | pub(crate) fn enable_wakeup_line(&self) { | 109 | pub(crate) fn enable_wakeup_line(&self) { |
| 110 | use crate::interrupt::typelevel::Interrupt; | ||
| 115 | use crate::pac::EXTI; | 111 | use crate::pac::EXTI; |
| 116 | 112 | ||
| 117 | #[cfg(stm32l0)] | 113 | <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend(); |
| 118 | EXTI.rtsr(0).modify(|w| w.set_line(20, true)); | 114 | unsafe { <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::enable() }; |
| 119 | #[cfg(stm32l0)] | ||
| 120 | EXTI.imr(0).modify(|w| w.set_line(20, true)); | ||
| 121 | 115 | ||
| 122 | #[cfg(not(stm32l0))] | 116 | EXTI.rtsr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); |
| 123 | EXTI.rtsr(0).modify(|w| w.set_line(22, true)); | 117 | EXTI.imr(0).modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); |
| 124 | #[cfg(not(stm32l0))] | ||
| 125 | EXTI.imr(0).modify(|w| w.set_line(22, true)); | ||
| 126 | } | 118 | } |
| 127 | 119 | ||
| 128 | #[cfg(feature = "low-power")] | 120 | #[cfg(feature = "low-power")] |
| @@ -138,17 +130,11 @@ impl super::Rtc { | |||
| 138 | regs.cr().modify(|w| w.set_wute(false)); | 130 | regs.cr().modify(|w| w.set_wute(false)); |
| 139 | regs.isr().modify(|w| w.set_wutf(false)); | 131 | regs.isr().modify(|w| w.set_wutf(false)); |
| 140 | 132 | ||
| 141 | #[cfg(not(stm32l0))] | 133 | crate::pac::EXTI |
| 142 | crate::pac::EXTI.pr(0).modify(|w| w.set_line(22, true)); | 134 | .pr(0) |
| 143 | 135 | .modify(|w| w.set_line(RTC::EXTI_WAKEUP_LINE, true)); | |
| 144 | #[cfg(stm32l0)] | ||
| 145 | crate::pac::EXTI.pr(0).modify(|w| w.set_line(20, true)); | ||
| 146 | |||
| 147 | #[cfg(not(stm32l0))] | ||
| 148 | crate::interrupt::typelevel::RTC_WKUP::unpend(); | ||
| 149 | 136 | ||
| 150 | #[cfg(stm32l0)] | 137 | <RTC as crate::rtc::sealed::Instance>::WakeupInterrupt::unpend(); |
| 151 | crate::interrupt::typelevel::RTC::unpend(); | ||
| 152 | }); | 138 | }); |
| 153 | 139 | ||
| 154 | critical_section::with(|cs| { | 140 | critical_section::with(|cs| { |
| @@ -280,6 +266,18 @@ impl super::Rtc { | |||
| 280 | impl sealed::Instance for crate::peripherals::RTC { | 266 | impl sealed::Instance for crate::peripherals::RTC { |
| 281 | const BACKUP_REGISTER_COUNT: usize = 20; | 267 | const BACKUP_REGISTER_COUNT: usize = 20; |
| 282 | 268 | ||
| 269 | #[cfg(all(feature = "low-power", stm32f4))] | ||
| 270 | const EXTI_WAKEUP_LINE: usize = 22; | ||
| 271 | |||
| 272 | #[cfg(all(feature = "low-power", stm32l0))] | ||
| 273 | const EXTI_WAKEUP_LINE: usize = 20; | ||
| 274 | |||
| 275 | #[cfg(all(feature = "low-power", stm32f4))] | ||
| 276 | type WakeupInterrupt = crate::interrupt::typelevel::RTC_WKUP; | ||
| 277 | |||
| 278 | #[cfg(all(feature = "low-power", stm32l0))] | ||
| 279 | type WakeupInterrupt = crate::interrupt::typelevel::RTC; | ||
| 280 | |||
| 283 | fn enable_peripheral_clk() { | 281 | fn enable_peripheral_clk() { |
| 284 | #[cfg(any(rtc_v2l4, rtc_v2wb))] | 282 | #[cfg(any(rtc_v2l4, rtc_v2wb))] |
| 285 | { | 283 | { |
