Skip to content

Commit 7326c2a

Browse files
committed
don't own DWT
This is fine as long as the user does not change or disable the cycle counter. It allows the other DWT features to be used freely by the application.
1 parent 2303279 commit 7326c2a

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/lib.rs

Lines changed: 9 additions & 10 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

@@ -18,10 +18,11 @@ use rtic_monotonic::Monotonic;
1818
/// Note that the SysTick interrupt must not be disabled longer than half the
1919
/// cycle counter overflow period (typically a couple seconds).
2020
///
21+
/// Note(Safety): Do not disable/enable or set/reset the DWT cycle counter.
22+
///
2123
/// When the `extend` feature is enabled, the cycle counter width is extended to
2224
/// `u64` by detecting and counting overflows.
2325
pub struct DwtSystick<const TIMER_HZ: u32> {
24-
dwt: DWT,
2526
systick: SYST,
2627
#[cfg(feature = "extend")]
2728
last: u64,
@@ -34,18 +35,15 @@ impl<const TIMER_HZ: u32> DwtSystick<TIMER_HZ> {
3435
/// so the speed calculated at runtime and the declared speed (generic parameter
3536
/// `TIMER_HZ`) can be compared.
3637
#[inline(always)]
37-
pub fn new(dcb: &mut DCB, dwt: DWT, systick: SYST, sysclk: u32) -> Self {
38+
pub fn new(dcb: &mut DCB, systick: SYST, sysclk: u32) -> Self {
3839
assert!(TIMER_HZ == sysclk);
3940

4041
dcb.enable_trace();
4142
DWT::unlock();
4243

43-
unsafe { dwt.cyccnt.write(0) };
44-
4544
// We do not start the counter here, it is started in `reset`.
4645

4746
DwtSystick {
48-
dwt,
4947
systick,
5048
#[cfg(feature = "extend")]
5149
last: 0,
@@ -63,7 +61,7 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
6361

6462
#[inline(always)]
6563
fn now(&mut self) -> Self::Instant {
66-
Self::Instant::from_ticks(self.dwt.cyccnt.read())
64+
Self::Instant::from_ticks(DWT::cycle_count())
6765
}
6866
} else {
6967
// Need to detect and track overflows.
@@ -76,7 +74,7 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
7674
fn now(&mut self) -> Self::Instant {
7775
let mut high = (self.last >> 32) as u32;
7876
let low = self.last as u32;
79-
let now = self.dwt.cyccnt.read();
77+
let now = DWT::cycle_count();
8078

8179
// Detect CYCCNT overflow
8280
if now < low {
@@ -90,12 +88,13 @@ impl<const TIMER_HZ: u32> Monotonic for DwtSystick<TIMER_HZ> {
9088
}
9189

9290
unsafe fn reset(&mut self) {
93-
self.dwt.enable_cycle_counter();
91+
// dwt.enable_cycle_counter();
92+
(*DWT::PTR).ctrl.modify(|r| r | 1);
9493

9594
self.systick.set_clock_source(SystClkSource::Core);
9695
self.systick.enable_counter();
9796

98-
self.dwt.cyccnt.write(0);
97+
(*DWT::PTR).cyccnt.write(0);
9998
}
10099

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

0 commit comments

Comments
 (0)