4.7. Watchdog
4.7.1. Overview
The watchdog is a countdown timer that can restart parts of the chip if it reaches zero. This can be used to restart the processor if software gets stuck in an infinite loop. The programmer must periodically write a value to the watchdog to stop it from reaching zero.
The watchdog is reset by
rst_n_run, which is deasserted as soon as the digital core supply (DVDD) is powered and stable, and the RUN pin is high. This allows the watchdog reset to feed into the power-on state machine (see
Section 2.13
) and reset controller (see
Section 2.14
), resetting their dependants if they are selected in the
WDSEL register. The
WDSEL register exists in both the power-on state machine and reset controller.
4.7.2. Tick generation
The watchdog reference clock,
clk_tick, is driven from
clk_ref. Ideally
clk_ref will be configured to use the Crystal Oscillator (
Section 2.16
) so that it provides an accurate reference clock. The reference clock is divided internally to generate a tick (nominally 1µs) to use as the watchdog tick. The tick is configured using the
TICK register.
i NOTE
To avoid duplicating logic, this tick is also distributed to the timer (see Section 4.6 ) and used as the timer reference.
The SDK starts the watchdog tick in
clocks_init:
SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_watchdog/watchdog.c Lines 16 - 18
16 void watchdog_start_tick(uint cycles) {
17 tick_start(TICK_WATCHDOG, cycles);18 }
4.7.3. Watchdog Counter
The watchdog counter is loaded by the LOAD register. The current value can be seen in CTRL.TIME .
⚠ WARNING
Due to a logic error, the watchdog counter is decremented twice per tick. Which means the programmer needs to program double the intended count down value. The SDK examples take this issue into account. See RP2040-E1 for more information.
4.7.4. Scratch Registers
The watchdog contains eight 32-bit scratch registers that can be used to store information between soft resets of the chip. A
rst_n_run event triggered by toggling the RUN pin or cycling the digital core supply (DVDD) will reset the scratch registers.
The bootrom checks the watchdog scratch registers for a magic number on boot. This can be used to soft reset the chip into some user specified code. See Section 2.8.1.1 for more information.
4.7.5. Programmer's Model
The SDK provides a
hardware_watchdog driver to control the watchdog.
4.7.5.1. Enabling the watchdog
SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_watchdog/watchdog.c Lines 42 - 74
42 // Helper function used by both watchdog_enable and watchdog_reboot
43 void _watchdog_enable(uint32_t delay_ms, bool pause_on_debug) {
44 valid_params_if(HARDWARE_WATCHDOG, delay_ms <= WATCHDOG_LOAD_BITS / (1000 *
WATCHDOG_XFACTOR));
45 hw_clear_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_ENABLE_BITS);
46
47 // Reset everything apart from ROSC and XOSC
48 hw_set_bits(&psm_hw->wdsel, PSM_WDSEL_BITS & ~(PSM_WDSEL_ROSC_BITS |
PSM_WDSEL_XOSC_BITS));
49
50 uint32_t dbg_bits = WATCHDOG_CTRL_PAUSE_DBG0_BITS |
51 WATCHDOG_CTRL_PAUSE_DBG1_BITS |
52 WATCHDOG_CTRL_PAUSE_JTAG_BITS;
53
54 if (pause_on_debug) {
55 hw_set_bits(&watchdog_hw->ctrl, dbg_bits);
56 } else {
57 hw_clear_bits(&watchdog_hw->ctrl, dbg_bits);
58 }
59
60 if (!delay_ms) {
61 hw_set_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_TRIGGER_BITS);
62 } else {
63 load_value = delay_ms * 1000;
64 load_value *= 2;65 if (load_value > WATCHDOG_LOAD_BITS) 66 load_value = WATCHDOG_LOAD_BITS; 67 68 watchdog_update(); 69 70 hw_set_bits(&watchdog_hw->ctrl, WATCHDOG_CTRL_ENABLE_BITS); 71 } 72 }
4.7.5.2. Updating the watchdog counter
SDK: https://github.com/raspberrypi/pico-sdk/blob/master/src/rp2_common/hardware_watchdog/watchdog.c Lines 24 - 28
24 static uint32_t load_value;
25
26 void watchdog_update(void) {
27 watchdog_hw->load = load_value;
28 }
4.7.5.3. Usage
The Pico Examples repository provides a
hello_watchdog example that uses the
hardware_watchdog to demonstrate use of the watchdog.
Pico Examples: https://github.com/raspberrypi/pico-examples/blob/master/watchdog/hello_watchdog/hello_watchdog.c Lines 11 - 33
11 int main() {
12 stdio_init_all();
13
14 if (watchdog_enable_caused_reboot()) {
15 printf("Rebooted by Watchdog!\n");
16 return 0;
17 } else {
18 printf("Clean boot\n");
19 }
20
21 // Enable the watchdog, requiring the watchdog to be updated every 100ms or the chip will
reboot
22 // second arg is pause on debug which means the watchdog will pause when stepping through
code
23 watchdog_enable(100, 1);
24
25 for (uint i = 0; i < 5; i++) {
26 printf("Updating watchdog %d\n", i);
27 watchdog_update();
28 }
29
30 // Wait in an infinite loop and don't update the watchdog so it reboots us
31 printf("Waiting to be rebooted by watchdog\n");
32 while(1);
33 }
4.7.6. List of Registers
The watchdog registers start at a base address of 0x40058000 (defined as WATCHDOG_BASE in SDK).
Table 545. List of WATCHDOG registers
| Offset | Name | Info |
|---|---|---|
| 0x00 | CTRL | Watchdog control |
| 0x04 | LOAD | Load the watchdog timer. |
| 0x08 | REASON | Logs the reason for the last reset. |
| 0x0c | SCRATCH0 | Scratch register |
| 0x10 | SCRATCH1 | Scratch register |
| 0x14 | SCRATCH2 | Scratch register |
| 0x18 | SCRATCH3 | Scratch register |
| 0x1c | SCRATCH4 | Scratch register |
| 0x20 | SCRATCH5 | Scratch register |
| 0x24 | SCRATCH6 | Scratch register |
| 0x28 | SCRATCH7 | Scratch register |
| 0x2c | TICK | Controls the tick generator |
WATCHDOG: CTRL Register
Offset: 0x00
Description
- Watchdog control
- The rst_wdsel register determines which subsystems are reset when the watchdog is triggered.
- The watchdog can be triggered in software.
Table 546. CTRL Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31 | TRIGGER: Trigger a watchdog reset | SC | 0x0 |
| 30 | ENABLE: When not enabled the watchdog timer is paused | RW | 0x0 |
| 29:27 | Reserved. | - | - |
| 26 | PAUSE_DBG1: Pause the watchdog timer when processor 1 is in debug mode | RW | 0x1 |
| 25 | PAUSE_DBG0: Pause the watchdog timer when processor 0 is in debug mode | RW | 0x1 |
| 24 | PAUSE_JTAG: Pause the watchdog timer when JTAG is accessing the bus fabric | RW | 0x1 |
| 23:0 | TIME: Indicates the number of ticks / 2 (see errata RP2040-E1) before a watchdog reset will be triggered | RO | 0x000000 |
WATCHDOG: LOAD Register
Offset: 0x04
Table 547. LOAD Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:24 | Reserved. | - | - |
| 23:0 | Load the watchdog timer. The maximum setting is 0xffffff which corresponds to 0xffffff / 2 ticks before triggering a watchdog reset (see errata RP2040-E1). | WF | 0x000000 |
WATCHDOG: REASON Register
Offset: 0x08
Description
Logs the reason for the last reset. Both bits are zero for the case of a hardware reset.
Table 548. REASON Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:2 | Reserved. | - | - |
| 1 | FORCE | RO | 0x0 |
| 0 | TIMER | RO | 0x0 |
WATCHDOG: SCRATCH0, SCRATCH1, ..., SCRATCH6, SCRATCH7 Registers
Offsets: 0x0c, 0x10, ..., 0x24, 0x28
Table 549. SCRATCH0, SCRATCH1, ..., SCRATCH6, SCRATCH7 Registers
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:0 | Scratch register. Information persists through soft reset of the chip. | RW | 0x00000000 |
WATCHDOG: TICK Register
Offset: 0x2c
Description
Controls the tick generator
Table 550. TICK Register
| Bits | Description | Type | Reset |
|---|---|---|---|
| 31:20 | Reserved. | - | - |
| 19:11 | COUNT : Count down timer: the remaining number clk_tick cycles before the next tick is generated. | RO | - |
| 10 | RUNNING : Is the tick generator running? | RO | - |
| 9 | ENABLE : start / stop tick generation | RW | 0x1 |
| 8:0 | CYCLES : Total number of clk_tick cycles before the next tick. | RW | 0x000 |