nRF51 SDK - S110 SoftDevice
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Application with DFU Service support

Application with DFU Service support

This page describes how to include and use the possibility of including the DFU BLE Service into an application so that Bootloader/DFU mode can be automatically entered.
After entering Bootloader/DFU mode then a DFU Over-The-Air update can be performed.

An example application based on Heart Rate Application has been created for testing this feature. The project file for this example is located together with the existing Heart Rate example.

The source code and project file for supporting BLE DFU Service can be found in the
<InstallFolder>\Nordic\nrf51822\Board\nrf6310\s110\ble_app_hrs
folder.

Open the project file ble_app_hrs_dfu.uvproj with Keil for testing and examine the BLE DFU Service functionality in a BLE example.

Testing

The DFU BLE Service support Heart Rate Application can be tested using the Master Control Panel as follows:

  1. Compile and program the application.
    • Observe that the Advertising LED is lit.
  2. Connect to the device from Master Control Panel (the device will be advertising as 'Nordic_HRM'), then perform service discovery.
    • Observe that the Connected LED is lit, and the Advertising LED is off.
  3. Enable 'DFU Notifications' by writing the value '0100' to the cccd in DFU Control Point.
  4. Writing 'Start DFU', i.e. the value '01' to the 'DFU Control Point'
  5. Notice that the connection to the BLE device is lost.
    • Will write 'SERVER: Received Link Loss' in the log window
  6. Press 'Back' in Master Control Panel' to enter the 'Discovered devices' overview.
  7. Clear the discovered devices and 'Start discovery'
  8. Verify that the device is now advertising as 'DfuTarg'
  9. Perform a DFU update with any test image or application you like

Supporting DFU in any BLE example

Any BLE application can easily be extended to support the DFU Service which allows nRF51 to enter DFU mode by writing 'DFU Start' to the DFU Service Control Point.

The following must be done to support entering of DFU mode from application

  • Add DFU related files to BLE example project
    • bootloader_util_arm.c
    • ble_dfu.c
    • dfu_app_handler.c
  • Implement the reset_prepare() function to allow application specific operations executed for a graceful system shutdown
  • Initializing the DFU Service
  • Propagate SoftDevice BLE events to the DFU Service

Including DFU files in application

When supporting the DFU BLE Service in an existing BLE application the following procedure can be used:

  1. Open the BLE application in Keil, e.g. Heart Rate example
  2. Add a new group, Dfu
  3. Adding the files:
    • bootloader_util_arm.c, found in <InstallFolder>\Nordic\nrf51822\Source\bootloader_dfu
    • dfu_app_handler.c, found in <InstallFolder>\Nordic\nrf51822\Source\bootloader_dfu
    • ble_dfu.c, found in <InstallFolder>\Nordic\nrf51822\Source\ble\ble_services

This can be seen in figure 1-3

dfu_existing_proj.png
Figure 1: Opening an existing Keil BLE project
dfu_add_group_proj.png
Figure 2: Adding a DFU Group to project
dfu_add_files_proj.png
Figure 3: Adding files to the DFU group

Initialising DFU Service in BLE example

In order to support the BLE DFU service in an application then the DFU Service must be initialized in the application.

The following code snippet shows how this is done in the Heart Rate example supporting DFU in main.c

#include "ble_dfu.h"
#include "dfu_app_handler.h"
ble_dfu_init_t dfus_init;
// Initialize the Device Firmware Update Service.
memset(&dfus_init, 0, sizeof(dfus_init));
dfus_init.evt_handler = dfu_app_on_dfu_evt;
dfus_init.error_handler = NULL; //service_error_handler - Not used as only the switch from app to DFU mode is required and not full dfu service.
err_code = ble_dfu_init(&m_dfus, &dfus_init);
APP_ERROR_CHECK(err_code);
dfu_app_reset_prepare_set(reset_prepare);

Remember that in order for the DFU Service to function properly then all BLE Stack events from the SoftDevice must be propagated to the DFU Service by calling ble_dfu_on_ble_evt containing the event as parameter.

BLE Stack events are usually progated to sub modules in main.c in a function named ble_evt_dispatch(ble_evt_t * p_ble_evt)

ble_dfu_on_ble_evt(&m_dfus, p_ble_evt);

Switching from application to Bootloader/DFU mode

Before switching from application to bootloader/DFU mode for receiving a new firmware image it might be important for the current application to shut down gracefully.

In order to support a graceful shutdown then the dfu_app_handler.c supports a reset_prepare function to be implemented in the application. This function will be executed prior to the reset and allows the application to perform any tasks considered important prior to reset.

Be aware that the reset will not occur until the reset_prepare function returns.

Such tasks could be:

  • Gracefully disconnect and close any active BLE links
  • Wait for pending flash operation to finish to ensure known state
  • Cleanup registers
  • Other

In main.c of Heart Rate example the prepare function contains

  • Graceful disconnect and closure of BLE links or advertising stop if no open connections
  • Turning off LEDs

Example of Reset prepare implementation:

static void reset_prepare(void)
{
uint32_t err_code;
if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
{
// Disconnect from peer.
err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
APP_ERROR_CHECK(err_code);
}
else
{
// If not connected, then the device will be advertising. Hence stop the advertising.
advertising_stop();
}
nrf_gpio_pin_clear(ADVERTISING_LED_PIN_NO);
nrf_gpio_pin_clear(CONNECTED_LED_PIN_NO);
err_code = ble_conn_params_stop();
APP_ERROR_CHECK(err_code);
}

Switching from running the application into Bootloader/DFU mode is done through writing to the control point of the DFU Service, see DFU Control Point for further details.

The below flowchart show the basic principle of switching from running a BLE Application to run the Bootloader/DFU.

ota_dfu_buttonless.png
Figure 4: Switching from application to Bootloader/DFU mode

After entering Bootloader/DFU mode then a SoftDevice, Bootloader or Application can be transfered Over-the-Air as described in Device Firmware Update over BLE.

Note
No acknowledgement will be sent when writing the Control Point with 'Start DFU' operation code. The system will close the connection and silently switch to Bootloader/DFU mode.