From dc1c8a5c81a6c98aedc80f68939bbef0bb5f7ef8 Mon Sep 17 00:00:00 2001 From: Christian Meusel Date: Sat, 2 Oct 2021 23:45:43 +0200 Subject: [PATCH] Switch back to MonoTimer from HAL This switches back from the local replacement for MonoTimer from 814c051 to the one re-added with HAL release 0.8.0. MonoTimers interface changed in the meantime so this commit takes this into account as well. --- src/11-usart/auxiliary/src/lib.rs | 8 ++-- src/11-usart/auxiliary/src/monotimer.rs | 54 ------------------------- 2 files changed, 3 insertions(+), 59 deletions(-) delete mode 100644 src/11-usart/auxiliary/src/monotimer.rs diff --git a/src/11-usart/auxiliary/src/lib.rs b/src/11-usart/auxiliary/src/lib.rs index ac126c80d..dedbe5c7d 100644 --- a/src/11-usart/auxiliary/src/lib.rs +++ b/src/11-usart/auxiliary/src/lib.rs @@ -9,17 +9,15 @@ pub use cortex_m::{asm::bkpt, iprint, iprintln, peripheral::ITM}; pub use cortex_m_rt::entry; pub use stm32f3_discovery::stm32f3xx_hal::pac::usart1; -pub mod monotimer; - use stm32f3_discovery::stm32f3xx_hal::{ prelude::*, serial::Serial, pac::{self, USART1}, + timer::MonoTimer, }; -use monotimer::MonoTimer; pub fn init() -> (&'static mut usart1::RegisterBlock, MonoTimer, ITM) { - let cp = cortex_m::Peripherals::take().unwrap(); + let mut cp = cortex_m::Peripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap(); let mut flash = dp.FLASH.constrain(); @@ -56,7 +54,7 @@ pub fn init() -> (&'static mut usart1::RegisterBlock, MonoTimer, ITM) { unsafe { ( &mut *(USART1::ptr() as *mut _), - MonoTimer::new(cp.DWT, clocks), + MonoTimer::new(cp.DWT, clocks, &mut cp.DCB), cp.ITM, ) } diff --git a/src/11-usart/auxiliary/src/monotimer.rs b/src/11-usart/auxiliary/src/monotimer.rs deleted file mode 100644 index be1d49fc3..000000000 --- a/src/11-usart/auxiliary/src/monotimer.rs +++ /dev/null @@ -1,54 +0,0 @@ -use stm32f3_discovery::stm32f3xx_hal as hal; - -use cortex_m::peripheral::DWT; -use hal::{ - rcc::Clocks, - time::rate::Hertz, -}; - -/// A monotonic nondecreasing timer. This is a resurrection of MonoTimer from -/// the stm32f3xx-hal where it got removed after 0.6.1. -#[derive(Clone, Copy)] -pub struct MonoTimer { - frequency: Hertz, -} - -// TODO: What about a refactoring to implement Clock from embedded-time? -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) - } -}