Skip to content

Commit

Permalink
Make the watchdog initialization not panic on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Dec 19, 2024
1 parent 2a3c6cf commit a2bb6ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
11 changes: 5 additions & 6 deletions components/boards/src/nk3am.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ pub fn power_handler(power: &mut POWER) {
}
}

pub fn init_watchdog(wdt: WDT) -> wdt::Parts<(WatchdogHandle<Hdl0>,)> {
pub fn init_watchdog(wdt: WDT) -> Result<wdt::Parts<(WatchdogHandle<Hdl0>,)>, WDT> {
const WDT_FREQUENCY: u32 = 32_768;
// Watchdog triggers after 3 minutes
const DURATION_SECONS: u32 = 3 * 60;
Expand All @@ -274,15 +274,14 @@ pub fn init_watchdog(wdt: WDT) -> wdt::Parts<(WatchdogHandle<Hdl0>,)> {
Ok(mut watchdog) => {
watchdog.set_lfosc_ticks(TICKS);
watchdog.enable_interrupt();
let parts = watchdog.activate::<One>();
let mut parts = watchdog.activate::<One>();
parts.handles.0.pet();
parts
Ok(parts)
}
Err(wdt) => {
let parts = Watchdog::try_recover::<One>(wdt)
.expect("Recover should always work for one handle");
let mut parts = Watchdog::try_recover::<One>(wdt)?;
parts.handles.0.pet();
parts
Ok(parts)
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions runners/embedded/src/bin/app-nrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod app {
use nrf52840_hal::{
gpiote::Gpiote,
rng::Rng,
wdt::{self, handles::Hdl0, Watchdog, WatchdogHandle},
wdt::{self, handles::Hdl0, WatchdogHandle},
};

use embedded_runner_lib::{VERSION, VERSION_STRING};
Expand All @@ -43,8 +43,7 @@ mod app {
struct LocalResources {
gpiote: Gpiote,
power: nrf52840_pac::POWER,
watchdog_handle: WatchdogHandle<Hdl0>,
_watchdog: Watchdog<wdt::Active>,
watchdog_parts: Option<wdt::Parts<WatchdogHandle<Hdl0>>>,
}

#[monotonic(binds = RTC0, default = true)]
Expand Down Expand Up @@ -147,14 +146,16 @@ mod app {
LocalResources {
gpiote: board_gpio.gpiote,
power: ctx.device.POWER,
watchdog_handle: wdt_parts.handles.0,
_watchdog: wdt_parts.watchdog,
watchdog_parts: wdt_parts.ok().map(|parts| wdt::Parts {
watchdog: parts.watchdog,
handles: parts.handles.0,
}),
},
init::Monotonics(rtc_mono),
)
}

#[idle(shared = [apps, apdu_dispatch, ctaphid_dispatch, usb_classes], local = [watchdog_handle])]
#[idle(shared = [apps, apdu_dispatch, ctaphid_dispatch, usb_classes], local = [watchdog_parts])]
fn idle(ctx: idle::Context) -> ! {
let idle::SharedResources {
mut apps,
Expand All @@ -168,7 +169,10 @@ mod app {
// cortex_m::asm::wfi();

loop {
ctx.local.watchdog_handle.pet();
ctx.local
.watchdog_parts
.as_mut()
.map(|mut parts| parts.handles.pet());

#[cfg(not(feature = "no-delog"))]
boards::init::Delogger::flush();
Expand Down

0 comments on commit a2bb6ef

Please sign in to comment.