diff options
| -rw-r--r-- | embassy-stm32/src/hrtim/traits.rs | 49 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 31 |
2 files changed, 24 insertions, 56 deletions
diff --git a/embassy-stm32/src/hrtim/traits.rs b/embassy-stm32/src/hrtim/traits.rs index 158a68862..095109598 100644 --- a/embassy-stm32/src/hrtim/traits.rs +++ b/embassy-stm32/src/hrtim/traits.rs | |||
| @@ -1,31 +1,17 @@ | |||
| 1 | use crate::rcc::sealed::RccPeripheral; | 1 | use crate::rcc::sealed::RccPeripheral; |
| 2 | use crate::time::Hertz; | 2 | use crate::time::Hertz; |
| 3 | 3 | ||
| 4 | #[repr(u8)] | ||
| 4 | #[derive(Clone, Copy)] | 5 | #[derive(Clone, Copy)] |
| 5 | pub(crate) enum Prescaler { | 6 | pub(crate) enum Prescaler { |
| 6 | Div1, | 7 | Div1 = 1, |
| 7 | Div2, | 8 | Div2 = 2, |
| 8 | Div4, | 9 | Div4 = 4, |
| 9 | Div8, | 10 | Div8 = 8, |
| 10 | Div16, | 11 | Div16 = 16, |
| 11 | Div32, | 12 | Div32 = 32, |
| 12 | Div64, | 13 | Div64 = 64, |
| 13 | Div128, | 14 | Div128 = 128, |
| 14 | } | ||
| 15 | |||
| 16 | impl From<Prescaler> for u32 { | ||
| 17 | fn from(val: Prescaler) -> Self { | ||
| 18 | match val { | ||
| 19 | Prescaler::Div1 => 1, | ||
| 20 | Prescaler::Div2 => 2, | ||
| 21 | Prescaler::Div4 => 4, | ||
| 22 | Prescaler::Div8 => 8, | ||
| 23 | Prescaler::Div16 => 16, | ||
| 24 | Prescaler::Div32 => 32, | ||
| 25 | Prescaler::Div64 => 64, | ||
| 26 | Prescaler::Div128 => 128, | ||
| 27 | } | ||
| 28 | } | ||
| 29 | } | 15 | } |
| 30 | 16 | ||
| 31 | impl From<Prescaler> for u8 { | 17 | impl From<Prescaler> for u8 { |
| @@ -72,7 +58,7 @@ impl Prescaler { | |||
| 72 | Prescaler::Div128, | 58 | Prescaler::Div128, |
| 73 | ] | 59 | ] |
| 74 | .iter() | 60 | .iter() |
| 75 | .skip_while(|psc| <Prescaler as Into<u32>>::into(**psc) <= val) | 61 | .skip_while(|psc| **psc as u32 <= val) |
| 76 | .next() | 62 | .next() |
| 77 | .unwrap() | 63 | .unwrap() |
| 78 | } | 64 | } |
| @@ -80,7 +66,7 @@ impl Prescaler { | |||
| 80 | pub fn compute_min_low_res(val: u32) -> Self { | 66 | pub fn compute_min_low_res(val: u32) -> Self { |
| 81 | *[Prescaler::Div32, Prescaler::Div64, Prescaler::Div128] | 67 | *[Prescaler::Div32, Prescaler::Div64, Prescaler::Div128] |
| 82 | .iter() | 68 | .iter() |
| 83 | .skip_while(|psc| <Prescaler as Into<u32>>::into(**psc) <= val) | 69 | .skip_while(|psc| **psc as u32 <= val) |
| 84 | .next() | 70 | .next() |
| 85 | .unwrap() | 71 | .unwrap() |
| 86 | } | 72 | } |
| @@ -126,8 +112,7 @@ foreach_interrupt! { | |||
| 126 | Prescaler::compute_min_low_res(psc_min) | 112 | Prescaler::compute_min_low_res(psc_min) |
| 127 | }; | 113 | }; |
| 128 | 114 | ||
| 129 | let psc_val: u32 = psc.into(); | 115 | let timer_f = 32 * (timer_f / psc as u32); |
| 130 | let timer_f = 32 * (timer_f / psc_val); | ||
| 131 | let per: u16 = (timer_f / f) as u16; | 116 | let per: u16 = (timer_f / f) as u16; |
| 132 | 117 | ||
| 133 | let regs = Self::regs(); | 118 | let regs = Self::regs(); |
| @@ -148,8 +133,7 @@ foreach_interrupt! { | |||
| 148 | Prescaler::compute_min_low_res(psc_min) | 133 | Prescaler::compute_min_low_res(psc_min) |
| 149 | }; | 134 | }; |
| 150 | 135 | ||
| 151 | let psc_val: u32 = psc.into(); | 136 | let timer_f = 32 * (timer_f / psc as u32); |
| 152 | let timer_f = 32 * (timer_f / psc_val); | ||
| 153 | let per: u16 = (timer_f / f) as u16; | 137 | let per: u16 = (timer_f / f) as u16; |
| 154 | 138 | ||
| 155 | let regs = Self::regs(); | 139 | let regs = Self::regs(); |
| @@ -163,20 +147,17 @@ foreach_interrupt! { | |||
| 163 | let regs = Self::regs(); | 147 | let regs = Self::regs(); |
| 164 | 148 | ||
| 165 | let channel_psc: Prescaler = regs.tim(channel).cr().read().ckpsc().into(); | 149 | let channel_psc: Prescaler = regs.tim(channel).cr().read().ckpsc().into(); |
| 166 | let psc_val: u32 = channel_psc.into(); | ||
| 167 | |||
| 168 | 150 | ||
| 169 | // The dead-time base clock runs 4 times slower than the hrtim base clock | 151 | // The dead-time base clock runs 4 times slower than the hrtim base clock |
| 170 | // u9::MAX = 511 | 152 | // u9::MAX = 511 |
| 171 | let psc_min = (psc_val * dead_time as u32) / (4 * 511); | 153 | let psc_min = (channel_psc as u32 * dead_time as u32) / (4 * 511); |
| 172 | let psc = if Self::regs().isr().read().dllrdy() { | 154 | let psc = if Self::regs().isr().read().dllrdy() { |
| 173 | Prescaler::compute_min_high_res(psc_min) | 155 | Prescaler::compute_min_high_res(psc_min) |
| 174 | } else { | 156 | } else { |
| 175 | Prescaler::compute_min_low_res(psc_min) | 157 | Prescaler::compute_min_low_res(psc_min) |
| 176 | }; | 158 | }; |
| 177 | 159 | ||
| 178 | let dt_psc_val: u32 = psc.into(); | 160 | let dt_val = (psc as u32 * dead_time as u32) / (4 * channel_psc as u32); |
| 179 | let dt_val = (dt_psc_val * dead_time as u32) / (4 * psc_val); | ||
| 180 | 161 | ||
| 181 | regs.tim(channel).dt().modify(|w| { | 162 | regs.tim(channel).dt().modify(|w| { |
| 182 | w.set_dtprsc(psc.into()); | 163 | w.set_dtprsc(psc.into()); |
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 | ); |
