MCU program implementation [Program transfer using ST-LINK]
![](https://en.depfields.com/wp-content/uploads/2020/06/stlink1.jpg)
Table of contents
Program transfer tool
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
When you create a new program and build it, you will usually encounter syntax errors, compiler errors, and link errors. These errors are usually caused by the programming language and its development environment.
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
This is a simple process, but only after all errors are resolved is the build successful and a binary file is created to be written to the microcontroller. Once the binary file is generated, a dedicated tool is used to transfer the file from the development environment of the PC to the microcontroller for writing.
![](https://en.depfields.com/wp-content/uploads/2020/06/debugger_en.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
The transfer/writing tool is ST-LINK, which uses the ARM MCU standard SWD communication. As explained in "Recommended development environment ", ST-LINK/V2 version 2 or higher is used as the transfer tool in the STM32CubeIDE development environment.
![](https://en.depfields.com/wp-content/uploads/2020/06/stlink1.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/06/stlink2.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
SWD can communicate by connecting two signal lines and GND. It can also function by directly connecting the pins PA13/PA14 for SWD on the microcontroller to ST-LINK debugging. This is fine for hobby use, but for products, insert a protection resistor or stabilize the signal with a pull-up resistor.
![](https://en.depfields.com/wp-content/uploads/2020/06/swd-with-interface_en.jpg)
Program Transfer Method
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
Connect the microcontroller and the ST-LINK/V2 transfer/writing tool, and connect the USB cable to the PC to complete the preparations.
There are two ways to transfer and write.
① Transfer from STM32CubeIDE menu
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
The first is the way to transfer the program from the development environment STM32CubeIDE menu, and clicking "Run"-"Run" will start the program transfer. The LED on the ST-LINK device blinks during the transfer. This method is convenient because it allows you to execute the program up to writing in the same environment as the build.
② Transfer from the application ST-LINK Utility
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
The second method is the way to use ST-LINK Utility, an application provided by STMicroelectronics to perform transfer writing. In this case, it is necessary to specify a binary file with the extension bin or a hex file, so it must be set to be generated by STM32CubeIDE in advance.
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
The generated file is located in the "Debug" folder under the project folder, so specify the target file. Using the ST-LINK Utility, you can not only transfer and write programs, but also specify program protections.
![](https://en.depfields.com/wp-content/uploads/2020/06/binary-file.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/06/debug-folder.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
Start ST-LINK Utirity and click "Target" - "Program & Verify" in the menu.
![](https://en.depfields.com/wp-content/uploads/2020/06/STLinkUtility_1.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
After specifying the location of the binary file to be transferred (1), press "Start" (2) to start program transfer.
![](https://en.depfields.com/wp-content/uploads/2020/06/STLinkUtility_2.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/06/STLinkUtility_3.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
When the transfer is complete, the program can be seen being written from address 0x08000000.
![](https://en.depfields.com/wp-content/uploads/2020/06/STLinkUtility_4.jpg)
Supplemental:
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
When transferring using the ST-LINK device for the first time with the STM32CubeIDE, an error may appear as shown in the figure below.
![](https://en.depfields.com/wp-content/uploads/2020/06/stlink_1.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
This is because the ST-LINK device firmware is out of date for the STM32CubeIDE and needs to be updated.
![](https://en.depfields.com/wp-content/uploads/2020/06/stlink_2_en.jpg)
![](https://en.depfields.com/wp-content/uploads/2020/05/instructor.jpg)
Please follow the instructions to update the software.
![](https://en.depfields.com/wp-content/uploads/2020/06/stlink_3.jpg)
Please use either (1) STM32CubeIDE or (2) ST-LINK Utility to perform write transfer in a convenient way.