From e738f5eaa13c0e30dc082c511d9659c5f35e7f7e Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 22 May 2020 12:56:49 +0800 Subject: [PATCH] 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 --- ports/nrf/supervisor/port.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index 36725293c8..89797e6dcb 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -131,6 +131,21 @@ safe_mode_t port_init(void) { analogin_init(); #endif + // If the board was reset by the WatchDogTimer, we may + // need to boot into safe mode. Reset the RESETREAS bit + // for the WatchDogTimer so we don't encounter this the + // next time we reboot. + if (NRF_POWER->RESETREAS & POWER_RESETREAS_DOG_Msk) { + NRF_POWER->RESETREAS = POWER_RESETREAS_DOG_Msk; + uint32_t usb_reg = NRF_POWER->USBREGSTATUS; + + // If USB is connected, then the user might be editing `code.py`, + // in which case we should reboot into Safe Mode. + if (usb_reg & POWER_USBREGSTATUS_VBUSDETECT_Msk) { + return WATCHDOG_RESET; + } + } + return NO_SAFE_MODE; }