diff options
| author | xoviat <[email protected]> | 2023-08-26 20:31:12 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-08-26 20:31:12 -0500 |
| commit | 1e430f74133acaeb31cb689ded3da77cb7b1c0ca (patch) | |
| tree | df2b4feb4919acb9e365ba6433ad361618ec2fbc /embassy-stm32/src/rtc/v2.rs | |
| parent | 2897670f2438525bc128cf016ee8f8a948edfbb7 (diff) | |
stm32: complete stop impl.
Diffstat (limited to 'embassy-stm32/src/rtc/v2.rs')
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 119 |
1 files changed, 21 insertions, 98 deletions
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index 525dc45a2..d73e7083c 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs | |||
| @@ -1,71 +1,12 @@ | |||
| 1 | use stm32_metapac::rtc::vals::{Init, Osel, Pol}; | 1 | use stm32_metapac::rtc::vals::{Init, Osel, Pol}; |
| 2 | 2 | ||
| 3 | #[cfg(feature = "low-power")] | ||
| 4 | use super::RtcInstant; | ||
| 3 | use super::{sealed, RtcClockSource, RtcConfig}; | 5 | use super::{sealed, RtcClockSource, RtcConfig}; |
| 4 | use crate::pac::rtc::Rtc; | 6 | use crate::pac::rtc::Rtc; |
| 5 | use crate::peripherals::RTC; | 7 | use crate::peripherals::RTC; |
| 6 | use crate::rtc::sealed::Instance; | 8 | use crate::rtc::sealed::Instance; |
| 7 | 9 | ||
| 8 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] | ||
| 9 | pub struct RtcInstant { | ||
| 10 | ssr: u16, | ||
| 11 | st: u8, | ||
| 12 | } | ||
| 13 | |||
| 14 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] | ||
| 15 | impl RtcInstant { | ||
| 16 | pub fn now() -> Self { | ||
| 17 | // TODO: read value twice | ||
| 18 | use crate::rtc::bcd2_to_byte; | ||
| 19 | |||
| 20 | let tr = RTC::regs().tr().read(); | ||
| 21 | let tr2 = RTC::regs().tr().read(); | ||
| 22 | let ssr = RTC::regs().ssr().read().ss(); | ||
| 23 | let ssr2 = RTC::regs().ssr().read().ss(); | ||
| 24 | |||
| 25 | let st = bcd2_to_byte((tr.st(), tr.su())); | ||
| 26 | let st2 = bcd2_to_byte((tr2.st(), tr2.su())); | ||
| 27 | |||
| 28 | assert!(st == st2); | ||
| 29 | assert!(ssr == ssr2); | ||
| 30 | |||
| 31 | let _ = RTC::regs().dr().read(); | ||
| 32 | |||
| 33 | trace!("rtc: instant now: st, ssr: {}, {}", st, ssr); | ||
| 34 | |||
| 35 | Self { ssr, st } | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] | ||
| 40 | impl core::ops::Sub for RtcInstant { | ||
| 41 | type Output = embassy_time::Duration; | ||
| 42 | |||
| 43 | fn sub(self, rhs: Self) -> Self::Output { | ||
| 44 | use embassy_time::{Duration, TICK_HZ}; | ||
| 45 | |||
| 46 | let st = if self.st < rhs.st { self.st + 60 } else { self.st }; | ||
| 47 | |||
| 48 | // TODO: read prescaler | ||
| 49 | |||
| 50 | let self_ticks = st as u32 * 256 + (255 - self.ssr as u32); | ||
| 51 | let other_ticks = rhs.st as u32 * 256 + (255 - rhs.ssr as u32); | ||
| 52 | let rtc_ticks = self_ticks - other_ticks; | ||
| 53 | |||
| 54 | trace!( | ||
| 55 | "rtc: instant sub: self, other, rtc ticks: {}, {}, {}", | ||
| 56 | self_ticks, | ||
| 57 | other_ticks, | ||
| 58 | rtc_ticks | ||
| 59 | ); | ||
| 60 | |||
| 61 | Duration::from_ticks( | ||
| 62 | ((((st as u32 * 256 + (255u32 - self.ssr as u32)) - (rhs.st as u32 * 256 + (255u32 - rhs.ssr as u32))) | ||
| 63 | * TICK_HZ as u32) as u32 | ||
| 64 | / 256u32) as u64, | ||
| 65 | ) | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | #[allow(dead_code)] | 10 | #[allow(dead_code)] |
| 70 | #[derive(Clone, Copy, Debug)] | 11 | #[derive(Clone, Copy, Debug)] |
| 71 | pub(crate) enum WakeupPrescaler { | 12 | pub(crate) enum WakeupPrescaler { |
| @@ -165,16 +106,11 @@ impl super::Rtc { | |||
| 165 | 106 | ||
| 166 | #[allow(dead_code)] | 107 | #[allow(dead_code)] |
| 167 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] | 108 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] |
| 168 | /// start the wakeup alarm and return the actual duration of the alarm | 109 | /// start the wakeup alarm and wtih a duration that is as close to but less than |
| 169 | /// the actual duration will be the closest value possible that is less | 110 | /// the requested duration, and record the instant the wakeup alarm was started |
| 170 | /// than the requested duration. | 111 | pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) { |
| 171 | /// | ||
| 172 | /// note: this api is exposed for testing purposes until low power is implemented. | ||
| 173 | /// it is not intended to be public | ||
| 174 | pub(crate) fn start_wakeup_alarm(&self, requested_duration: embassy_time::Duration) -> RtcInstant { | ||
| 175 | use embassy_time::{Duration, TICK_HZ}; | 112 | use embassy_time::{Duration, TICK_HZ}; |
| 176 | 113 | ||
| 177 | use crate::interrupt::typelevel::Interrupt; | ||
| 178 | use crate::rcc::get_freqs; | 114 | use crate::rcc::get_freqs; |
| 179 | 115 | ||
| 180 | let rtc_hz = unsafe { get_freqs() }.rtc.unwrap().0 as u64; | 116 | let rtc_hz = unsafe { get_freqs() }.rtc.unwrap().0 as u64; |
| @@ -206,47 +142,34 @@ impl super::Rtc { | |||
| 206 | 142 | ||
| 207 | trace!("rtc: start wakeup alarm for {} ms", duration.as_millis()); | 143 | trace!("rtc: start wakeup alarm for {} ms", duration.as_millis()); |
| 208 | 144 | ||
| 209 | // self.write(false, |regs| { | 145 | critical_section::with(|cs| assert!(self.stop_time.borrow(cs).replace(Some(RtcInstant::now())).is_none())) |
| 210 | // regs.cr().modify(|w| w.set_wutie(false)); | ||
| 211 | // | ||
| 212 | // regs.isr().modify(|w| w.set_wutf(false)); | ||
| 213 | // | ||
| 214 | // regs.cr().modify(|w| w.set_wutie(true)); | ||
| 215 | // }); | ||
| 216 | // | ||
| 217 | // trace!("rtc: clear wuf..."); | ||
| 218 | // crate::pac::PWR.cr1().modify(|w| w.set_cwuf(true)); | ||
| 219 | // while crate::pac::PWR.csr1().read().wuf() {} | ||
| 220 | // trace!("rtc: clear wuf...done"); | ||
| 221 | // | ||
| 222 | // crate::pac::PWR | ||
| 223 | // .cr1() | ||
| 224 | // .modify(|w| w.set_pdds(crate::pac::pwr::vals::Pdds::STANDBY_MODE)); | ||
| 225 | // | ||
| 226 | // // crate::pac::PWR.cr1().modify(|w| w.set_lpds(true)); | ||
| 227 | // | ||
| 228 | // // crate::pac::EXTI.pr(0).modify(|w| w.set_line(22, true)); | ||
| 229 | // crate::interrupt::typelevel::RTC_WKUP::unpend(); | ||
| 230 | |||
| 231 | RtcInstant::now() | ||
| 232 | } | 146 | } |
| 233 | 147 | ||
| 234 | #[allow(dead_code)] | 148 | #[allow(dead_code)] |
| 235 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] | 149 | #[cfg(all(feature = "time", any(stm32wb, stm32f4)))] |
| 236 | /// stop the wakeup alarm and return the time remaining | 150 | /// stop the wakeup alarm and return the time elapsed since `start_wakeup_alarm` |
| 237 | /// | 151 | /// was called, otherwise none |
| 238 | /// note: this api is exposed for testing purposes until low power is implemented. | 152 | pub(crate) fn stop_wakeup_alarm(&self) -> Option<embassy_time::Duration> { |
| 239 | /// it is not intended to be public | 153 | use crate::interrupt::typelevel::Interrupt; |
| 240 | pub(crate) fn stop_wakeup_alarm(&self) -> RtcInstant { | 154 | |
| 241 | trace!("rtc: stop wakeup alarm..."); | 155 | trace!("rtc: stop wakeup alarm..."); |
| 242 | 156 | ||
| 243 | self.write(false, |regs| { | 157 | self.write(false, |regs| { |
| 244 | regs.cr().modify(|w| w.set_wutie(false)); | 158 | regs.cr().modify(|w| w.set_wutie(false)); |
| 245 | regs.cr().modify(|w| w.set_wute(false)); | 159 | regs.cr().modify(|w| w.set_wute(false)); |
| 246 | regs.isr().modify(|w| w.set_wutf(false)); | 160 | regs.isr().modify(|w| w.set_wutf(false)); |
| 161 | |||
| 162 | crate::pac::EXTI.pr(0).modify(|w| w.set_line(22, true)); | ||
| 163 | crate::interrupt::typelevel::RTC_WKUP::unpend(); | ||
| 247 | }); | 164 | }); |
| 248 | 165 | ||
| 249 | RtcInstant::now() | 166 | critical_section::with(|cs| { |
| 167 | if let Some(stop_time) = self.stop_time.borrow(cs).take() { | ||
| 168 | Some(RtcInstant::now() - stop_time) | ||
| 169 | } else { | ||
| 170 | None | ||
| 171 | } | ||
| 172 | }) | ||
| 250 | } | 173 | } |
| 251 | 174 | ||
| 252 | #[allow(dead_code)] | 175 | #[allow(dead_code)] |
