0
votes

Considering a microcontroller(in my case STM32L4 series) once the new firmware image is written into the flash, how the new start address of the application can be intimated to the bootloader?

1
Look at any of the bootloader examples provided - leppie
Can u please help share any site which has that sort of an example. My search for the same ended fruitless. - Ginu Jacob
Probably worth clarifying in your question why you dont use a fixed address (sector) for the application which could be hardwired into the bootloader. - Ricibob

1 Answers

2
votes

Normally the bootloader should know the sector where the application has been written. If this is not case then there are two options:
1. When you write the firmware image to a particular sector - record in flash in a known sector the firmware sector i.e in a known sector write a struct bootloaderInfo that records the sector address of the firmware and any other info that is useful for the bootloader e.g a CRC for the firmware image, a firmware version etc. The bootloader can read this struct (its always in a known sector) and from that know where the sector where the firmware is to run.
2. The bootloader could scan flash sectors looking for the firmware image. This is not really recommended but would work. A sector with a valid firmware image will have a. a valid stack pointer and b. a valid reset vector. They could be tested with the following macros:

#define STACK_POINTER_RANGE  0x2FFE0000  
#define IS_VALID_STACK_POINTER(address) (((*(volatile uint32_t*)address) & STACK_POINTER_RANGE) == SRAM_BASE)

#define IS_VALID_RESET_VECTOR(sectorAddress) \
    (((*(uint32_t*)(sectorAddress+4)) & 0x8FF0000 ) == sectorAddress)

Once the application address in known the bootloader can jump to it in the normal way:

uint32_t JumpAddress = *(__IO uint32_t*)(sector + 4);
pFunction JumpToApplication = (pFunction)JumpAddress;
// Initialize user application's Stack Pointer:
__set_MSP(*(__IO uint32_t*)sector);
JumpToApplication();