2.3 Bootloader Trigger Methods
The bootloader can be invoked using these methods:
- The bootloader runs on every system reset, or when there is no valid firmware in the device. The bootloader continuously waits in a loop to receive the firmware from the host to upgrade. The firmware is considered valid if the first word at the application start address is Bank A (0x00002000), and Bank B (0x00082000) is not 0xFFFFFFFF. Normally this word contains an initial stack pointer value, therefore it will never be 0xFFFFFFFF unless the device is erased. On a system reset, the bootloader checks whether a trigger to upgrade the firmware is present. If there is no valid trigger for the firmware upgrade, it tries to run the existing firmware. If there is no valid firmware, it jumps to a loop waiting to receive the firmware from the host.
- The bootloader provides a function
bootloader_Trigger()
which allows the user to upgrade the existing application. Thebootloader_Trigger()
function checks a switch press event or a pattern in the SRAM to know if there is a request to upgrade the existing application. The code for thebootloader_Trigger()
function is shown below. This trigger function is called from the bootloader system initialization function.#define BTL_TRIGGER_PATTERN 0x5048434D static uint32_t *ramStart = (uint32_t *)BTL_TRIGGER_RAM_START; bool bootloader_Trigger(void) { uint32_t i; // Cheap delay. This should give at leat 1 ms delay. for (i = 0; i < 2000; i++) { asm("nop"); } /* Check for Bootloader Trigger Pattern in first 16 Bytes of RAM to enter Bootloader. */ if (BTL_TRIGGER_PATTERN == ramStart[0] && BTL_TRIGGER_PATTERN == ramStart[1] && BTL_TRIGGER_PATTERN == ramStart[2] && BTL_TRIGGER_PATTERN == ramStart[3]) { ramStart[0] = 0; return true; } /* Check for Switch press to enter Bootloader */ if (SWITCH_Get() == 0) { return true; } return false; }
The following methods can be used to upgrade the firmware while the application is running:
- External Trigger: While the
application is running, the user presses the external system reset switch and a user
switch simultaneously. The device resets and starts running the bootloader. Since
the user switch is pressed, the
bootloader_Trigger()
function detects the switch press using theSWITCH_Get()
function and returns true, indicating a firmware upgrade is requested. The bootloader takes care of receiving the data from the host and upgrades the device. - Software application Trigger: If an application does not have an option for an external trigger or application requirement to upgrade the firmware based on specific commands, it can use a software trigger method to run the bootloader for the firmware upgrade.
- The application implements the
invoke_bootloader()
function. While the application is running, it intends to upgrade by itself. The application will call theinvoke_bootloader()
function and fill a dedicated area in the SRAM with a known pattern (0x5048434D) and issue a software reset. This prefilled SRAM pattern is compared in thebootloader_trigger()
function, and if there is a match, thebootloader_trigger()
function returns true indicating a firmware upgrade is requested. The bootloader takes care of receiving the data from the host and upgrades the device.
The following code can be used by the application to request the bootloader execution:
void invoke_bootloader(void)
{
uint32_t *sram = (uint32_t *)BTL_TRIGGER_RAM_START;
sram[0] = 0x5048434D;
sram[1] = 0x5048434D;
sram[2] = 0x5048434D;
sram[3] = 0x5048434D;
NVIC_SystemReset();
}