|
8 | 8 |
|
9 | 9 | use core::convert::{From, TryFrom};
|
10 | 10 |
|
| 11 | +use cortex_m::peripheral::DWT; |
| 12 | +use void::Void; |
| 13 | + |
11 | 14 | use crate::hal::timer::{CountDown, Periodic};
|
12 | 15 | #[cfg(any(
|
13 | 16 | feature = "stm32f301",
|
@@ -58,7 +61,51 @@ use crate::pac::{TIM15, TIM16, TIM17, TIM2, TIM6};
|
58 | 61 | use crate::pac::{TIM3, TIM7};
|
59 | 62 | use crate::rcc::{Clocks, APB1, APB2};
|
60 | 63 | 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 | +} |
62 | 109 |
|
63 | 110 | /// Associated clocks with timers
|
64 | 111 | pub trait PclkSrc {
|
|
0 commit comments