From 8bd5ce504341de30124f4ac7f5f1301c7cdc6fb4 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 12 Sep 2024 14:47:27 -0700 Subject: [PATCH] Set UF2 boot value without SD Fallback to setting GPREGRET without SD if it isn't active. This fixes resetting into UF2 when BLE isn't active. Fixes #9423 --- ports/nordic/common-hal/microcontroller/__init__.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/ports/nordic/common-hal/microcontroller/__init__.c b/ports/nordic/common-hal/microcontroller/__init__.c index 69024cbb296e..86e7916d754a 100644 --- a/ports/nordic/common-hal/microcontroller/__init__.c +++ b/ports/nordic/common-hal/microcontroller/__init__.c @@ -21,6 +21,7 @@ #include "supervisor/shared/safe_mode.h" #include "nrfx_glue.h" #include "nrf_nvic.h" +#include "nrf_power.h" // This routine should work even when interrupts are disabled. Used by OneWire // for precise timing. @@ -61,10 +62,14 @@ void common_hal_mcu_enable_interrupts() { void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) { enum { DFU_MAGIC_UF2_RESET = 0x57 }; + uint8_t new_value = 0; if (runmode == RUNMODE_BOOTLOADER || runmode == RUNMODE_UF2) { - sd_power_gpregret_set(0, DFU_MAGIC_UF2_RESET); - } else { - sd_power_gpregret_set(0, 0); + new_value = DFU_MAGIC_UF2_RESET; + } + int err_code = sd_power_gpregret_set(0, DFU_MAGIC_UF2_RESET); + if (err_code != NRF_SUCCESS) { + // Set it without the soft device if the SD failed. (It may be off.) + nrf_power_gpregret_set(NRF_POWER, new_value); } if (runmode == RUNMODE_SAFE_MODE) { safe_mode_on_next_reset(SAFE_MODE_PROGRAMMATIC);