1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use crate::rcc::Clocks;
use crate::time::Hertz;
use cortex_m::peripheral::{DCB, DWT};
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
pub trait DwtExt {
fn constrain(self, dcb: DCB, clocks: Clocks) -> Dwt;
}
impl DwtExt for DWT {
fn constrain(mut self, mut dcb: DCB, clocks: Clocks) -> Dwt {
dcb.enable_trace();
self.enable_cycle_counter();
Dwt {
dwt: self,
dcb,
clocks,
}
}
}
pub struct Dwt {
dwt: DWT,
dcb: DCB,
clocks: Clocks,
}
impl Dwt {
pub unsafe fn release(self) -> (DWT, DCB) {
(self.dwt, self.dcb)
}
pub fn delay(&self) -> Delay {
Delay {
clock: self.clocks.hclk(),
}
}
pub fn stopwatch<'i>(&self, times: &'i mut [u32]) -> StopWatch<'i> {
StopWatch::new(times, self.clocks.hclk())
}
pub fn measure<F: FnOnce()>(&self, f: F) -> ClockDuration {
let mut times: [u32; 2] = [0; 2];
let mut sw = self.stopwatch(&mut times);
f();
sw.lap().lap_time(1).unwrap()
}
}
#[derive(Clone, Copy)]
pub struct Delay {
clock: Hertz,
}
impl Delay {
pub fn delay(duration: ClockDuration) {
let ticks = duration.ticks as u64;
Delay::delay_ticks(DWT::get_cycle_count(), ticks);
}
fn delay_ticks(mut start: u32, ticks: u64) {
if ticks < (core::u32::MAX / 2) as u64 {
let ticks = ticks as u32;
while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {}
} else if ticks <= core::u32::MAX as u64 {
let mut ticks = ticks as u32;
ticks -= core::u32::MAX / 2;
while (DWT::get_cycle_count().wrapping_sub(start)) < core::u32::MAX / 2 {}
start -= core::u32::MAX / 2;
while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {}
} else {
let mut rest = (ticks >> 32) as u32;
let ticks = (ticks & core::u32::MAX as u64) as u32;
loop {
while (DWT::get_cycle_count().wrapping_sub(start)) < ticks {}
if rest == 0 {
break;
}
rest -= 1;
while (DWT::get_cycle_count().wrapping_sub(start)) > ticks {}
}
}
}
}
impl<T: Into<u64>> DelayUs<T> for Delay {
fn delay_us(&mut self, us: T) {
let start = DWT::get_cycle_count();
let ticks = (us.into() * self.clock.0 as u64) / 1_000_000;
Delay::delay_ticks(start, ticks);
}
}
impl<T: Into<u64>> DelayMs<T> for Delay {
fn delay_ms(&mut self, ms: T) {
let start = DWT::get_cycle_count();
let ticks = (ms.into() * self.clock.0 as u64) / 1_000;
Delay::delay_ticks(start, ticks);
}
}
pub struct StopWatch<'l> {
times: &'l mut [u32],
timei: usize,
clock: Hertz,
}
impl<'l> StopWatch<'l> {
fn new(times: &'l mut [u32], clock: Hertz) -> Self {
assert!(times.len() >= 2);
let mut sw = StopWatch {
times,
timei: 0,
clock,
};
sw.reset();
sw
}
pub fn lap_count(&self) -> usize {
self.timei
}
pub fn reset(&mut self) {
self.timei = 0;
self.times[0] = DWT::get_cycle_count();
}
pub fn lap(&mut self) -> &mut Self {
let c = DWT::get_cycle_count();
if self.timei < self.times.len() {
self.timei += 1;
}
self.times[self.timei] = c;
self
}
pub fn lap_time(&self, n: usize) -> Option<ClockDuration> {
if (n < 1) || (self.timei < n) {
None
} else {
Some(ClockDuration {
ticks: self.times[n].wrapping_sub(self.times[n - 1]),
clock: self.clock,
})
}
}
}
#[derive(Clone, Copy)]
pub struct ClockDuration {
ticks: u32,
clock: Hertz,
}
impl ClockDuration {
pub fn as_ticks(self) -> u32 {
self.ticks
}
pub fn as_millis(self) -> u64 {
self.ticks as u64 * 1_000 / self.clock.0 as u64
}
pub fn as_micros(self) -> u64 {
self.ticks as u64 * 1_000_000 / self.clock.0 as u64
}
pub fn as_nanos(self) -> u64 {
self.ticks as u64 * 1_000_000_000 / self.clock.0 as u64
}
pub fn as_secs_f32(self) -> f32 {
self.ticks as f32 / self.clock.0 as f32
}
pub fn as_secs_f64(self) -> f64 {
self.ticks as f64 / self.clock.0 as f64
}
}