From fd6a42f1c0a1c83b66f4368616b8d13ca150099d Mon Sep 17 00:00:00 2001 From: astapleton Date: Thu, 7 Sep 2023 10:18:30 -0700 Subject: [PATCH] time: Add time module from stm32h7xx-hal --- src/lib.rs | 3 +++ src/time.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/time.rs diff --git a/src/lib.rs b/src/lib.rs index e118b94..002c129 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,3 +42,6 @@ pub mod prelude; #[cfg(feature = "device-selected")] pub mod pwr; + +#[cfg(feature = "device-selected")] +pub mod time; diff --git a/src/time.rs b/src/time.rs new file mode 100644 index 0000000..a87fc7f --- /dev/null +++ b/src/time.rs @@ -0,0 +1,38 @@ +//! Time units + +pub use fugit::{ + HertzU32 as Hertz, KilohertzU32 as KiloHertz, MegahertzU32 as MegaHertz, + MicrosDurationU32 as MicroSeconds, MillisDurationU32 as MilliSeconds, + NanosDurationU32 as NanoSeconds, +}; + +//use core::time::Duration; +use cortex_m::peripheral::DWT; + +/// Bits per second +pub type Bps = Hertz; + +/// Extension trait that adds convenience methods to the `u32` type +pub trait U32Ext { + /// Wrap in `Bps` + fn bps(self) -> Bps; +} + +impl U32Ext for u32 { + fn bps(self) -> Bps { + Bps::from_raw(self) + } +} + +/// A measurement of a monotonically nondecreasing clock +#[derive(Clone, Copy)] +pub struct Instant { + now: u32, +} + +impl Instant { + /// Ticks elapsed since the `Instant` was created + pub fn elapsed(&self) -> u32 { + DWT::cycle_count().wrapping_sub(self.now) + } +}