Skip to content

Commit 7c48cf2

Browse files
authored
Merge pull request #247 from Sh3Rm4n/monotimer
Readd MonoTimer
2 parents 0f751b2 + a2d0c31 commit 7c48cf2

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10-
No changes.
10+
### Added
11+
12+
- Readd MonoTimer. This was accidentally removed before. [#247]
1113

1214
## [v0.7.0] - 2021-06-18
1315

@@ -333,6 +335,7 @@ let clocks = rcc
333335
[defmt]: https://github.com/knurling-rs/defmt
334336
[filter]: https://defmt.ferrous-systems.com/filtering.html
335337

338+
[#247]: https://github.com/stm32-rs/stm32f3xx-hal/pull/247
336339
[#238]: https://github.com/stm32-rs/stm32f3xx-hal/pull/238
337340
[#234]: https://github.com/stm32-rs/stm32f3xx-hal/pull/234
338341
[#232]: https://github.com/stm32-rs/stm32f3xx-hal/pull/232

src/timer.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
99
use core::convert::{From, TryFrom};
1010

11+
use cortex_m::peripheral::DWT;
12+
use void::Void;
13+
1114
use crate::hal::timer::{CountDown, Periodic};
1215
#[cfg(any(
1316
feature = "stm32f301",
@@ -58,7 +61,51 @@ use crate::pac::{TIM15, TIM16, TIM17, TIM2, TIM6};
5861
use crate::pac::{TIM3, TIM7};
5962
use crate::rcc::{Clocks, APB1, APB2};
6063
use crate::time::rate::*;
61-
use void::Void;
64+
65+
/// A monotonic nondecreasing timer.
66+
#[derive(Clone, Copy)]
67+
pub struct MonoTimer {
68+
frequency: Hertz,
69+
}
70+
71+
impl MonoTimer {
72+
/// Creates a new `Monotonic` timer
73+
pub fn new(mut dwt: DWT, clocks: Clocks) -> Self {
74+
dwt.enable_cycle_counter();
75+
76+
// now the CYCCNT counter can't be stopped or resetted
77+
drop(dwt);
78+
79+
MonoTimer {
80+
frequency: clocks.hclk(),
81+
}
82+
}
83+
84+
/// Returns the frequency at which the monotonic timer is operating at
85+
pub fn frequency(self) -> Hertz {
86+
self.frequency
87+
}
88+
89+
/// Returns an `Instant` corresponding to "now"
90+
pub fn now(self) -> Instant {
91+
Instant {
92+
now: DWT::get_cycle_count(),
93+
}
94+
}
95+
}
96+
97+
/// A measurement of a monotonically nondecreasing clock
98+
#[derive(Clone, Copy)]
99+
pub struct Instant {
100+
now: u32,
101+
}
102+
103+
impl Instant {
104+
/// Ticks elapsed since the `Instant` was created
105+
pub fn elapsed(self) -> u32 {
106+
DWT::get_cycle_count().wrapping_sub(self.now)
107+
}
108+
}
62109

63110
/// Associated clocks with timers
64111
pub trait PclkSrc {

0 commit comments

Comments
 (0)