diff options
| author | xoviat <[email protected]> | 2023-09-03 11:40:34 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-09-03 11:40:34 -0500 |
| commit | 2e6f4237f2410aa18c9866a5a1a5ed1f3bec8a4e (patch) | |
| tree | 763777fc48d9f1450316b239dea5ac4fbd27f6f6 /embassy-stm32/src/rtc/v2.rs | |
| parent | ac11635b0b97b7013ef280f7e978c13e86a132b8 (diff) | |
stm32: cleanup psc design
Diffstat (limited to 'embassy-stm32/src/rtc/v2.rs')
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 31 |
1 files changed, 9 insertions, 22 deletions
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index 482b6e75d..62d8d4f9c 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs | |||
| @@ -6,12 +6,13 @@ 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)] | ||
| 9 | #[derive(Clone, Copy, Debug)] | 10 | #[derive(Clone, Copy, Debug)] |
| 10 | pub(crate) enum WakeupPrescaler { | 11 | pub(crate) enum WakeupPrescaler { |
| 11 | Div2, | 12 | Div2 = 2, |
| 12 | Div4, | 13 | Div4 = 4, |
| 13 | Div8, | 14 | Div8 = 8, |
| 14 | Div16, | 15 | Div16 = 16, |
| 15 | } | 16 | } |
| 16 | 17 | ||
| 17 | #[cfg(any(stm32wb, stm32f4))] | 18 | #[cfg(any(stm32wb, stm32f4))] |
| @@ -43,17 +44,6 @@ impl From<crate::pac::rtc::vals::Wucksel> for WakeupPrescaler { | |||
| 43 | } | 44 | } |
| 44 | } | 45 | } |
| 45 | 46 | ||
| 46 | impl From<WakeupPrescaler> for u32 { | ||
| 47 | fn from(val: WakeupPrescaler) -> Self { | ||
| 48 | match val { | ||
| 49 | WakeupPrescaler::Div2 => 2, | ||
| 50 | WakeupPrescaler::Div4 => 4, | ||
| 51 | WakeupPrescaler::Div8 => 8, | ||
| 52 | WakeupPrescaler::Div16 => 16, | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | #[allow(dead_code)] | 47 | #[allow(dead_code)] |
| 58 | impl WakeupPrescaler { | 48 | impl WakeupPrescaler { |
| 59 | pub fn compute_min(val: u32) -> Self { | 49 | pub fn compute_min(val: u32) -> Self { |
| @@ -64,7 +54,7 @@ impl WakeupPrescaler { | |||
| 64 | WakeupPrescaler::Div16, | 54 | WakeupPrescaler::Div16, |
| 65 | ] | 55 | ] |
| 66 | .iter() | 56 | .iter() |
| 67 | .skip_while(|psc| <WakeupPrescaler as Into<u32>>::into(**psc) <= val) | 57 | .skip_while(|psc| **psc as u32 <= val) |
| 68 | .next() | 58 | .next() |
| 69 | .unwrap_or(&WakeupPrescaler::Div16) | 59 | .unwrap_or(&WakeupPrescaler::Div16) |
| 70 | } | 60 | } |
| @@ -85,7 +75,7 @@ impl super::Rtc { | |||
| 85 | 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); |
| 86 | 76 | ||
| 87 | // adjust the rtc ticks to the prescaler and subtract one rtc tick | 77 | // adjust the rtc ticks to the prescaler and subtract one rtc tick |
| 88 | let rtc_ticks = rtc_ticks / (<WakeupPrescaler as Into<u32>>::into(prescaler) as u64); | 78 | let rtc_ticks = rtc_ticks / prescaler as u64; |
| 89 | let rtc_ticks = if rtc_ticks >= u16::MAX as u64 { | 79 | let rtc_ticks = if rtc_ticks >= u16::MAX as u64 { |
| 90 | u16::MAX - 1 | 80 | u16::MAX - 1 |
| 91 | } else { | 81 | } else { |
| @@ -106,11 +96,8 @@ impl super::Rtc { | |||
| 106 | 96 | ||
| 107 | trace!( | 97 | trace!( |
| 108 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", | 98 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", |
| 109 | Duration::from_ticks( | 99 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), |
| 110 | rtc_ticks as u64 * TICK_HZ * (<WakeupPrescaler as Into<u32>>::into(prescaler) as u64) / rtc_hz, | 100 | prescaler as u32, |
| 111 | ) | ||
| 112 | .as_millis(), | ||
| 113 | <WakeupPrescaler as Into<u32>>::into(prescaler), | ||
| 114 | rtc_ticks, | 101 | rtc_ticks, |
| 115 | self.instant(), | 102 | self.instant(), |
| 116 | ); | 103 | ); |
