diff options
Diffstat (limited to 'src/cyccnt.rs')
-rw-r--r-- | src/cyccnt.rs | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/cyccnt.rs b/src/cyccnt.rs index 86969cb1..6bc2ef0a 100644 --- a/src/cyccnt.rs +++ b/src/cyccnt.rs @@ -30,6 +30,10 @@ pub struct Instant { impl Instant { /// Returns an instant corresponding to "now" + /// + /// *HEADS UP* this function can, and will, return nonsensical values if called within `init`. + /// Only use it in `idle` and tasks. In `init`, use the `init::Context.start` field, or the + /// `CYCCNT::zero` function, instead of this function pub fn now() -> Self { Instant { inner: DWT::get_cycle_count() as i32, @@ -38,12 +42,14 @@ impl Instant { /// Returns the amount of time elapsed since this instant was created. pub fn elapsed(&self) -> Duration { - Instant::now() - *self + let diff = Instant::now().inner.wrapping_sub(self.inner); + assert!(diff >= 0, "instant now is earlier than self"); + Duration { inner: diff as u32 } } /// Returns the amount of time elapsed from another instant to this one. pub fn duration_since(&self, earlier: Instant) -> Duration { - let diff = self.inner - earlier.inner; + let diff = self.inner.wrapping_sub(earlier.inner); assert!(diff >= 0, "second instant is later than self"); Duration { inner: diff as u32 } } |