From bd01e90bfa4895b45e41ad538cb24a959b0b58ab Mon Sep 17 00:00:00 2001 From: chemicstry Date: Sun, 10 Jul 2022 20:38:30 +0300 Subject: Implement IWDG timeout calculation --- examples/stm32f4/src/bin/wdt.rs | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/stm32f4/src/bin/wdt.rs (limited to 'examples') diff --git a/examples/stm32f4/src/bin/wdt.rs b/examples/stm32f4/src/bin/wdt.rs new file mode 100644 index 000000000..41e1f4c7b --- /dev/null +++ b/examples/stm32f4/src/bin/wdt.rs @@ -0,0 +1,44 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +use defmt::*; +use embassy::executor::Spawner; +use embassy::time::{Duration, Timer}; +use embassy_stm32::gpio::{Level, Output, Speed}; +use embassy_stm32::wdg::IndependentWatchdog; +use embassy_stm32::Peripherals; +use {defmt_rtt as _, panic_probe as _}; + +#[embassy::main] +async fn main(_spawner: Spawner, p: Peripherals) { + info!("Hello World!"); + + let mut led = Output::new(p.PB7, Level::High, Speed::Low); + + let mut wdt = IndependentWatchdog::new(p.IWDG, Duration::from_secs(1)); + unsafe { + wdt.unleash(); + } + + let mut i = 0; + + loop { + info!("high"); + led.set_high(); + Timer::after(Duration::from_millis(300)).await; + + info!("low"); + led.set_low(); + Timer::after(Duration::from_millis(300)).await; + + if i < 5 { + info!("Petting watchdog"); + unsafe { + wdt.pet(); + } + } + + i += 1; + } +} -- cgit From bd741a40191d127e804d668366e92a8fc43c5b18 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Sun, 10 Jul 2022 21:08:30 +0300 Subject: Add comments to watchdog example --- examples/stm32f4/src/bin/wdt.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'examples') diff --git a/examples/stm32f4/src/bin/wdt.rs b/examples/stm32f4/src/bin/wdt.rs index 41e1f4c7b..a23cfe9c0 100644 --- a/examples/stm32f4/src/bin/wdt.rs +++ b/examples/stm32f4/src/bin/wdt.rs @@ -32,6 +32,8 @@ async fn main(_spawner: Spawner, p: Peripherals) { led.set_low(); Timer::after(Duration::from_millis(300)).await; + // Pet watchdog for 5 iterations and then stop. + // MCU should restart in 1 second after the last pet. if i < 5 { info!("Petting watchdog"); unsafe { -- cgit From d7d1e46a5fb115b0993d4ac37c0152b1dd5f78c3 Mon Sep 17 00:00:00 2001 From: chemicstry Date: Mon, 11 Jul 2022 00:00:33 +0300 Subject: Use u32 instead of Duration for IWDG --- examples/stm32f4/src/bin/wdt.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/stm32f4/src/bin/wdt.rs b/examples/stm32f4/src/bin/wdt.rs index a23cfe9c0..bfc487c31 100644 --- a/examples/stm32f4/src/bin/wdt.rs +++ b/examples/stm32f4/src/bin/wdt.rs @@ -16,7 +16,7 @@ async fn main(_spawner: Spawner, p: Peripherals) { let mut led = Output::new(p.PB7, Level::High, Speed::Low); - let mut wdt = IndependentWatchdog::new(p.IWDG, Duration::from_secs(1)); + let mut wdt = IndependentWatchdog::new(p.IWDG, 1_000_000); unsafe { wdt.unleash(); } -- cgit