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.
- Install STM32CubeProgrammer
- Connect ST-LINK to PC and to target board
- Open CubeProgrammer → in the right panel select ST-LINK
- Click Connect
- Go to Erasing & Programming
- Click Browse → select your .hex
-
(Optional) tick:
- Verify programming
- Run after programming
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
Linux/macOS example
STM32_Programmer_CLI -c port=SWD freq=4000 -w firmware.hex -v -rst
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
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

Top comments (0)