nRF51 SDK - S110 SoftDevice
|
You can extend any BLE application to support the BLE DFU Service, so that you can easily enter bootloader mode to update the device firmware at any time.
The following changes are required to add BLE DFU Service support to an application:
With these changes to the application, the application can restart the device in bootloader mode to accept firmware updates. To remotely trigger the application to restart the device in bootloader mode, you must write "DFU Start" to the DFU Service Control Point. See Switching to bootloader/DFU mode.
Perform the following steps to include the files that are required to support the BLE DFU Service in an application:
C:\Keil\ARM\Pack\NordicSemiconductor\nRF_Libraries\<version>\bootloader_dfu
if you are using Keil packs and <InstallFolder>\components\libraries\bootloader_dfu
if you are using the repository distribution variant of the SDK.)C:\Keil\ARM\Pack\NordicSemiconductor\nRF_Libraries\<version>\bootloader_dfu
if you are using Keil packs and <InstallFolder>\components\libraries\bootloader_dfu
if you are using the repository distribution variant of the SDK.)C:\Keil\ARM\Pack\NordicSemiconductor\nRF_BLE\<version>\ble_services\ble_dfu
if you are using Keil packs and <InstallFolder>\components\ble\ble_services\ble_dfu
if you are using the repository distribution variant of the SDK.)Before the application switches to bootloader mode, it should shut down gracefully. To achieve that, you must implement a function that is called before the shutdown.
dfu_app_handler.c
supports a reset_prepare callback function, which will be executed before the reset and allows the application to perform any tasks that are considered important before restarting in bootloader mode. For example:
The reset will not occur until the reset_prepare function returns.
See the following example of a reset_prepare function from main.c
of the Heart Rate Application with BLE DFU Service support:
To be able to trigger an update remotely, you must initialize the BLE DFU Service and enable the Service Changed characteristic. In addition, you must make sure that the Device Manager knows about the new Service Changed characteristics.
The BLE DFU Service is required to connect to the application and trigger a restart into bootloader mode. To be able to write to the BLE DFU Service, you must initialize the service in the application.
See the following example code from main.c
of the Heart Rate Application with BLE DFU Service support:
#include "ble_dfu.h" #include "dfu_app_handler.h"
You must enable the Service Changed characteristic so that changes in the application are indicated to other devices, most importantly the DFU controller.
To enable the characteristic, simply set IS_SRVC_CHANGED_CHARACT_PRESENT to 1 in the application:
#define IS_SRVC_CHANGED_CHARACT_PRESENT 0
The Device Manager keeps track of the characteristics that are enabled for an application. Therefore, you must update the Device Manager configuration to account for the two new Service Changed characteristics (one for the application itself and one for the DFU Service).
Set the maximum number of characteristic client descriptors in the file device_manager_cnfg.h
(located in C:\Keil\ARM\Pack\NordicSemiconductor\nRF_BLE\<version>\device_manager\config
if you are using Keil packs and <InstallFolder>\components\ble\device_manager\config
if you are using the repository distribution variant of the SDK):
#define DM_GATT_CCCD_COUNT 4
The BLE DFU Service relies on BLE stack events. Therefore, for the service to function properly, all BLE Stack events from the SoftDevice must be propagated to the DFU Service.
To propagate BLE stack events to the BLE DFU Service, call ble_dfu_on_ble_evt with the BLE stack event as a parameter. In most SDK examples, BLE stack events are propagated to submodules in the function ble_evt_dispatch(ble_evt_t * p_ble_evt) in main.c
.
See the following example code from main.c
of the Heart Rate Application with BLE DFU Service support: