2019-10-16 02:11:35 +02:00
|
|
|
//! Data Watchpoint Trace (DWT) unit's CYCle CouNTer (CYCCNT)
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
use core::{
|
|
|
|
cmp::Ordering,
|
|
|
|
convert::{Infallible, TryInto},
|
2019-10-16 02:12:05 +02:00
|
|
|
fmt, ops,
|
2019-06-13 23:56:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
use cortex_m::peripheral::DWT;
|
|
|
|
|
2019-07-11 13:28:25 +02:00
|
|
|
use crate::Fraction;
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
/// A measurement of the CYCCNT. Opaque and useful only with `Duration`
|
|
|
|
///
|
|
|
|
/// This data type is only available on ARMv7-M
|
2019-07-11 13:28:25 +02:00
|
|
|
///
|
2019-10-16 02:11:35 +02:00
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Adding or subtracting a `Duration` of more than `(1 << 31)` cycles to an `Instant` effectively
|
|
|
|
/// makes it "wrap around" and creates an incorrect value. This is also true if the operation is
|
|
|
|
/// done in steps, e.g. `(instant + dur) + dur` where `dur` is `(1 << 30)` ticks.
|
2019-06-13 23:56:59 +02:00
|
|
|
#[derive(Clone, Copy, Eq, PartialEq)]
|
|
|
|
pub struct Instant {
|
|
|
|
inner: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Instant {
|
|
|
|
/// Returns an instant corresponding to "now"
|
2020-01-21 22:23:20 +01:00
|
|
|
///
|
|
|
|
/// *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
|
2019-06-13 23:56:59 +02:00
|
|
|
pub fn now() -> Self {
|
|
|
|
Instant {
|
|
|
|
inner: DWT::get_cycle_count() as i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the amount of time elapsed since this instant was created.
|
|
|
|
pub fn elapsed(&self) -> Duration {
|
2019-11-18 16:36:17 +01:00
|
|
|
let diff = Instant::now().inner.wrapping_sub(self.inner);
|
|
|
|
assert!(diff >= 0, "instant now is earlier than self");
|
|
|
|
Duration { inner: diff as u32 }
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the amount of time elapsed from another instant to this one.
|
|
|
|
pub fn duration_since(&self, earlier: Instant) -> Duration {
|
2019-11-18 16:36:17 +01:00
|
|
|
let diff = self.inner.wrapping_sub(earlier.inner);
|
2019-06-13 23:56:59 +02:00
|
|
|
assert!(diff >= 0, "second instant is later than self");
|
|
|
|
Duration { inner: diff as u32 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Instant {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
f.debug_tuple("Instant")
|
|
|
|
.field(&(self.inner as u32))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::AddAssign<Duration> for Instant {
|
|
|
|
fn add_assign(&mut self, dur: Duration) {
|
2019-10-16 02:11:35 +02:00
|
|
|
// NOTE this is a debug assertion because there's no foolproof way to detect a wrap around;
|
|
|
|
// the user may write `(instant + dur) + dur` where `dur` is `(1<<31)-1` ticks.
|
2019-06-13 23:56:59 +02:00
|
|
|
debug_assert!(dur.inner < (1 << 31));
|
|
|
|
self.inner = self.inner.wrapping_add(dur.inner as i32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add<Duration> for Instant {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(mut self, dur: Duration) -> Self {
|
|
|
|
self += dur;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::SubAssign<Duration> for Instant {
|
|
|
|
fn sub_assign(&mut self, dur: Duration) {
|
2019-10-16 02:11:35 +02:00
|
|
|
// NOTE see the NOTE in `<Instant as AddAssign<Duration>>::add_assign`
|
2019-06-13 23:56:59 +02:00
|
|
|
debug_assert!(dur.inner < (1 << 31));
|
|
|
|
self.inner = self.inner.wrapping_sub(dur.inner as i32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<Duration> for Instant {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(mut self, dur: Duration) -> Self {
|
|
|
|
self -= dur;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<Instant> for Instant {
|
|
|
|
type Output = Duration;
|
|
|
|
|
|
|
|
fn sub(self, other: Instant) -> Duration {
|
|
|
|
self.duration_since(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ord for Instant {
|
|
|
|
fn cmp(&self, rhs: &Self) -> Ordering {
|
|
|
|
self.inner.wrapping_sub(rhs.inner).cmp(&0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialOrd for Instant {
|
|
|
|
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> {
|
|
|
|
Some(self.cmp(rhs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A `Duration` type to represent a span of time.
|
|
|
|
///
|
|
|
|
/// This data type is only available on ARMv7-M
|
2019-10-16 02:11:35 +02:00
|
|
|
///
|
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// This type is *not* appropriate for representing time spans in the order of, or larger than,
|
|
|
|
/// seconds because it can hold a maximum of `(1 << 31)` "ticks" where each tick is the inverse of
|
|
|
|
/// the CPU frequency, which usually is dozens of MHz.
|
2019-06-13 23:56:59 +02:00
|
|
|
#[derive(Clone, Copy, Default, Eq, Ord, PartialEq, PartialOrd)]
|
|
|
|
pub struct Duration {
|
|
|
|
inner: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Duration {
|
2019-06-24 14:09:12 +02:00
|
|
|
/// Creates a new `Duration` from the specified number of clock cycles
|
|
|
|
pub fn from_cycles(cycles: u32) -> Self {
|
|
|
|
Duration { inner: cycles }
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
/// Returns the total number of clock cycles contained by this `Duration`
|
|
|
|
pub fn as_cycles(&self) -> u32 {
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryInto<u32> for Duration {
|
|
|
|
type Error = Infallible;
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<u32, Infallible> {
|
|
|
|
Ok(self.as_cycles())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::AddAssign for Duration {
|
|
|
|
fn add_assign(&mut self, dur: Duration) {
|
|
|
|
self.inner += dur.inner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Add<Duration> for Duration {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
|
|
Duration {
|
|
|
|
inner: self.inner + other.inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::SubAssign for Duration {
|
|
|
|
fn sub_assign(&mut self, rhs: Duration) {
|
|
|
|
self.inner -= rhs.inner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ops::Sub<Duration> for Duration {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn sub(self, rhs: Self) -> Self {
|
|
|
|
Duration {
|
|
|
|
inner: self.inner - rhs.inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds the `cycles` method to the `u32` type
|
|
|
|
///
|
|
|
|
/// This trait is only available on ARMv7-M
|
|
|
|
pub trait U32Ext {
|
|
|
|
/// Converts the `u32` value into clock cycles
|
|
|
|
fn cycles(self) -> Duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl U32Ext for u32 {
|
|
|
|
fn cycles(self) -> Duration {
|
|
|
|
Duration { inner: self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementation of the `Monotonic` trait based on CYCle CouNTer
|
|
|
|
pub struct CYCCNT;
|
|
|
|
|
2019-06-24 14:09:12 +02:00
|
|
|
impl crate::Monotonic for CYCCNT {
|
2019-06-13 23:56:59 +02:00
|
|
|
type Instant = Instant;
|
|
|
|
|
2019-07-11 13:28:25 +02:00
|
|
|
fn ratio() -> Fraction {
|
|
|
|
Fraction {
|
|
|
|
numerator: 1,
|
|
|
|
denominator: 1,
|
|
|
|
}
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn reset() {
|
|
|
|
(0xE0001004 as *mut u32).write_volatile(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn now() -> Instant {
|
|
|
|
Instant::now()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn zero() -> Instant {
|
2019-10-16 02:12:05 +02:00
|
|
|
Instant { inner: 0 }
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|