From e946aabd6dc6f1f1b3971a68629f8fec2be310ec Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 11 Aug 2018 11:43:57 +0700 Subject: [PATCH] uf2 does not depend on softdevice --- .../libraries/bootloader_dfu/bootloader.c | 20 +++++++++--- src/main.c | 31 ++++++++++++------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c index 6fb0c0d..0c720df 100644 --- a/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c +++ b/lib/sdk11/components/libraries/bootloader_dfu/bootloader.c @@ -182,14 +182,26 @@ bool bootloader_app_is_valid(uint32_t app_addr) static void bootloader_settings_save(bootloader_settings_t * p_settings) { + uint8_t sd_en = false; + sd_softdevice_is_enabled(&sd_en); + + if ( sd_en ) + { uint32_t err_code = pstorage_clear(&m_bootsettings_handle, sizeof(bootloader_settings_t)); APP_ERROR_CHECK(err_code); err_code = pstorage_store(&m_bootsettings_handle, - (uint8_t *)p_settings, + (uint8_t *) p_settings, sizeof(bootloader_settings_t), 0); APP_ERROR_CHECK(err_code); + } + else + { + flash_write(BOOTLOADER_SETTINGS_ADDRESS, p_settings, sizeof(bootloader_settings_t)); + flash_flush(); + m_update_status = BOOTLOADER_COMPLETE; + } } @@ -373,13 +385,11 @@ void bootloader_app_start(uint32_t app_addr) { // If the applications CRC has been checked and passed, the magic number will be written and we // can start the application safely. - uint32_t err_code = sd_softdevice_disable(); - APP_ERROR_CHECK(err_code); + APP_ERROR_CHECK ( sd_softdevice_disable() ); interrupts_disable(); - err_code = sd_softdevice_vector_table_base_set(CODE_REGION_1_START); - APP_ERROR_CHECK(err_code); + APP_ERROR_CHECK( sd_softdevice_vector_table_base_set(CODE_REGION_1_START) ); bootloader_util_app_start(CODE_REGION_1_START); } diff --git a/src/main.c b/src/main.c index 8eb4b25..267ce2c 100644 --- a/src/main.c +++ b/src/main.c @@ -164,7 +164,7 @@ static void blinky_handler(void) led_control(LED_BLUE, state); } - // Feed all Watchdog just in case application enable it (WDT last through a hump from application to bootloader) + // Feed all Watchdog just in case application enable it (WDT last through a jump from application to bootloader) if ( nrf_wdt_started() ) { for (uint8_t i=0; i<8; i++) nrf_wdt_reload_request_set(i); @@ -223,6 +223,11 @@ void board_teardown(void) usb_teardown(); } +void softdev_mbr_init(void) +{ + sd_mbr_command_t com = { .command = SD_MBR_COMMAND_INIT_SD }; + sd_mbr_command(&com); +} /** * Initializes the SoftDevice and the BLE event interrupt. @@ -232,11 +237,7 @@ void board_teardown(void) */ uint32_t softdev_init(bool init_softdevice) { - if (init_softdevice) - { - sd_mbr_command_t com = { .command = SD_MBR_COMMAND_INIT_SD }; - APP_ERROR_CHECK( sd_mbr_command(&com) ); - } + if (init_softdevice) softdev_mbr_init(); // map vector table to bootloader address APP_ERROR_CHECK( sd_softdevice_vector_table_base_set(BOOTLOADER_REGION_START) ); @@ -308,7 +309,7 @@ void softdev_teardown(void) int main(void) { // SD is already Initialized in case of BOOTLOADER_DFU_OTA_MAGIC - bool const sd_inited = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_OTA_MAGIC); + bool sd_inited = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_OTA_MAGIC); // Start Bootloader in BLE OTA mode _ota_update = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_OTA_MAGIC) || @@ -333,15 +334,14 @@ int main(void) { APP_ERROR_CHECK( bootloader_dfu_sd_update_continue() ); softdev_init(!sd_inited); + sd_inited = true; APP_ERROR_CHECK( bootloader_dfu_sd_update_finalize() ); } else { - softdev_init(!sd_inited); + // softdev_init(); } - usb_init(); - /*------------- Determine DFU mode (Serial, OTA, FRESET or normal) -------------*/ // DFU button pressed dfu_start = dfu_start || button_pressed(BUTTON_DFU); @@ -352,7 +352,13 @@ int main(void) if ( dfu_start || !bootloader_app_is_valid(DFU_BANK_0_REGION_START) ) { // Enable BLE if in OTA -// if ( _ota_update ) softdev_ble_enable(); + if ( _ota_update ) + { + softdev_init(!sd_inited); + sd_inited = true; + } + + usb_init(); // Initiate an update of the firmware. APP_ERROR_CHECK( bootloader_dfu_start(_ota_update, 0) ); @@ -381,6 +387,9 @@ int main(void) if (bootloader_app_is_valid(DFU_BANK_0_REGION_START) && !bootloader_dfu_sd_in_progress()) { + // MBR must be init before start application + if ( !sd_inited ) softdev_mbr_init(); + // Select a bank region to use as application region. // @note: Only applications running from DFU_BANK_0_REGION_START is supported. bootloader_app_start(DFU_BANK_0_REGION_START);