What is SWD Serial Wire Debug? | Master Beginner-Friendly Guide (2025)

What is SWD Serial Wire Debug? | Master Beginner-Friendly Guide (2025)

SWD Serial Wire Debug : When working with microcontrollers, debugging is a key part of development. One common way to debug embedded systems is using SWD, short for Serial Wire Debug. It’s a modern, efficient way to communicate with a microcontroller during development.

Let’s break it down in a simple way.

What is SWD Serial Wire Debug?

Serial Wire Debug (SWD) is a two-wire protocol developed by ARM for debugging ARM Cortex-M microcontrollers. It is an alternative to the older JTAG protocol, which uses more pins.

SWD allows you to:

  • Load firmware into the microcontroller.
  • Set breakpoints and step through code.
  • Read and write memory.
  • Monitor and control the processor state.

SWD Serial Wire Debug vs JTAG

FeatureSWD (Serial Wire Debug)JTAG
Wires24-5
SpeedFastFast
Pin UsageLow (good for small MCUs)Higher (more GPIO needed)
ComplexitySimplerMore complex
Debug AccessFull accessFull access

So, SWD is preferred when you have limited pins available and are working with ARM-based chips.

How SWD Serial Wire Debug Works

SWD uses only 2 main signals:

  1. SWDIO – Serial Wire Debug Input/Output
    Acts like data line (similar to SDA in I2C).
  2. SWCLK – Serial Wire Clock
    Acts like the clock line (similar to SCL in I2C).

Optionally, you might also have:

  • nRESET – To reset the microcontroller from the debugger (optional).
  • GND – A common ground between your debug tool and target board (mandatory).

Tools Involved

To use SWD, you need:

  • A microcontroller that supports SWD (e.g., STM32, NXP, etc.).
  • A debug probe, such as:
    • ST-Link (for STM32)
    • J-Link (Segger)
    • CMSIS-DAP
  • A debugger software like:
    • STM32CubeIDE
    • Keil uVision
    • OpenOCD with GDB

SWD Serial Wire Debug Pinout Example (STM32)

Here’s a common SWD pinout on STM32 boards:

Pin NameFunction
SWDIOData
SWCLKClock
GNDGround
3.3VPower supply
NRSTReset (optional)

Why Use SWD?

  • 👨‍💻 Easy Debugging: Load, step, break, and inspect code easily.
  • 🧠 Access RAM/Flash: Read/write memory and registers.
  • 🔄 Flash Programming: Upload firmware via the debug interface.
  • 📉 Fewer Pins: Perfect for space-constrained designs.
  • 🧰 Low-Cost Tools: Many low-cost debug probes available.

Real-Life Use Case

Let’s say you’re working with an STM32 microcontroller. You want to flash your code and debug it:

  1. Connect ST-Link to SWDIO, SWCLK, GND, and optionally NRST.
  2. Open STM32CubeIDE and start a debug session.
  3. You can now:
    • Set breakpoints in code.
    • See variables change in real time.
    • Step through the program.

Prerequisites

Before you start, make sure you have:

  1. ✅ A microcontroller board (e.g., STM32F103, STM32 Nucleo).
  2. ✅ A debug probe (e.g., ST-Link, J-Link, or CMSIS-DAP).
  3. ✅ A PC with debugging software installed (e.g., STM32CubeIDE, Keil uVision, OpenOCD + GDB).
  4. Wires or a USB cable to connect the debugger.
  5. ✅ Basic USB driver for the debug probe installed.

Step-by-Step: Connecting SWD

1. Identify SWD Pins on the Microcontroller

Look for the following pins on your MCU or dev board:

  • SWDIO – Data line
  • SWCLK – Clock line
  • GND – Ground
  • NRST – (Optional, Reset line)
  • VCC – Target voltage reference (often 3.3V)

You can find these in the datasheet or board pinout diagram.

2. Connect the Debug Probe

Match these pins between the debugger and the target board:

Debugger PinTarget Board Pin
SWDIOSWDIO
SWCLKSWCLK
GNDGND
NRST (opt.)NRST
VCC (ref.)3.3V or 5V

Ensure you connect GND properly. Without a common ground, SWD won’t work.

3. Power the Target

  • Some debug probes (like ST-Link) can provide power.
  • Or power the board externally via USB or battery.

Step-by-Step: Debugging

4. Open Your IDE or Debug Software

Examples:

  • STM32CubeIDE for STM32
  • Keil uVision
  • VS Code with GDB + OpenOCD
  • Segger Ozone for J-Link

5. Create or Open a Project

  • Write or import your embedded C/C++ code.
  • Make sure the correct MCU is selected.
  • Configure the debugger settings if needed.

6. Build Your Code

  • Click Build / Compile.
  • Ensure the project compiles without errors.

7. Start Debug Session

  • Click Debug (Bug icon).
  • Your IDE will flash the firmware to the MCU via SWD.
  • It will then pause at main() (or wherever you’ve set breakpoints).

8. Debugging Features You Can Use

Once debugging starts, you can:

  • 🔴 Set breakpoints
  • 🔁 Step over/into code
  • 🧠 Watch variables and memory
  • 🛑 Halt/resume the target
  • 🔍 Monitor registers and peripherals

⏹️ 9. Stop Debugging

  • Click Terminate or Stop Debugging in the IDE.

Summary

StepAction
1Identify SWDIO, SWCLK, GND, etc.
2Connect debug probe to target board
3Power the board
4Open IDE and load project
5Build the code
6Start debug session
7Set breakpoints, inspect memory
8Stop debug session

You can also Visit other tutorials of Embedded Prep 

Special thanks to @mr-raj for contributing to this article on EmbeddedPrep

Leave a Reply

Your email address will not be published. Required fields are marked *