/**@brief Function for bootloader main entry. */ int main(void) { uint32_t err_code; bool dfu_start = false; bool app_reset = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_START); // This check ensures that the defined fields in the bootloader corresponds with actual // setting in the nRF51 chip. APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START); APP_ERROR_CHECK_BOOL(NRF_FICR->CODEPAGESIZE == CODE_PAGE_SIZE); // Initialize. timers_init(); gpiote_init(); buttons_init(); bootloader_init(); // Check if we reset in the middle of a firmware update if (bootloader_dfu_sd_in_progress()) { err_code = bootloader_dfu_sd_update_continue(); APP_ERROR_CHECK(err_code); softdevice_init(!app_reset); scheduler_init(); err_code = bootloader_dfu_sd_update_finalize(); APP_ERROR_CHECK(err_code); } else { // If stack is present then continue initialization of bootloader. softdevice_init(!app_reset); scheduler_init(); } // Check if the Bootloader Control pin is low. If so, enter the bootloader // mode. dfu_start = (nrf_gpio_pin_read(BOOTLOADER_CTRL_PIN) == 0); // If the Bootloader Control pin is low or the application in the flash // is not valid, enter the bootloader mode. if (dfu_start || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START))) { err_code = sd_power_gpregret_clr(POWER_GPREGRET_GPREGRET_Msk); APP_ERROR_CHECK(err_code); // Initiate an update of the firmware. err_code = bootloader_dfu_start(); APP_ERROR_CHECK(err_code); } // If the application was or now is valid, run it if (bootloader_app_is_valid(DFU_BANK_0_REGION_START)) { // 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); } NVIC_SystemReset(); }
/*! @brief Handler for general errors above the SoftDevice layer. Typically we can' recover from this so we do a reset. */ void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name) { ASSERT_STATUS_RET_VOID( error_code ); // Set info that reboot occurred due to soft error sd_power_gpregret_clr(0xFFFFFFFF); sd_power_gpregret_set(0xFADE0002/*GP_REG_RET_APP_ERROR_CAUSE_REBOOT*/); NVIC_SystemReset(); }
uint32_t ble_dfu_buttonless_bootloader_start_finalize(void) { uint32_t err_code; NRF_LOG_DEBUG("In ble_dfu_buttonless_bootloader_start_finalize\r\n"); err_code = sd_power_gpregret_clr(0, 0xffffffff); VERIFY_SUCCESS(err_code); err_code = sd_power_gpregret_set(0, BOOTLOADER_DFU_START); VERIFY_SUCCESS(err_code); // Indicate that the Secure DFU bootloader will be entered m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER); // Signal that DFU mode is to be enter to the power management module nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_DFU); return NRF_SUCCESS; }
/**@brief Function for preparing the reset, disabling SoftDevice, and jumping to the bootloader. * * @param[in] conn_handle Connection handle for peer requesting to enter DFU mode. */ static void bootloader_start(uint16_t conn_handle) { uint32_t err_code; uint16_t sys_serv_attr_len = sizeof(m_peer_data.sys_serv_attr); err_code = sd_ble_gatts_sys_attr_get(conn_handle, m_peer_data.sys_serv_attr, &sys_serv_attr_len, BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS); if (err_code != NRF_SUCCESS) { // Any error at this stage means the system service attributes could not be fetched. // This means the service changed indication cannot be sent in DFU mode, but connection // is still possible to establish. } m_reset_prepare(); err_code = sd_power_gpregret_clr(0xFF); APP_ERROR_CHECK(err_code); err_code = sd_power_gpregret_set(BOOTLOADER_DFU_START); APP_ERROR_CHECK(err_code); err_code = sd_softdevice_disable(); APP_ERROR_CHECK(err_code); err_code = sd_softdevice_vector_table_base_set(NRF_UICR->NRFFW[0]); APP_ERROR_CHECK(err_code); dfu_app_peer_data_set(conn_handle); NVIC_ClearPendingIRQ(SWI2_IRQn); interrupts_disable(); bootloader_util_app_start(NRF_UICR->NRFFW[0]); }
/**@brief Function for bootloader main entry. */ int main(void) { uint32_t err_code; bool dfu_start = false; bool app_reset = (NRF_POWER->GPREGRET == BOOTLOADER_DFU_START); leds_init(); // This check ensures that the defined fields in the bootloader corresponds with actual // setting in the nRF51 chip. APP_ERROR_CHECK_BOOL(*((uint32_t *)NRF_UICR_BOOT_START_ADDRESS) == BOOTLOADER_REGION_START); APP_ERROR_CHECK_BOOL(NRF_FICR->CODEPAGESIZE == CODE_PAGE_SIZE); // Initialize. timers_init(); gpiote_init(); buttons_init(); (void)bootloader_init(); if (bootloader_dfu_sd_in_progress()) { err_code = bootloader_dfu_sd_update_continue(); APP_ERROR_CHECK(err_code); ble_stack_init(!app_reset); scheduler_init(); err_code = bootloader_dfu_sd_update_finalize(); APP_ERROR_CHECK(err_code); } else { // If stack is present then continue initialization of bootloader. ble_stack_init(!app_reset); scheduler_init(); } dfu_start = app_reset; // dfu_start |= ((nrf_gpio_pin_read(BOOTLOADER_BUTTON_PIN) == 0) ? true: false); if (dfu_start || (!bootloader_app_is_valid(DFU_BANK_0_REGION_START))) { err_code = sd_power_gpregret_clr(POWER_GPREGRET_GPREGRET_Msk); APP_ERROR_CHECK(err_code); nrf_gpio_pin_set(LED_0); // Initiate an update of the firmware. err_code = bootloader_dfu_start(); APP_ERROR_CHECK(err_code); nrf_gpio_pin_clear(LED_0); } if (bootloader_app_is_valid(DFU_BANK_0_REGION_START)) { leds_off(); // 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); } nrf_gpio_pin_clear(LED_0); nrf_gpio_pin_clear(LED_1); NVIC_SystemReset(); }
void sd_clrBootloader(uint8_t mask) { sd_power_gpregret_clr(mask); }