|
| 1 | +//! Debug and trace and stuff |
| 2 | +
|
| 3 | +use crate::rcc::Clocks; |
| 4 | +use crate::time::Hertz; |
| 5 | +use cortex_m::peripheral::{DCB, DWT}; |
| 6 | +use embedded_hal::blocking::delay::{DelayMs, DelayUs}; |
| 7 | + |
| 8 | +pub trait DwtExt { |
| 9 | + fn constrain(self, dcb: DCB, clocks: Clocks) -> Dwt; |
| 10 | +} |
| 11 | +impl DwtExt for DWT { |
| 12 | + /// Enable trace unit and cycle counter |
| 13 | + fn constrain(mut self, mut dcb: DCB, clocks: Clocks) -> Dwt { |
| 14 | + dcb.enable_trace(); |
| 15 | + self.enable_cycle_counter(); |
| 16 | + Dwt { |
| 17 | + dwt: self, |
| 18 | + dcb, |
| 19 | + clocks, |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +pub struct Dwt { |
| 25 | + dwt: DWT, |
| 26 | + dcb: DCB, |
| 27 | + clocks: Clocks, |
| 28 | +} |
| 29 | +impl Dwt { |
| 30 | + /// Release the dwt and dcb control |
| 31 | + /// # Safety |
| 32 | + /// All instances of Delay and StopWatch become invalid after this |
| 33 | + pub unsafe fn release(self) -> (DWT, DCB) { |
| 34 | + (self.dwt, self.dcb) |
| 35 | + } |
| 36 | + /// Create a delay instance |
| 37 | + pub fn delay(&self) -> Delay { |
| 38 | + Delay { |
| 39 | + clock: self.clocks.hclk(), |
| 40 | + } |
| 41 | + } |
| 42 | + /// Create a stopwatch instance |
| 43 | + /// # Arguments |
| 44 | + /// * `times` - Array which will be holding the timings in ticks (max laps == times.len()-1) |
| 45 | + pub fn stopwatch<'i>(&self, times: &'i mut [u32]) -> StopWatch<'i> { |
| 46 | + StopWatch::new(times, self.clocks.hclk()) |
| 47 | + } |
| 48 | + /// Measure cycles it takes to execute f |
| 49 | + pub fn measure<F: FnOnce()>(&self, f: F) -> ClockDuration { |
| 50 | + let mut times: [u32; 2] = [0; 2]; |
| 51 | + let mut sw = self.stopwatch(&mut times); |
| 52 | + f(); |
| 53 | + sw.lap().lap_time(1).unwrap() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Clone, Copy)] |
| 58 | +pub struct Delay { |
| 59 | + clock: Hertz, |
| 60 | +} |
| 61 | +impl Delay { |
| 62 | + /// Delay for ClockDuration::ticks |
| 63 | + pub fn delay(duration: ClockDuration) { |
| 64 | + let ticks = duration.ticks as u64; |
| 65 | + Delay::delay_ticks(DWT::get_cycle_count(), ticks); |
| 66 | + } |
| 67 | + /// Delay ticks |
| 68 | + /// NOTE DCB and DWT need to be set up for this to work, so it is private |
| 69 | + fn delay_ticks(mut start: u32, ticks: u64) { |
| 70 | + if ticks < (u32::MAX / 2) as u64 { |
| 71 | + // Simple delay |
| 72 | + let ticks = ticks as u32; |
| 73 | + while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {} |
| 74 | + } else if ticks <= u32::MAX as u64 { |
| 75 | + // Try to avoid race conditions by limiting delay to u32::MAX / 2 |
| 76 | + let mut ticks = ticks as u32; |
| 77 | + ticks -= u32::MAX / 2; |
| 78 | + while (DWT::get_cycle_count().wrapping_sub(start)) < u32::MAX / 2 {} |
| 79 | + start -= u32::MAX / 2; |
| 80 | + while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {} |
| 81 | + } else { |
| 82 | + // Delay for ticks, then delay for rest * u32::MAX |
| 83 | + let mut rest = (ticks >> 32) as u32; |
| 84 | + let ticks = (ticks & u32::MAX as u64) as u32; |
| 85 | + loop { |
| 86 | + while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {} |
| 87 | + if rest == 0 { |
| 88 | + break; |
| 89 | + } |
| 90 | + rest -= 1; |
| 91 | + while (DWT::get_cycle_count().wrapping_sub(start)) > ticks {} |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +// Implement DelayUs/DelayMs for various integer types |
| 98 | +impl<T: Into<u64>> DelayUs<T> for Delay { |
| 99 | + fn delay_us(&mut self, us: T) { |
| 100 | + // Convert us to ticks |
| 101 | + let start = DWT::get_cycle_count(); |
| 102 | + let ticks = (us.into() * self.clock.0 as u64) / 1_000_000; |
| 103 | + Delay::delay_ticks(start, ticks); |
| 104 | + } |
| 105 | +} |
| 106 | +impl<T: Into<u64>> DelayMs<T> for Delay { |
| 107 | + fn delay_ms(&mut self, ms: T) { |
| 108 | + // Convert ms to ticks |
| 109 | + let start = DWT::get_cycle_count(); |
| 110 | + let ticks = (ms.into() * self.clock.0 as u64) / 1_000; |
| 111 | + Delay::delay_ticks(start, ticks); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +/// Very simple stopwatch |
| 116 | +pub struct StopWatch<'l> { |
| 117 | + times: &'l mut [u32], |
| 118 | + timei: usize, |
| 119 | + clock: Hertz, |
| 120 | +} |
| 121 | +impl<'l> StopWatch<'l> { |
| 122 | + /// Create a new instance (Private because dwt/dcb should be set up) |
| 123 | + /// # Arguments |
| 124 | + /// * `times` - Array which will be holding the timings (max laps == times.len()-1) |
| 125 | + /// * `clock` - The DWT cycle counters clock |
| 126 | + fn new(times: &'l mut [u32], clock: Hertz) -> Self { |
| 127 | + assert!(times.len() >= 2); |
| 128 | + let mut sw = StopWatch { |
| 129 | + times, |
| 130 | + timei: 0, |
| 131 | + clock, |
| 132 | + }; |
| 133 | + sw.reset(); |
| 134 | + sw |
| 135 | + } |
| 136 | + /// Returns the numbers of laps recorded |
| 137 | + pub fn lap_count(&self) -> usize { |
| 138 | + self.timei |
| 139 | + } |
| 140 | + /// Resets recorded laps to 0 and sets 0 offset |
| 141 | + pub fn reset(&mut self) { |
| 142 | + self.timei = 0; |
| 143 | + self.times[0] = DWT::get_cycle_count(); |
| 144 | + } |
| 145 | + /// Record a new lap |
| 146 | + /// NOTE If lap count exceeds maximum, the last lap is updated |
| 147 | + pub fn lap(&mut self) -> &mut Self { |
| 148 | + let c = DWT::get_cycle_count(); |
| 149 | + if self.timei < self.times.len() { |
| 150 | + self.timei += 1; |
| 151 | + } |
| 152 | + self.times[self.timei] = c; |
| 153 | + self |
| 154 | + } |
| 155 | + /// Calculate the time of lap n (n starting with 1) |
| 156 | + /// NOTE Returns None if 'n' is out of range |
| 157 | + pub fn lap_time(&self, n: usize) -> Option<ClockDuration> { |
| 158 | + if (n < 1) || (self.timei < n) { |
| 159 | + None |
| 160 | + } else { |
| 161 | + Some(ClockDuration { |
| 162 | + ticks: self.times[n].wrapping_sub(self.times[n - 1]), |
| 163 | + clock: self.clock, |
| 164 | + }) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/// Clock difference with capability to calculate SI units (s) |
| 170 | +#[derive(Clone, Copy)] |
| 171 | +pub struct ClockDuration { |
| 172 | + ticks: u32, |
| 173 | + clock: Hertz, |
| 174 | +} |
| 175 | +impl ClockDuration { |
| 176 | + /// Returns ticks |
| 177 | + pub fn as_ticks(self) -> u32 { |
| 178 | + self.ticks |
| 179 | + } |
| 180 | + /// Returns calculated milliseconds as integer |
| 181 | + pub fn as_millis(self) -> u64 { |
| 182 | + self.ticks as u64 * 1_000 / self.clock.0 as u64 |
| 183 | + } |
| 184 | + /// Returns calculated microseconds as integer |
| 185 | + pub fn as_micros(self) -> u64 { |
| 186 | + self.ticks as u64 * 1_000_000 / self.clock.0 as u64 |
| 187 | + } |
| 188 | + /// Returns calculated nanoseconds as integer |
| 189 | + pub fn as_nanos(self) -> u64 { |
| 190 | + self.ticks as u64 * 1_000_000_000 / self.clock.0 as u64 |
| 191 | + } |
| 192 | + /// Return calculated seconds as 32-bit float |
| 193 | + pub fn as_secs_f32(self) -> f32 { |
| 194 | + self.ticks as f32 / self.clock.0 as f32 |
| 195 | + } |
| 196 | + /// Return calculated seconds as 64-bit float |
| 197 | + pub fn as_secs_f64(self) -> f64 { |
| 198 | + self.ticks as f64 / self.clock.0 as f64 |
| 199 | + } |
| 200 | +} |
0 commit comments