diff --git a/src/main.c b/src/main.c index 0011640..1111220 100644 --- a/src/main.c +++ b/src/main.c @@ -48,7 +48,10 @@ #include "nrf_soc.h" #include "nrf_nvic.h" #include "app_error.h" +#include "nrf_gpio.h" #include "ble.h" +#include "nrf.h" +#include "ble_hci.h" #include "app_scheduler.h" #include "nrf_error.h" @@ -56,10 +59,13 @@ #include "pstorage_platform.h" #include "nrf_mbr.h" +#include "pstorage.h" +#include "nrfx_nvmc.h" #ifdef NRF_USBD #include "uf2/uf2.h" +#include "nrf_usbd.h" #include "tusb.h" void usb_init(bool cdc_only); @@ -218,6 +224,9 @@ int main(void) { bootloader_app_start(); } + // No application was loaded, reset the system with the OTA DFU update + NRF_POWER->GPREGRET = 0xA8; + NVIC_SystemReset(); } @@ -242,17 +251,14 @@ static void check_dfu_mode(void) { (((*dbl_reset_mem) == DFU_DBL_RESET_MAGIC) && reason_reset_pin); // Clear GPREGRET if it is our values - if (dfu_start || dfu_skip) { - NRF_POWER->GPREGRET = 0; - } + if (dfu_start || dfu_skip) NRF_POWER->GPREGRET = 0; // skip dfu entirely - if (dfu_skip) { - return; - } + if (dfu_skip) return; /*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/ - dfu_start = dfu_start || button_pressed(BUTTON_DFU); // DFU button pressed + // DFU button pressed + dfu_start = dfu_start || button_pressed(BUTTON_DFU); // DFU + FRESET are pressed --> OTA _ota_dfu = _ota_dfu || (button_pressed(BUTTON_DFU) && button_pressed(BUTTON_FRESET)); @@ -260,16 +266,16 @@ static void check_dfu_mode(void) { bool const valid_app = bootloader_app_is_valid(); bool const just_start_app = valid_app && !dfu_start && (*dbl_reset_mem) == DFU_DBL_RESET_APP; - if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET()) { - dfu_start = 1; - } + if (!just_start_app && APP_ASKS_FOR_SINGLE_TAP_RESET()) dfu_start = 1; // App mode: Double Reset detection or DFU startup for nrf52832 if (!(just_start_app || dfu_start || !valid_app)) { #ifdef NRF52832_XXAA /* Even DFU is not active, we still force an 1000 ms dfu serial mode when startup * to support auto programming from Arduino IDE - * Note: Double Reset WONT work with nrf52832 since all its SRAM got cleared with GPIO reset. */ + * + * Note: Double Reset WONT work with nrf52832 since all its SRAM got cleared with GPIO reset. + */ bootloader_dfu_start(false, DFU_SERIAL_STARTUP_INTERVAL, false); #else // Note: RESETREAS is not clear by bootloader, it should be cleared by application upon init()