|
| 1 | +const std = @import("std"); |
| 2 | +const microzig = @import("microzig"); |
| 3 | +const WATCHDOG = microzig.chip.peripherals.WATCHDOG; |
| 4 | +const PSM = microzig.chip.peripherals.PSM; |
| 5 | +const hw = @import("hw.zig"); |
| 6 | +const cpu = @import("compatibility.zig").cpu; |
| 7 | + |
| 8 | +pub const Config = |
| 9 | + switch (cpu) { |
| 10 | + .RP2040 => struct { |
| 11 | + // See errata RP2040-E1, duration ends up getting multiplied by 2 reducing the allowable delay range |
| 12 | + duration_us: u23, |
| 13 | + pause_during_debug: bool, |
| 14 | + }, |
| 15 | + .RP2350 => struct { |
| 16 | + duration_us: u24, |
| 17 | + pause_during_debug: bool, |
| 18 | + }, |
| 19 | +}; |
| 20 | + |
| 21 | +var previous_watchdog_delay: ?u24 = null; |
| 22 | + |
| 23 | +/// Configure and enable the watchdog timer |
| 24 | +pub fn apply(config: Config) void { |
| 25 | + previous_watchdog_delay = config.duration_us; |
| 26 | + |
| 27 | + // Disable WD during changing settings |
| 28 | + disable(); |
| 29 | + |
| 30 | + switch (cpu) { |
| 31 | + .RP2040 => hw.set_alias(&PSM.WDSEL).write(.{ |
| 32 | + .ROSC = 0, |
| 33 | + .XOSC = 0, |
| 34 | + .CLOCKS = 1, |
| 35 | + .RESETS = 1, |
| 36 | + .BUSFABRIC = 1, |
| 37 | + .ROM = 1, |
| 38 | + .SRAM0 = 1, |
| 39 | + .SRAM1 = 1, |
| 40 | + .SRAM2 = 1, |
| 41 | + .SRAM3 = 1, |
| 42 | + .SRAM4 = 1, |
| 43 | + .SRAM5 = 1, |
| 44 | + .XIP = 1, |
| 45 | + .VREG_AND_CHIP_RESET = 1, |
| 46 | + .SIO = 1, |
| 47 | + .PROC0 = 1, |
| 48 | + .PROC1 = 1, |
| 49 | + .padding = 0, |
| 50 | + }), |
| 51 | + .RP2350 => hw.set_alias(&PSM.WDSEL).write(.{ |
| 52 | + .PROC_COLD = 1, |
| 53 | + .OTP = 1, |
| 54 | + .ROSC = 0, |
| 55 | + .XOSC = 0, |
| 56 | + .RESETS = 1, |
| 57 | + .CLOCKS = 1, |
| 58 | + .PSM_READY = 1, |
| 59 | + .BUSFABRIC = 1, |
| 60 | + .ROM = 1, |
| 61 | + .BOOTRAM = 1, |
| 62 | + .SRAM0 = 1, |
| 63 | + .SRAM1 = 1, |
| 64 | + .SRAM2 = 1, |
| 65 | + .SRAM3 = 1, |
| 66 | + .SRAM4 = 1, |
| 67 | + .SRAM5 = 1, |
| 68 | + .SRAM6 = 1, |
| 69 | + .SRAM7 = 1, |
| 70 | + .SRAM8 = 1, |
| 71 | + .SRAM9 = 1, |
| 72 | + .XIP = 1, |
| 73 | + .SIO = 1, |
| 74 | + .ACCESSCTRL = 1, |
| 75 | + .PROC0 = 1, |
| 76 | + .PROC1 = 1, |
| 77 | + .padding = 0, |
| 78 | + }), |
| 79 | + } |
| 80 | + // Tell RESETS hardware to reset everything except ROSC/XOSC on a watchdog reset |
| 81 | + |
| 82 | + // See errata RP2040-E1, duration needs to be multiplied by 2 for RP2040 |
| 83 | + const duration: u24 = if (cpu == .RP2040) @as(u24, config.duration_us) << 1 else config.duration_us; |
| 84 | + WATCHDOG.LOAD.write(.{ |
| 85 | + .LOAD = duration, |
| 86 | + .padding = 0, |
| 87 | + }); |
| 88 | + hw.set_alias(&WATCHDOG.CTRL).write(.{ |
| 89 | + .TIME = 0, |
| 90 | + .PAUSE_JTAG = if (config.pause_during_debug) 1 else 0, |
| 91 | + .PAUSE_DBG0 = if (config.pause_during_debug) 1 else 0, |
| 92 | + .PAUSE_DBG1 = if (config.pause_during_debug) 1 else 0, |
| 93 | + .reserved30 = 0, |
| 94 | + .ENABLE = 0, |
| 95 | + .TRIGGER = 0, |
| 96 | + }); |
| 97 | + |
| 98 | + enable(); |
| 99 | +} |
| 100 | + |
| 101 | +/// Used in a scratch register to determine if the reboot was due to a user enabled |
| 102 | +/// watchdog reset, or ROM code using the watchdog |
| 103 | +const WATCHDOG_NON_REBOOT_MAGIC: usize = 0x6ab73121; |
| 104 | + |
| 105 | +/// Enable (resume) the watchdog timer |
| 106 | +pub fn enable() void { |
| 107 | + // Update scratch[4] to distinguish from both the magic number used for rebooting to a |
| 108 | + // specific address, or 0 used to reboot into regular flash path |
| 109 | + WATCHDOG.SCRATCH4.write(.{ .SCRATCH4 = WATCHDOG_NON_REBOOT_MAGIC }); |
| 110 | + |
| 111 | + hw.set_alias(&WATCHDOG.CTRL).write(.{ |
| 112 | + .TIME = 0, |
| 113 | + .PAUSE_JTAG = 0, |
| 114 | + .PAUSE_DBG0 = 0, |
| 115 | + .PAUSE_DBG1 = 0, |
| 116 | + .reserved30 = 0, |
| 117 | + .ENABLE = 1, |
| 118 | + .TRIGGER = 0, |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +/// Disable (pause) the watchdog timer |
| 123 | +pub inline fn disable() void { |
| 124 | + hw.clear_alias(&WATCHDOG.CTRL).write(.{ |
| 125 | + .TIME = 0, |
| 126 | + .PAUSE_JTAG = 0, |
| 127 | + .PAUSE_DBG0 = 0, |
| 128 | + .PAUSE_DBG1 = 0, |
| 129 | + .reserved30 = 0, |
| 130 | + .ENABLE = 1, |
| 131 | + .TRIGGER = 0, |
| 132 | + }); |
| 133 | +} |
| 134 | + |
| 135 | +/// Update the watchdog delay (pet the watchdog), this should be called periodically |
| 136 | +/// to keep the watchdog from triggering |
| 137 | +/// |
| 138 | +/// null for new_delay uses the previously configured delay from apply() |
| 139 | +pub fn update(delay_us: ?u24) void { |
| 140 | + var duration_us: u24 = if (delay_us) |nd| nd else if (previous_watchdog_delay) |pd| pd else std.debug.panic("update() called before watchdog configured with apply()", .{}); |
| 141 | + |
| 142 | + // See errata RP2040-E1, duration needs to be multiplied by 2 for RP2040 |
| 143 | + if (cpu == .RP2040) duration_us = duration_us << 1; |
| 144 | + WATCHDOG.LOAD.write(.{ |
| 145 | + .LOAD = duration_us, |
| 146 | + .padding = 0, |
| 147 | + }); |
| 148 | +} |
| 149 | + |
| 150 | +/// Force a watchdog processor reset to occur |
| 151 | +/// |
| 152 | +/// WARNING: Will reset the MCU! |
| 153 | +pub inline fn force() void { |
| 154 | + hw.set_alias(&WATCHDOG.CTRL).write(.{ |
| 155 | + .TIME = 0, |
| 156 | + .PAUSE_JTAG = 0, |
| 157 | + .PAUSE_DBG0 = 0, |
| 158 | + .PAUSE_DBG1 = 0, |
| 159 | + .reserved30 = 0, |
| 160 | + .ENABLE = 0, |
| 161 | + .TRIGGER = 1, |
| 162 | + }); |
| 163 | +} |
| 164 | + |
| 165 | +/// The remaining us until a watchdog triggers |
| 166 | +/// |
| 167 | +/// NOTE: Due to a silicon bug on the RP2040 this always |
| 168 | +/// just returns the configured ticks, NOT the remaining |
| 169 | +/// ticks. RP2350 functions as expected. |
| 170 | +pub fn remaining_us() u24 { |
| 171 | + const rd = WATCHDOG.CTRL.read(); |
| 172 | + return switch (cpu) { |
| 173 | + .RP2040 => rd.TIME / 2, |
| 174 | + .RP2350 => rd.TIME, |
| 175 | + }; |
| 176 | +} |
| 177 | + |
| 178 | +const TriggerType = enum { |
| 179 | + UserInitiatedTimer, |
| 180 | + UserInitiatedForce, |
| 181 | + RomInitiatedTimer, |
| 182 | + RomInitiatedForce, |
| 183 | +}; |
| 184 | + |
| 185 | +/// Check if the watchdog was the reason for the last reboot |
| 186 | +/// |
| 187 | +/// TODO: RP2350 SDK masks this with a ROM function: |
| 188 | +/// return watchdog_hw->reason && rom_get_last_boot_type() == BOOT_TYPE_NORMAL; |
| 189 | +pub fn caused_reboot() ?TriggerType { |
| 190 | + const scratch4 = WATCHDOG.SCRATCH4.read(); |
| 191 | + const reason = WATCHDOG.REASON.read(); |
| 192 | + if (reason.TIMER == 0 and reason.FORCE == 0) return null; |
| 193 | + if (scratch4.SCRATCH4 == WATCHDOG_NON_REBOOT_MAGIC) { |
| 194 | + return if (reason.TIMER == 1) .UserInitiatedTimer else .UserInitiatedForce; |
| 195 | + } else { |
| 196 | + return if (reason.TIMER == 1) .RomInitiatedTimer else .RomInitiatedForce; |
| 197 | + } |
| 198 | +} |
0 commit comments