Skip to content
Snippets Groups Projects
Commit c2409c0a authored by Jorge Aparicio's avatar Jorge Aparicio
Browse files

add `time::Instant` API

parent d9d0252a
No related branches found
No related tags found
No related merge requests found
//! Time units //! Time units
use cortex_m::peripheral::DWT;
use rcc::Clocks;
/// Bits per second /// Bits per second
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Bps(pub u32); pub struct Bps(pub u32);
...@@ -66,3 +70,48 @@ impl Into<KiloHertz> for MegaHertz { ...@@ -66,3 +70,48 @@ impl Into<KiloHertz> for MegaHertz {
KiloHertz(self.0 * 1_000) KiloHertz(self.0 * 1_000)
} }
} }
/// A monotonic nondecreasing timer
#[derive(Clone, Copy)]
pub struct MonoTimer {
frequency: Hertz,
}
impl MonoTimer {
/// Creates a new `Monotonic` timer
pub fn new(mut dwt: DWT, clocks: Clocks) -> Self {
dwt.enable_cycle_counter();
// now the CYCCNT counter can't be stopped or resetted
drop(dwt);
MonoTimer {
frequency: clocks.sysclk(),
}
}
/// Returns the frequency at which the monotonic timer is operating at
pub fn frequency(&self) -> Hertz {
self.frequency
}
/// Returns an `Instant` corresponding to "now"
pub fn now(&self) -> Instant {
Instant {
now: DWT::get_cycle_count(),
}
}
}
/// 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::get_cycle_count().wrapping_sub(self.now)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment