DEV Community

Hedy
Hedy

Posted on

How to flash hex file into STM32 using ST-LINK/V2?

Here’s the standard, reliable way to flash a .hex onto an STM32 using ST-LINK/V2 (works for most STM32 families).

1) Wire ST-LINK/V2 to STM32 (SWD)

Use SWD (not JTAG) unless you specifically need JTAG.

Minimum signals

  • VTref / VCC (Target voltage sense) → target 3.3V
  • GND → target GND
  • SWDIO
  • SWCLK
  • NRST (recommended, helps connect when firmware broke SWD)

Notes

  • ST-LINK/V2 typically does not power the target reliably (some clones can, many shouldn’t). Power the board normally.
  • Keep wires short.

2) Flash with STM32CubeProgrammer (GUI)

This is the easiest.

  1. Install STM32CubeProgrammer
  2. Connect ST-LINK to PC and to target board
  3. Open CubeProgrammer → in the right panel select ST-LINK
  4. Click Connect
  5. Go to Erasing & Programming
  6. Click Browse → select your .hex
  7. (Optional) tick:

    • Verify programming
    • Run after programming
  8. Click Start Programming

If it fails to connect:

  • Enable “Connect under reset” (or hold reset button while connecting)
  • Ensure NRST is connected
  • Lower SWD frequency (e.g., 100 kHz–1 MHz)

3) Flash with STM32CubeProgrammer (command line)

Good for automation/CI.

Windows example

STM32_Programmer_CLI.exe -c port=SWD freq=4000 -w "C:\path\firmware.hex" -v -rst
Enter fullscreen mode Exit fullscreen mode

Linux/macOS example

STM32_Programmer_CLI -c port=SWD freq=4000 -w firmware.hex -v -rst
Enter fullscreen mode Exit fullscreen mode

Common useful options:

  • -e all : mass erase first
  • -rst : reset after programming
  • -v : verify

Example with erase:

STM32_Programmer_CLI -c port=SWD freq=1000 -e all -w firmware.hex -v -rst
Enter fullscreen mode Exit fullscreen mode

4) Typical problems & fixes
“No target connected”

  • Target not powered or VTref not connected
  • SWDIO/SWCLK swapped
  • Missing common GND
  • Firmware reconfigured SWD pins / put chip in low power

Fix:

  • Use Connect under reset
  • Wire NRST
  • Lower SWD speed

“Readout protection” / can’t program

  • RDP enabled (Level 1)

Fix:

  • Do Full chip erase (this will wipe flash)

HEX loads but firmware doesn’t run

  • Wrong boot configuration (BOOT0/BOOT1 pins)
  • Vector table/address mismatch (wrong linker script)
  • Needs option bytes (watchdog, BOR, boot address)

5) Quick checklist

  • Which STM32 part/board (e.g., STM32F103 “Blue Pill”, STM32F401, STM32H7, etc.)?
  • Is target powered at 3.3V and VTref connected?
  • Are you using CubeProgrammer or ST-Link Utility?

Top comments (0)