APP跳转到bootloader,通过nrf_power_gpregret2_set写NRF_POWER->GPREGRET2报SOFTDEVICE: INVALID MEMORY ACCESS错误

我需要实现从APP跳转到bootloader,参考ble_app_buttonless_dfu例程,只不过不是通过nrf_connect来触发跳转,而是在收到云端的升级信息后,跳转到bootloader,所以,我直接借鉴了ble_app_buttonless_dfu例程的main.c中的这个函数的方法。

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的时候,直接调用

buttonless_dfu_sdh_state_observer(NRF_SDH_EVT_STATE_DISABLED, NULL);

试图进入bootloader,但是调试一直报错

SOFTDEVICE: INVALID MEMORY ACCESS

分析错误原因,有softdevice,又是内存访问错误,估计跟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中已经帮我们写好了接口:

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。

上一篇:Dynamic CRM 2013学习笔记(三十)Linq使用报错 A proxy type with the name account has been defined by another assembly


下一篇:超级好用的小程序版蓝牙调试工具:Ble蓝牙开发助手