前言
SDK版本15.3
評估闆:pca10040
在 uart 的例程中添加 DFU 功能,使用 s132 的協定棧,因為官方的 BootLoader 工程用的是s132的協定棧。
一、準備工作
在開始實驗之前必須先準備以下軟體:
- gcc-arm-none-eabi-7-2018-q2-update-win32.exe
由于使用加密的dfu需要用到micro-ecc庫進行簽名驗證,需要micro_ecc_lib_nrf52.lib,而官方的sdk中并沒有加入micro_ecc_lib_nrf52.lib,是以需要我們自己編譯
直接在官網下載下傳或者用我下載下傳好的,
官網下載下傳位址:https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
我下載下傳好的:https://download.csdn.net/download/m_pfly_fish/12152291
15.3的sdk用的是7-2018-q2-update版本的gcc,安裝這個版本可以跳過編譯micro-ecc庫時對Makefile.windows檔案的修改。用其他版本的gcc時,需要根據版本修改該檔案中的gcc路徑和版本資訊
- make-3.81.exe
官網下載下傳位址:http://gnuwin32.sourceforge.net/packages/make.htm
我下好的:https://download.csdn.net/download/m_pfly_fish/12153126
Windows系統的gnu編譯器,執行make用
- micro-ecc-master.zip
官網下載下傳位址:https://github.com/kmackay/micro-ecc
Nordic的DFU中采用了micro-ecc實作ECDSA算法,編譯micro_ecc_lib_nrf52.lib時所用到的源碼
- python-2.7.12.amd64.msi
官網下載下傳位址:https://www.python.org/downloads/
我下好的:https://download.csdn.net/download/m_pfly_fish/12153141
nrfutil需要用到python-2.7的環境
- nRFgo Studio
從官網下就行
我下好的:https://download.csdn.net/download/m_pfly_fish/12051047
主要提供nrfjprog.exe和mergehex.exe。這兩個軟體在nRFgo Studio的安裝目錄下找到 C:\Program Files (x86)\Nordic Semiconductor\nrf5x\bin 安裝之後用cmd輸入nrfjprog和mergehex,檢視是否安裝可用。
- oreutils-5.3.0.exe
官網下載下傳位址:http://gnuwin32.sourceforge.net/packages/coreutils.htm
我下好的:https://download.csdn.net/download/m_pfly_fish/12153167
二、軟體安裝
2.1 輕按兩下安裝 make-3.81.exe
一直next,注意安裝目錄使用預設的c盤
2.2 輕按兩下安裝 coreutils-5.3.0.exe
一直next,注意安裝目錄使用預設的c盤
2.3 輕按兩下安裝 gcc-arm-none-eabi-7-2018-q2-update-win32.exe
一直next,注意安裝目錄使用預設的c盤
安裝完成後需要将路徑C:\Program Files (x86)\GNU Tools ARM Embedded\7 2018-q2-update\bin;和C:\Program Files (x86)\GnuWin32\bin添加到環境變量Path中
我因為之前還安裝了 4.9 2015q3 的 gcc 是以Path中多一個4.9版本的路徑
2.4 安裝 python-2.7.12.amd64.msi
一直next,注意安裝目錄使用預設的c盤
安裝完畢後,同樣需要把python的路徑加到環境變量中
添加完成後需要安裝 nrfutil
打開 cmd 指令行視窗,跳轉至 C:\Python27 目錄下,輸入python -m pip install nrfutil等待安裝完成
三、修改編譯BootLoader工程
直接編譯sdk中的secure_bootloader工程時會出現以下報錯,找不到 uECC.h,micro_ecc_lib_nrf52.lib 和 pk
error: #5: cannot open source input file "uECC.h": No such file or directory
error: L6002U: Could not open file ..\..\..\..\..\external\micro-ecc\nrf52hf_keil\armgcc\micro_ecc_lib_nrf52.lib: No such file or directory
Error: L6218E: Undefined symbol pk (referred from nrf_dfu_validation.o).
3.1 先解決 缺少 uECC.h 的問題
将下載下傳的 micro-ecc-master.zip 解壓後修改檔案夾名為 micro-ecc,然後拷貝至 nRF5_SDK_15.3.0_59ac345\external\micro-ecc 目錄下
在該檔案夾中可以找到 uECC.h
3.2 解決缺少 micro_ecc_lib_nrf52.lib 的問題
如果你使用的是15.3的sdk,并且安裝了 gcc-arm-none-eabi-7-2018-q2-update-win32.exe 版本的gcc,那麼直接輕按兩下運作build_all.bat 腳本就可以自動編譯出 micro_ecc_lib_nrf52.lib ,在 nRF5_SDK_15.3.0_59ac345\external\micro-ecc\nrf52hf_keil\armgcc 目錄中可以找到 micro_ecc_lib_nrf52.lib
假如使用的不是15.3版本的sdk或者使用的不是 gcc-arm-none-eabi-7-2018-q2-update-win32.exe版本的gcc,那麼需要修改 nRF5_SDK_15.3.0_59ac345\components\toolchain\gcc 目錄下 Makefile.windows 檔案
GNU_INSTALL_ROOT := C:/Program Files (x86)/GNU Tools ARM Embedded/7 2018-q2-update/bin/
GNU_VERSION := 7.3.1
GNU_PREFIX := arm-none-eabi
将gcc編譯器路徑更換為實際路徑和實際版本
我使用 4.9 2015q3版本的 gcc 時,将修改 Makefile.windows 檔案修改為:
GNU_INSTALL_ROOT := C:/Program Files (x86)/GNU Tools ARM Embedded/4.9 2015q3/bin/
GNU_VERSION := 6.3.0
GNU_PREFIX := arm-none-eabi
3.3 解決缺少 pk 的問題
打開cmd指令行,輸入一下指令
nrfutil.exe keys generate private.pem
nrfutil.exe keys display --key pk --format code private.pem --out_file public_key.c
這樣生成的秘鑰就儲存在 public_key.c中,打開public_key.c。把數組pk複制到報錯的位置。
3.4 修改BootLoader工程代碼
解決完報錯後,BootLoader工程還不能正常工作,需要根據情況修改代碼
- 如果LED燈IO口跟secure_bootloader工程預設引腳不同或原來LED引腳用作其他功能,需要将dfu_observer()函數中LED燈相關代碼屏蔽,否則會一直運作在bootloader,不跳轉到application
3.5 配置sdk_config檔案
将進入DFU方式改為無按鍵的BLE連接配接發送指令方式
四、修改Application工程
4.1 配置sdk_config檔案
- 使能 dfu service
- 修改 VS UUID COUNT 值,因為dfu service使用了 128bit 的自定義的 base uuid,是以這裡需要在原先的 count 值上+1
4.2 修改ram空間
因為添加了一個自定義的 base uuid,是以需要修改 ram 空間,每增加一個uuid,IRAM1的起始位址就要增加0x10,size 同時減少0x10。
4.3 添加Include目錄
- …/…/…/…/…/…/components/libraries/bootloader
- …/…/…/…/…/…/components/libraries/bootloader/ble_dfu
- …/…/…/…/…/…/components/libraries/bootloader/dfu
- …/…/…/…/…/…/components/libraries/svc
4.4 添加源檔案
在工程中增加一個檔案夾nRF_DFU,并添加以下檔案:
- \components\ble\ble_services\ble_dfu\ble_dfu.c
- \components\ble\ble_services\ble_dfu\ble_dfu_bonded.c
- \components\ble\ble_services\ble_dfu\ble_dfu_unbonded.c
在工程中增加一個檔案夾nRF_SVC,并添加以下檔案:
- \components\libraries\bootloader\dfu\nrf_dfu_svci.c
4.5添加宏
添加下列項:
- NRF_DFU_TRANSPORT_BLE=1
- BL_SETTINGS_ACCESS_ONLY
- NRF_DFU_SVCI_ENABLED
4.6 修改 main.c
添加頭檔案
#include "nrf_dfu_ble_svci_bond_sharing.h"
#include "nrf_svci_async_function.h"
#include "nrf_svci_async_handler.h"
#include "ble_dfu.h"
#include "nrf_power.h"
#include "nrf_bootloader_info.h"
添加代碼
/**@brief Handler for shutdown preparation.
*
* @details During shutdown procedures, this function will be called at a 1 second interval
* untill the function returns true. When the function returns true, it means that the
* app is ready to reset to DFU mode.
*
* @param[in] event Power manager event.
*
* @retval True if shutdown is allowed by this power manager handler, otherwise false.
*/
static bool app_shutdown_handler(nrf_pwr_mgmt_evt_t event)
{
switch (event)
{
case NRF_PWR_MGMT_EVT_PREPARE_DFU:
NRF_LOG_INFO("Power management wants to reset to DFU mode.");
// YOUR_JOB: Get ready to reset into DFU mode
//
// If you aren't finished with any ongoing tasks, return "false" to
// signal to the system that reset is impossible at this stage.
//
// Here is an example using a variable to delay resetting the device.
//
// if (!m_ready_for_reset)
// {
// return false;
// }
// else
//{
//
// // Device ready to enter
// uint32_t err_code;
// err_code = sd_softdevice_disable();
// APP_ERROR_CHECK(err_code);
// err_code = app_timer_stop_all();
// APP_ERROR_CHECK(err_code);
//}
break;
default:
// YOUR_JOB: Implement any of the other events available from the power management module:
// -NRF_PWR_MGMT_EVT_PREPARE_SYSOFF
// -NRF_PWR_MGMT_EVT_PREPARE_WAKEUP
// -NRF_PWR_MGMT_EVT_PREPARE_RESET
return true;
}
NRF_LOG_INFO("Power management allowed to reset to DFU mode.");
return true;
}
//lint -esym(528, m_app_shutdown_handler)
/**@brief Register application shutdown handler with priority 0.
*/
NRF_PWR_MGMT_HANDLER_REGISTER(app_shutdown_handler, 0);
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);
}
}
/* nrf_sdh state observer. */
NRF_SDH_STATE_OBSERVER(m_buttonless_dfu_state_obs, 0) =
{
.handler = buttonless_dfu_sdh_state_observer,
};
static void advertising_config_get(ble_adv_modes_config_t * p_config)
{
memset(p_config, 0, sizeof(ble_adv_modes_config_t));
p_config->ble_adv_fast_enabled = true;
p_config->ble_adv_fast_interval = APP_ADV_INTERVAL;
p_config->ble_adv_fast_timeout = APP_ADV_DURATION;
}
static void disconnect(uint16_t conn_handle, void * p_context)
{
UNUSED_PARAMETER(p_context);
ret_code_t err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
if (err_code != NRF_SUCCESS)
{
NRF_LOG_WARNING("Failed to disconnect connection. Connection handle: %d Error: %d", conn_handle, err_code);
}
else
{
NRF_LOG_DEBUG("Disconnected connection handle %d", conn_handle);
}
}
// 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.");
// Prevent device from advertising on disconnect.
ble_adv_modes_config_t config;
advertising_config_get(&config);
config.ble_adv_on_disconnect_disabled = true;
ble_advertising_modes_config_set(&m_advertising, &config);
// Disconnect all other 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.
uint32_t conn_count = ble_conn_state_for_each_connected(disconnect, NULL);
NRF_LOG_INFO("Disconnected %d links.", conn_count);
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;
}
}
在 int main(void) 中添加以下代碼
ret_code_t err_code;
// Initialize the async SVCI interface to bootloader before any interrupts are enabled.
err_code = ble_dfu_buttonless_async_svci_init();
APP_ERROR_CHECK(err_code);
在static void services_init(void)中添加以下代碼
ble_dfu_buttonless_init_t dfus_init = {0};
dfus_init.evt_handler = ble_dfu_evt_handler;
err_code = ble_dfu_buttonless_init(&dfus_init);
APP_ERROR_CHECK(err_code);
五、生成燒錄固件
直接燒錄softdevice、bootloader和application,會發現application并未運作,晶片一直跑在Bootloader中。
晶片啟動後先進入Bootloader,檢測Bootloader Settings中的資料,如果這些資料訓示Flash中有一個有效的Application,則跳轉進入Application。Bootloader Settings是Flash中的一段區域,它包含了Application的大小、CRC等資料,執行DFU時也會在這裡存儲狀态資訊。
正常執行DFU時,Bootloader自動生成和維護Bootloader Settings資訊。而燒錄過程不同,需要手動寫入。可以根據application.hex生成一個bl_settings.hex,以産生這些資料,然後燒錄這個hex,而我們一般将需要燒錄的bl_settings.hex、softdevice、bootloader 和 application 檔案合并成一個檔案,友善量産燒錄
5.1 生成bl_settings.hex
打開cmd指令視窗 輸入 nrfutil.exe settings generate --family NRF52 --application nrf52832_xxaa_app.hex --application-version 0 --bootloader-version 0 --bl-settings-version 2 bootloader_settings.hex
application-version、bootloader-version、bl-settings-version 分别是 application 版本号、 bootloader 版本号和 bl-settings版本号,可以自定。
5.2 合并 bl_settings.hex、softdevice、bootloader 和 application 檔案
合并 BootLoader 檔案和 softdevice 檔案:mergehex.exe --merge nrf52832_xxaa_s132_bootloader.hex s132_nrf52_6.1.1_softdevice.hex --output production_final1.hex
合并 application 檔案:mergehex.exe --merge production_final1.hex nrf52832_xxaa_app.hex --output production_final2.hex
合并 bl_settings.hex 檔案:mergehex.exe --merge production_final2.hex bootloader_settings.hex --output production_final.hex
production_final 檔案就是最終燒錄固件,通過以下指令可以直接燒錄固件至開發闆中:
- nrfjprog -f NRF52 --eraseall
- nrfjprog -f NRF52 --program "production_final.hex" --verify
- nrfjprog -f NRF52 --reset
六、打包ota固件
打包OTA所需的zip檔案。這裡隻介紹更新 application 的方法
打開cmd指令視窗 輸入 nrfutil pkg generate --hw-version 52 --sd-req 0xB7 --application-version 1 --application nrf52832_xxaa_app.hex --key-file private.pem nrf52832_xxaa_app.zip
其中
--sd-req 0xB7,0xB7 是協定棧版本,檢視方式可以用nrf go連接配接闆子後檢視
--application-version 1 是 application 版本,自定
--key-file private.pem 是之前編譯 bootloader 工程時生成的私鑰檔案
生成的壓縮包就是 ota 檔案。
七、OTA操作流程
以下用圖檔來示範操作 ota 流程
發送df指令,控制裝置進入BootLoader
BootLoader模式下的裝置名可以在BootLoader工程中進行更改
點選右上dfu按鈕進行開始ota
ota完成後,裝置會自動重新開機,跳轉至 application
參考
https://blog.csdn.net/qq_36347513/article/details/103744653
https://blog.csdn.net/jdsnpgxj/article/details/80772727
(有空再把dfu的原理總結出來)