Working with microcontrollers STM32 requires reliable tools for flashing, debugging and configuration. Official software STM32CubeProgrammer from STMicroelectronics has become the de facto standard for engineers, thanks to the support of all families STM32, flexible interfaces (SWD, JTAG, UART, USB DFU) and integration with STM32CubeIDE. However, even experienced developers are faced with nuances: from connection errors to memory protection problems.
This article is not just a retelling of the documentation. Here you will find practical diagrams for connecting debugging adapters (ST-Link, J-Link), analysis of typical errors (for example, Error: No STM32 target found!), as well as optimized scenarios for mass production - from code protection to firmware acceleration via Quad-SPI. Particular attention is paid to working with STM32H7 And STM32U5where standard methods often fail.
What is STM32CubeProgrammer and why is it needed?
STM32CubeProgrammer is a universal tool for programming and debugging microcontrollers STM32, replacing outdated utilities like Flash Loader Demonstrator or STVP. Its key advantages:
- 🔹Support all STM32 families - from STM32F0 to STM32H7 And STM32U5, including new kernels
Cortex-M33/M7. - 🔹Multi-protocol work:
SWD(up to 50 MHz),JTAG,UART,USB DFU,I2C,SPI,CAN. - 🔹 Built-in functions for code protection: installation
RDP(Read Protection), configurationOPTBYTES. - 🔹 Integration with STM32CubeIDE And Keil/IAR via plugins.
- 🔹Support scripts (CLI) for automating firmware in mass production.
The main difference from alternatives (for example, OpenOCD or J-Flash) - official support STMicroelectronics, which is critical for industrial projects. For example, only CubeProgrammer works correctly with protected memory areas in STM32H7, where OpenOCD may fail to read OPTBYTES.
⚠️ Attention: Versions STM32CubeProgrammer below 2.12 do not support core microcontrollersCortex-M33(For example, STM32U5). Before working with new chips, be sure to update the software viaSTM32CubeMXor official website.
Installation and initial setup
Official installer STM32CubeProgrammer available on the website STMicroelectronics in section STM32CubeProg. The installation process is standard, but there are key points:
- Version selection: Available for Windows
32-bitAnd64-bitassemblies. For Linux/MacOS - only64-bit(requiresJavafor GUI). - Drivers: When connecting for the first time ST-Link or J-Link the system may request drivers. Use the package
STSW-LINK009for ST-Link. - Access rights: On Linux, add a user to a group
dialout:sudo usermod -a -G dialout $USER
After installation, check functionality via the command line:
STM32_Programmer_CLI --version
The response must contain the current version (for example, v2.14.0).
- ST-Link
- J-Link
- DAPLink
- Other (specify in comments)
Connection to microcontroller: SWD vs JTAG vs UART
The choice of interface depends on the task. SWD (Serial Wire Debug) - the most popular option due to its simplicity (only 2 signals: CLK And DIO). However, to debug multi-threaded applications on STM32H7 may be required JTAG (5 signals) as it supports simultaneous debugging of multiple cores.
| Interface | Speed | Number of signals | When to use |
|---|---|---|---|
SWD |
Up to 50 MHz | 2 (CLK, DIO) |
Firmware, basic debugging |
JTAG |
Up to 15 MHz | 5 (TDI, TDO, TMS, TCK, GND) |
Multi-threaded debugging, tracing |
UART |
Up to 115200 baud | 2 (TX, RX) |
Bootloader update, logging |
USB DFU |
Full-Speed (12 Mbit/s) | 2 (USB D+, USB D-) |
Firmware without a debugger (for example, for mass production) |
To connect via SWD use the following diagram (example for STM32F407):
- 🔌
SWDIO→PA13(orPB3on some boards). - 🔌
SWCLK→PA14(orPB4). - 🔌
GND→ generalGNDfees. - 🔌
3.3V→VCC(optional if the target board is not powered).
⚠️ Attention: When connecting to boards with STM32H7 throughSWDThe default speed (4 MHz) may cause errors. Set frequency1 MHzin settings CubeProgrammer (Settings → SWD Frequency).
Is ST-Link/J-Link connected to USB?|Is power supplied to the target board?|Are SWDIO/SWCLK connected correctly?|Are drivers for the adapter installed?-->
Microcontroller firmware: step-by-step instructions
Let's look at the firmware process using an example STM32F407 through SWD using file .hex:
- Launching CubeProgrammer: Open the GUI version or use the CLI (for example, for automation).
- Connection: Click
Connect→ select portSWDand frequency (recommended4 MHzfor STM32F4). - Read ID: After connecting, check
Device ID(must match the datasheet, for example,0x413for STM32F407). - Uploading a file: In the section
Fileselect.hexor.binfile. For.binindicate the starting address (for example,0x08000000). - Firmware: Click
Download→ wait for completion (progress is displayed in the log). - Check: After flashing, run
Verifyto compare data in flash and the original file.
For mass production it is more convenient to use CLI mode. Example command for firmware firmware.hex with automatic erasure and verification:
STM32_Programmer_CLI -c port=SWD -d firmware.hex -v -rst
Flag -rst performs a reset after flashing.
If the firmware is interrupted with an error Error: Flash memory not erased, add a flag -e all to force erase of all flash memory before recording.
Working with memory protection and options (OPTBYTES)
Microcontrollers STM32 support several levels of code protection:
- 🔐
RDP (Read Protection): Disables reading flash memory through the debugger. Installed viaOption Bytes. - 🔐
Write Protection (WRP): Blocks writing to certain flash sectors. - 🔐
Secure Area: Secure areas for cryptographic keys (available in STM32H7/U5).
To install RDP in CubeProgrammer:
- Connect to MK via
SWD. - Go to section
Option Bytes. - In the field
Read ProtectionselectLevel 1(read prohibition) orLevel 2(full blocking, including JTAG). - Click
Apply→ confirm the action.
⚠️ Attention: InstallationRDP Level 2irreversibly blocks access to the microcontroller via a debugger. The only way to unlock it is to completely erase the flash viaUSB DFU(if supported) or chip replacement.
How to bypass RDP Level 1 without erasing flash?
In some cases (for example, for STM32F1) you can exploit a vulnerability in the bootloader (USART bootloader) by sending a special command 0x44 to temporarily remove protection. However, this method does not work on new families (STM32H7, STM32U5) and may cause data corruption.
Common mistakes and their solutions
Even with correct connection STM32CubeProgrammer may give errors. Let's look at the most common ones:
| Error | Reason | Solution |
|---|---|---|
No STM32 target found! |
Incorrect connection SWD or lack of food. |
Check SWDIO/SWCLK and submission 3.3V. Try reducing the frequency to 1 MHz. |
Error: Flash memory not erased |
Flash sectors are blocked WRP or damaged. |
Execute Erase → Full chip erase or check Option Bytes. |
Error: Option Bytes write failed |
Trying to record OPTBYTES with active protection RDP. |
Remove RDP (if possible) or use USB DFU to unlock. |
Error: Cannot connect to the target |
Driver conflict or busy port (for example, OpenOCD). | Close all programs that use the debugger. Reinstall drivers ST-Link. |
To diagnose the connection, use the command:
STM32_Programmer_CLI -c port=SWD -r
It will display detailed information about the target device, including Device ID and condition Option Bytes.
If STM32CubeProgrammer does not see the microcontroller, but power is supplied, check the circuit NRST. On some boards (for example, Blue Pill) requires a lift NRST to 3.3V through a resistor 10kΩ.
Automation of firmware for production
In mass production, manual firmware is unacceptable. STM32CubeProgrammer supports automation through:
- 📜 CLI scripts: Sequence of commands in
.bator.shfile. - 📜 STM32CubeProgrammer API: Integration with Python or C# via CLI call.
- 📜 Plugins for STM32CubeIDE: Automatic firmware after building the project.
Example script for flashing firmware for 10 devices in a row with logging:
@echo offfor /L %%i in (1,1,10) do (
echo Прошивка устройства %%i
STM32_Programmer_CLI -c port=SWD -d firmware.hex -v -logs log_%%i.txt
if %ERRORLEVEL% neq 0 (
echo Ошибка на устройстве %%i
pause
)
)
Logs will be saved in files log_1.txt, log_2.txt etc.
To speed up firmware in production:
- 🚀 Use
Quad-SPIinstead ofSWD(available at STM32H7/U5). - 🚀 Disable verification (
-v off) if you are confident in stability. - 🚀 Flash only changed sectors (flag
-s).
FAQ: Frequently asked questions about STM32CubeProgrammer
Is it possible to flash STM32 via UART without ST-Link?
Yes, if enabled in the microcontroller bootloader via USART (bootloader). To do this:
- Connect
TX/RXto appropriate conclusions (for example,PA9/PA10for STM32F4). - Hold the button
BOOT0ableHIGHwhen power is applied. - Send the file via STM32CubeProgrammer in mode
UART(default speed is115200baud).
Limitation: This method does not work if the flash is protected RDP Level 2.
How to flash STM32H7 via Quad-SPI?
STM32H7 supports firmware via Quad-SPI (up to 4 data lines), which speeds up the process by 2-3 times. To do this:
- Connect
QSPIto the adapter (for example, ST-Link with supportQSPI). - B CubeProgrammer select port
QSPIand specify the configuration (for example,4-4-4for four data lines). - Upload the file in the format
.binwith alignment along the sector boundary (usually256 bytes).
Transfer speed can reach 100 MHz, but requires stable power supply (recommended 3.3V with current not less 500 mA).
Why doesn't CubeProgrammer see J-Link?
The problem is usually related to:
- 🔌 Lack of drivers J-Link (install J-Link Software Pack).
- 🔌 Conflicts with other programs (for example, OZone or Keil). Close all IDEs before connecting.
- 🔌 Incorrect configuration in CubeProgrammer: select
J-Linkin the list of adapters (Settings → Debug Probe).
To diagnose, run J-Link Commander and check if the adapter is detected.
How to reset RDP Level 2 on STM32?
If installed RDP Level 2, standard methods (via SWD/JTAG) don't work. Possible solutions:
- 🔄 Use
USB DFU(if supported by the chip). To do this: - Connect
USBto the portDFU(For example,PA11/PA12for STM32F4). - Hold
BOOT0inHIGHwhen power is applied. - Run CubeProgrammer in mode
USBand executeFull erase.
DFU (For example, STM32F0) will be required flashing via JTAG with an external voltage source ("hot-plug" method).⚠️ Attention: Reset RDP Level 2 erases all flash memory, including Option Bytes.
Which version of CubeProgrammer should I choose for STM32U5?
For STM32U5 (core Cortex-M33) version required not lower than 2.12. It is recommended to use the latest stable build (at the time of writing - 2.14.0), since it contains:
- 🔹Fixed reading errors
OPTBYTESforTrustZone. - 🔹 Added support
Secure BootAndSecure Firmware Update (SFU). - 🔹 Optimized work with
Quad-SPI(speed up to133 MHz).
You can download the current version at ST official website.