Skip to content

Commit fd6a42f

Browse files
committed
time: Add time module from stm32h7xx-hal
1 parent 35d3a65 commit fd6a42f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,6 @@ pub mod prelude;
4242

4343
#[cfg(feature = "device-selected")]
4444
pub mod pwr;
45+
46+
#[cfg(feature = "device-selected")]
47+
pub mod time;

src/time.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//! Time units
2+
3+
pub use fugit::{
4+
HertzU32 as Hertz, KilohertzU32 as KiloHertz, MegahertzU32 as MegaHertz,
5+
MicrosDurationU32 as MicroSeconds, MillisDurationU32 as MilliSeconds,
6+
NanosDurationU32 as NanoSeconds,
7+
};
8+
9+
//use core::time::Duration;
10+
use cortex_m::peripheral::DWT;
11+
12+
/// Bits per second
13+
pub type Bps = Hertz;
14+
15+
/// Extension trait that adds convenience methods to the `u32` type
16+
pub trait U32Ext {
17+
/// Wrap in `Bps`
18+
fn bps(self) -> Bps;
19+
}
20+
21+
impl U32Ext for u32 {
22+
fn bps(self) -> Bps {
23+
Bps::from_raw(self)
24+
}
25+
}
26+
27+
/// A measurement of a monotonically nondecreasing clock
28+
#[derive(Clone, Copy)]
29+
pub struct Instant {
30+
now: u32,
31+
}
32+
33+
impl Instant {
34+
/// Ticks elapsed since the `Instant` was created
35+
pub fn elapsed(&self) -> u32 {
36+
DWT::cycle_count().wrapping_sub(self.now)
37+
}
38+
}

0 commit comments

Comments
 (0)