From 8c0e2748db61c0b1794a2921e7a813fd8fa92529 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Fri, 1 Dec 2023 22:06:48 -0600 Subject: [PATCH] stm32/timer: Fix frequency off-by-one --- embassy-stm32/src/timer/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/timer/mod.rs b/embassy-stm32/src/timer/mod.rs index 913bfed2..2313a5b9 100644 --- a/embassy-stm32/src/timer/mod.rs +++ b/embassy-stm32/src/timer/mod.rs @@ -46,7 +46,10 @@ pub(crate) mod sealed { assert!(f > 0); let pclk_ticks_per_timer_period = timer_f / f; 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(); regs.psc().write(|r| r.set_psc(psc));