diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-08-07 14:26:28 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-08-07 14:26:28 +0200 |
| commit | 2540a960e5d97473253a56198bf9205f4f3f7980 (patch) | |
| tree | e5e761284560fa55e3983839c0a4fd3ed18b320e /examples | |
| parent | e1abba69b762e586c680123fbe28290a298e2b41 (diff) | |
nrf: add WDT driver
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/nrf/src/bin/wdt.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/examples/nrf/src/bin/wdt.rs b/examples/nrf/src/bin/wdt.rs new file mode 100644 index 000000000..53df7b0b7 --- /dev/null +++ b/examples/nrf/src/bin/wdt.rs | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | #![allow(incomplete_features)] | ||
| 5 | |||
| 6 | #[path = "../example_common.rs"] | ||
| 7 | mod example_common; | ||
| 8 | |||
| 9 | use defmt::*; | ||
| 10 | use embassy::executor::Spawner; | ||
| 11 | use embassy_nrf::gpio::{Input, Pull}; | ||
| 12 | use embassy_nrf::gpiote::PortInput; | ||
| 13 | use embassy_nrf::wdt::{Config, Watchdog}; | ||
| 14 | use embassy_nrf::Peripherals; | ||
| 15 | use embassy_traits::gpio::{WaitForHigh, WaitForLow}; | ||
| 16 | |||
| 17 | #[embassy::main] | ||
| 18 | async fn main(_spawner: Spawner, p: Peripherals) { | ||
| 19 | info!("Hello World!"); | ||
| 20 | |||
| 21 | let mut config = Config::default(); | ||
| 22 | config.timeout_ticks = 32768 * 3; // 3 seconds | ||
| 23 | |||
| 24 | // This is needed for `probe-run` to be able to catch the panic message | ||
| 25 | // in the WDT interrupt. The core resets 2 ticks after firing the interrupt. | ||
| 26 | config.run_during_debug_halt = false; | ||
| 27 | |||
| 28 | let (_wdt, [mut handle]) = match Watchdog::try_new::<1>(p.WDT, config) { | ||
| 29 | Ok(x) => x, | ||
| 30 | Err(_) => { | ||
| 31 | info!("Watchdog already active with wrong config, waiting for it to timeout..."); | ||
| 32 | loop {} | ||
| 33 | } | ||
| 34 | }; | ||
| 35 | |||
| 36 | let mut button = PortInput::new(Input::new(p.P0_11, Pull::Up)); | ||
| 37 | |||
| 38 | info!("Watchdog started, press button 1 to pet it or I'll reset in 3 seconds!"); | ||
| 39 | |||
| 40 | loop { | ||
| 41 | button.wait_for_high().await; | ||
| 42 | button.wait_for_low().await; | ||
| 43 | info!("Button pressed, petting watchdog!"); | ||
| 44 | handle.pet(); | ||
| 45 | } | ||
| 46 | } | ||
