m5stack_core/io/watchdog.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! Production hardware watchdog (RWDT) backstop.
3
4use embassy_time::{Duration, Timer};
5use esp_hal::rtc_cntl::{Rtc, RwdtStage};
6
7/// Arm the RWDT (Stage0 = hardware system reset on timeout) and feed it
8/// forever from the calling executor.
9///
10/// Spawn this on the executor whose wedging you want to catch: if that
11/// executor stops being scheduled, the feed stops and the RWDT hard-resets
12/// the chip `timeout_secs` later. NOT a substitute for bounding individual
13/// operations — a subsystem hang with the executor still alive keeps feeding.
14///
15/// `Rtc` is consumed; construct it from
16/// [`SystemResources::lpwr`](crate::board::SystemResources::lpwr).
17pub async fn watchdog_feed_loop(
18 mut rtc: Rtc<'static>,
19 timeout_secs: u64,
20 feed_every_secs: u64,
21) -> ! {
22 rtc.rwdt.set_timeout(
23 RwdtStage::Stage0,
24 esp_hal::time::Duration::from_secs(timeout_secs),
25 );
26 rtc.rwdt.enable();
27 info!(
28 "[wdt] RWDT armed ({} s reset), feeding every {} s",
29 timeout_secs, feed_every_secs
30 );
31 loop {
32 rtc.rwdt.feed();
33 Timer::after(Duration::from_secs(feed_every_secs)).await;
34 }
35}