48з. Programmers and debuggers for STM32 and STM8: ST-LINK V2/V3 and clones, CMSIS-DAP, J-Link, command-line flashing
In 48ж the firmware was written by a standalone box without a computer. But on a developer's desk and at most programming stations something else is used: a debug adapter plugged into the computer's USB. It is the programmer and the debugger at once: the same little box writes the image, halts the core, reads memory and provides a console through a virtual COM port.
This section covers: what is inside an adapter and how adapters differ; an overview table — ST-LINK/V2, the on-board V2-1 on Nucleo and Discovery boards, the STLINK-V3 family, Chinese clones, CMSIS-DAP, J-Link, a GDB server right inside the adapter, FTDI-based dongles; wiring — power, the reference voltage, NRST and connecting under reset; STM8 and SWIM; a Python speed model — the SWD clock and the USB overhead; command-line flashing with five tools; a script for production flashing; how to choose; pitfalls.
What is not repeated here: how the adapter talks to the core over SWD (DP, AP, halting, breakpoints) — in 35д; output through the debugger — RTT in 21а and semihosting in 35а; the factory bootloader without an adapter — 48д; your own box for production — 48ж.
Sources (topics only, no retelling of the text): ST user manuals for ST-LINK/V2 and STLINK-V3 (connectors, voltages, clocks), the STM32CubeProgrammer manual (connection modes, CLI commands), the Arm CMSIS-DAP specification (DAP commands, versions 1 and 2), UM0470 (SWIM), the OpenOCD and pyOCD documentation. Timing figures come from our own model.
Terms used in the documentation
Term Meaning ST-LINK/V2-1, STLINK-V3 ST adapter generations: on-board and standalone on-board debugger a debugger soldered onto a development board CMSIS-DAP v1 (HID) / v2 (WinUSB, bulk) an open adapter protocol in two USB transport variants VTref, target voltage sense the target reference voltage input connect under reset connecting while NRST is held low SWD frequency (adapter speed) the SWD clock frequency clone / counterfeit probe an adapter clone, often on a counterfeit chip probe firmware upgrade updating the firmware of the adapter itself
48з.1 What an adapter does
A debug adapter is a translator between the computer's USB and the chip's debug pins. Inside is its own microcontroller (an STM32 in ST-LINK), level buffers and firmware that takes commands over USB and drives the SWDIO and SWCLK lines (or JTAG, or SWIM for STM8). On top sits a tool on the computer: STM32CubeProgrammer, OpenOCD, pyOCD or the IDE's GDB server.
Besides SWD almost all ST adapters provide two more things: a virtual COM port (VCP) — a USB-UART to the target pins, and an SWO input for trace output. STLINK-V3 adds a bridge: SPI, I2C, CAN and GPIO from the computer.
What mainly sets adapters apart:
- which interfaces they have: SWD, JTAG, SWIM, SWO, VCP;
- what target voltage the buffers accept and whether there is a VTref input;
- the maximum SWD clock;
- the USB transport: a proprietary protocol, HID or bulk — speed depends on it more than on the SWD clock (48з.6);
- which tools work with it and whether its firmware can be updated.
48з.2 Overview table
The values are typical for original products according to manufacturers' manuals and descriptions; revisions and clones differ.
| Adapter | Interfaces | Target voltage | SWD, max | Tools |
|---|---|---|---|---|
| ST-LINK/V2 (standalone) | SWD, JTAG, SWIM, SWO | 1.65–5.5 V | 4 MHz | CubeProgrammer, OpenOCD, pyOCD, st-flash, stm8flash |
| ST-LINK/V2-1 (Nucleo, Discovery) | SWD, SWO, VCP | as the on-board target (3.3 V) | 4 MHz | the same, except SWIM |
| STLINK-V3 (MINIE, SET, PWR) | SWD, JTAG, SWO, VCP, bridge | 1.65–3.6 V | 24 MHz | CubeProgrammer, OpenOCD, pyOCD |
| "Key fob" ST-LINK/V2 clone | SWD, SWIM | 3.3 V only | 4 MHz (less in practice) | the same, with the caveats of 48з.3 |
| CMSIS-DAP (DAPLink etc.) | SWD, JTAG, sometimes SWO, VCP | depends on the board | 1–10 MHz | OpenOCD, pyOCD, Keil, CubeIDE |
| J-Link | SWD, JTAG, SWO | 1.2–5 V | 4–50 MHz by model | own package, OpenOCD, pyOCD |
| GDB server in the adapter (Black Magic Probe) | SWD, JTAG, SWO, UART | 3.3 V (with VTref on newer ones) | a few MHz | GDB directly, no tool |
| FT2232H dongle (ESP-Prog and similar) | JTAG, SWD through a resistor | 3.3 V | up to 30 MHz JTAG | OpenOCD |
The table shows the main limitation of STM8: only ST-LINK has SWIM (the original V2 and its clones). The on-board V2-1 on Nucleo-STM32 does not bring SWIM out, STLINK-V3 lacks it, and so do CMSIS-DAP and J-Link. For STM8 keep an ST-LINK/V2 or an STM8 Discovery board on the desk.
48з.3 ST-LINK clones: what is wrong
A "key fob" clone costs as much as a lunch and really does flash. But it has its own quirks, which surface at the most inconvenient moment.
A counterfeit or different chip inside. Clones carry an STM32F103, a copy of it from another manufacturer, or a different chip altogether. The adapter firmware is copied from ST. It works until a tool checks authenticity.
The firmware update refuses. The ST-LINK upgrade tool sees the wrong chip or the wrong ID and refuses, and new tool versions sometimes refuse to work with old adapter firmware. The result — the clone "suddenly" stops working with CubeProgrammer after an IDE update. OpenOCD and st-flash are more tolerant of such things.
No SWO, no VCP, no VTref. The fob connector carries SWDIO, SWCLK, SWIM, RST, 3.3 V, 5 V and ground. There is no SWO trace — for output through the debugger RTT remains (21а). There is no reference voltage: the buffers work at 3.3 V only, a target at 1.8 V or 5 V is not for it.
The RST pin is not always NRST. On some clones RST is wired to a line that the adapter firmware uses only for SWIM, not as NRST for STM32. Connecting under reset (48з.5) then does not work — check with a continuity tester or an oscilloscope.
Low real clock. 4 MHz is claimed, but in practice edges degrade on the wire and errors already occur at 1.8 MHz. Start with a low clock.
Conclusion: a clone is fine for learning and for a one-off STM8 job. For work with SWO, different voltages and reliable updates — an original ST-LINK or the one built into a Nucleo board.
48з.4 CMSIS-DAP, J-Link, a GDB server in the adapter, FTDI
CMSIS-DAP is an open adapter protocol from Arm. The tool sends DAP commands (write and read DP and AP registers, a block of words, reset), and the adapter firmware turns them into SWD clocks. There are many firmwares: DAPLink from Arm, vendor ones for development boards, open projects on RP2040 and STM32.
There are two transport versions:
- v1 — HID. The adapter pretends to be an input device: no driver is needed, but a report is 64 bytes, and exchange happens no more often than one 1 ms USB frame in each direction.
- v2 — bulk (WinUSB). Ordinary bulk packets, 512 bytes each at high speed, the Windows driver is installed automatically from the descriptor. Speed is several times higher (48з.6).
If an adapter has both, tools pick v2. If the firmware is old and only offers v1, update it — that is the main speed gain.
J-Link is a commercial SEGGER adapter with its own protocol and its own tool package (flashing, GDB server, RTT). A wide voltage range, fast Flash loaders, support for almost every chip. There are educational versions (EDU) — for non-commercial use only — and OEM variants built into other boards. The licence terms of the variants differ: read them before putting an EDU on a production line.
A GDB server in the adapter is the Black Magic Probe approach: a GDB server runs
inside the adapter, the computer sees it as a COM port, and GDB connects
directly (target extended-remote). Neither OpenOCD nor CubeProgrammer is
needed; convenient on Linux and in scripts.
An FTDI dongle (FT2232H — for example, ESP-Prog) is a USB chip with an MPSSE mode that OpenOCD turns into JTAG. SWD is obtained through a resistor between TDI and TDO, but it is a compromise: no SWO and no SWIM. Good for JTAG chains and when the dongle is already lying on the desk.
48з.5 Wiring: power, VTref, NRST, SWIM
The minimal SWD connector has four lines: SWDIO, SWCLK, GND and VTref. NRST is the fifth and highly desirable one, SWO is the sixth.
Ground. One common, short ground. Without it SWD "sees it, then doesn't".
VTref. The adapter measures the target voltage and adjusts its buffers. It is an input, not a supply: the target is powered from its own source. If VTref is not connected, ST-LINK/V2 and V3 report that there is no target voltage and will not start. Tools show the measured value — the first thing to look at when a connection fails.
Powering the target from the adapter. Fobs have 3.3 V and 5 V on the connector, the standalone ST-LINK/V2 does not. Powering a board from the adapter is fine for small boards, but the current is limited by USB and the adapter's regulator.
NRST and connecting under reset. If the target firmware right after start-up disables the SWD pins, puts the core to sleep or reconfigures the clocks, a normal connection does not make it in time. The solution is connect under reset: the adapter holds NRST low, connects to the DP, sets a core halt on the reset vector (how this is done — in 35д) and only then releases NRST. The core halts before the first firmware instruction. Without the NRST wire this mode is impossible — which is why NRST is always brought out to the connector.
SWIM for STM8. One SWIM line plus NRST, GND and VTref. Without NRST, entering SWIM is unreliable: the adapter starts the entry sequence while holding the chip in reset. The SWIM protocol and its timing are covered in 48ж.5.
48з.6 Speed: SWD clock versus USB
Programming time consists of three parts: the SWD wire, the USB transport and
writing the Flash itself. The third part is the same for any adapter, and the
model model48h.py computes the first two. Writing a word to DRW with
address auto-increment takes 46 SWD clocks, i.e. 11.50 µs per word at 4 MHz.
The SWD wire only, by clock:
| Size | 1.8 MHz | 4 MHz | 8 MHz | 24 MHz |
|---|---|---|---|---|
| 64 KB | 0.42 s | 0.19 s | 0.09 s | 0.03 s |
| 256 KB | 1.67 s | 0.75 s | 0.38 s | 0.13 s |
| 1024 KB | 6.70 s | 3.01 s | 1.51 s | 0.50 s |
Now the USB transport for CMSIS-DAP. A block write command has 5 bytes of header and 4 bytes per word: a 64-byte HID report holds 14 words, a 512-byte bulk packet holds 126 words. An HID report goes out and the reply comes back one 1 ms frame each — 2 ms per report; bulk at high speed takes 0.25 ms per packet. The wire and USB do not overlap in the model — an upper bound:
| Size | wire 4 MHz | v1 HID, 64 B | total v1 | v2 bulk, 512 B | total v2 |
|---|---|---|---|---|---|
| 64 KB | 0.19 s | 2.34 s | 2.53 s | 0.03 s | 0.22 s |
| 256 KB | 0.75 s | 9.36 s | 10.12 s | 0.13 s | 0.88 s |
| 1024 KB | 3.01 s | 37.45 s | 40.46 s | 0.52 s | 3.53 s |
Conclusion: for an HID adapter the bottleneck is USB, not SWD. A 1024 KB image takes 40.46 s over v1 and 3.53 s over v2, and raising the SWD clock with v1 is almost useless. Adapters with a proprietary protocol (ST-LINK, J-Link) and a loader in target RAM come closer to the "wire only" table plus the chip's Flash write time.
48з.7 Command-line flashing
The IDE flashes with a button, but a script, a build server and
production need the command line. Below are typical commands of five tools.
The flags are given from memory of common versions and need checking
against the --help of the installed version: option names change from release
to release. In all examples the image is written, verified and the target is reset.
STM32CubeProgrammer, the ST command-line tool:
STM32_Programmer_CLI -l
STM32_Programmer_CLI -c port=SWD freq=4000 mode=UR reset=HWrst \
-w build/fw.bin 0x08000000 -v -rst
Here mode=UR is connect under reset, -v is verification after writing,
and -l lists the connected adapters with their serial numbers.
OpenOCD is an open debug server; the program command writes, verifies and
resets:
openocd -f interface/stlink.cfg -f target/stm32f1x.cfg \
-c "adapter speed 4000" \
-c "program build/fw.elf verify reset exit"
For CMSIS-DAP only the first file changes: interface/cmsis-dap.cfg.
pyOCD is a server and tool in Python that understands CMSIS-DAP, ST-LINK and J-Link:
pyocd list
pyocd flash -t stm32f103rc -f 4000000 build/fw.hex
st-flash is a small open tool for ST-LINK only:
st-info --probe
st-flash --reset write build/fw.bin 0x08000000
stm8flash is an open tool for STM8 over SWIM:
stm8flash -c stlinkv2 -p stm8s103f3 -w build/fw.ihx
stm8flash -c stlinkv2 -p stm8s103f3 -v build/fw.ihx
CubeProgrammer also has a SWIM mode for STM8, but a separate tool is more convenient: it is simpler and smaller.
Common rules for all tools:
- the address for
.binis given explicitly,.hexand.elfcarry addresses inside; - without verification a command may end in success after a failed write;
- the tool's exit code is the only thing a script can rely on, not the output text.
48з.8 Production flashing with a script
When there are tens and hundreds of boards and no standalone box from 48ж, a computer, an ST-LINK and a script are enough. The script waits for Enter, flashes, writes a serial number into a separate Flash word, verifies and keeps a log. The serial number address is the last Flash page, and the firmware reads it.
import csv, datetime, subprocess
CLI = "STM32_Programmer_CLI"
CONN = ["-c", "port=SWD", "freq=4000", "mode=UR", "reset=HWrst"]
IMAGE, BASE = "build/fw.bin", "0x08000000"
SN_ADDR = "0x0801FC00" # last page of a 128 KB part, read by firmware
def run(args):
r = subprocess.run([CLI] + CONN + args, capture_output=True, text=True)
return r.returncode == 0
serial = int(input("first serial: "))
with open("flash_log.csv", "a", newline="") as log:
w = csv.writer(log)
while input("board in place, Enter (q to stop): ") != "q":
ok = run(["-e", "all", "-w", IMAGE, BASE, "-v"]) and \
run(["-w32", SN_ADDR, "0x%08X" % serial, "-v", "-rst"])
w.writerow([datetime.datetime.now().isoformat(timespec="seconds"),
serial, "PASS" if ok else "FAIL"])
log.flush()
print("PASS %d" % serial if ok else "FAIL, serial %d kept" % serial)
if ok:
serial += 1
What matters here:
- the number is incremented only after a successful flash — numbers without gaps;
- the log is flushed to disk after every board: a computer crash does not eat a shift;
- a full erase before writing — a board with someone's old firmware will not leave leftovers in unwritten pages;
- writing the number with a separate command is simpler than patching the image, but costs a second connection; injecting the number into the image itself is covered in 48ж.7.
If there are several adapters on one computer, each is selected by
the serial number from the -l list (the sn= key in the connection string) — this is how
gang flashing of several boards at once works.
48з.9 How to choose
Learning and first STM32 boards. A Nucleo board: it already has an ST-LINK/V2-1 with VCP and SWO, no separate adapter is needed. Its on-board debugger can be split off with jumpers and used to flash external boards.
STM8. Only ST-LINK/V2 (original or clone) — SWIM is nowhere else.
Your own STM32 board, daily work. STLINK-V3MINIE: cheap, 24 MHz, VCP, VTref; if you need a 5 V target — ST-LINK/V2 or J-Link.
Many different chips, not only ST. J-Link or CMSIS-DAP v2 with pyOCD/OpenOCD — one adapter for everything.
Linux, scripts, no extra servers. Black Magic Probe or CMSIS-DAP with OpenOCD.
Production. ST-LINK/V3 or J-Link with a script (48з.8) for small batches; for large ones — a standalone box (48ж) or a gang programmer. Clones are not used in production: an IDE update can stop the line.
48з.10 Pitfalls
"No target connected" with a live board. VTref or ground is not connected, SWDIO and SWCLK are swapped, the target has no power. Look at what target voltage the tool reported.
Connects once, then never again. The firmware disables the SWD pins or goes into deep sleep. Connect under reset with the NRST wire; if NRST is not brought out — the factory bootloader (48д).
After an IDE update the adapter "disappeared". The tool wants new adapter firmware; on a clone the update refuses. Roll the tool back or use OpenOCD and st-flash.
Flashed, but does not work. A .bin was written to the wrong address or
without verification. Always an explicit address and -v / verify.
Slow even though the clock is high. The adapter runs CMSIS-DAP v1 — see 48з.6; update the adapter firmware to v2.
Errors at a high clock. A long ribbon cable, no ground next to SWCLK. A shorter wire or a lower clock; 24 MHz over 20 cm of wire is a lottery.
Readout protection is on — the tool sees nothing. With RDP level 1 the adapter connects but cannot read memory; removing protection erases the whole Flash. Level 2 is irreversible — no adapter helps with it.
48з.11 Summary
- A debug adapter translates USB into SWD, JTAG or SWIM, plus VCP and SWO on many; the differences are interfaces, voltage, clock and USB transport.
- SWIM for STM8 — only on ST-LINK/V2 and its clones.
- Clones flash, but without SWO and VTref, at 3.3 V only, and break on tool updates.
- CMSIS-DAP v1 over HID is limited by USB: 1024 KB takes 40.46 s versus 3.53 s for v2 over bulk; the SWD wire at 4 MHz takes 11.50 µs per word.
- Mandatory wires: ground, VTref, SWDIO, SWCLK, NRST — without NRST there is no connect under reset.
- The command line: CubeProgrammer, OpenOCD, pyOCD, st-flash, stm8flash; always with verification, and in scripts — by exit code.
- For a series — a script that increments the number after success and logs every board.
Next: section 48и "Production flashing" — fixtures, firmware protection, unique ID, serial numbers and the log.