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 | |
| parent | 2897670f2438525bc128cf016ee8f8a948edfbb7 (diff) | |
stm32: complete stop impl.
Diffstat (limited to 'embassy-stm32/src/rtc')
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 79 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 119 |
2 files changed, 100 insertions, 98 deletions
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index a6102077a..1f1abb78d 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -1,6 +1,14 @@ | |||
| 1 | //! RTC peripheral abstraction | 1 | //! RTC peripheral abstraction |
| 2 | mod datetime; | 2 | mod datetime; |
| 3 | 3 | ||
| 4 | #[cfg(feature = "low-power")] | ||
| 5 | use core::cell::Cell; | ||
| 6 | |||
| 7 | #[cfg(feature = "low-power")] | ||
| 8 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 9 | #[cfg(feature = "low-power")] | ||
| 10 | use embassy_sync::blocking_mutex::Mutex; | ||
| 11 | |||
| 4 | pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; | 12 | pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; |
| 5 | 13 | ||
| 6 | /// refer to AN4759 to compare features of RTC2 and RTC3 | 14 | /// refer to AN4759 to compare features of RTC2 and RTC3 |
| @@ -30,9 +38,73 @@ pub enum RtcError { | |||
| 30 | NotRunning, | 38 | NotRunning, |
| 31 | } | 39 | } |
| 32 | 40 | ||
| 41 | #[cfg(feature = "low-power")] | ||
| 42 | /// Represents an instant in time that can be substracted to compute a duration | ||
| 43 | struct RtcInstant { | ||
| 44 | second: u8, | ||
| 45 | subsecond: u16, | ||
| 46 | } | ||
| 47 | |||
| 48 | #[cfg(feature = "low-power")] | ||
| 49 | impl RtcInstant { | ||
| 50 | pub fn now() -> Self { | ||
| 51 | let tr = RTC::regs().tr().read(); | ||
| 52 | let tr2 = RTC::regs().tr().read(); | ||
| 53 | let ssr = RTC::regs().ssr().read().ss(); | ||
| 54 | let ssr2 = RTC::regs().ssr().read().ss(); | ||
| 55 | |||
| 56 | let st = bcd2_to_byte((tr.st(), tr.su())); | ||
| 57 | let st2 = bcd2_to_byte((tr2.st(), tr2.su())); | ||
| 58 | |||
| 59 | assert!(st == st2); | ||
| 60 | assert!(ssr == ssr2); | ||
| 61 | |||
| 62 | let _ = RTC::regs().dr().read(); | ||
| 63 | |||
| 64 | let subsecond = ssr; | ||
| 65 | let second = st; | ||
| 66 | |||
| 67 | // trace!("rtc: instant now: st, ssr: {}, {}", st, ssr); | ||
| 68 | |||
| 69 | Self { second, subsecond } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | #[cfg(feature = "low-power")] | ||
| 74 | impl core::ops::Sub for RtcInstant { | ||
| 75 | type Output = embassy_time::Duration; | ||
| 76 | |||
| 77 | fn sub(self, rhs: Self) -> Self::Output { | ||
| 78 | use embassy_time::{Duration, TICK_HZ}; | ||
| 79 | |||
| 80 | let second = if self.second < rhs.second { | ||
| 81 | self.second + 60 | ||
| 82 | } else { | ||
| 83 | self.second | ||
| 84 | }; | ||
| 85 | |||
| 86 | // TODO: read prescaler | ||
| 87 | |||
| 88 | let self_ticks = second as u32 * 256 + (255 - self.subsecond as u32); | ||
| 89 | let other_ticks = rhs.second as u32 * 256 + (255 - rhs.subsecond as u32); | ||
| 90 | let rtc_ticks = self_ticks - other_ticks; | ||
| 91 | |||
| 92 | // trace!( | ||
| 93 | // "rtc: instant sub: self, other, rtc ticks: {}, {}, {}", | ||
| 94 | // self_ticks, | ||
| 95 | // other_ticks, | ||
| 96 | // rtc_ticks | ||
| 97 | // ); | ||
| 98 | |||
| 99 | Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / 256u32) as u64) | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 33 | /// RTC Abstraction | 103 | /// RTC Abstraction |
| 34 | pub struct Rtc { | 104 | pub struct Rtc { |
| 35 | rtc_config: RtcConfig, | 105 | rtc_config: RtcConfig, |
| 106 | #[cfg(feature = "low-power")] | ||
| 107 | stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<RtcInstant>>>, | ||
| 36 | } | 108 | } |
| 37 | 109 | ||
| 38 | #[derive(Copy, Clone, Debug, PartialEq)] | 110 | #[derive(Copy, Clone, Debug, PartialEq)] |
| @@ -108,8 +180,15 @@ impl Rtc { | |||
| 108 | pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self { | 180 | pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self { |
| 109 | RTC::enable_peripheral_clk(); | 181 | RTC::enable_peripheral_clk(); |
| 110 | 182 | ||
| 183 | #[cfg(not(feature = "low-power"))] | ||
| 111 | let mut rtc_struct = Self { rtc_config }; | 184 | let mut rtc_struct = Self { rtc_config }; |
| 112 | 185 | ||
| 186 | #[cfg(feature = "low-power")] | ||
| 187 | let mut rtc_struct = Self { | ||
| 188 | rtc_config, | ||
| 189 | stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), | ||
| 190 | }; | ||
| 191 | |||
| 113 | Self::enable(); | 192 | Self::enable(); |
| 114 | 193 | ||
| 115 | rtc_struct.configure(rtc_config); | 194 | rtc_struct.configure(rtc_config); |
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)] |
