Scan to FollowLearn Embedded Systems Together, learn and grow together

Overview
1.1 What is the SWD Protocol
SWD (Serial Wire Debug) is a two-wire debugging interface protocol developed by ARM, specifically designed for debugging and programming Cortex-M series microcontrollers.
As a replacement for the traditional JTAG interface, SWD significantly reduces the number of pins while maintaining powerful debugging capabilities, making it the mainstream choice for embedded system debugging.
1.2 Development History
The SWD protocol has evolved with the introduction of Cortex-M processors:
- 2006: First introduced with Cortex-M3
- Subsequent versions gradually improved, supporting more features
- It has now become part of the ARM debugging interface standard
1.3 Comparison of SWD and JTAG
| Feature | SWD | JTAG |
|---|---|---|
| Number of Pins | 2 wires (minimum configuration) | 4 wires (minimum) |
| Speed | Higher | Medium |
| Wiring Complexity | Low | Higher |
| Functionality Integrity | Complete debugging functionality | Full functionality |
| Applicable Scenarios | Cortex-M series | Widely applicable |
Physical Layer
2.1 Interface Pin Definitions
The standard SWD interface includes the following signals:
- SWDIO (Serial Wire Debug I/O):
- Bidirectional data line
- Open-drain or push-pull output configuration
- Typically requires a pull-up resistor
- Clock signal provided by the debugger
- Typical frequency 1MHz-50MHz
- Data sampled on the rising edge
- RESET: System reset
- VREF: Reference voltage
- SWO: Serial Wire Output (for tracing)
2.2 Electrical Characteristics
- Voltage levels: Typically 1.2V-3.3V (consistent with the target system)
- Maximum clock frequency: Depends on the target device (usually up to 50MHz)
- Input impedance: High impedance (typically >100kΩ)
- Driving capability: Able to drive at least 20pF load
Data Link Layer
3.1 Basic Communication Format
SWD communication is based on a request-response model, with each transaction consisting of:
- Request packet sent by the host (debugger)
- Response from the target device
- Data transfer (read or write)
3.2 Data Frame Structure
Request Packet (8 bits):
Start Bit(1) | Transfer Direction(1) | APnDP(1) | Address A[2:3](3) | Parity(1) | Stop Bit(1) | Idle Between Packets(1)
- Start Bit: Fixed at 1
- Transfer Direction: 1=write, 0=read
- APnDP: 0=DP access, 1=AP access
- Address A[2:3]: Address bits 2 and 3
- Parity: Make the count of 1s odd
Response Packet (3 bits):
Response(3) | Idle(1)
Response Types:
- OK(001): Success
- WAIT(010): Retry
- FAULT(100): Error
- Others: Reserved
3.3 Data Transfer Timing
- Host sends request packet (8 SWCLK cycles)
- Target returns response (3 SWCLK cycles)
- If it is a read operation, target returns data (32 bits + parity)
- If it is a write operation, host sends data (32 bits + parity)
- Final idle cycle
Transaction Types
4.1 Debug Port (DP) Access
DP registers include:
- CTRL/STAT (0x0): Control status register
- SELECT (0x8): Select current AP and register
- RDBUFF (0xC): Read buffer
4.2 Access Port (AP) Access
After selecting AP through the DP’s SELECT register, the following can be accessed:
- AP registers (0x0-0xC)
- Memory-mapped registers
4.3 Typical Transaction Flow
- Initialize connection (send at least 50 SWCLK cycles high)
- Send SWD switch sequence (16-bit 0xE79E)
- Read DP’s IDCODE register to verify connection
- Configure DP and AP registers
- Perform memory access or debugging operation
Initialization and Connection Establishment
5.1 Connection Sequence
- After power-up, hold high for at least 50 SWCLK cycles
- Send SWD switch sequence (16-bit 0xE79E)
- Send SWD reset request (0xFFFFFFFF)
- Send SWD switch sequence again
- Read IDCODE to verify connection
5.2 Line Reset
When communication issues arise, the following can be sent:
- At least 50 SWCLK cycles high
- Or send more than 8 consecutive 1s (causing protocol error)
SWD Debug Architecture
6.1 Overview of CoreSight Architecture
SWD is part of the ARM CoreSight debug architecture, which includes:
- Debug Access Port (DAP)
- Multiple Access Ports (AP)
- Memory-mapped debug components
6.2 Debug Components
- Flash Patch and Breakpoint (FPB): Hardware breakpoints
- Data Watchpoint and Trace (DWT): Data watchpoints
- Instrumentation Trace Macrocell (ITM): Software tracing
- Embedded Trace Macrocell (ETM): Instruction tracing
SWD Performance Optimization
7.1 Methods to Improve Transfer Efficiency
- Use appropriate clock frequency (typically 10-20MHz is best)
- Keep AP selection unchanged during bulk transfers
- Utilize automatic address increment feature
- Use polling and waiting judiciously
7.2 Error Handling Mechanisms
- Wait and retry on response errors
- Reduce clock frequency on consecutive errors
- Reinitialize connection on severe errors
- Use DP’s ABORT register to clear error states
Practical Applications
8.1 Typical Debugger Implementations
Modern debuggers such as J-Link and ST-Link support the SWD protocol, typically containing:
- USB interface chip
- SWD protocol processor
- Voltage level conversion circuit
- Debug firmware
8.2 Hardware Design Considerations
- Keep wiring as short as possible (<15cm)
- Avoid running parallel to high-frequency signals
- Add appropriate pull-up resistors (typically 10kΩ)
- Add buffers if necessary
- Ensure good power decoupling
8.3 Common Issue Troubleshooting
- Unable to Connect:
- Check power and reset signals
- Verify SWDIO/SWCLK connections
- Try reducing clock frequency
- Check signal integrity
- Add appropriate termination resistors
- Shorten cable length
- Verify target voltage
- Check timing parameters
- Ensure correct AP selection
Conclusion
The SWD protocol, with its simple two-wire design and powerful debugging capabilities, has become the de facto standard for debugging Cortex-M series microcontrollers.
Understanding the working principles and implementation details of the SWD protocol is crucial for embedded system development and debugging.
With the rise of IoT and edge computing, the SWD protocol will continue to play an important role in embedded debugging due to its low pin count and high efficiency advantages.

Follow 【Learn Embedded Systems Together】 to become better together!
If you find this article helpful, click “Share”, “Like”, or “Recommend”!