Skip to content

Commit 8d08686

Browse files
committed
clean up new()
* Move the SysTick clock source configuration to `new()`. * Add comments about the double reset of the cycle counter.
1 parent f6c508f commit 8d08686

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,20 @@ impl<const TIMER_HZ: u32> DwtSystick<TIMER_HZ> {
3434
/// so the speed calculated at runtime and the declared speed (generic parameter
3535
/// `TIMER_HZ`) can be compared.
3636
#[inline(always)]
37-
pub fn new(dcb: &mut DCB, dwt: DWT, systick: SYST, sysclk: u32) -> Self {
37+
pub fn new(dcb: &mut DCB, dwt: DWT, mut systick: SYST, sysclk: u32) -> Self {
3838
assert!(TIMER_HZ == sysclk);
39-
assert!(DWT::has_cycle_counter());
4039

4140
dcb.enable_trace();
4241
DWT::unlock();
42+
assert!(DWT::has_cycle_counter());
4343

44+
// Clear the cycle counter here so scheduling (`set_compare()`) before
45+
// `reset()` works correctly.
4446
unsafe { dwt.cyccnt.write(0) };
4547

46-
// We do not start the counter here, it is started in `reset`.
48+
systick.set_clock_source(SystClkSource::Core);
49+
50+
// We do not start the counters here but in `reset()`.
4751

4852
DwtSystick {
4953
dwt,
@@ -93,9 +97,9 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
9397
unsafe fn reset(&mut self) {
9498
self.dwt.enable_cycle_counter();
9599

96-
self.systick.set_clock_source(SystClkSource::Core);
97100
self.systick.enable_counter();
98101

102+
// Reset the cycle counter to locate the epoch.
99103
self.dwt.cyccnt.write(0);
100104
}
101105

0 commit comments

Comments
 (0)