STM32 Flash Loader how-to

How to use STM32 Flash Loader

You can get the official flashing utility from ST website here.

After installing, you can find two interesting executable files (depending on where you installed it, typically in the "STMicroelectronics\Software\Flash Loader Demo" subdirectory): STMFlashLoader.exe and STMFlashLoader Demo.exe. The first one is a commad-line utility, second has a GUI interface. Command-line is good for integrating into your IDE / toolchain, for example Eclipse.

A few useful CLI commands

There are parameters that you need to adjust to match your environment. For example "--pn" is the number under which the usb-to-serial virtual COM port appears in your computer. You can find this out for example in Control Panel -> Device Manager -> Ports (COM & LPT), in my case the port is COM43:

Global Flash Erase

STMFlashLoader.exe -c --pn 43 --br 115200 --db 8 --pr EVEN --sb 1 --ec OFF --to 10000 --co OFF
-i STM32F2_1024K -e --all

Binary File Upload

STMFlashLoader.exe -c --pn 43 --br 115200 --db 8 --pr EVEN --sb 1 --ec OFF --to 10000 --co OFF
-i STM32F2_1024K -d --fn application.bin --a 08000000 --v --o

The "--a" parameter is base address (in hex) where to flash the "application.bin" (an arbitrary filename your code was compiled into). This is typically 0x08000000 to get the file into the beginning of the code space, so it starts executing on reset, however if you are uploading other data (e.g. sound samples) you may want to change this address to point somewhere higher in the address space. In order to convert ELF file to BIN use the objcopy.exe utility, which should be present in your toolchain directory - with TrueStudio it is usually in "ARMTools\arm-atollic-eabi\bin\". Usage is:

objcopy.exe -O binary application.elf application.bin

Re-Flash the Binary

These operations can be combined, for example the next command will first erase memory and then flash the binary. Not entire memory is erased (that would be "-e --all" option as in global erase example), it is sufficient to erase only the sectors your binary code spans across (in our case the first, or "0-th" sector as they are numbered from zero).

The option "--sec x y1 y2..." allows you to enumerate sectors you want to erase, x = amount of sectors to erase, y1, y2, ... = sectors numbers.

STMFlashLoader.exe -c --pn 43 --br 115200 --db 8 --pr EVEN --sb 1 --ec OFF --to 10000 --co OFF
-i STM32F2_1024K -e --sec 1 0 -d --fn application.bin --a 08000000 --v --o

Please see the 3rd screenshot from GUI utility below which shows FLASH memory map, sector numbers and addresses. For example, if your binary is larger than 32kB but smaller than 48kB, you need to erase first 3 sectors.

GUI Version

For uploading a binary file manually, the GUI version is good. It should auto-detect the COM port.

After clicking "next", you should see this:

Click "next" again. Now the utility displays the FLASH memory map for your MCU. The 1MByte space is usually divided into 11 sectors of various sizes (4x16kB, 1x64kB and 7x128kB). This is important as one sector is the minimal space which can, and must, be erased at once (while programming can be done gradually, it is only possible to flip bits from 1 to 0, erased space reads as 0xFF).

Click "next" again, now you can choose the desired operation and/or a binary file to flash. With blank MCUs it is not necessary to erase anything at all. When re-flashing the binary, it is sufficient to only erase necessary pages - the utility will determine which sectors need to be erased automatically, based on binary file size.

Click "next" again, the indicator informs you about the progress.

If everything went well, you should see this result, otherwise retry some of the previous steps - together or separately. Due to FLASH memory being slow, you may sometimes get warnings/errors related to erasing the memory, even if everything went well. In such case, next time try to flash the binary with "No Erase" option selected.