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(mut rtc: Rtc<'static>, timeout_secs: u64, feed_every_secs: u64) -> ! {
18 rtc.rwdt
19 .set_timeout(RwdtStage::Stage0, esp_hal::time::Duration::from_secs(timeout_secs));
20 rtc.rwdt.enable();
21 info!(
22 "[wdt] RWDT armed ({} s reset), feeding every {} s",
23 timeout_secs, feed_every_secs
24 );
25 loop {
26 rtc.rwdt.feed();
27 Timer::after(Duration::from_secs(feed_every_secs)).await;
28 }
29}