-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use defmt_rtt as _; | ||
use panic_probe as _; | ||
|
||
use cortex_m::peripheral::{self, DWT}; | ||
|
||
use stm32f3xx_hal as hal; | ||
|
||
use hal::rcc::APB1; | ||
use hal::timer::{MonoTimer, Timer}; | ||
use hal::{pac, prelude::*}; | ||
|
||
use pac::TIM2; | ||
|
||
struct State { | ||
timer: Option<Timer<TIM2>>, | ||
mono_timer: MonoTimer, | ||
apb1: APB1, | ||
} | ||
|
||
#[defmt_test::tests] | ||
mod tests { | ||
use super::*; | ||
use defmt::{self, assert, unwrap}; | ||
use hal::time::fixed_point::FixedPoint; | ||
|
||
#[init] | ||
fn init() -> State { | ||
let mut cp = unwrap!(peripheral::Peripherals::take()); | ||
let dp = unwrap!(pac::Peripherals::take()); | ||
|
||
let mut rcc = dp.RCC.constrain(); | ||
let mut flash = dp.FLASH.constrain(); | ||
let clocks = rcc.cfgr.freeze(&mut flash.acr); | ||
|
||
// Let's use a timer, which is avaliable for every chip | ||
let timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1); | ||
let mono_timer = MonoTimer::new(cp.DWT, clocks, &mut cp.DCB); | ||
|
||
assert!(mono_timer.frequency() == clocks.hclk()); | ||
|
||
State { | ||
timer: Some(timer), | ||
mono_timer, | ||
apb1: rcc.apb1, | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_delay(state: &mut State) { | ||
let mut timer = unwrap!(state.timer.take()); | ||
defmt::trace!("{}", state.mono_timer); | ||
let instant = state.mono_timer.now(); | ||
let freqcyc = state.mono_timer.frequency().integer(); | ||
|
||
timer.start(1.Hz()); | ||
unwrap!(nb::block!(timer.wait()).ok()); | ||
let elapsed = instant.elapsed(); | ||
|
||
defmt::info!("elapsed: {}", elapsed); | ||
assert!(((freqcyc - 1000)..(freqcyc + 1000)).contains(&elapsed)); | ||
|
||
state.timer = Some(timer); | ||
} | ||
} |