Skip to content

Commit e738f5e

Browse files
committed
nrf: boot into safe mode sometimes for watchdog reset
If the watchdog resets the system and we're plugged into USB, boot into safe mode. Signed-off-by: Sean Cross <[email protected]>
1 parent 8c5df5f commit e738f5e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

ports/nrf/supervisor/port.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ safe_mode_t port_init(void) {
131131
analogin_init();
132132
#endif
133133

134+
// If the board was reset by the WatchDogTimer, we may
135+
// need to boot into safe mode. Reset the RESETREAS bit
136+
// for the WatchDogTimer so we don't encounter this the
137+
// next time we reboot.
138+
if (NRF_POWER->RESETREAS & POWER_RESETREAS_DOG_Msk) {
139+
NRF_POWER->RESETREAS = POWER_RESETREAS_DOG_Msk;
140+
uint32_t usb_reg = NRF_POWER->USBREGSTATUS;
141+
142+
// If USB is connected, then the user might be editing `code.py`,
143+
// in which case we should reboot into Safe Mode.
144+
if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk) {
145+
return WATCHDOG_RESET;
146+
}
147+
}
148+
134149
return NO_SAFE_MODE;
135150
}
136151

0 commit comments

Comments
 (0)