1. 程式人生 > >nRF52832 BLE_DFU空中升級OTA(三)準備升級工程(SDK14.2.0)

nRF52832 BLE_DFU空中升級OTA(三)準備升級工程(SDK14.2.0)

準備需要加入DFU功能的工程

在工程main檔案services_init函式中加入DFU服務

 1 uint32_t err_code;
 2 
 3 // Initialize the async SVCI interface to bootloader.
 4 err_code = ble_dfu_buttonless_async_svci_init();
 5 //APP_ERROR_CHECK(err_code);
 6 if(NRF_ERROR_NO_MEM != err_code) 
 7 {
 8     ble_dfu_buttonless_init_t dfus_init =
 9
{ 10 .evt_handler = ble_dfu_evt_handler 11 }; 12 13 err_code = ble_dfu_buttonless_init(&dfus_init); 14 APP_ERROR_CHECK(err_code); 15 }

 

加入DFU事件處理函式

 1 // YOUR_JOB: Update this code if you want to do anything given a DFU event (optional).
 2 /**@brief Function for handling dfu events from the Buttonless Secure DFU service
3 * 4 * @param[in] event Event from the Buttonless Secure DFU service. 5 */ 6 static void ble_dfu_evt_handler(ble_dfu_buttonless_evt_type_t event) 7 { 8 switch (event) 9 { 10 case BLE_DFU_EVT_BOOTLOADER_ENTER_PREPARE: 11 NRF_LOG_INFO("Device is preparing to enter bootloader mode.
"); 12 // YOUR_JOB: Disconnect all bonded devices that currently are connected. 13 // This is required to receive a service changed indication 14 // on bootup after a successful (or aborted) Device Firmware Update. 15 break; 16 17 case BLE_DFU_EVT_BOOTLOADER_ENTER: 18 // YOUR_JOB: Write app-specific unwritten data to FLASH, control finalization of this 19 // by delaying reset by reporting false in app_shutdown_handler 20 NRF_LOG_INFO("Device will enter bootloader mode."); 21 break; 22 23 case BLE_DFU_EVT_BOOTLOADER_ENTER_FAILED: 24 NRF_LOG_ERROR("Request to enter bootloader mode failed asynchroneously."); 25 // YOUR_JOB: Take corrective measures to resolve the issue 26 // like calling APP_ERROR_CHECK to reset the device. 27 break; 28 29 case BLE_DFU_EVT_RESPONSE_SEND_ERROR: 30 NRF_LOG_ERROR("Request to send a response to client failed."); 31 // YOUR_JOB: Take corrective measures to resolve the issue 32 // like calling APP_ERROR_CHECK to reset the device. 33 APP_ERROR_CHECK(false); 34 break; 35 36 default: 37 NRF_LOG_ERROR("Unknown event from ble_dfu_buttonless."); 38 break; 39 } 40 }

 

如果不需要事件處理就加入下面的程式碼

 1 uint32_t err_code;
 2 
 3 // Initialize the async SVCI interface to bootloader.
 4 err_code = ble_dfu_buttonless_async_svci_init();
 5 if(NRF_ERROR_NO_MEM != err_code) 
 6 {
 7     ble_dfu_buttonless_init_t dfus_init;
 8  
 9     err_code = ble_dfu_buttonless_init(&dfus_init);
10     APP_ERROR_CHECK(err_code);
11 }

 

加入幾個必要的檔案到工程

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

 

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

 

並加入以下標頭檔案路徑

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

SDK_14.2.0工程\components\libraries\svc

 

Main.c 標頭檔案中加入

#include "ble_dfu.h"

 

記得修改sdk_config.hNRF_SDH_BLE_VS_UUID_COUNT

1 // <o> NRF_SDH_BLE_VS_UUID_COUNT - The number of vendor-specific UUIDs. 
2 #ifndef NRF_SDH_BLE_VS_UUID_COUNT
3 #define NRF_SDH_BLE_VS_UUID_COUNT 1    //預設為0
4 #endif 

 

開啟RTT列印LOG

1 //==========================================================
2 // <e> NRF_LOG_ENABLED - Logging module for nRF5 SDK
3 //==========================================================
4 #ifndef NRF_LOG_ENABLED
5 #define NRF_LOG_ENABLED 1
6 #endif

以及

1 //==========================================================
2 // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
3 //==========================================================
4 #ifndef NRF_LOG_BACKEND_RTT_ENABLED
5 #define NRF_LOG_BACKEND_RTT_ENABLED 1
6 #endif

 

編譯一下,下載觀察RTT資訊修改RAM地址,再次編譯後沒有問題,就可以用新生成的hex檔案來製作升級zip了。