StM32F4xx-Discovery Bootloader

Keywords: Embedded systems, ARM, STM32F4, Bootloader

Bootloader…..!!!! Bootloader is a tiny piece of embedded code, active a boot time, that loads (Loader) program into RAM for execution (if required) AND/OR has the capability to Update firmware by Programming/ Reprogramming internal flash.

Figure-1: Bootloader functionalities

Imagine a device in field (say IoT) with a possible bug in its firmware but there is no direct physical access to the device after its deployment. In such scenario an embedded bootloader enables adds functionality to update firmware remotely. e.g. cell phone firmware update.

STM32F4xx devices come preprogrammed with embedded bootloader that provides the facility of downloading program to internal flash via:USARTCANUSB OTG

NOTE:

1): The embedded bootloader of STM32F4xx devices only provides the facility of flash programming. It doesn’t have any “Debug Stub” as part of bootloader to provide debug facility.

2): Updating/Programming internal flash via USART, the peripheral clock is provided from internal RC oscillator of 16MHz, while in case of USB OTG and CAN an external HSE clock is required that should be multiple of 1Mhz (4Mhz-26Mhz).

So where is this bootloader code and how it can be accessed? Lets look at the memory map of STM32F4xx microcontrollers:

As can be seen from Memory Map, the memory space between 0x1FFF,0000 and 0x1FFF,740F is assigned to System memory + OTP. Out of this space 30Kbytes are assigned to System memory while the remaining 528-bytes are reserved for OTP memory. This 30Kbytes of System memory is the space where STM32 bootloader resides. It comes pre-configured, all you have to do is to boot from this region and it will take care of the rest by listening to data traffic on above mentioned ports and programming internal flash memory on code block.

Lets see how STM43F4-Discovery board can be configured to boot from System Memory and subsequently from preprogrammed bootloader.

Boot Configuration:

By default ARM Cortex-M processors upon reset starts executing code from Code Space in memory map i.e. 0x0000,0000 – 0x1FFF,FFFF. This is fixed but STM32F4xx implementation provides the flexibility of booting from other memory regions by aliasing/mapping different memory spaces at Code Space/region. The following table shows how to boot STM32Fxx devices from different memory spaces [1].

Boot mode selection pins Boot Mode Aliased/Mapped Memory
BOOT1 BOOT0
x 0 Main Flash memory Main Flash memory is selected as the boot space
0 1 System memory System memory is selected as the boot space
1 1 Embedded SRAM Embedded SRAM is selected as the boot space

So in order to boot STM32Fxx devices from System memory, BOOT1 = must be at Logic-0 and BOOT0 = must be at logic-1 at the time of RESET. Lets have some fun with STM32F4xx-Discovery board and program it via its embedded bootloader.

Hardware Requirements:

  1. USB-to-Serial Cable
  2. STM32F4xx device (STM32F407-Discovery in my case)
  3. Flash Loader Demonstrator

Software Pre-Requisites:

1): Make sure that appropriate driver for USB-to-Serial Cable is installed on host PC. This can be verified via Device Manager. Remember there shouldn’t be any yellow warning sign on Driver as shown below figure.

Figure-2: USB-to-Serial Driver

2): Make sure Flash Loader Demonstrator as mentioned in above Hardware Requirements, is installed on host PC.

3): A test program in .hex/.bin format. For test purpose simple Blinky .hex for STM32F407 can be downloaded from here.

Steps:

Lets do some real work. Following are the steps to program STM32F4xx devices through their embedded bootloader via one of USART.

1) – Connect on-board Boot0 pin to on-board Vdd. Similarly connect Boot1 pin to on-board GND. On STM32F4xx boards Boot1 is multiplexed with PB2 pin. If you don’t see any Boot1, connect PB2 to GND. On STM32F4-Discovery both Vdd and GND pins are next to Boot0, and Boot1 respectively and can be connected via jumpers.

2) – Connect USB to Serial TxRx and GND pins to one of the following STM32F4xx USART pins. On STM32F4-Discovery, U(S)ART-1 is hardware configured for USB OTG using some capacitor so it may not work as expected. Following are the U(S)ART pins on STM32F4-Discovery board.USART1 (PA9/PA10)USART3 (PB10/11 and PC10/11)

Possible Connection (Excluding USART1) between Serial Cable and USART3 is as follow.TXD –> PB10, RXD –> PB11TXD –> PC10, RXD –> PC11

3) – Connect USB-to-Serial Cable TXD pin to Rx of STM32F4xx USART, and USB-to-Serial RXD pin to Tx of STM32F4xx USART. Also connect GND pin of the board to USB-to-Serial cable ground wire.

4) – Connect USB-to-Serial cable to host PC and Power up Discovery board.

5) – Open Flash Loader Software and select USB-to-Serial port (shown in Device Manager Figure-1). In my case its COM9. Leave rest of the settings as it is. Click Next. If you see any error stating “No response from the target…..”, Press the on-board Reset button (black) and click next.

Figure-3: USB Flash Downloader

If everything is configured as described above, you should see the following message. Click Next.

Figure-4: Bootloader Configured, Target Identified

6) – Select the target board/Microcontroller which will most probably be set automatically. As shown in Figure-4, the flash loader is connected to STM43F4 board having 1024k OR 1Mbytes of flash memory. Click Next.

Figure-5: Target device selection

7) – In this step you have to Select what you want to do with internal Flash. Either you can Erase it or Program it. As we are programming device so we will select “Download to device”. Select program file (.hex/.bin) and check Verify after download. Click Next.

Figure-6: Selecting Program binaries

8) – Wait for the Flash Loader software to program and verify STM32F4xx internal flash and give success message as shown in bellow figure.

Figure-7: Downloading Code

9) – Remove Jumbers (Boot0,1) and Serial Cable, Press Reset button on board. You will see the LED blinking.

Figure-8: Discovery board with blinky LED

[1] – Reference manual-RM0090



54 thoughts on “StM32F4xx-Discovery Bootloader”

Leave a Reply

Your email address will not be published. Required fields are marked *