From d928663baeb851e830e04e469f3a338ad3f4bc15 Mon Sep 17 00:00:00 2001 From: Torin Cooper-Bennun <tcbennun@maxiluxsystems.com> Date: Tue, 16 Apr 2024 15:13:31 +0100 Subject: [PATCH] stm32: adc: fix blocking_delay_us() overflowing when sys freq is high e.g. H503 running at 250 MHz resulted in an upper bound of 17 us here. casting up to u64 for intermediate calc allows the upper bound to be increased by a factor of 1e6 --- embassy-stm32/src/adc/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 12c5751bd..2ff2ed6a8 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs @@ -76,7 +76,12 @@ pub(crate) fn blocking_delay_us(us: u32) { #[cfg(time)] embassy_time::block_for(embassy_time::Duration::from_micros(us)); #[cfg(not(time))] - cortex_m::asm::delay(unsafe { crate::rcc::get_freqs() }.sys.unwrap().0 * us / 1_000_000); + { + let freq = unsafe { crate::rcc::get_freqs() }.sys.unwrap().0 as u64; + let us = us as u64; + let cycles = freq * us / 1_000_000; + cortex_m::asm::delay(cycles as u32); + } } /// ADC instance.