There have been several requests among users to explain more about loading programs in to flash memory of STM32 microcontrollers.
This, of course, is a broad topic but let us focus on how to achieve the result – flash program into MCU. Depending on what arsenal is on your desk you can do in several different ways. Let’s try to go through them practically.
All STM32 microcontrollers come with built-in bootloaders that are programmed during production. Depending on device type flash memory can be programmed using one of the interfaces like USART1 or USART2, USB, CAN. Mostly we are dealing with low, medium and high-density devices so they can access bootloader using USART1 interface. USART1 can be connected to the computer using RS232 interface or USART to USB driver chip like FT232. To enter bootloader special MCU pins has to be set to proper logical values. These are called BOOT0 and BOOT1 pins on a microcontroller. Boot pins can have several cases as you can see:
There are three cases: the first case is when BOOT0 pin is tied to ground and BOOT1 is open or at logical state 0 then after reset program is executed from Main Flash Memory. This is your normal configuration when running applications. The second case (BOOT1=0; BOOT0=1) means that after reset execution starts at System memory where built, int bootloader resides. This is the case when we need to upload binaries via USART1. And the third case means that program execution is performed in SRAM. So let’s focus on the second case – System memory. In many development boards, you will find jumper pins where you can select BOOT0 and BOOT1 configurations. For instance, in STM32F103RBT6 board these looks like this:
In the image, you can see that the jumper setting already chooses to run internal bootloader. What we do next is download Flash Loader Demonstrator which is a tool to interact with the bootloader. Install it and launch. You will see the screen where you will be able to select USART parameters. Be sure to select correct COM port:
Connect board USART1 to PC using RS232 cable and hit the reset button. Then you can click Next to go to the next screen.
It confirms that you successfully entered bootloader. Hit Next to proceed to the device selection screen.
The target device is already selected automatically. If not – select the right one from drop-down list. You can also see here other information like the bootloader protocol version (2.2), Flash memory mapping. Click Next if the selected device seems OK. You get to the screen where you can do all sort of things:
You can erase all or selected memory pages. Download a .hex (.bin or .x19) to microcontroller flash memory. Select ‘Jump to the user program‘ if you want to execute the program immediately after download. You can also select to Verify after download to make sure if the upload was successful. You can also upload an existing binary from device Flash memory into the file to back up it. You can enable or disable Flash protection. And finally, proceed to ‘Edit option bytes‘ to set User Option bytes or add write protection to selected memory pages.
Let’s leave that behind and simply download the .hex file into the device. Just select the file from dialog menu select to verify after downloading a jump to user program like this:
Hit Next, and in a few seconds, it is done.
Here we can see that binaries are successfully downloaded and takes 2804 bytes and took 1 second to program.
When programming is done, you can return BOOT0 pin to ground to execute programs from main Flash memory after next reset.