Skip to content

Commit

Permalink
time: Add time module from stm32h7xx-hal
Browse files Browse the repository at this point in the history
  • Loading branch information
astapleton committed Jul 16, 2024
1 parent 35d3a65 commit fd6a42f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ pub mod prelude;

#[cfg(feature = "device-selected")]
pub mod pwr;

#[cfg(feature = "device-selected")]
pub mod time;
38 changes: 38 additions & 0 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit fd6a42f

Please sign in to comment.