The ARM Serial Wire Debug (SWD) protocol is a two-pin debug interface standard developed by ARM for debugging ARM Cortex microcontrollers. It provides a low-pin-count, high-performance alternative to JTAG for accessing and debugging ARM cores.
Overview of ARM SWD
ARM SWD uses just two signals for bidirectional communication – SWCLK and SWDIO. SWCLK (Serial Wire Clock) is the clock signal, while SWDIO (Serial Wire Data Input/Output) carries all data in and out of the target system. By combining data and clock together on a single wire, SWD achieves high-speed communication and debugging while minimizing pin count.
The SWD interface connects the debug probe to the target microcontroller’s dedicated debug port. This port provides access to key debug components like the Debug Access Port (DAP), breakpoints, watchpoints, and various registers within the ARM core like program counter, memory, and peripherals.
Using the SWD interface and debug port, programmers can halt and resume code execution, set software and hardware breakpoints, read/write memory and registers, flash new firmware, and more – all critical capabilities for debugging ARM chips.
Key Capabilities of ARM SWD
Here are some of the major capabilities provided by ARM’s SWD protocol:
- Halting and resuming code execution: SWD allows debug tools to halt a running program, examine processor state, memory, peripherals etc., and resume execution.
- Breakpoints: Set software and hardware breakpoints to pause execution at specific code addresses or conditions.
- Registers access: Read and write ARM core registers like program counter, stack pointer, general purpose registers, and more.
- Memory access: Read and write contents of memory including Flash, SRAM, and device peripherals.
- Flash programming: SWD allows flashing new firmware onto the target device’s memory.
- Real-time code debugging: View mixed C and assembly code, local variables, call stack, threads, etc. while programs run.
- Trace buffer streaming: Some SWD implementations support streaming trace/log data in real-time for advanced debugging.
SWD vs JTAG
SWD was designed by ARM as a successor to the long-standing JTAG (Joint Test Action Group) interface. While JTAG provides robust debugging, it requires at least 5 pins on the target device. SWD achieves similar debugging capabilities using just 2 pins – saving silicon area and cost.
Here’s a quick comparison between JTAG and SWD:
- JTAG uses a 5+ wire interface (TDI, TDO, TCK, TMS, TRSTn). SWD uses just 2 wires (SWCLK, SWDIO).
- JTAG supports advanced features like boundary scan testing. SWD focuses only on debug/programming.
- Maximum clock speed is generally faster on SWD vs JTAG.
- JTAG requires target chips to include a special JTAG circuitry called TAP. SWD uses simpler debug components like DAP.
- SWD uses fewer wires and pins, making routing on complex SoCs easier.
For most debugging purposes, SWD provides all the necessary capabilities while minimizing complexity and cost. Hence it is commonly used for debugging ARM Cortex-M and Cortex-R series cores where pin count is important.
How ARM SWD Works
The SWD protocol establishes a bidirectional communication channel between the debug probe and the target device’s debug port. It works as follows:
- The debug probe (host) connects to the target’s SWCLK and SWDIO pins.
- A handshake is established by syncing clock signals and exchanging version information.
- The host sends requests to read/write registers and memory on the target system.
- Target responds with acknowledgements and data.
- All data is timed and synchronized using the SWCLK clock signal.
- Parity and CRC checks help ensure data validity and prevent synchronization loss.
By packaging both data and clock together in SWDIO, SWD achieves high throughput while using very few pins. The bidirectional nature of SWDIO allows both the host and target to exchange packets as needed.
SWD Packet Format
All data on SWD is wrapped in specially formatted packets. Each packet has a few key elements:
- Start bit – Indicates start of a new packet.
- AP/DP select bit – Selects Access Port (AP) or Debug Port (DP).
- Read/Write bit – Specifies a read or write operation.
- Address – Register or memory being accessed.
- Parity – Odd parity bit for error checking.
- Stop bit – Marks end of packet.
- Park bit – Added by host between packets.
The target system responds by sending acknowledgement packets paired with data for read requests.
Debug Components Used by SWD
SWD relies on a few key components built into ARM chips to enable effective debugging:
DAP (Debug Access Port)
The Debug Access Port manages the connection between the SWD host and the chip’s internal buses and debug components. It has registers for debugger authentication, access control, and more.
Breakpoints
Up to 16 hardware breakpoints can be set that halt execution when a certain memory address is accessed.
Watchpoints
Watchpoints are similar to breakpoints but trigger on data accesses like reads or writes to specific memory locations.
JTAG-DP
The JTAG-DP module provides seamless SWD to JTAG translation in chips that support both interfaces.
Debug Probes for ARM SWD
To interface with the target device’s SWD port, a debugging tool called a debug probe is needed. These probes handle the SWD protocol and packet generation to communicate with the chip. They allow programmers to access the target core in easy ways using a debugger GUI on a host machine.
Some common SWD debug probes include:
- ST-LINK by STMicroelectronics
- J-Link by Segger
- ULINKplus by Keil
- Redlink by NXP
- OpenOCD and RPi SWD tools for DIY probes
Each probe vendor provides software to control the debugging process from a host computer, such as STMCubeIDE, Keil MDK, Segger J-Link tools etc.
Using ARM SWD
To use SWD to debug and program an ARM chip, follow these basic steps:
- Connect the SWCLK and SWDIO pins on the target board to the debug probe.
- Connect the GND pins between the target and probe for a common ground.
- Connect the debug probe to the host PC via USB or Ethernet.
- Launch the debug software and browse for the target device.
- The software will connect to the target using the SWD interface.
- You can now view registers, set breakpoints, program memory, and debug as needed!
The wide adoption of SWD by ARM and its partners has made embedded debugging highly accessible. Debug probes like ST-LINK are inexpensive yet provide professional-grade JTAG-like debugging via SWD. With the help of SWD, even hobbyists can efficiently debug ARM chips right from their laptops.
Conclusion
ARM’s Serial Wire Debug provides a fast 2-pin debug interface for ARM cores. It minimizes the pin count and provides real-time access to critical debugging components like breakpoints, watchpoints, and DAP. By packaging clock and data together, SWD achieves high throughput and efficient synchronization. The widespread availability of SWD debug probes has enabled ARM’s dominance in the 32-bit embedded market.