Skip to content

Commit a27db2c

Browse files
committed
use DWT for no-sideeffect ops
1 parent 8d08686 commit a27db2c

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! # `Monotonic` implementation based on DWT and SysTick
1+
//! # `Monotonic` implementation based on DWT cycle counter and SysTick
22
33
#![no_std]
44

@@ -34,16 +34,16 @@ 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, mut systick: SYST, sysclk: u32) -> Self {
37+
pub fn new(dcb: &mut DCB, mut dwt: DWT, mut systick: SYST, sysclk: u32) -> Self {
3838
assert!(TIMER_HZ == sysclk);
3939

4040
dcb.enable_trace();
4141
DWT::unlock();
4242
assert!(DWT::has_cycle_counter());
4343

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

4848
systick.set_clock_source(SystClkSource::Core);
4949

@@ -68,7 +68,7 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
6868

6969
#[inline(always)]
7070
fn now(&mut self) -> Self::Instant {
71-
Self::Instant::from_ticks(self.dwt.cyccnt.read())
71+
Self::Instant::from_ticks(DWT::cycle_count())
7272
}
7373
} else {
7474
// Need to detect and track overflows.
@@ -81,7 +81,7 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
8181
fn now(&mut self) -> Self::Instant {
8282
let mut high = (self.last >> 32) as u32;
8383
let low = self.last as u32;
84-
let now = self.dwt.cyccnt.read();
84+
let now = DWT::cycle_count();
8585

8686
// Detect CYCCNT overflow
8787
if now < low {
@@ -95,12 +95,11 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
9595
}
9696

9797
unsafe fn reset(&mut self) {
98-
self.dwt.enable_cycle_counter();
99-
10098
self.systick.enable_counter();
10199

102-
// Reset the cycle counter to locate the epoch.
103-
self.dwt.cyccnt.write(0);
100+
// Enable and reset the cycle counter to locate the epoch.
101+
self.dwt.enable_cycle_counter();
102+
self.dwt.set_cycle_count(0);
104103
}
105104

106105
fn set_compare(&mut self, val: Self::Instant) {

0 commit comments

Comments
 (0)