我需要實現(xiàn)從APP跳轉(zhuǎn)到bootloader,參考ble_app_buttonless_dfu例程,只不過不是通過nrf_connect來觸發(fā)跳轉(zhuǎn),而是在收到云端的升級信息后,跳轉(zhuǎn)到bootloader,所以,我直接借鑒了ble_app_buttonless_dfu例程的main.c中的這個函數(shù)的方法。
static void buttonless_dfu_sdh_state_observer(nrf_sdh_state_evt_t state, void * p_context)
{
if (state == NRF_SDH_EVT_STATE_DISABLED)
{
// Softdevice was disabled before going into reset. Inform bootloader to skip CRC on next boot.
nrf_power_gpregret2_set(BOOTLOADER_DFU_SKIP_CRC);
//Go to system off.
nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
}
}
我在完成藍牙連接清理工作后,需要進入bootloader的時候,直接調(diào)用
buttonless_dfu_sdh_state_observer(NRF_SDH_EVT_STATE_DISABLED, NULL);
試圖進入bootloader,但是調(diào)試一直報錯
SOFTDEVICE: INVALID MEMORY ACCESS
分析錯誤原因,有softdevice,又是內(nèi)存訪問錯誤,估計跟softdevice有關吧,Google一番,有前輩提示:
Then the softdevice is enabled, access to some peripherals is blocked or restricted.
The POWER peripheral have restricted access, which means it can be accessed through the softdevice API
When using the SoftDevice I would recommend using the formal interface.
With no SoftDevice installed your code would work fine.
uint32_t resultCode = sd_power_gpregret_set(0, 0x01);
// For specification see here:
/**@brief Set bits in the general purpose retention registers (NRF_POWER->GPREGRET*).
*
* @param[in] gpregret_id 0 for GPREGRET, 1 for GPREGRET2.
* @param[in] gpregret_msk Bits to be set in the GPREGRET register.
*
* @retval ::NRF_SUCCESS
*/
SVCALL(SD_POWER_GPREGRET_SET, uint32_t, sd_power_gpregret_set(uint32_t gpregret_id, uint32_t gpregret_msk));
哦,對哦,我使能了softdevice,所以應該用sd的相關接口來操作,而ble_dfu.c中已經(jīng)幫我們寫好了接口:
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;
}
所以,接下來就簡單了
#include "ble_dfu.h"
然后
ble_dfu_buttonless_bootloader_start_finalize();
當然如果你不需要藍牙事件通知,可以注釋掉
m_dfu.evt_handler(BLE_DFU_EVT_BOOTLOADER_ENTER);
搞定,非常順暢的進入bootloader。