Skip to content

Commit 64036b0

Browse files
bors[bot]JOE1994
andauthored
Merge #160
160: doc comment updates for 'src/dwt.rs' r=therealprof a=JOE1994 Closes #158 This commit contains the following changes * new doc comment on conditions of measuring time with DWT Cycle Counter (that using Cycle Counter cannot correctly measure time of tasks that take longer than `u32::MAX`) * minor changes to doc comments to improve visual formatting Thank you for reviewing this PR 👍 Co-authored-by: JOE1994 <[email protected]>
2 parents 0668e1c + a90ee83 commit 64036b0

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/dwt.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ impl DwtExt for DWT {
2121
}
2222
}
2323

24+
/// DWT (Data Watchpoint and Trace) unit
2425
pub struct Dwt {
2526
dwt: DWT,
2627
dcb: DCB,
@@ -45,7 +46,11 @@ impl Dwt {
4546
pub fn stopwatch<'i>(&self, times: &'i mut [u32]) -> StopWatch<'i> {
4647
StopWatch::new(times, self.clocks.hclk())
4748
}
48-
/// Measure cycles it takes to execute f
49+
/// Measure cycles it takes to execute closure `f`.
50+
///
51+
/// Since DWT Cycle Counter is a 32-bit counter that wraps around to 0 on overflow,
52+
/// users should be aware that `Dwt::measure` cannot correctly measure running time of
53+
/// closures which take longer than `u32::MAX` cycles
4954
pub fn measure<F: FnOnce()>(&self, f: F) -> ClockDuration {
5055
let mut times: [u32; 2] = [0; 2];
5156
let mut sw = self.stopwatch(&mut times);
@@ -59,7 +64,7 @@ pub struct Delay {
5964
clock: Hertz,
6065
}
6166
impl Delay {
62-
/// Delay for ClockDuration::ticks
67+
/// Delay for `ClockDuration::ticks`
6368
pub fn delay(duration: ClockDuration) {
6469
let ticks = duration.ticks as u64;
6570
Delay::delay_ticks(DWT::get_cycle_count(), ticks);
@@ -112,7 +117,11 @@ impl<T: Into<u64>> DelayMs<T> for Delay {
112117
}
113118
}
114119

115-
/// Very simple stopwatch
120+
/// Very simple stopwatch which reads from DWT Cycle Counter to record timing.
121+
///
122+
/// Since DWT Cycle Counter is a 32-bit counter that wraps around to 0 on overflow,
123+
/// users should be aware that `StopWatch` cannot correctly measure laps
124+
/// which take longer than `u32::MAX` cycles
116125
pub struct StopWatch<'l> {
117126
times: &'l mut [u32],
118127
timei: usize,
@@ -142,8 +151,9 @@ impl<'l> StopWatch<'l> {
142151
self.timei = 0;
143152
self.times[0] = DWT::get_cycle_count();
144153
}
145-
/// Record a new lap
146-
/// NOTE If lap count exceeds maximum, the last lap is updated
154+
/// Record a new lap.
155+
///
156+
/// If lap count exceeds maximum, the last lap is updated
147157
pub fn lap(&mut self) -> &mut Self {
148158
let c = DWT::get_cycle_count();
149159
if self.timei < self.times.len() {
@@ -152,8 +162,9 @@ impl<'l> StopWatch<'l> {
152162
self.times[self.timei] = c;
153163
self
154164
}
155-
/// Calculate the time of lap n (n starting with 1)
156-
/// NOTE Returns None if 'n' is out of range
165+
/// Calculate the time of lap n (n starting with 1).
166+
///
167+
/// Returns None if `n` is out of range
157168
pub fn lap_time(&self, n: usize) -> Option<ClockDuration> {
158169
if (n < 1) || (self.timei < n) {
159170
None

0 commit comments

Comments
 (0)