diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-10-12 00:34:47 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-10-12 16:20:34 +0200 |
| commit | 97ca0e77bf6e6f36aae18cb57fbfa8e583597327 (patch) | |
| tree | 20f7a2f1e27e0d30e530047e819b6efeaf2bd9cc /embassy-stm32/src/time_driver.rs | |
| parent | 66e399b5c61653f1f66cd3fd1592936e4085d6b5 (diff) | |
stm32: avoid creating many tiny critical sections in init.
Saves 292 bytes on stm32f0 bilnky with max optimizations (from 3132 to 2840).
Diffstat (limited to 'embassy-stm32/src/time_driver.rs')
| -rw-r--r-- | embassy-stm32/src/time_driver.rs | 56 |
1 files changed, 27 insertions, 29 deletions
diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index baea20aef..add8be831 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs | |||
| @@ -152,45 +152,43 @@ embassy_time::time_driver_impl!(static DRIVER: RtcDriver = RtcDriver { | |||
| 152 | }); | 152 | }); |
| 153 | 153 | ||
| 154 | impl RtcDriver { | 154 | impl RtcDriver { |
| 155 | fn init(&'static self) { | 155 | fn init(&'static self, cs: critical_section::CriticalSection) { |
| 156 | let r = T::regs_gp16(); | 156 | let r = T::regs_gp16(); |
| 157 | 157 | ||
| 158 | <T as RccPeripheral>::enable_and_reset(); | 158 | <T as RccPeripheral>::enable_and_reset_with_cs(cs); |
| 159 | 159 | ||
| 160 | let timer_freq = T::frequency(); | 160 | let timer_freq = T::frequency(); |
| 161 | 161 | ||
| 162 | critical_section::with(|_| { | 162 | r.cr1().modify(|w| w.set_cen(false)); |
| 163 | r.cr1().modify(|w| w.set_cen(false)); | 163 | r.cnt().write(|w| w.set_cnt(0)); |
| 164 | r.cnt().write(|w| w.set_cnt(0)); | ||
| 165 | 164 | ||
| 166 | let psc = timer_freq.0 / TICK_HZ as u32 - 1; | 165 | let psc = timer_freq.0 / TICK_HZ as u32 - 1; |
| 167 | let psc: u16 = match psc.try_into() { | 166 | let psc: u16 = match psc.try_into() { |
| 168 | Err(_) => panic!("psc division overflow: {}", psc), | 167 | Err(_) => panic!("psc division overflow: {}", psc), |
| 169 | Ok(n) => n, | 168 | Ok(n) => n, |
| 170 | }; | 169 | }; |
| 171 | 170 | ||
| 172 | r.psc().write(|w| w.set_psc(psc)); | 171 | r.psc().write(|w| w.set_psc(psc)); |
| 173 | r.arr().write(|w| w.set_arr(u16::MAX)); | 172 | r.arr().write(|w| w.set_arr(u16::MAX)); |
| 174 | 173 | ||
| 175 | // Set URS, generate update and clear URS | 174 | // Set URS, generate update and clear URS |
| 176 | r.cr1().modify(|w| w.set_urs(vals::Urs::COUNTERONLY)); | 175 | r.cr1().modify(|w| w.set_urs(vals::Urs::COUNTERONLY)); |
| 177 | r.egr().write(|w| w.set_ug(true)); | 176 | r.egr().write(|w| w.set_ug(true)); |
| 178 | r.cr1().modify(|w| w.set_urs(vals::Urs::ANYEVENT)); | 177 | r.cr1().modify(|w| w.set_urs(vals::Urs::ANYEVENT)); |
| 179 | 178 | ||
| 180 | // Mid-way point | 179 | // Mid-way point |
| 181 | r.ccr(0).write(|w| w.set_ccr(0x8000)); | 180 | r.ccr(0).write(|w| w.set_ccr(0x8000)); |
| 182 | 181 | ||
| 183 | // Enable overflow and half-overflow interrupts | 182 | // Enable overflow and half-overflow interrupts |
| 184 | r.dier().write(|w| { | 183 | r.dier().write(|w| { |
| 185 | w.set_uie(true); | 184 | w.set_uie(true); |
| 186 | w.set_ccie(0, true); | 185 | w.set_ccie(0, true); |
| 187 | }); | 186 | }); |
| 188 | 187 | ||
| 189 | <T as BasicInstance>::Interrupt::unpend(); | 188 | <T as BasicInstance>::Interrupt::unpend(); |
| 190 | unsafe { <T as BasicInstance>::Interrupt::enable() }; | 189 | unsafe { <T as BasicInstance>::Interrupt::enable() }; |
| 191 | 190 | ||
| 192 | r.cr1().modify(|w| w.set_cen(true)); | 191 | r.cr1().modify(|w| w.set_cen(true)); |
| 193 | }) | ||
| 194 | } | 192 | } |
| 195 | 193 | ||
| 196 | fn on_interrupt(&self) { | 194 | fn on_interrupt(&self) { |
| @@ -462,6 +460,6 @@ pub(crate) fn get_driver() -> &'static RtcDriver { | |||
| 462 | &DRIVER | 460 | &DRIVER |
| 463 | } | 461 | } |
| 464 | 462 | ||
| 465 | pub(crate) fn init() { | 463 | pub(crate) fn init(cs: CriticalSection) { |
| 466 | DRIVER.init() | 464 | DRIVER.init(cs) |
| 467 | } | 465 | } |
