stm32 timer: fix 32bit timer off by 1 ARR error

This commit is contained in:
ftk 2024-04-27 15:49:30 +03:00
parent 34074e6eb0
commit e7bfd7bac9

View File

@ -257,7 +257,10 @@ impl<'d, T: CoreInstance> Timer<'d, T> {
TimerBits::Bits32 => {
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 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();
regs.psc().write_value(psc);