Skip to content

Commit 340026d

Browse files
committed
move MonoTimer to dwt
1 parent e31050a commit 340026d

File tree

3 files changed

+56
-53
lines changed

3 files changed

+56
-53
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12-
- Unify serial trait impls for embedded-hal 0.2 & 1.0
12+
- Move `MonoTimer` from `timer` to dwt mode [#448]
13+
- Unify serial trait impls for embedded-hal 0.2 & 1.0 [#447]
1314
- Add possibility to select Timer master mode
1415
- Add inherent impl of `embedded_hal::Pwm` methods on `Pwm`s [#439]
1516
- Use `embedded-dma` v0.2 [#440]
@@ -57,6 +58,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
5758
[#443]: https://github.com/stm32-rs/stm32f4xx-hal/pull/443
5859
[#441]: https://github.com/stm32-rs/stm32f4xx-hal/pull/441
5960
[#430]: https://github.com/stm32-rs/stm32f4xx-hal/pull/430
61+
[#447]: https://github.com/stm32-rs/stm32f4xx-hal/pull/447
62+
[#448]: https://github.com/stm32-rs/stm32f4xx-hal/pull/448
6063

6164
### Changed
6265

src/dwt.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,54 @@ impl ClockDuration {
226226
self.ticks as f64 / self.clock.raw() as f64
227227
}
228228
}
229+
230+
/// A monotonic non-decreasing timer
231+
///
232+
/// This uses the timer in the debug watch trace peripheral. This means, that if the
233+
/// core is stopped, the timer does not count up. This may be relevant if you are using
234+
/// cortex_m_semihosting::hprintln for debugging in which case the timer will be stopped
235+
/// while printing
236+
#[derive(Clone, Copy)]
237+
pub struct MonoTimer {
238+
frequency: Hertz,
239+
}
240+
241+
impl MonoTimer {
242+
/// Creates a new `Monotonic` timer
243+
pub fn new(mut dwt: DWT, mut dcb: DCB, clocks: &Clocks) -> Self {
244+
dcb.enable_trace();
245+
dwt.enable_cycle_counter();
246+
247+
// now the CYCCNT counter can't be stopped or reset
248+
drop(dwt);
249+
250+
MonoTimer {
251+
frequency: clocks.hclk(),
252+
}
253+
}
254+
255+
/// Returns the frequency at which the monotonic timer is operating at
256+
pub fn frequency(self) -> Hertz {
257+
self.frequency
258+
}
259+
260+
/// Returns an `Instant` corresponding to "now"
261+
pub fn now(self) -> Instant {
262+
Instant {
263+
now: DWT::cycle_count(),
264+
}
265+
}
266+
}
267+
268+
/// A measurement of a monotonically non-decreasing clock
269+
#[derive(Clone, Copy)]
270+
pub struct Instant {
271+
now: u32,
272+
}
273+
274+
impl Instant {
275+
/// Ticks elapsed since the `Instant` was created
276+
pub fn elapsed(self) -> u32 {
277+
DWT::cycle_count().wrapping_sub(self.now)
278+
}
279+
}

src/timer.rs

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(non_upper_case_globals)]
66

77
use cortex_m::peripheral::syst::SystClkSource;
8-
use cortex_m::peripheral::{DCB, DWT, SYST};
8+
use cortex_m::peripheral::SYST;
99
use embedded_hal::timer::{Cancel, CountDown, Periodic};
1010
use void::Void;
1111

@@ -154,57 +154,6 @@ impl Cancel for CountDownTimer<SYST> {
154154
}
155155
}
156156

157-
/// A monotonic non-decreasing timer
158-
///
159-
/// This uses the timer in the debug watch trace peripheral. This means, that if the
160-
/// core is stopped, the timer does not count up. This may be relevant if you are using
161-
/// cortex_m_semihosting::hprintln for debugging in which case the timer will be stopped
162-
/// while printing
163-
#[derive(Clone, Copy)]
164-
pub struct MonoTimer {
165-
frequency: Hertz,
166-
}
167-
168-
impl MonoTimer {
169-
/// Creates a new `Monotonic` timer
170-
pub fn new(mut dwt: DWT, mut dcb: DCB, clocks: &Clocks) -> Self {
171-
dcb.enable_trace();
172-
dwt.enable_cycle_counter();
173-
174-
// now the CYCCNT counter can't be stopped or reset
175-
drop(dwt);
176-
177-
MonoTimer {
178-
frequency: clocks.hclk(),
179-
}
180-
}
181-
182-
/// Returns the frequency at which the monotonic timer is operating at
183-
pub fn frequency(self) -> Hertz {
184-
self.frequency
185-
}
186-
187-
/// Returns an `Instant` corresponding to "now"
188-
pub fn now(self) -> Instant {
189-
Instant {
190-
now: DWT::cycle_count(),
191-
}
192-
}
193-
}
194-
195-
/// A measurement of a monotonically non-decreasing clock
196-
#[derive(Clone, Copy)]
197-
pub struct Instant {
198-
now: u32,
199-
}
200-
201-
impl Instant {
202-
/// Ticks elapsed since the `Instant` was created
203-
pub fn elapsed(self) -> u32 {
204-
DWT::cycle_count().wrapping_sub(self.now)
205-
}
206-
}
207-
208157
#[derive(Clone, Copy, Debug, PartialEq)]
209158
#[repr(u8)]
210159
pub enum Ocm {

0 commit comments

Comments
 (0)