nRF51 SDK: Bootloader/DFU Keil project

| nRF51 SDK

- Home
- Related Pages
- API Reference
- Data Structures

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
Bootloader/DFU Keil project

# Bootloader/DFU compile targets

The Bootloader/DFU project can be built using Keil uVision.

The project file contains three targets

Target | Description
--- | ---
dfu_dual_bank_hci | Project target using dual bank solution and image is transferred using HCI Transport layer on UART. See Serial (HCI)
dfu_single_bank_hci | Project target using single bank solution and image is transferred using HCI Transport layer on UART. See Serial (HCI)
dfu_dual_bank_ble (s110) | Project target using dual bank solution and image is transferred using BLE with the S110 SoftDevice. See BLE
dfu_dual_bank_ble (s310) | Project target using dual bank solution and image is transferred using BLE with the S310 SoftDevice. See BLE

The target can be selected in the project chooser in Keil as seen in Figure 1.

The single bank principle is described further in Single Bank Flash Layout.
Dual bank description is found in Dual Bank Flash Layout.

Figure 1: Choosing bootloader project in Keil.

In the nRF51 series, a SoftDevice will be placed at Code Region 0, starting at address `0x00000000`.
In the Bootloader/DFU example, the S110 SoftDevice is used and thus it must be present a this address, (see the nRF51822 DK User Guide on how to flash the SoftDevice).

The SoftDevice will usually start the application located at the beginning of code region 1, which is `0x00014000`. In order for the SoftDevice to be able to initiate a bootloader, instead of an application, the UICR.BOOTLOADERADDR register must be written. This register is located at address `0x10001014` .

The Bootloader/DFU is located at address `0x0003C800`, as seen in Figure 2..

Figure 2: Bootloader/DFU Flash layout overview.

In the Bootloader/DFU project, the writing of the UICR.BOOTLOADERADDR is linked into the HEX file and will be written automatically when flashing the bootloader with `nrfjprog.exe`, see Bootloader/DFU Flashing.

This is ensured by the following code in `bootloader_util_arm.c`

uint32_t m_uicr_bootloader_start_address __attribute__((at(NRF_UICR_BOOT_START_ADDRESS))) = BOOTLOADER_REGION_START;

`BOOTLOADER_REGION_START` must point to the correct location of the bootloader in the flash. The location in this example is `0x0003C800`.

# Bootloader/DFU Flashing

The J-Link Target Driver used for flashing other example applications in the nRF51 SDK does not support writing of the UICR register.

Therefore it is necessary to use the `nrfjprog.exe` tool for programming the nRF51 with the Bootloader/DFU project.
The `nrfjprog.exe` is delivered as part of the nRF51 SDK installer, and should therefore already be present.

The Bootloader/DFU project is configured to use `nrfjprog.exe` per default, and is expecting the tool to be present in windows' system path. `nrfjprog.exe` programs the compiled and linked HEX file to the nRF51 chip when flashing from Keil uVision.

Figure 3 shows the settings for `nrfjprog.exec` in Keil.

Figure 3: Flash tool configuration in Keil uVision

If more than one J-Link is present in the system, a pop-up windows will appear requiring the user to choose which to use. Choose the J-Link attached to the Development Kit containing the nRF51 Chip to be flashed with Bootloader/DFU. See Figure 5.

Figure 4: nrfjprog.exe Pop-up for J-Link selection

# Bootloader/DFU Application Image Conversion

The DFU supports only binary images for data transfer as described in DFU Transport Layers.

When compiling an application, such as Heart Rate Measurement, two images are created.

- A HEX file.
- An AXF file.

Keil uVision contains a command line tool, `fromelf.exe`, which can convert the AXF file into a binary image.
The tool is located in the Keil installation folder as:

    <Keil-folder>\ARM\ARMCC\bin\fromelf.exe

Converting an AXF image into BIN file can be done as:

    <Keil-folder>\ARM\ARMCC\bin\fromelf.exe --bin --output <outfile.bin> <infile.axf>

# Bootloader/DFU Flash relocation.

In case the Bootloader/DFU software is extended with additional functionality and a larger code size is needed, then it is possible to move the start address of the Bootloader/DFU. If moving the bootloader to a different location, as example `0x00038000`, then the `BOOTLOADER_REGION_START` defined in `dfu_types.h` must be updated accordingly.

#define BOOTLOADER_REGION_START 0x0003C800

For the linker to work correctly, then changing `BOOTLOADER_REGION_START` also requires change to the Keil project. In Keil, go to `project->options for target`, and change the IROM1 Start address to the new value. Also remember to increase the Size field (default `0x3400` ).

As example, moving from address `0x0003C800`, Size `0x3400` to new address `0x00038000` gives 0x4800 additional bytes (18 pages), thus the new size becomes `0x8200`.

Note
  Changing the Bootloader/DFU Start address requires the UICR register to be erased. To ensure correct behaviour, the following procedure should be followed
  1) Erase the whole chip with `nrfjprog.exe`
  2) Flash the SoftDevice
  3) Flash the Bootloader/DFU with the new address.

Figure 5 shows the setting in Keil to modify, if the Bootloader/DFU start address must be changed.

Note
  All Start addresses MUST be page aligned and the page size is 0x400 (1024d) bytes.

Figure 5: Bootloader/DFU Start address setting in Keil.

# Troubleshooting

This section covers some known issues and their solution for the Bootloader/DFU project

Error | Solution
--- | ---
linking... ._build\bootloader.axf: Error: L6406E: No space in execution regions with .ANY selector matching ``..... | This error message will be seen if the Bootloader/DFU image does not fit into the Size specified in the project setting, try using higher optimization level, -O3, or increase the Bootloader/DFU code area as described in Bootloader/DFU compile targets
No Algorithm found for: 10001014H - 10001017H Partial Erase Done (areas with no algorithms skipped!) No Algorithm found for: 10001014H - 10001017H | Those error messages are seen when trying to flash with the J-Link Target Driver, as it does not support writing of UICR. Use `nrfjprog.exe` instead.
Bootloader/DFU project suddenly stopped working after changing Bootloader/DFU Start address and re-flashing | After changing the Bootloader/DFU Start address, the nRF51822 Chip should be erased and re-programmed with the SoftDevice and then with the Bootloader/DFU.
— Error: failed to execute '"nrfjprog.exe" ..... | nrfjprog.exe could not be found in windows path. Add nrfjprog.exe to windows %PATH% or specify full path to nrfjprog in Keil-'Options for Target'-'Utilities' as seen in Figure 3.
Uploaded application does not run | Verify that the application has been compiled with start address equal to the value in CLENR0 register. For S110 SoftDevice this would be Address `0x00014000` and for S310 SoftDevice it would be `0x00020000`.