How to Debug ARM Cortex-M Using SWD: Step-by-Step Guide
SWD, connect the SWD interface pins (SWDIO, SWCLK, GND, and optionally RESET) between your debugger and the target board. Use a compatible debugger and software like OpenOCD or vendor IDEs to load code, set breakpoints, and inspect memory and registers via the SWD protocol.Why This Happens
Debugging issues with ARM Cortex-M using SWD often happen because the debugger is not properly connected or configured. Common problems include missing connections on SWD pins, incorrect target voltage settings, or disabled debug interfaces in the microcontroller's option bytes.
For example, if the SWD clock or data lines are not connected correctly, the debugger cannot communicate with the chip, causing connection failures.
// Example of incorrect SWD connection setup in a debug configuration file interface cmsis-dap transport select swd // Incorrect transport type for JTAG causes failure to connect adapter_khz 1000 source [find target/stm32f4x.cfg]
The Fix
To fix this, ensure the debugger uses the SWD transport, not JTAG, and that the SWD pins (SWDIO and SWCLK) are correctly wired. Also, verify the target voltage matches the debugger's settings and that the microcontroller's debug interface is enabled.
Use a debug configuration like this to correctly set SWD:
interface cmsis-dap
transport select swd
adapter_khz 1000
source [find target/stm32f4x.cfg]
Prevention
To avoid SWD debugging issues in the future, always double-check your hardware connections before powering the device. Use labeled cables and pinouts to prevent wiring mistakes. Configure your debugger software explicitly for SWD, not JTAG, and confirm the target MCU debug settings are enabled.
Regularly update your debugger firmware and software tools to support the latest devices and protocols. Using vendor-provided debug configurations can reduce setup errors.
Related Errors
Other common errors include:
- No connection: caused by missing power or ground on the target board.
- Permission denied: debugger software lacks access rights to USB devices.
- Target not halted: the MCU is running and not responding to debug halt commands.
Quick fixes involve checking power, running debugger software as administrator, and resetting the target MCU.