Merge pull request #2876 from ftk/timer32fix

stm32 timer: fix 32bit timer off by one ARR error
This commit is contained in:
Dario Nieuwenhuis 2024-04-28 21:24:14 +02:00 committed by GitHub
commit 14225eb3a0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -257,7 +257,10 @@ impl<'d, T: CoreInstance> Timer<'d, T> {
TimerBits::Bits32 => { TimerBits::Bits32 => {
let pclk_ticks_per_timer_period = (timer_f / f) as u64; let pclk_ticks_per_timer_period = (timer_f / f) as u64;
let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into()); let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 32)).try_into());
let arr: u32 = unwrap!((pclk_ticks_per_timer_period / (psc as u64 + 1)).try_into()); let divide_by = pclk_ticks_per_timer_period / (u64::from(psc) + 1);
// the timer counts `0..=arr`, we want it to count `0..divide_by`
let arr: u32 = unwrap!(u32::try_from(divide_by - 1));
let regs = self.regs_gp32_unchecked(); let regs = self.regs_gp32_unchecked();
regs.psc().write_value(psc); regs.psc().write_value(psc);