Skip to content

Commit 03d2011

Browse files
committed
Add watchdog test
1 parent 297866e commit 03d2011

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

testsuite/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ harness = false
3232
name = "gpio_input_puller"
3333
harness = false
3434

35+
[[test]]
36+
name = "watchdog"
37+
harness = false
38+
3539
[dev-dependencies]
3640
cfg-if = "1.0"
3741
cortex-m = "0.7.0"

testsuite/tests/watchdog.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use defmt_rtt as _;
5+
use panic_probe as _;
6+
7+
use cortex_m::asm;
8+
9+
use core::convert::TryFrom;
10+
11+
use hal::prelude::*;
12+
use stm32f3xx_hal as hal;
13+
14+
use hal::rcc::Clocks;
15+
use hal::watchdog::IndependentWatchDog;
16+
17+
struct State {
18+
iwdg: IndependentWatchDog,
19+
clocks: Clocks,
20+
}
21+
22+
#[defmt_test::tests]
23+
mod tests {
24+
use super::*;
25+
use defmt::{assert_eq, unwrap};
26+
use hal::time::duration::*; // imports all duration-related types and traits
27+
use hal::time::duration::{Duration, Milliseconds, Nanoseconds, Seconds};
28+
use hal::time::rate::Rate;
29+
use hal::time::rate::*; // imports all rate-related types and traits
30+
use hal::time::TimeInt;
31+
32+
const INTERVAL: Milliseconds = Milliseconds(100u32);
33+
34+
#[init]
35+
fn init() -> State {
36+
let dp = unwrap!(hal::pac::Peripherals::take());
37+
38+
let mut rcc = dp.RCC.constrain();
39+
let mut flash = dp.FLASH.constrain();
40+
41+
// Watchdog makes sure this gets restarted periodically if nothing happens
42+
let mut iwdg = IndependentWatchDog::new(dp.IWDG);
43+
44+
// iwdg.stop_on_debug(&dp.DBGMCU, true);
45+
iwdg.start(INTERVAL);
46+
47+
State {
48+
iwdg,
49+
clocks: rcc.cfgr.freeze(&mut flash.acr),
50+
}
51+
}
52+
53+
#[test]
54+
fn setup_equals_interval(state: &mut State) {
55+
state.iwdg.feed();
56+
assert!(state.iwdg.interval() == INTERVAL);
57+
}
58+
59+
#[test]
60+
// A watchdog trigger is not cleanly oberserable other than that
61+
// the probe-run output is repeated many times (as the core is restareted).
62+
fn feed(state: &mut State) {
63+
// Calculate some overhead which is introduced by asm::delay
64+
let interval_wo_overhead = INTERVAL - 35u32.milliseconds();
65+
let delay: u32 = (u32::try_from(
66+
Nanoseconds::from(interval_wo_overhead).integer()
67+
/ u64::from(
68+
state
69+
.clocks
70+
.sysclk()
71+
.to_duration::<Nanoseconds>()
72+
.unwrap()
73+
.integer(),
74+
),
75+
)
76+
.unwrap());
77+
defmt::info!("Delay = {}", delay);
78+
for _ in 0..5 {
79+
state.iwdg.feed();
80+
81+
asm::delay(delay);
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)