diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cyccnt.rs | 10 | ||||
-rw-r--r-- | src/tq.rs | 7 |
2 files changed, 15 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 } } @@ -68,6 +68,13 @@ where .map(|x| x / ratio.denominator) }) { None => MAX, + + // ARM Architecture Reference Manual says: + // "Setting SYST_RVR to zero has the effect of + // disabling the SysTick counter independently + // of the counter enable bit." + Some(0) => 1, + Some(x) => cmp::min(MAX, x), }; mem::transmute::<_, SYST>(()).set_reload(dur); |