We already learned what a bootloader is and why it is needed in this article.
Before we start coding, here is some theory. The microcontroller address space has to be divided into a few blocks. The first one will store the bootloader code and its reset vector. The second one will store the application code and its reset vector. The rest is free space, that can be allocated by the application in a future firmware update.
We will create two projects – one for the bootloader itself and one for the application code. The bootloader code will:
- execute after power-on-reset
- check if a button is pressed
- if so, it will initialize the UART
- erase the application code flash space
- wait for data to arrive over UART and write it to the application code flash space
- finally, it will jump to the application code
The application code will be very simple – it will just blink a LED. The board has 4 LEDs on it – red, green, yellow and blue. We will create two different projects – one that blinks the red LED and another that blinks the blue LED. We will use these two projects to test our bootloader code in a later stage.
For this particular microcontroller, the flash is divided into 8 sectors, that are shown in table 4 of the reference manual. We will place the bootloader code in Sector 0 and the application code in Sector 1. For
In the bootloader target settings we set the flash space of Sector 0 – from 0x08000000 to 0x08003FFF.
In the application target settings we set the flash space of Sector 1 – from 0x08004000 to 0x08007FFF.
In the next part we will write the application code