Skip to content

Readd MonoTimer #247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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