Skip to content

Commit

Permalink
Add timer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sh3Rm4n committed Aug 1, 2021
1 parent 0a16d09 commit 09bfcef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions testsuite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ harness = false
name = "rcc"
harness = false

[[test]]
name = "timer"
harness = false

[[test]]
name = "gpio_input"
harness = false
Expand Down
67 changes: 67 additions & 0 deletions testsuite/tests/timer.rs
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);
}
}

0 comments on commit 09bfcef

Please sign in to comment.