Write a custom bootloader with the STM32L1 series in C

1.7k views Asked by At

I'm writting a Bootloader for an embedded card STM32L152RE in C. this bootloader is called when a message is sent on the USART. it call an interruption and when the message is received, the bootloader is executed. Then, i have to flash the memory of my card with the new HEXA code sent on the USART by the computer and reset.

At this point, my bootloader is able to respond to the interruption and read the frames sent by the computer.

I just don't know how to flash my memory. I've got functions in my lib like: FLASH_Unlock(void), FLASH_ErasePage(uint32_t Page_Address), and FLASH_FastProgramWord(uint32_t Address, uint32_t Data)

I have to erase the flash but not the part where my bootloader is. That's why i created a new section in my linker descriptor like this

  .bootsection :
  {
    . = ALIGN(4);
    KEEP(*(.bootsection)) /* Bootloader code */
    . = ALIGN(4);
  } >FLASH

and my memory is arranged like this : MEMORY

{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 512K
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 80K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

My question is, how do i protect my bootloader from erasing itself when i erase the flash with the new program and how do i flash properly my flash with my HEXA code sent from the USART.

1

There are 1 answers

5
unwind On

First, you simply must make sure that your bootloader is in the proper place. This ought to be linked to the hardware's requirements, since it needs to know where it is in order to run it when the data arrives.

Second, your bootloader code is of course free to inspect the incoming data; it must contain target addresses. So, it can analyze the incoming data and make sure it doesn't collide with where the bootloader resides.

This:

FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 512K

looks like the entire flash memory, you want to decrease the LENGTH to something suitable for the bootloader, probably 16 or 32 KB.