Page 1 of 1

STM32 Programming. Part 14: Flashing the STM32 via ST-Link

Posted: 17 Oct 2023, 06:14
by Oleg
image.png
image.png (27.56 KiB)
Viewed 3541 times
Let's say, digging the Internet, we found a very interesting electronic device on the STM32 microcontroller, and all the schematics and firmware is attached. But here's the trouble, we have never downloaded firmware into STM32, we have only worked with AVRs before. No problem! In this article we will understand this issue, because loading ready firmware into STM32 is no more difficult than in the good old AVR-ki, and maybe even easier!

Programmer

In the previous article we got acquainted with several variants of ST-Link programmer. In this example I will use the stm32f4discovery debug board as a programmer, simply because I have it. In order to use this debug board as a programmer, you need to do 2 things:

Remove the jumpers that connect the programming lines of the embedded ST-Link to the microcontroller soldered on the board
Connect the signal lines of the programmer to the external microcontroller with the help of a comb, where the SWD bus is taken out.
The picture below shows the pinout of the SWD connector:
image.png
image.png (443.49 KiB)
Viewed 3541 times
Chinese debug boards with the stm32f103c8 microcontroller have a corresponding connector through which you can load the firmware into the MC. These boards look like this:
image.png
image.png (208.41 KiB)
Viewed 3541 times
The pins on the programming connector are labeled GND, CLK, DIO, 3V3. Connection to the programmer is made in this way:
  • GND - GND
  • CLK - SWCLK
  • DIO - SWDIO
My layouts look like this:
image.png
image.png (555.49 KiB)
Viewed 3541 times
In this case, the debug board with stm32f103c8 is soldered to the "grid" where it is connected to the other components on the back side with an insulated wire. The same wire is used to connect the programmer to the debug board:
image.png
image.png (296.48 KiB)
Viewed 3541 times
Software

Now let's move on to the software component. We will need a driver for the programmer and control software, through which we will load the firmware into the microcontroller. To do this we need to download the STM32 ST-LINK utility package on the official site www.st.com. And what should be done before downloading something from www.st.com? That's right! You need to register with them!

So, we got the archive with STM32 ST-LINK Utility package. After installation we connect the programmer to PC via USB and connect power to the debug board. If the drivers for the programmer are not installed automatically, then go to Device Manager, find our STM32 STLink, select Update Driver->Run search on this computer. After that everything should work. Then, from the "Start" menu run the program "STM32 ST-LINK Utility":
image.png
image.png (16.36 KiB)
Viewed 3541 times
The ST-LINK Utility interface looks like this:
image.png
image.png (55.78 KiB)
Viewed 3541 times
Next, we need to make some minor adjustments. In the menu select Target->Settings...
image.png
image.png (99.6 KiB)
Viewed 3541 times
It opens a window like this:
image.png
image.png (68.04 KiB)
Viewed 3541 times
Set the settings as shown on the screenshot and click OK. After that the programmer will automatically connect to the microcontroller being flashed. Let's run through the control buttons:

"Connect to microcontroller" - connects the programmer to the MC. This stops the firmware operation. After that you can perform further manipulations with the flash memory of the MC.

"Disconnect from microcontroller" - after we have done everything, press this button and SL-Link disconnects from the MC, at the same time the firmware loaded into flash-memory starts.

"Clear chip" - pressing this button erases all flash memory of the microcontroller. This must be done before loading another firmware into the MC.

In order to flash our .hex or .bin file into the MC you should do the following. Select Target->Programm... in the menu.
image.png
image.png (167.9 KiB)
Viewed 3541 times
After that we get a window for selecting the firmware file. Select the file we need, and then this window appears:
image.png
image.png (36.08 KiB)
Viewed 3541 times
Here we need to press Start to start the process. After the firmware has been downloaded to the MC, click on the "Disconnect from microcontroller" button.

Those who have worked with AVR microcontrollers before know about such a thing as fuse bits. If you set them incorrectly in AVRs, the firmware may not work correctly. Good news for you: in STM32 there are no fuse bits! It is enough just to pour a file with the control program into the MC and everything will work.

Well, that's probably the end of it, thank you all for your attention.