前期準備
1、生成secure_bootloader
由於編譯BootLoader需要安全加密,按照網上大多數教程基本可以完成
注意初始生成的祕鑰key 在之後的升級過程中應該始終保持一致,否則無法升級
2、移植dfu生成ble-uart-with-dfu 固件
爲了讓ble_app_uart具有DFU功能,有2種做法,一是把NUS服務移植到ble_app_buttonless_dfu中,這種方法相對來說更簡單,但不利於理解DFU的移植原理;二是把DFU服務移植到ble_app_uart中,現在把DFU服務移植到ble_app_uart中。
1)打開如下工程:\examples\ble_peripheral\ble_app_uart\pca10040\s132\arm5_no_packs
- 在nus服務函數中 添加DFU服務。代碼如下:
#ifdef DFU_SUPPORT
ble_dfu_buttonless_init_t dfus_init = {0};
// Initialize the async SVCI interface to bootloader.
err_code = ble_dfu_buttonless_async_svci_init();
APP_ERROR_CHECK(err_code);
dfus_init.evt_handler = ble_dfu_evt_handler;
err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
#endif
其中定義ble_dfu_evt_handler,從ble_app_buttonless_dfu工程中複製即可。
Nordic對每一個模塊都有一個編譯總開關,因此還需到sdk_config.h中使能DFU模塊,如下:
#ifndef BLE_DFU_ENABLED
#define BLE_DFU_ENABLED 1
#endif
// <q> NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS - Buttonless DFU supports bonds.
#ifndef NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS
#define NRF_DFU_BLE_BUTTONLESS_SUPPORTS_BONDS 0
#endif
- 修改softdevice配置。現在整個應用包括2個vs_uuid:NUS和DFU,相應地ATT table size也要變大,然後應用程序RAM起始地址也需要跟着變,如下(注:這裏的attr_tab_size設置得稍稍偏大):
// <o> NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE - Attribute Table size in bytes. The size must be a multiple of 4.
#ifndef NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE
#define NRF_SDH_BLE_GATTS_ATTR_TAB_SIZE 1600
#endif
// <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 2
#endif
修改應用程序RAM起始地址,如下:
- 添加上述函數涉及到的文件,宏定義及包含相關目錄。首先添加如下DFU目錄及相關文件:
在define中添加這些宏:DEBUG DFU_SUPPORT BL_SETTINGS_ACCESS_ONLY NRF_DFU_SVCI_ENABLED NRF_DFU_TRANSPORT_BLE=1,其中DEBUG宏只是爲了調試方便而設置的,跟DFU本身無關。DFU_SUPPORT是我用來控制我添加的DFU代碼的,刪掉DFU_SUPPORT,將不編譯所有DFU有關代碼。其餘的宏都是系統自帶的,如果要支持DFU,也是必須要定義的。
然後包含如下目錄:
- 在main.c中添加如下頭文件定義:
#ifdef DFU_SUPPORT
#include "ble_dfu.h"
#endif
6)編譯工程,生成帶dfu的工程,該工程不能直接運行,必須加上boot-settings ,協議棧 sd,BootLoader一起燒錄才能正常運行 。
3、 批處理生成固件包
其中必須準備 第一步生成的祕鑰 以及私鑰,放在同一文件下 好進行批處理操作。
@echo on
:: 準備 BootLoader 和 固件app文件
copy "D:\52832\nRF5SDK15209412b96\nRF5_SDK_15.2.0_9412b96\examples\dfu\secure_bootloader\pca10040_ble\arm5_no_packs\_build\nrf52832_xxaa_s132.hex" "bootloader.hex"
copy "D:\bond_nrf52_work\nRF5_SDK_15.2.0_9412b96\examples\ble_peripheral\ble_app_uart\pca10040\s132\arm5_no_packs\_build\nrf52832_xxaa.hex" "nrf52832_xxaa_app.hex"
::第一步 生成bootloader setting文件
nrfutil.exe settings generate --family NRF52 --application nrf52832_xxaa_app.hex --application-version 0 --bootloader-version 0 --bl-settings-version 1 bootloader_settings.hex
::第二步 合併bootloader softdevice
mergehex.exe --merge bootloader.hex "D:\52832\nRF5SDK15209412b96\nRF5_SDK_15.2.0_9412b96\components\softdevice\s132\hex\s132_nrf52_6.1.0_softdevice.hex" --output production_final1.hex
::再合併application
mergehex.exe --merge production_final1.hex nrf52832_xxaa_app.hex --output production_final2.hex
::合併bootloader setting
mergehex.exe --merge production_final2.hex bootloader_settings.hex --output production_final.hex
::刪除中間文件
del bootloader_settings.hex
del production_final1.hex
del production_final2.hex
::第四步 下載固件
"C:\Program Files (x86)\Nordic Semiconductor\nrf5x\bin\nrfjprog" -f NRF52 --eraseall
"C:\Program Files (x86)\Nordic Semiconductor\nrf5x\bin\nrfjprog" -f NRF52 --program "production_final.hex" --verify
"C:\Program Files (x86)\Nordic Semiconductor\nrf5x\bin\nrfjprog" -f NRF52 --reset
:: 第五步 製作升級包
nrfutil pkg generate --hw-version 52 --sd-req 0xaf --application-version 0xff --application nrf52832_xxaa_app.hex --key-file private.pem nrf52832_dfu_app.zip
pause