Skip to content

Commit

Permalink
Readd MonoTimer
Browse files Browse the repository at this point in the history
This was accidentally removed in stm32-rs#192
  • Loading branch information
Sh3Rm4n committed Jun 22, 2021
1 parent 0f751b2 commit a2d0c31
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

No changes.
### Added

- Readd MonoTimer. This was accidentally removed before. [#247]

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

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

[#247]: https://github.com/stm32-rs/stm32f3xx-hal/pull/247
[#238]: https://github.com/stm32-rs/stm32f3xx-hal/pull/238
[#234]: https://github.com/stm32-rs/stm32f3xx-hal/pull/234
[#232]: https://github.com/stm32-rs/stm32f3xx-hal/pull/232
Expand Down
49 changes: 48 additions & 1 deletion src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

use core::convert::{From, TryFrom};

use cortex_m::peripheral::DWT;
use void::Void;

use crate::hal::timer::{CountDown, Periodic};
#[cfg(any(
feature = "stm32f301",
Expand Down Expand Up @@ -58,7 +61,51 @@ use crate::pac::{TIM15, TIM16, TIM17, TIM2, TIM6};
use crate::pac::{TIM3, TIM7};
use crate::rcc::{Clocks, APB1, APB2};
use crate::time::rate::*;
use void::Void;

/// A monotonic nondecreasing timer.
#[derive(Clone, Copy)]
pub struct MonoTimer {
frequency: Hertz,
}

impl MonoTimer {
/// Creates a new `Monotonic` timer
pub fn new(mut dwt: DWT, clocks: Clocks) -> Self {
dwt.enable_cycle_counter();

// now the CYCCNT counter can't be stopped or resetted
drop(dwt);

MonoTimer {
frequency: clocks.hclk(),
}
}

/// Returns the frequency at which the monotonic timer is operating at
pub fn frequency(self) -> Hertz {
self.frequency
}

/// Returns an `Instant` corresponding to "now"
pub fn now(self) -> Instant {
Instant {
now: DWT::get_cycle_count(),
}
}
}

/// A measurement of a monotonically nondecreasing clock
#[derive(Clone, Copy)]
pub struct Instant {
now: u32,
}

impl Instant {
/// Ticks elapsed since the `Instant` was created
pub fn elapsed(self) -> u32 {
DWT::get_cycle_count().wrapping_sub(self.now)
}
}

/// Associated clocks with timers
pub trait PclkSrc {
Expand Down

0 comments on commit a2d0c31

Please sign in to comment.