How Can I Add Custom Application Code To DFU Bootloader?

Platform is SDK v17.1 / 52840 / secure bootloader with BLE.

I would like to add some application logic to the bootloader.

The idea is to periodically poll some spi devices and depending on the result have the bootloader run the application.

Power management would control sleep time until something interesting happens that causes the bootloader to start the application.

Where is the place in the bootloader code that I could hook-up the code described above?

Parents
  • Hi,

    Generally it is a good idea to make keep the bootloader small, so without knowing more I would consider if it would be better do start the app, and then check this in the start of the application instead.

    That said, if you want to do this in the bootloader I suggest doing configuration at startup, like in the bootloader's main function. And to do things periodically, expand the timer concept in nrf_bootloader_dfu_timers.c to add a timer for your repeated work. You can refer to an SPI example to see how to do the SPI related parts, and which files and configuration you need for that. Also, remember than when adding code to the bootloader it will increase in size, so you must change the linker settings, including the start address to be able to accommodate a larger bootloader.

  • Thanks for the info.

    The reason for this request is that while in the bootloader my code will communicate with some spi device and decide when to actually run the application.

    This brings up another point. Where in the bootloader code can I check to see if an application image exists and if it exists how can I cause it to be executed when I decide to run it?

  • davidb said:
    The reason for this request is that while in the bootloader my code will communicate with some spi device and decide when to actually run the application.

    It is still not clear to me why you want to do this in the bootloader? Would it not make more sense to start the app, and in the beginning of the app decide if you should proceed or not? Generally, putting code that is not related to boot or DFU in the bootloader is not a good idea (though there can of course be exceptions).

    davidb said:
    This brings up another point. Where in the bootloader code can I check to see if an application image exists and if it exists how can I cause it to be executed when I decide to run it?

    You can look at the app_is_valid() function in components/libraries/bootloader/nrf_bootloader.c. This checks several tings to check if an application is present, most importantly a flag indicating that it is and a CRC or signature, depending on the configuration.

Reply
  • davidb said:
    The reason for this request is that while in the bootloader my code will communicate with some spi device and decide when to actually run the application.

    It is still not clear to me why you want to do this in the bootloader? Would it not make more sense to start the app, and in the beginning of the app decide if you should proceed or not? Generally, putting code that is not related to boot or DFU in the bootloader is not a good idea (though there can of course be exceptions).

    davidb said:
    This brings up another point. Where in the bootloader code can I check to see if an application image exists and if it exists how can I cause it to be executed when I decide to run it?

    You can look at the app_is_valid() function in components/libraries/bootloader/nrf_bootloader.c. This checks several tings to check if an application is present, most importantly a flag indicating that it is and a CRC or signature, depending on the configuration.

Children
  • I have have followed the examples for spi configuration in my bootloader based on pca10056_s140_ble example.

    Under nRF_Drivers folder I added nrf_drv_spi.c and nrfx_spim.c.

    Updated sdk_config.h with:

    #ifndef NRFX_SPIM_ENABLED
    #define NRFX_SPIM_ENABLED 1
    #endif

    Under Application folder I added my spi driver code spi_nrf52840.[ch]. This driver works when built into our non-bootloader standalone application.

    I get build failures building the bootloader:

    spi_nrf52840.c:25:7: error: unknown type name 'nrf_drv_spi_config_t'.

    I added #include "nrf_drv_spi.h" to the driver and still get the same failure.

    Even though I included the header file I still get this error.

  • Never mind. I found some #includes that were causing the errors.

    Compile is good now.

Related