From e7370a4000daac053ff1dd69abfbb15fd5adc0de Mon Sep 17 00:00:00 2001 From: Dan Whitman Date: Thu, 6 Feb 2025 11:56:14 -0500 Subject: [PATCH] feat(time)!: Changes the type defs in the `time` module to use `u64` for durations instead of `u32` * This allows for delays and durations longer than 4 seconds in parts of the HAL where durations are stored or passed in nanoseconds. * Also ensures that the type defs in the `time` module are used throughout the HAL, as some places still imported and used `fugit` durations explicitly. * Replaces `fugit::ExtU32` in the prelude with `fugit::ExtU64`, and also adds `fugit::ExtU64Ceil`. examples: Updates applicable examples to use the HAL duration types instead of those directly from `fugit` * feather_m0: async_timer * metro_m4: async_timer --- boards/feather_m0/examples/async_timer.rs | 6 ++---- boards/metro_m4/examples/async_timer.rs | 6 ++---- hal/src/peripherals/timer/async_api.rs | 7 ++++--- hal/src/peripherals/timer/d11.rs | 3 +-- hal/src/peripherals/timer/d5x.rs | 3 +-- hal/src/prelude.rs | 6 ++---- hal/src/rtc/mod.rs | 6 ++---- hal/src/time.rs | 8 ++++---- hal/src/timer_params.rs | 5 ++--- hal/src/timer_traits.rs | 4 ++-- 10 files changed, 22 insertions(+), 32 deletions(-) diff --git a/boards/feather_m0/examples/async_timer.rs b/boards/feather_m0/examples/async_timer.rs index 589abd31f581..59ae2a6f1d3f 100644 --- a/boards/feather_m0/examples/async_timer.rs +++ b/boards/feather_m0/examples/async_timer.rs @@ -1,6 +1,7 @@ #![no_std] #![no_main] +use atsamd_hal::time::Milliseconds; #[cfg(not(feature = "use_semihosting"))] use panic_halt as _; #[cfg(feature = "use_semihosting")] @@ -11,7 +12,6 @@ use feather_m0 as bsp; use hal::{ clock::{enable_internal_32kosc, ClockGenId, ClockSource, GenericClockController}, ehal::digital::StatefulOutputPin, - fugit::MillisDurationU32, pac::Tc4, timer::TimerCounter, }; @@ -48,9 +48,7 @@ async fn main(_s: embassy_executor::Spawner) { let mut timer = timer.into_future(Irqs); loop { - timer - .delay(MillisDurationU32::from_ticks(500).convert()) - .await; + timer.delay(Milliseconds::from_ticks(500).convert()).await; red_led.toggle().unwrap(); } } diff --git a/boards/metro_m4/examples/async_timer.rs b/boards/metro_m4/examples/async_timer.rs index 3e29366e5af9..62cba1f97a1b 100644 --- a/boards/metro_m4/examples/async_timer.rs +++ b/boards/metro_m4/examples/async_timer.rs @@ -1,13 +1,13 @@ #![no_std] #![no_main] +use atsamd_hal::time::Milliseconds; #[cfg(not(feature = "use_semihosting"))] use panic_halt as _; #[cfg(feature = "use_semihosting")] use panic_semihosting as _; use bsp::{hal, pac, pin_alias}; -use hal::fugit::MillisDurationU32; use hal::{ clock::GenericClockController, ehal::digital::StatefulOutputPin, pac::Tc4, timer::TimerCounter, }; @@ -41,9 +41,7 @@ async fn main(_s: embassy_executor::Spawner) { let mut timer = timer.into_future(Irqs); loop { - timer - .delay(MillisDurationU32::from_ticks(500).convert()) - .await; + timer.delay(Milliseconds::from_ticks(500).convert()).await; red_led.toggle().unwrap(); } } diff --git a/hal/src/peripherals/timer/async_api.rs b/hal/src/peripherals/timer/async_api.rs index c94bee9ac8ac..3e41bc60c549 100644 --- a/hal/src/peripherals/timer/async_api.rs +++ b/hal/src/peripherals/timer/async_api.rs @@ -6,6 +6,7 @@ use crate::{ async_hal::interrupts::{Binding, Handler, Interrupt}, pac, + time::Nanoseconds, timer_traits::InterruptDrivenTimer, typelevel::Sealed, }; @@ -17,7 +18,6 @@ use core::{ task::{Poll, Waker}, }; use embassy_sync::waitqueue::AtomicWaker; -use fugit::NanosDurationU32; use portable_atomic::AtomicBool; use crate::peripherals::timer; @@ -186,7 +186,7 @@ where { /// Delay asynchronously #[inline] - pub async fn delay(&mut self, count: NanosDurationU32) { + pub async fn delay(&mut self, count: Nanoseconds) { self.timer.start(count); self.timer.enable_interrupt(); @@ -217,7 +217,8 @@ where T: AsyncCount16, { async fn delay_ns(&mut self, ns: u32) { - self.delay(NanosDurationU32::from_ticks(ns).convert()).await; + self.delay(Nanoseconds::from_ticks(ns.into()).convert()) + .await; } } diff --git a/hal/src/peripherals/timer/d11.rs b/hal/src/peripherals/timer/d11.rs index 00b6ff98584c..56f684502702 100644 --- a/hal/src/peripherals/timer/d11.rs +++ b/hal/src/peripherals/timer/d11.rs @@ -2,7 +2,6 @@ use core::convert::Infallible; use atsamd_hal_macros::hal_cfg; -use fugit::NanosDurationU32; use crate::ehal_02::timer::{CountDown, Periodic}; use crate::pac::Pm; @@ -86,7 +85,7 @@ where self.tc.count_16().intenset().write(|w| w.ovf().set_bit()); } - fn start>(&mut self, timeout: T) { + fn start>(&mut self, timeout: T) { let params = TimerParams::new_ns(timeout.into(), self.freq); let divider = params.divider; let cycles = params.cycles; diff --git a/hal/src/peripherals/timer/d5x.rs b/hal/src/peripherals/timer/d5x.rs index 2cd9a2ea43df..8f0893f84356 100644 --- a/hal/src/peripherals/timer/d5x.rs +++ b/hal/src/peripherals/timer/d5x.rs @@ -2,7 +2,6 @@ use core::convert::Infallible; use atsamd_hal_macros::hal_cfg; -use fugit::NanosDurationU32; use crate::ehal_02::timer::{CountDown, Periodic}; use crate::pac::tc0::Count16 as Count16Reg; @@ -85,7 +84,7 @@ where fn start(&mut self, timeout: T) where - T: Into, + T: Into, { let params = TimerParams::new_ns(timeout.into(), self.freq); let divider = params.divider; diff --git a/hal/src/prelude.rs b/hal/src/prelude.rs index d7f1b3cb1350..307da62bbc8f 100644 --- a/hal/src/prelude.rs +++ b/hal/src/prelude.rs @@ -1,7 +1,8 @@ //! Import the prelude to gain convenient access to helper traits pub use crate::eic::EicPin; pub use crate::timer_traits::InterruptDrivenTimer; -pub use fugit::ExtU32 as _; +pub use fugit::ExtU64 as _; +pub use fugit::ExtU64Ceil as _; pub use fugit::RateExtU32 as _; // embedded-hal doesn’t yet have v2 in its prelude, so we need to @@ -14,6 +15,3 @@ pub use crate::ehal_02::prelude::*; #[cfg(feature = "rtic")] pub use rtic_time::Monotonic as _; - -#[cfg(feature = "rtic")] -pub use fugit::{ExtU64, ExtU64Ceil}; diff --git a/hal/src/rtc/mod.rs b/hal/src/rtc/mod.rs index 66d1cfe7e51a..6b3e2c1fb15c 100644 --- a/hal/src/rtc/mod.rs +++ b/hal/src/rtc/mod.rs @@ -1,6 +1,5 @@ //! Real-time clock/counter use atsamd_hal_macros::{hal_cfg, hal_macro_helper}; -use fugit::NanosDurationU32; use crate::ehal_02; use crate::pac; @@ -383,7 +382,7 @@ impl InterruptDrivenTimer for Rtc { fn start(&mut self, timeout: T) where - T: Into, + T: Into, { let params = TimerParams::new_us(timeout, self.rtc_clock_freq); let divider = params.divider; @@ -458,8 +457,7 @@ impl TimerParams { pub fn new_us(timeout: impl Into, src_freq: impl Into) -> Self { let timeout = timeout.into(); let src_freq = src_freq.into(); - let ticks: u32 = - (timeout.to_nanos() as u64 * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32; + let ticks: u32 = (timeout.to_nanos() * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32; Self::new_from_ticks(ticks) } diff --git a/hal/src/time.rs b/hal/src/time.rs index 980802802906..f79bd5f61ea3 100644 --- a/hal/src/time.rs +++ b/hal/src/time.rs @@ -14,13 +14,13 @@ pub type MegaHertz = fugit::MegahertzU32; // Period based /// Seconds -pub type Seconds = fugit::SecsDurationU32; +pub type Seconds = fugit::SecsDurationU64; /// Milliseconds -pub type Milliseconds = fugit::MillisDurationU32; +pub type Milliseconds = fugit::MillisDurationU64; /// Microseconds -pub type Microseconds = fugit::MicrosDurationU32; +pub type Microseconds = fugit::MicrosDurationU64; /// Nanoseconds -pub type Nanoseconds = fugit::NanosDurationU32; +pub type Nanoseconds = fugit::NanosDurationU64; diff --git a/hal/src/timer_params.rs b/hal/src/timer_params.rs index 33d7d225f293..c6f733b8eb4a 100644 --- a/hal/src/timer_params.rs +++ b/hal/src/timer_params.rs @@ -17,8 +17,7 @@ impl TimerParams { /// calculates TimerParams from a given period based timeout. pub fn new_ns(timeout: Nanoseconds, src_freq: Hertz) -> Self { - let ticks: u32 = - (timeout.to_nanos() as u64 * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32; + let ticks: u32 = (timeout.to_nanos() * src_freq.to_Hz() as u64 / 1_000_000_000_u64) as u32; Self::new_from_ticks(ticks) } @@ -51,7 +50,7 @@ impl TimerParams { #[cfg(test)] mod tests { - use crate::fugit::{ExtU32, RateExtU32}; + use crate::fugit::{ExtU64, RateExtU32}; use crate::timer_params::TimerParams; #[test] diff --git a/hal/src/timer_traits.rs b/hal/src/timer_traits.rs index a2bc858ce750..e57e9b213b51 100644 --- a/hal/src/timer_traits.rs +++ b/hal/src/timer_traits.rs @@ -1,6 +1,6 @@ use core::convert::Infallible; -use fugit::NanosDurationU32; +use crate::time::Nanoseconds; /// Specifies a timer that can enable & disable an interrupt that fires /// when the timer expires @@ -9,7 +9,7 @@ pub trait InterruptDrivenTimer { fn enable_interrupt(&mut self); /// Start the timer with a given timeout in nanoseconds - fn start>(&mut self, timeout: T); + fn start>(&mut self, timeout: T); /// Wait for the timer to finish counting down **without blocking**. fn wait(&mut self) -> nb::Result<(), Infallible>;