Skip to content

Commit

Permalink
stm32: adc: fix blocking_delay_us() overflowing when sys freq is high
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tcbennun committed Apr 16, 2024
1 parent 8e850de commit d928663
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion embassy-stm32/src/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d928663

Please sign in to comment.