Description
This example program shows a simple implementation for a watchdog across the two cores. It uses both cores, each with its kernel.
There’s only one watchdog hardware device on the RP2040 and the RP2350, hence it does not suffice to periodically reload the counter from one core, as the other might be in locked in a faulty loop. Therefore, we use a monitoring thread per core. The one on core 0 controls the hardware device, and observes an “I am OK” flag set by a corresponding thread on core 1.
Core 0 Threads (Module WatchdogC0
)
- Thread 0: the usual blinker, period-triggered, “heartbeat” indicator.
- Thread 1: cause the watchdog to trigger (see test cases below), print to terminal, showing the resets by the watchdog.
- Thread 2: watchdog thread: periodically check the “I am OK” flag set by core 1, trigger the watchdog if missing; periodically reload the watchdog counter to prevent it from triggering a reset.
Core 1 Threads (Module WatchdogC1
)
- Thread 0: watchdog countdown, cause the watchdog to trigger.
- Thread 1: watchdog thread: periodically set the “I am OK” flag.
Test Cases
There are two test cases, run alternately:
- Core 0 reset: thread 1 counts down, then enters an infinite loop, causing the watchdog to time out and trigger; the countdown time is shorter than the one on core 1, so core 0 triggers before core 1;
- Core 1 reset: thread 0 counts down, then enters an infinite loop, preventing thread 1 from setting the “I am OK” flag, causing thread 2 on core 0 to trigger the watchdog; the countdown time on core 0 is longer than the one on core 1, so core 1 triggers before core 0.
Other
- The
SCRATCH0
register of the watchdog is used for the “I am OK” flag as well as for configuring the test case. This register can be set and cleared atomically from both cores. - The watchdog monitoring could easily be integrated into the kernel and scheduler.
Output Terminals
See Set-up, two-terminal set-up.
Build and Run
Repository
- libv2: for RP2040/Pico and RP2350/Pico2: Watchdog
- lib (v1): this program is no longer being maintained: WatchdogProt1