Merge pull request #2240 from GrantM11235/fix-freq-off-by-one

stm32/timer: Fix frequency off-by-one
This commit is contained in:
Ulf Lilleengen 2023-12-02 08:13:50 +00:00 committed by GitHub
commit a31ae52d1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -46,7 +46,10 @@ pub(crate) mod sealed {
assert!(f > 0); assert!(f > 0);
let pclk_ticks_per_timer_period = timer_f / f; let pclk_ticks_per_timer_period = timer_f / f;
let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into()); let psc: u16 = unwrap!(((pclk_ticks_per_timer_period - 1) / (1 << 16)).try_into());
let arr: u16 = unwrap!((pclk_ticks_per_timer_period / (u32::from(psc) + 1)).try_into()); let divide_by = pclk_ticks_per_timer_period / (u32::from(psc) + 1);
// the timer counts `0..=arr`, we want it to count `0..divide_by`
let arr = unwrap!(u16::try_from(divide_by - 1));
let regs = Self::regs(); let regs = Self::regs();
regs.psc().write(|r| r.set_psc(psc)); regs.psc().write(|r| r.set_psc(psc));