We already learned what a bootloader is and why it is needed in this article.

Today we will start developing our own bootloader. For this particular example, we will be using STM32F411 discovery board and FT2232H mini module. The project is based on ARM Keil MDK.

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.

Microcontroller address space

We will create two projects – one for the bootloader itself and one for the application code. The bootloader code will:

  1. execute after power-on-reset
  2. check if a button is pressed
    1. if so, it will initialize the UART
    2. erase the application code flash space
    3. wait for data to arrive over UART and write it to the application code flash space
  3. 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 more practical approach, sectors 1 to 7 can be allocated for the application code.

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