From 64b9c28eca4822a3ba1bd07d20964c2291c01cf5 Mon Sep 17 00:00:00 2001 From: xoviat Date: Thu, 13 Nov 2025 14:42:14 -0600 Subject: stm32: extract block_for_us remove from pub api --- embassy-stm32/src/adc/c0.rs | 2 +- embassy-stm32/src/adc/f1.rs | 4 ++-- embassy-stm32/src/adc/f3.rs | 2 +- embassy-stm32/src/adc/mod.rs | 20 ++------------------ embassy-stm32/src/dsihost.rs | 17 +++++------------ embassy-stm32/src/eth/generic_phy.rs | 14 +------------- embassy-stm32/src/lib.rs | 14 ++++++++++++++ embassy-stm32/src/opamp.rs | 13 +++---------- examples/stm32f469/src/bin/dsi_bsp.rs | 14 +++++++------- 9 files changed, 36 insertions(+), 64 deletions(-) diff --git a/embassy-stm32/src/adc/c0.rs b/embassy-stm32/src/adc/c0.rs index 983e7c10d..3bdca7edb 100644 --- a/embassy-stm32/src/adc/c0.rs +++ b/embassy-stm32/src/adc/c0.rs @@ -223,7 +223,7 @@ impl<'d, T: AnyInstance> Adc<'d, T> { // "The software must wait for the ADC voltage regulator startup time." // See datasheet for the value. - blocking_delay_us(TIME_ADC_VOLTAGE_REGUALTOR_STARTUP_US + 1); + blocking_delay_us(TIME_ADC_VOLTAGE_REGUALTOR_STARTUP_US as u64 + 1); T::regs().cfgr1().modify(|reg| reg.set_res(resolution)); diff --git a/embassy-stm32/src/adc/f1.rs b/embassy-stm32/src/adc/f1.rs index f6220de78..d6c6f480b 100644 --- a/embassy-stm32/src/adc/f1.rs +++ b/embassy-stm32/src/adc/f1.rs @@ -43,7 +43,7 @@ impl<'d, T: Instance> Adc<'d, T> { // 11.4: Before starting a calibration, the ADC must have been in power-on state (ADON bit = ‘1’) // for at least two ADC clock cycles. - blocking_delay_us((1_000_000 * 2) / Self::freq().0 + 1); + blocking_delay_us((1_000_000 * 2) / Self::freq().0 as u64 + 1); // Reset calibration T::regs().cr2().modify(|reg| reg.set_rstcal(true)); @@ -58,7 +58,7 @@ impl<'d, T: Instance> Adc<'d, T> { } // One cycle after calibration - blocking_delay_us((1_000_000 * 1) / Self::freq().0 + 1); + blocking_delay_us((1_000_000 * 1) / Self::freq().0 as u64 + 1); T::Interrupt::unpend(); unsafe { T::Interrupt::enable() }; diff --git a/embassy-stm32/src/adc/f3.rs b/embassy-stm32/src/adc/f3.rs index 4a77f3c5b..29bfdac97 100644 --- a/embassy-stm32/src/adc/f3.rs +++ b/embassy-stm32/src/adc/f3.rs @@ -62,7 +62,7 @@ impl<'d, T: Instance> Adc<'d, T> { while T::regs().cr().read().adcal() {} // Wait more than 4 clock cycles after adcal is cleared (RM0364 p. 223). - blocking_delay_us((1_000_000 * 4) / Self::freq().0 + 1); + blocking_delay_us((1_000_000 * 4) / Self::freq().0 as u64 + 1); // Enable the adc T::regs().cr().modify(|w| w.set_aden(true)); diff --git a/embassy-stm32/src/adc/mod.rs b/embassy-stm32/src/adc/mod.rs index 549f2f5a5..5ec08a22d 100644 --- a/embassy-stm32/src/adc/mod.rs +++ b/embassy-stm32/src/adc/mod.rs @@ -35,6 +35,8 @@ pub use ringbuffered::RingBufferedAdc; #[path = "adc4.rs"] pub mod adc4; +#[allow(unused)] +pub(self) use crate::block_for_us as blocking_delay_us; pub use crate::pac::adc::vals; #[cfg(not(any(adc_f1, adc_f3v3)))] pub use crate::pac::adc::vals::Res as Resolution; @@ -140,24 +142,6 @@ impl BasicAnyInstance for T { ))] impl AnyInstance for T {} -/// Performs a busy-wait delay for a specified number of microseconds. -#[allow(unused)] -pub(crate) fn blocking_delay_us(us: u32) { - cfg_if::cfg_if! { - // this does strange things on stm32wlx in low power mode depending on exactly when it's called - // as in sometimes 15 us (1 tick) would take > 20 seconds. - if #[cfg(all(feature = "time", all(not(feature = "low-power"), not(stm32wlex))))] { - let duration = embassy_time::Duration::from_micros(us as u64); - embassy_time::block_for(duration); - } else { - let freq = unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 as u64; - let us = us as u64; - let cycles = freq * us / 1_000_000; - cortex_m::asm::delay(cycles as u32); - } - } -} - #[cfg(any(adc_c0, adc_v3, adc_g0, adc_h5, adc_h7rs, adc_u0, adc_v4, adc_u5))] /// Number of samples used for averaging. #[derive(Copy, Clone, Debug)] diff --git a/embassy-stm32/src/dsihost.rs b/embassy-stm32/src/dsihost.rs index 59a2cbcdb..b8945820c 100644 --- a/embassy-stm32/src/dsihost.rs +++ b/embassy-stm32/src/dsihost.rs @@ -5,18 +5,11 @@ use core::marker::PhantomData; use embassy_hal_internal::PeripheralType; //use crate::gpio::{AnyPin, SealedPin}; +use crate::block_for_us; use crate::gpio::{AfType, AnyPin, OutputType, Speed}; use crate::rcc::{self, RccPeripheral}; use crate::{Peri, peripherals}; -/// Performs a busy-wait delay for a specified number of microseconds. -pub fn blocking_delay_ms(ms: u32) { - #[cfg(feature = "time")] - embassy_time::block_for(embassy_time::Duration::from_millis(ms as u64)); - #[cfg(not(feature = "time"))] - cortex_m::asm::delay(unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 / 1_000 * ms); -} - /// PacketTypes extracted from CubeMX #[repr(u8)] #[allow(dead_code)] @@ -334,7 +327,7 @@ impl<'d, T: Instance> DsiHost<'d, T> { if T::regs().gpsr().read().cmdfe() { return Ok(()); } - blocking_delay_ms(1); + block_for_us(1_000); } Err(Error::FifoTimeout) } @@ -345,7 +338,7 @@ impl<'d, T: Instance> DsiHost<'d, T> { if !T::regs().gpsr().read().cmdff() { return Ok(()); } - blocking_delay_ms(1); + block_for_us(1_000); } Err(Error::FifoTimeout) } @@ -356,7 +349,7 @@ impl<'d, T: Instance> DsiHost<'d, T> { if !self.read_busy() { return Ok(()); } - blocking_delay_ms(1); + block_for_us(1_000); } Err(Error::ReadTimeout) } @@ -367,7 +360,7 @@ impl<'d, T: Instance> DsiHost<'d, T> { if !T::regs().gpsr().read().prdfe() { return Ok(()); } - blocking_delay_ms(1); + block_for_us(1_000); } Err(Error::FifoTimeout) } diff --git a/embassy-stm32/src/eth/generic_phy.rs b/embassy-stm32/src/eth/generic_phy.rs index 774beef80..947874d7f 100644 --- a/embassy-stm32/src/eth/generic_phy.rs +++ b/embassy-stm32/src/eth/generic_phy.rs @@ -8,6 +8,7 @@ use embassy_time::{Duration, Timer}; use futures_util::FutureExt; use super::{Phy, StationManagement}; +use crate::block_for_us as blocking_delay_us; #[allow(dead_code)] mod phy_consts { @@ -76,19 +77,6 @@ impl GenericPhy { } } -// TODO: Factor out to shared functionality -fn blocking_delay_us(us: u32) { - #[cfg(feature = "time")] - embassy_time::block_for(Duration::from_micros(us as u64)); - #[cfg(not(feature = "time"))] - { - let freq = unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 as u64; - let us = us as u64; - let cycles = freq * us / 1_000_000; - cortex_m::asm::delay(cycles as u32); - } -} - impl Phy for GenericPhy { fn phy_reset(&mut self, sm: &mut S) { // Detect SMI address diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 680edf433..6e492946a 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -658,3 +658,17 @@ fn init_hw(config: Config) -> Peripherals { p }) } + +/// Performs a busy-wait delay for a specified number of microseconds. +#[allow(unused)] +pub(crate) fn block_for_us(us: u64) { + cfg_if::cfg_if! { + // this does strange things on stm32wlx in low power mode depending on exactly when it's called + // as in sometimes 15 us (1 tick) would take > 20 seconds. + if #[cfg(all(feature = "time", all(not(feature = "low-power"), not(stm32wlex))))] { + embassy_time::block_for(embassy_time::Duration::from_micros(us)); + } else { + cortex_m::asm::delay(unsafe { rcc::get_freqs().sys.to_hertz().unwrap().0 as u64 * us / 1_000_000 } as u32); + } + } +} diff --git a/embassy-stm32/src/opamp.rs b/embassy-stm32/src/opamp.rs index ac8d5de21..4a55f5bd3 100644 --- a/embassy-stm32/src/opamp.rs +++ b/embassy-stm32/src/opamp.rs @@ -4,19 +4,12 @@ use embassy_hal_internal::PeripheralType; use crate::Peri; +#[cfg(opamp_v5)] +use crate::block_for_us; use crate::pac::opamp::vals::*; #[cfg(not(any(stm32g4, stm32f3)))] use crate::rcc::RccInfo; -/// Performs a busy-wait delay for a specified number of microseconds. -#[cfg(opamp_v5)] -fn blocking_delay_ms(ms: u32) { - #[cfg(feature = "time")] - embassy_time::block_for(embassy_time::Duration::from_millis(ms as u64)); - #[cfg(not(feature = "time"))] - cortex_m::asm::delay(unsafe { crate::rcc::get_freqs() }.sys.to_hertz().unwrap().0 / 1_000 * ms); -} - /// Gain #[allow(missing_docs)] #[derive(Clone, Copy)] @@ -439,7 +432,7 @@ impl<'d, T: Instance> OpAmp<'d, T> { // The closer the trimming value is to the optimum trimming value, the longer it takes to stabilize // (with a maximum stabilization time remaining below 2 ms in any case) -- RM0440 25.3.7 - blocking_delay_ms(2); + block_for_us(2_000); if !T::regs().csr().read().calout() { if mid == 0 { diff --git a/examples/stm32f469/src/bin/dsi_bsp.rs b/examples/stm32f469/src/bin/dsi_bsp.rs index d659291ff..7ba4da72b 100644 --- a/examples/stm32f469/src/bin/dsi_bsp.rs +++ b/examples/stm32f469/src/bin/dsi_bsp.rs @@ -3,7 +3,7 @@ use defmt::*; use embassy_executor::Spawner; -use embassy_stm32::dsihost::{DsiHost, PacketType, blocking_delay_ms}; +use embassy_stm32::dsihost::{DsiHost, PacketType}; use embassy_stm32::gpio::{Level, Output, Speed}; use embassy_stm32::ltdc::Ltdc; use embassy_stm32::pac::dsihost::regs::{Ier0, Ier1}; @@ -13,7 +13,7 @@ use embassy_stm32::rcc::{ AHBPrescaler, APBPrescaler, Hse, HseMode, Pll, PllMul, PllPDiv, PllPreDiv, PllQDiv, PllRDiv, PllSource, Sysclk, }; use embassy_stm32::time::mhz; -use embassy_time::Timer; +use embassy_time::{Duration, Timer, block_for}; use {defmt_rtt as _, panic_probe as _}; enum _Orientation { @@ -444,7 +444,7 @@ async fn main(_spawner: Spawner) { dsi.enable_wrapper_dsi(); // First, delay 120 ms (reason unknown, STM32 Cube Example does it) - blocking_delay_ms(120); + block_for(Duration::from_millis(120)); // 1 to 26 dsi.write_cmd(0, NT35510_WRITES_0[0], &NT35510_WRITES_0[1..]).unwrap(); @@ -480,7 +480,7 @@ async fn main(_spawner: Spawner) { dsi.write_cmd(0, NT35510_WRITES_37[0], &NT35510_WRITES_37[1..]).unwrap(); // Add a delay, otherwise MADCTL not taken - blocking_delay_ms(200); + block_for(Duration::from_millis(200)); // Configure orientation as landscape dsi.write_cmd(0, NT35510_MADCTL_LANDSCAPE[0], &NT35510_MADCTL_LANDSCAPE[1..]) @@ -494,7 +494,7 @@ async fn main(_spawner: Spawner) { dsi.write_cmd(0, NT35510_WRITES_27[0], &NT35510_WRITES_27[1..]).unwrap(); // Wait for sleep out exit - blocking_delay_ms(120); + block_for(Duration::from_millis(120)); // Configure COLOR_CODING dsi.write_cmd(0, NT35510_WRITES_37[0], &NT35510_WRITES_37[1..]).unwrap(); @@ -590,7 +590,7 @@ async fn main(_spawner: Spawner) { //LTDC->SRCR = LTDC_SRCR_IMR; LTDC.srcr().modify(|w| w.set_imr(Imr::RELOAD)); - blocking_delay_ms(5000); + block_for(Duration::from_millis(5000)); const READ_SIZE: u16 = 1; let mut data = [1u8; READ_SIZE as usize]; @@ -606,7 +606,7 @@ async fn main(_spawner: Spawner) { .unwrap(); info!("Display ID3: {:#04x}", data); - blocking_delay_ms(500); + block_for(Duration::from_millis(500)); info!("Config done, start blinking LED"); loop { -- cgit