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), configuration OPTBYTES.
  • 🔹 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 microcontrollers Cortex-M33 (For example, STM32U5). Before working with new chips, be sure to update the software via STM32CubeMX or 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:

  1. Version selection: Available for Windows 32-bit And 64-bit assemblies. For Linux/MacOS - only 64-bit (requires Java for GUI).
  2. Drivers: When connecting for the first time ST-Link or J-Link the system may request drivers. Use the package STSW-LINK009 for ST-Link.
  3. 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).

📊 Which adapter do you use to flash STM32 firmware?
  • 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):

  • 🔌 SWDIOPA13 (or PB3 on some boards).
  • 🔌 SWCLKPA14 (or PB4).
  • 🔌 GND → general GND fees.
  • 🔌 3.3VVCC (optional if the target board is not powered).
⚠️ Attention: When connecting to boards with STM32H7 through SWD The default speed (4 MHz) may cause errors. Set frequency 1 MHz in 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:

  1. Launching CubeProgrammer: Open the GUI version or use the CLI (for example, for automation).
  2. Connection: Click Connect → select port SWD and frequency (recommended 4 MHz for STM32F4).
  3. Read ID: After connecting, check Device ID (must match the datasheet, for example, 0x413 for STM32F407).
  4. Uploading a file: In the section File select .hex or .bin file. For .bin indicate the starting address (for example, 0x08000000).
  5. Firmware: Click Download → wait for completion (progress is displayed in the log).
  6. Check: After flashing, run Verify to 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 via Option 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:

  1. Connect to MK via SWD.
  2. Go to section Option Bytes.
  3. In the field Read Protection select Level 1 (read prohibition) or Level 2 (full blocking, including JTAG).
  4. Click Apply → confirm the action.
⚠️ Attention: Installation RDP Level 2 irreversibly blocks access to the microcontroller via a debugger. The only way to unlock it is to completely erase the flash via USB 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 EraseFull 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 .bat or .sh file.
  • 📜 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 off

for /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-SPI instead of SWD (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:

  1. Connect TX/RX to appropriate conclusions (for example, PA9/PA10 for STM32F4).
  2. Hold the button BOOT0 able HIGH when power is applied.
  3. Send the file via STM32CubeProgrammer in mode UART (default speed is 115200 baud).

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:

  1. Connect QSPI to the adapter (for example, ST-Link with support QSPI).
  2. B CubeProgrammer select port QSPI and specify the configuration (for example, 4-4-4 for four data lines).
  3. Upload the file in the format .bin with alignment along the sector boundary (usually 256 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-Link in 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:
    1. Connect USB to the port DFU (For example, PA11/PA12 for STM32F4).
    2. Hold BOOT0 in HIGH when power is applied.
    3. Run CubeProgrammer in mode USB and execute Full erase.
  • 🔄 For chips without 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 OPTBYTES for TrustZone.
    • 🔹 Added support Secure Boot And Secure Firmware Update (SFU).
    • 🔹 Optimized work with Quad-SPI (speed up to 133 MHz).
    • You can download the current version at ST official website.