Uploading Images to Internal/External Flash Using OTA DFU

Background

This code example has a related User's Guide, here: Uploading Firmware Images Using OTA DFU

Description

The attached code implements image uploading to the slots using the standard OTA process. It is also extended by allowing the users to select a slot to upload to and a slot to boot from. You are free to modify this example according to your needs.

Setting up

To create a project with multi-slot OTA DFU, follow these steps:

  1. Create an Internal Storage Bootloader (multiple images) or a SPI Flash Storage Bootloader (multiple images) project in Simplicity Studio.

  2. Check the Storage Slot configuration in AppBuilder and modify it according to your needs.

  3. Generate and build the project.

  4. Create a SoC – Empty project.

  5. Remove the Silicon Labs OTA service from your GATT database if it is present.

  6. Add the Silicon Labs OTA service in Visual GATT Editor (BLE GATT Configurator).

    1. On the left plane, select Services.

    2. Find Silicon Labs OTA services.

    3. Drag and drop it to the right pane.

    4. Select the Silicon Labs OTA Data characteristic.

    5. Set its type to user (the content will not be stored to database but processed by the application).

    6. Set its length to 244.

    7. Set Write and Write Without Response properties to true.

    8. Select the Silicon Labs OTA control characteristic.

    9. set its type the user.

    10. Set Write and Write Without Response properties to true.

  7. Add Slot Manager service in Visual GATT Editor (only needed if you want to manage multiple slots).

    1. Add a new service, named e.g., Slot Manager Service.

    2. Add a new characteristic within this service called e.g., Upload Slot (this will be used to select a slot to upload data to).

    3. Set its ID to upload_slot (needed for reference in the code).

    4. Set its type to hex.

    5. Set its length to 1.

    6. Add Read and Write properties.

    7. Add a new characteristic within this service called e.g.,Bootload Slot (this will be used to trigger bootload from the given slot).

    8. Set its ID to boot_slot (needed for reference in the code).

    9. Set its type to hex.

    10. Set its length to 1.

    11. Add Write property.

  8. Now your GATT database should look like this:

  9. Click Generate to build the GATT database.

  10. Copy the attached ota_dfu_multislot.c and ota_dfu_multislot.h files into the project.

  11. Copy btl_interface.c and btl_interface_storage.c from C:\SiliconLabs\SimplicityStudio\v4\developer\sdks\gecko_sdk_suite\v2.x\platform\bootloader\api to your project.

  12. Go to Project Properties > C/C++ Build > Settings and add the following folders to the Includes:

               "$/platform/bootloader"
               "${StudioSdkPath}/platform/bootloader/api"

    or copy the .h files from these folders to your project

  13. Add the followings to main.c:

    1. Add #include “ota_dfu_multislot.h” to the beginning of main.

    2. Add ota_dfu_handle_event(evt); after evt = gecko_wait_event();

    3. Remove the code that triggers DFU reset upon writing into ota_control characteristic.

        ```
        //case gecko_evt_gatt_server_user_write_request_id:
        // ...
        //break;
        ```
    4. Save main.c.

  14. Build the project.

  15. Add the bootloader image to your application image as described here:

    Adding Gecko bootloader to Bluetooth projects

    Note: setting gecko_bootloader=true in the .isc file will copy btl_interface.c and btl_interface_storage.c into your project again. Remove the instances you copied before to avoid duplicated definitions).

  16. Flash the image to your device.

Usage

  1. Create full.gbl file from the application you want to upload. (Note that because the uploader is implemented in the user application and not in the Apploader, also implement the uploader in the application to be uploaded.) E.g.

    1. Make a copy of your multi-slot OTA DFU project.

    2. Change the Device Name from “Empty Example” to “Updated app” in the Visual GATT Editor.

    3. Press Generate.

    4. Build the project.

    5. Run create_bl_files.bat.

    6. Find the .gbl files in output_gbl folder.

  2. Copy full.gbl to your smartphone (in case of Android copy it to SiliconLabs_BGApp/OTAFiles/myproject, if the iOS upload it to iCloud).

  3. Open EFR Connect app on your smartphone.

  4. Find your device with Bluetooth browser (advertising as Empty Example) and connect to it.

  5. Find the unknown service and open it (this is your custom service including Upload slot and Bootload slot characteristics).

  6. Open the first characteristic (this is the upload characteristic) and write the slot number, that you want to upload to, in it.

  7. In the local menu select OTA.

  8. Select the partial OTA tab.

  9. Select the folder and the full.gbl file you want to upload.

  10. Click OTA. The file will be uploaded to the selected slot.

  11. Open the second characteristic in the unknown service (Bootload slot) and write the slot number, that you want to load the application from, in it. The device will be reset and the application will be loaded.

  12. Disconnect and find your device advertising itself as “Updated app”.

Source