Full name: Serial Wire Debug.
Definition
SWD (Serial Wire Debug) is a two-wire interface, defined by Arm [397], through which an external debug probe controls and inspects a processor chip. One wire carries a clock from the probe; the other carries data in both directions. Through SWD the probe can read and write memory and registers while the program runs, halt the processor, step it one instruction at a time, and program flash memory §6.6. SWD is part of the Arm Debug Interface architecture and needs fewer pins than JTAG §6.26 (Joint Test Action Group) debugging.
The idea
A firmware bug appears only on the device. With SWD, a developer stops the processor at a chosen instruction, reads variables and registers, changes a value and lets the program continue. A test setup can also read the device’s flash memory between test runs to collect results. None of this needs help from the firmware, because the probe talks to debug hardware inside the chip, not to the program.
That power has two consequences. First, a probe is not a passive observer. The processor is only one part of a system on chip §6.2 (SoC), a chip that also holds timers, a radio, a watchdog timer §6.16 and other peripherals, and a halt stops only the processor; the other parts, and every device the chip talks to, keep running. Halting one actor in a play while the others keep performing is a fair picture: when the actor resumes, the scene has moved on. The picture stops being accurate in one respect: a chip can be set to freeze some other parts, such as certain timers, together with the processor. Second, whoever connects a probe can read every secret in memory and replace the firmware, so production devices usually lock the debug port.
How it works
The wires. SWCLK is the clock, driven by the probe; SWDIO is the data line, driven by the probe or by the chip in turn. The probe also needs ground, usually a reference voltage from the target so that it signals at the target’s logic level, and often the reset line. SWO (Serial Wire Output) is an optional extra pin that streams trace data out of the chip; it is not part of SWD. SWD can share pins with JTAG: SWDIO uses the pin of the JTAG mode-select signal and SWCLK that of the JTAG clock, and a fixed bit sequence switches protocols.
One transfer. Every SWD operation reads or writes one 32-bit register. The probe sends an 8-bit request that names the register, says read or write, and includes a parity bit. The data line changes direction, and the chip answers with a 3-bit acknowledgment: OK, WAIT (busy, try again) or FAULT (an earlier access failed). After OK, 32 data bits and a parity bit travel in the requested direction.
The layers behind the wires. The Arm Debug Interface (ADI) specification defines what the transfers reach. The debug port (DP) is the chip’s end of the SWD link, and behind it sit one or more access ports (AP). A memory access port (MEM-AP) turns each request into a read or write on the chip’s internal bus, so the probe sees the same addresses as the processor: RAM, flash memory, peripheral registers and the processor’s debug registers. The memory access port is a bus master, a unit that starts its own reads and writes on the bus, like the processor or a DMA §4.36 (direct memory access) controller, so its accesses share the bus with the running program and can delay it by a few cycles.
Powering up the debug logic. Before the first memory access, the probe sets two request bits in the debug port’s control register, CDBGPWRUPREQ and CSYSPWRUPREQ, which ask the chip to power up its debug logic and the system’s connection to it. While these requests stay set, the debug logic stays powered and clocked; on many chips this raises the current draw and can keep the chip out of its deepest sleep states.
Halting and stepping. On an Armv7-M processor such as the Cortex-M4 §6.3, the probe controls execution through DHCSR, the Debug Halting Control and Status Register (address 0xE000EDF0); every write to it carries the key value 0xA05F in its upper 16 bits. Setting C_DEBUGEN enables halting debug, and setting C_HALT as well stops the processor at an instruction boundary: it executes nothing and handles no interrupts §6.11, and S_HALT reads 1. Clearing C_HALT resumes execution, and C_STEP executes a single instruction. With halting debug enabled, hardware breakpoints §14.29, watchpoints and the BKPT instruction also halt the processor (see the breakpoint and watchpoint entry). Software on the processor cannot change C_DEBUGEN: only the debugger can write it, and apart from the debugger only a power-on reset §6.17 returns it to 0. Other debug settings also survive a reset that is not a power-on reset, among them vector catch, which halts the processor as it comes out of reset.
Flash memory and protection. To program flash memory, the probe typically loads a small programming routine into RAM and runs it, or drives the flash controller’s registers directly. Most microcontrollers §6.1 can lock the debug port so that the memory access port refuses access to memory. This readout protection §16.5 keeps secrets and firmware from being read out; on many chips the only way to unlock again is to erase the whole flash memory.
What a halt does to the rest of the chip.
The processor stops: no instructions, no interrupt handlers §6.12, no threads. In the Armv7-M architecture, the DWT (Data Watchpoint and Trace) cycle counter and the SysTick timer, the processor’s built-in system tick timer, do not count while the processor is halted.
Peripheral timers and real-time counters §6.19, low-frequency timers that keep time, run from their own clocks and keep counting, unless the chip has a debug-freeze setting for them and it is turned on.
A watchdog timer either keeps counting, and then resets the chip when the halt outlasts its remaining timeout §9.34, or pauses during the halt; many chips have a setting that chooses between the two. A reset caused by the watchdog also ends the halt.
Communication peripherals keep receiving: a UART §6.23 (universal asynchronous receiver-transmitter) whose incoming bytes no software reads reports an overrun and loses data.
A radio protocol misses its deadlines, because its software no longer runs, while the peer device runs on its own clock. A Bluetooth Low Energy (BLE) §7.1 peer ends the connection when its supervision timeout §7.8 passes without a valid packet, and after resuming, the local protocol software may report an error or reset.
Software timers driven by a peripheral timer see a jump in time when the processor resumes, and every timer that expired during the halt fires at once.
Worked example
Times are in milliseconds. The processor stops at a breakpoint at , and the developer resumes it at . The processor runs at 64 MHz (megahertz). The firmware last fed its watchdog, that is, restarted its countdown of 1,000 ms, at , and last exchanged a packet with a BLE peer, whose supervision timeout is 500 ms, at .
The peer hears nothing more and ends the connection at .
If the watchdog keeps counting during a halt, it expires at and resets the chip, which also ends the halt; the developer then inspects a device that has already restarted.
If the watchdog pauses during a halt, it still has ms left when the processor resumes and shows no trace of the halt.
The DWT cycle counter §6.20 stops for the whole halt and misses cycles, while a timer that counts 32,768 times per second from its own clock advances counts. Comparing the two after resuming reveals the halt.
A test that observed the disconnection at has learned about the debugging session, not about the firmware, which, running undisturbed, would have sent its next packet on time.
Stated precisely
On an Armv7-M processor, a halt has this contract. While S_HALT in DHCSR reads 1, the processor executes no instructions and takes no interrupts or other exceptions, and neither the DWT cycle counter nor the SysTick timer counts. Everything outside the processor follows its own clock, unless a chip-specific debug setting freezes it. A reset that is not a power-on reset ends the halted state but leaves some debug settings in place; a power-on reset clears all of them.
A measurement taken across a halt describes uninterrupted execution only if every component that influences the measured outcome either froze together with the processor or made progress during the halt that is known and accounted for. These components include peripheral timers, watchdogs, radios, communication peripherals and every external device that keeps its own time.
SWD access needs physical access to the debug pins and an unlocked debug port. Given both, it lets the holder read and write all memory that the memory access port reaches and control execution, so a threat model §1.2 that admits physical access must count the debug port as a way in.
Reading memory without changing the device
A test setup that reads the device’s flash memory over SWD between trials must leave the device in the state of a device that was never connected. Five measures achieve that.
Connect without halting or resetting. Many tools halt or reset the processor when they connect, by default; use the connect mode that does neither, which tools often call attach.
Read through the memory access port while the program runs. The read does not stop the processor; its only effects are a few cycles of bus contention and the powered-up debug logic.
Leave no debug settings behind. Remove breakpoints and watchpoints, clear vector catch, and clear
C_DEBUGEN. WithC_DEBUGENstill set, aBKPTinstruction halts the program and waits for a debugger, where it would otherwise raise an exception, normally the HardFault exception, the processor’s catch-all fault handler §6.15.Power the debug logic down at the end by clearing
CDBGPWRUPREQandCSYSPWRUPREQ. J-Link §6.27 probe software, by default, clears the debug enable bits when a session closes, so that the chip can reach its low-power modes, and its settingSetDbgPowerDownOnClosecontrols this. Clearing those bits includesTRCENA, which stops a DWT cycle counter that the application enabled for its own use.Check the result. Because some debug state survives every reset except a power-on reset, either switch the device’s power off and on before each trial, or show with a control trial, run without the read, that the read changes nothing that is measured.
Limits and common mistakes
A probe disturbs the device even without halting it. Reads through the memory access port take bus cycles, and powered debug logic changes the current draw and the sleep behavior, so power measurements taken with a probe attached do not describe the device without it.
Freeze settings are chip-specific. The Arm architecture defines what stops inside the processor; what each peripheral does during a halt is in the chip’s reference manual, and the defaults differ between chips.
A reset that is not a power-on reset does not clear all debug state. A device reset by the probe or by software can still run with halting debug enabled, vector catch set and breakpoints in place.
SWD, SWO and JTAG are different things. SWD is the two-wire debug protocol, SWO is an optional trace output pin, and JTAG is the older multi-pin test and debug interface that SWD replaces on Arm chips.
A locked debug port can refuse memory access although the wiring is correct and the debug port answers, so a probe error can mean a protected chip, not a wiring fault.
Questions answered
How can debugger halt change timers, radio behavior, or watchdogs?
A debugger halt stops only the processor, while timers, radios, watchdogs and every external device follow their own clocks, so the system drifts away from what the halted software expects.
Timers: on an Arm Cortex-M processor, the DWT (Data Watchpoint and Trace) cycle counter and the SysTick system timer stop during a halt, while peripheral timers and real-time counters keep counting unless a chip-specific debug-freeze setting stops them. Durations measured with a stopped counter omit the halt, and software timers driven by a running peripheral timer all fire at once after resuming.
Radio behavior: the protocol software misses its deadlines while the peer keeps its own timers; a Bluetooth Low Energy (BLE) peer ends the connection once its supervision timeout passes without a packet, and the local protocol software may report an error or reset after resuming.
Watchdogs: a watchdog timer that keeps running resets the chip once the halt outlasts its remaining timeout, which also ends the halt; one that pauses during halts shows no trace of them. Many chips have a setting that chooses between the two.
An observation that spans a halt therefore describes a modified run, and it applies to undisturbed execution only if every component that affects the outcome froze with the processor or its progress during the halt is accounted for. Settings left behind after the session, such as halting debug left enabled or the debug logic left powered, can change the device’s behavior even after the probe disconnects.
Related entries: DWT cycle counter (§6.20), GDB (§6.28), Deadline versus timeout (§9.34), Watchdog timer (§6.16), Supervision timeout (§7.8), Readout protection (§16.5), JTAG (§6.26), J-Link (§6.27), Breakpoint and watchpoint (§14.29). Further reading: [34, 81, 397].