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/hrtim | |
| parent | ac11635b0b97b7013ef280f7e978c13e86a132b8 (diff) | |
stm32: cleanup psc design
Diffstat (limited to 'embassy-stm32/src/hrtim')
| -rw-r--r-- | embassy-stm32/src/hrtim/traits.rs | 49 |
1 files changed, 15 insertions, 34 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()); |
