# Modifying the Application

Modify the application with the following steps:

1. Copy the ble_peripheral_direction_finding sample application into your workspace. The C/C++ perspective opens and displays your newly copied project, as shown below in the "Bringing the Project Into the Workspace" figure.

Figure: Bringing the Project Into the Workspace

1. Add the FOTA software component to your project by using the RTE Configuration Wizard (the ble_peripheral_direction_finding.rteconfig file). When you select the FOTA component and click save, libfota.a, fota.bin, mkfotaimg.exe and the tool mkfotaimg.py are copied into your project, under RTE > Device > <device>. In addition, you need to deselect the BLE Stack component as shown in the "Adding the FOTA Software Component" figure.

Figure: Adding the FOTA Software Component

1. Modify the source code to set the FOTA version number and ID:
   1. In app.h, define your version numbers and ID:

/*--------------------------------------------

* Application Version

* -------------------------------------------------------------------------------- */

#define APP_VER_ID "DF"

#define APP_VER_MAJOR 1

#define APP_VER_MINOR 0

#define APP_VER_REVISION 0

2. In app.c, include sys_fota.h and use the SYS_FOTA_VERSION macro to set the version:

#include "sys_fota.h"

/* ----------------------------------------------------------------------------

* Application Version

* ------------------------------------------------------------------------- */

SYS_FOTA_VERSION(APP_VER_ID, APP_VER_MAJOR, APP_VER_MINOR, APP_VER_REVISION);

1. Modify the sample application to add random and seed initialization functions in app.c. These functions are initially defined in ble_protocol_support.c, which is excluded for the FOTA stack, as shown in the "Adding the FOTA Software Component" figure.

void srand_func(uint32_t seed)

{

srand(seed);

}

int rand_func(void)

{

return rand();

}

1. Modify the sample application to activate the DFU when the FOTA_GPIO on the RSL EVB is connected to ground.

| a. | In app.h, define an appropriate GPIO as FOTA_GPIO. For example:

#define FOTA_GPIO 1

| b. | In app_init.c, modify the DeviceInit() function to configure GPIO1 as a GPIO input:

void DeviceInit(void)

{

...

/* Configure FOTA_GPIO as GPIO input */

SYS_GPIO_CONFIG(FOTA_GPIO, (GPIO_MODE_GPIO_IN | GPIO_LPF_DISABLE | GPIO_WEAK_PULL_UP | GPIO_6X_DRIVE));

...

}

| c. | In app.c, add the code below in the while(1) loop of the main() function, to start the DFU when the FOTA GPIO is connected to ground:

while (1)

{

...

/* Start update when FOTA_GPIO is connected to ground */

if ((Sys_GPIO_Read(FOTA_GPIO)) == 0)

{

Sys_Fota_StartDfu(1);

}

...

}

The DFU component is activated by calling the Sys_Fota_StartDfu(mode) function defined in sys_fota.h. The mode value 0 is for an application without the Bluetooth Low Energy stack (for example, blinky), and 1 is for an application that uses the Bluetooth Low Energy stack (such as ble_peripheral_server).

1. Replace your linker script sections.ld file with the one that contains the FOTA placement, and also replace your startup.S file, as shown in the "Modifying the Application" figure. The new linker script and startup.S file can be copied from ble_peripheral_server_fota, or directly from the CMSIS-Pack root folder as follows:
   - Non-Bluetooth Low Energy Application:
     - sections.ld and startup.s: <CMSIS Pack root folder> > ONSemiconductor > <device> > <version> > firmware > source > lib > fota > app > non_ble
   - Bluetooth Low Energy Application:
     - sections.ld and startup.s: <CMSIS Pack root folder>> ONSemiconductor > <device> > <version> > firmware > source > lib > fota > app

1. Modify the project post-build steps (see the "Post-Build Steps" figure) to generate the FOTA image in both Debug and Release build configurations, by going to Project > Properties > C/C++ Build > Settings > Build Steps > Post-build steps. Two steps are required to generate the .fota image. First, we need to generate the application sub-image .bin file using objcopy. Then, we use the mkfotaimg.exe tool to generate the FOTA image (.fota file). Both commands can be concatenated with && and added to the post-build steps as in the example that follows. To use this command, copy the code from the Post-Build steps of the existing sample application ble_peripheral_server_fota, as the code in the text below is not directly copyable.

${cross_prefix}objcopy -O binary "${BuildArtifactFileName}" "${BuildArtifactFileBaseName}.bin" && "${ProjDirPath}/RTE/Device/RSL15/mkfotaimg.exe" -o "${BuildArtifactFileBaseName}.fota" "${ProjDirPath}/RTE/Device/RSL15/fota.bin" "${BuildArtifactFileBaseName}.bin"

Figure: Post-Build Steps

1. Build the ble_peripheral_direction_finding application. If no error occurs, the FOTA image (ble_peripheral_direction_finding.fota) can be found in the Debug or Release folder.