There has been several requests among users to explain more about loading programs in to flash memory of STM32 microcontrollers.
This of course is a wide topic but lets focus on how to achieve result – flash program in to MCU. Depending on what arsenal is on your desk you can do in several different ways. Let’s try to go through them in practical way.
All STM32 microcontrollers comes with built-in bootloaders that are programmed during production. Depending on device type flash memory can be programmed using one of 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 computer using RS232 interface or USART to USB driver chip like FT232. In order 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: 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 executing programs. 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 third case means that program execution is performed in SRAM. So lets focus on 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 image you can see that jumper settings already selects to run internal bootloader. What we do next is download Flash Loader Demonstrator which is a tool to interact with bootloader. Install it and launch. You will see 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 reset button. Then you can click Next to go to the next screen.
It confirms that you succesfully entered bootloader. Hit Next to proceed to device selection screen.
Target device is already selected automatically. If not – select right one from drop down list. You can also see here other information like bootloader protocol version (2.2), Flash memory mapping. Click Next if selected device seems OK. You get to screen where you can do all sort of things:
You can erase all or selected memory pages. Download a .hex (.bin or .x19) to microcoller flash memory. Select ‘Jump to the user program‘ if you want to execute program immediately after download. You can also select to Verify after download to make sure if upload was successful. You can also upload existing binary from device Flash memory in to 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 .hex file in to device. Simply select file from dialog menu select to verify after download an 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 in order to execute programs from main Flash memory after next reset.