nRF52832 BLE_DFU空中升级OTA(三)准备升级工程(SDK14.2.0)

时间:2023-03-09 07:20:17
nRF52832 BLE_DFU空中升级OTA(三)准备升级工程(SDK14.2.0)

准备需要加入DFU功能的工程

在工程main文件services_init函数中加入DFU服务

 uint32_t err_code;

 // Initialize the async SVCI interface to bootloader.
err_code = ble_dfu_buttonless_async_svci_init();
//APP_ERROR_CHECK(err_code);
if(NRF_ERROR_NO_MEM != err_code)
{
ble_dfu_buttonless_init_t dfus_init =
{
.evt_handler = ble_dfu_evt_handler
}; err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
}

加入DFU事件处理函数

 // YOUR_JOB: Update this code if you want to do anything given a DFU event (optional).
/**@brief Function for handling dfu events from the Buttonless Secure DFU service
*
* @param[in] event Event from the Buttonless Secure DFU service.
*/
static void ble_dfu_evt_handler(ble_dfu_buttonless_evt_type_t event)
{
switch (event)
{
case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE:
NRF_LOG_INFO("Device is preparing to enter bootloader mode.");
// YOUR_JOB: Disconnect all bonded devices that currently are connected.
// This is required to receive a service changed indication
// on bootup after a successful (or aborted) Device Firmware Update.
break; case BLE_DFU_EVT_BOOTLOADER_ENTER:
// YOUR_JOB: Write app-specific unwritten data to FLASH, control finalization of this
// by delaying reset by reporting false in app_shutdown_handler
NRF_LOG_INFO("Device will enter bootloader mode.");
break; case BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED:
NRF_LOG_ERROR("Request to enter bootloader mode failed asynchroneously.");
// YOUR_JOB: Take corrective measures to resolve the issue
// like calling APP_ERROR_CHECK to reset the device.
break; case BLE_DFU_EVT_RESPONSE_SEND_ERROR:
NRF_LOG_ERROR("Request to send a response to client failed.");
// YOUR_JOB: Take corrective measures to resolve the issue
// like calling APP_ERROR_CHECK to reset the device.
APP_ERROR_CHECK(false);
break; default:
NRF_LOG_ERROR("Unknown event from ble_dfu_buttonless.");
break;
}
}

加入几个必要的文件到工程

SDK_14.2.0工程\components\ble\ble_services\ble_dfu下的

nRF52832 BLE_DFU空中升级OTA(三)准备升级工程(SDK14.2.0)

SDK_14.2.0工程\components\libraries\bootloader\dfu下的

nRF52832 BLE_DFU空中升级OTA(三)准备升级工程(SDK14.2.0)

并加入以下头文件路径

SDK_14.2.0工程\components\libraries\bootloader\dfu

SDK_14.2.0工程\components\libraries\svc

Main.c 头文件中加入

#include "ble_dfu.h"

记得修改sdk_config.h中NRF_SDH_BLE_VS_UUID_COUNT

 // <o> NRF_SDH_BLE_VS_UUID_COUNT - The number of vendor-specific UUIDs.
#ifndef NRF_SDH_BLE_VS_UUID_COUNT
#define NRF_SDH_BLE_VS_UUID_COUNT 1 //默认为0
#endif

开启RTT打印LOG

 //==========================================================
// <e> NRF_LOG_ENABLED - Logging module for nRF5 SDK
//==========================================================
#ifndef NRF_LOG_ENABLED
#define NRF_LOG_ENABLED 1
#endif

以及

 //==========================================================
// <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
//==========================================================
#ifndef NRF_LOG_BACKEND_RTT_ENABLED
#define NRF_LOG_BACKEND_RTT_ENABLED 1
#endif

编译一下,下载观察RTT信息修改RAM地址,再次编译后没有问题,就可以用新生成的hex文件来制作升级zip了。