OpenOCD, Debug Adapters & JTAG/SWD-Embedded Systems Training in Hyderabad

🚀 Learn BLE on Linux (BlueZ) – Programming Part 1

Hands-on Bluetooth Low Energy course. Special price: ₹399

Enroll Now on Udemy →
💬 Need help or career guidance? Ask us!Contact Us →

ARM Cortex-M Programming
← PREV_LECNEXT_LEC →

OpenOCD, Debug Adapters & JTAG/SWD

Understand how to download and debug ARM firmware: OpenOCD architecture, JTAG vs SWD protocols, popular debug adapters, and setting up your debugging environment.

Pages 341–350OpenOCDJTAG / SWDST-LINKJ-LinkGDB Remote Debug

1. Downloading Executable to Target

After building firmware.elf on the host PC, you need to transfer it to the MCU’s internal flash memory. The MCU cannot run directly from the host — it needs the binary burned into its non-volatile flash so it persists across power cycles.

Firmware Download Chain
Host PC
firmware.elf
OpenOCD
GDB Client
→
USB
Debug Adapter
ST-LINK / J-Link
Protocol converter:
USB ↔ SWD/JTAG
→
SWD / JTAG
STM32F4
ARM Cortex-M4
Flash controller
DAP (Debug Port)

The debug adapter bridges the gap between USB (which the host speaks) and the debug protocol (JTAG or SWD) that the MCU understands. OpenOCD runs on the host and communicates through the adapter to control the MCU’s debug port.

2. OpenOCD: Open On-Chip Debugger

OpenOCD is a free, open-source host application that enables:

  • Flash programming — erase flash, write .bin or .elf to target flash memory
  • Debug interface — acts as a GDB server (port 3333), so GDB can control the target
  • Telnet interface — command-line interface (port 4444) for manual flashing/debugging
  • Multi-target support — supports hundreds of boards and MCUs
OpenOCD Remote Debugging Architecture
Host PC
GDB Client → connects to port 3333
Telnet Client → connects to port 4444
OpenOCD
GDB server (3333)
Telnet server (4444)
Flash driver
USB driver
Development Board
ST-LINK chip
USB ↔ SWD converter
STM32F407
DAP → AHB-AP → Core
Flash controller

Starting OpenOCD

Shell

# Start OpenOCD with the STM32F4 discovery board config
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg

# Expected output:
# Info : STLINK V2J39S0 (API v2) VID:PID 0483:3748
# Info : clock speed 2000 kHz
# Info : stm32f4x.cpu: hardware has 6 breakpoints, 4 watchpoints
# Info : Listening on port 3333 for gdb connections
# Info : Listening on port 4444 for telnet connections

Flash via Telnet

Telnet Session (port 4444)

# Connect: telnet localhost 4444
halt                                   # stop the CPU
flash write_image erase firmware.elf   # erase and write
reset run                              # reset and run
exit

Flash via GDB

GDB Session (port 3333)

# Launch GDB with the ELF file
arm-none-eabi-gdb firmware.elf

# Connect to OpenOCD GDB server
(gdb) target remote localhost:3333

# Load firmware into target flash
(gdb) load

# Reset and run
(gdb) monitor reset halt
(gdb) continue

# Set a breakpoint at main
(gdb) break main
(gdb) continue

# Step through code
(gdb) next
(gdb) step

# Inspect a variable
(gdb) print blink_period_ms

3. JTAG vs SWD Protocols

Both JTAG and SWD are debug interfaces on ARM Cortex-M microcontrollers. They provide access to the Core Debug registers, memory, and the flash controller.

JTAG (Joint Test Action Group)

  • IEEE 1149.1 standard
  • 4 pins: TCK, TMS, TDI, TDO (+ optional TRST)
  • Daisy-chain multiple devices on one bus
  • Supports boundary scan testing
  • Slightly faster for large data transfers
  • Available on most ARM CPUs

SWD (Serial Wire Debug)

  • ARM proprietary protocol
  • 2 pins: SWDIO (bidirectional), SWDCLK
  • Ideal for small packages with limited pins
  • Does NOT support boundary scan
  • Debugging-only (no JTAG boundary scan)
  • Default on Cortex-M, sufficient for most uses

STM32F4 recommendation: Always use SWD. It only needs 2 wires (SWDIO + SWDCLK + GND), is fully supported by ST-LINK, and supports all debug features including flash programming, breakpoints, watchpoints, and trace (with SWDIO used as SWO).

SWD Connection Pinout

SWD Pinout (10-pin Cortex Debug Connector)

Pin 1:  VCC        (target power, 3.3V)       Pin 2:  SWDIO
Pin 3:  GND                                   Pin 4:  SWDCLK
Pin 5:  GND                                   Pin 6:  SWO (optional trace)
Pin 7:  KEY (no connect)                      Pin 8:  NC
Pin 9:  GND                                   Pin 10: nRESET

4. Popular Debug Adapters

SEGGER J-Link EDU
JTAG + SWD
Supports ARM7/9/11, Cortex-M/R/A, RISC-V, and more. Download speed up to 1 MB/s. The industry standard for professional ARM debugging. EDU version for educational use.
ST-LINK/V2 (standalone)
SWD + JTAG
Official STMicroelectronics in-circuit debugger/programmer for STM8 and STM32. Available as a standalone USB dongle. Widely supported by STM32CubeIDE and OpenOCD.
ULINK2 (Keil/ARM)
JTAG + SWD
Debug adapter for use with Keil MDK (uVision IDE). Supports on-the-fly inspection while running at full speed. Includes real-time trace capabilities.
CMSIS-DAP / DAPLink
SWD
Open-source firmware standard from ARM. Appears as USB HID device — no driver installation needed. Used in many development boards including Arduino and mbed targets.
📷
Suggested images
📷

Suggested image: Photo comparison of ST-LINK/V2 USB dongle, SEGGER J-Link EDU mini, and a development board with onboard debugger.

5. STM32F4-DISC1 Onboard Debugger

The STM32F4 Discovery board includes an onboard ST-LINK-V2A debugger. This means you don’t need to buy a separate debug adapter — the board itself acts as the adapter via USB.

STM32F4-DISC1 Debug Features:

  • Embedded ST-LINK-V2A in-circuit programmer and debugger
  • Supports SWD protocol only (no JTAG on this board)
  • USB Micro-B connector for host connection
  • Can also be used as a standalone ST-LINK for debugging external boards
  • LED indicates USB activity (LD1: COM, LD2: PWR)

AHB-AP: How the Debugger Accesses Memory

The SWD/JTAG port connects to the Debug Access Port (DAP) inside the MCU. The DAP contains an AHB-AP (Advanced High-performance Bus Access Port) that lets the debugger read and write any memory-mapped address — including SRAM, flash, and peripheral registers — while the CPU is halted or even running.

ARM Debug Access Path
Debug Adapter
ST-LINK V2A
→
SWD
SWJ-DP/SW-DP
Serial Wire
Debug Port
→
JTAG-DP bus
AHB-AP
AHB Access Port
reads/writes memory
→
AHB bus
Core + Memory
CPU, SRAM,
Flash, Peripherals

6. Programming Exercises

Exercise 1 — Flash Using OpenOCD

Compile the bare-metal LED project from the previous lecture and flash it using OpenOCD via the telnet interface. Verify the LEDs start blinking after reset.

Shell — Full Flash Sequence

# Terminal 1: Start OpenOCD
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg

# Terminal 2: Connect via GDB and flash
arm-none-eabi-gdb firmware.elf
(gdb) target remote :3333
(gdb) monitor reset halt
(gdb) load
(gdb) monitor reset run

Exercise 2 — Set Breakpoints and Inspect Variables

Add a global variable uint32_t led_toggle_count = 0 that increments each time the LED toggles. Set a breakpoint inside the loop and use GDB to inspect its value after each toggle.

GDB Commands

(gdb) break main.c:35          # break at line 35 (GPIOD_ODR ^= LED_PINS)
(gdb) continue
(gdb) print led_toggle_count   # should print 0
(gdb) continue
(gdb) print led_toggle_count   # should print 1
(gdb) watch led_toggle_count   # hardware watchpoint — breaks when value changes
(gdb) continue                 # runs until watchpoint fires

Exercise 3 — Memory Inspection via GDB

Use GDB to inspect GPIOD registers directly during debugging. Verify the MODER and ODR values match your expectations.

GDB — Memory Examination

# Examine 4 bytes at GPIOD_MODER (0x40020C00)
(gdb) x/1wx 0x40020C00
# Expected: bits 24-31 = 0x55 (output mode for PD12-15)

# Examine GPIOD_ODR (0x40020C14)
(gdb) x/1wx 0x40020C14
# Bits 12-15 reflect current LED state

7. FAQ

Can I use the STM32F4-DISC1 to debug a different STM32 board?

Yes! The Discovery board’s ST-LINK can act as a standalone programmer for external targets. Disconnect the CN3 jumpers to isolate the ST-LINK from the onboard STM32, then connect SWDIO, SWDCLK, GND (and optionally 3.3V) to the target board’s SWD header. This is a common technique for flashing custom hardware during development.

What is the difference between OpenOCD and STM32CubeProgrammer?

STM32CubeProgrammer is an official ST GUI tool for flashing STM32 devices. It is user-friendly but limited to STM32. OpenOCD is an open-source command-line tool that supports hundreds of MCU families from many vendors. OpenOCD also acts as a GDB server for debugging, while STM32CubeProgrammer is primarily for flashing. For IDE-free development, OpenOCD + GDB is the professional choice.

What is SWO / SWV and how does it relate to SWD?

SWO (Serial Wire Output) is an additional optional pin on the SWD connector. It provides real-time trace output — the MCU can send printf-style debug messages and timing information over SWO without halting execution. On STM32, SWO uses the ITM (Instrumentation Trace Macrocell) to format trace packets. This is the mechanism behind the ITM_SendChar() approach used earlier in the course for SWD-based printf.

Quick Knowledge Check

1. How many signal lines does SWD require (minimum)?

  • A. 1 (just SWDIO)
  • B. 2 (SWDIO + SWDCLK)
  • C. 4 (same as JTAG)
  • D. 8 (full parallel bus)
Check Answer
Correct answer: B. SWD uses 2 signal lines: SWDIO (bidirectional data) and SWDCLK (clock). Plus GND and optionally VCC.

2. On which port does OpenOCD listen for GDB connections?

  • A. Port 22 (SSH)
  • B. Port 4444 (Telnet)
  • C. Port 3333 (GDB)
  • D. Port 8080 (HTTP)
Check Answer
Correct answer: C. GDB connects to OpenOCD on port 3333. Port 4444 is for the Telnet command interface.

3. What is the role of the AHB-AP in the ARM debug architecture?

  • A. Converts USB to SWD signals
  • B. Provides debugger access to the AHB bus (memory, peripherals)
  • C. Manages power to the debug port
  • D. Stores debug breakpoint addresses
Check Answer
Correct answer: B. The AHB-AP (AHB Access Port) exposes the processor AHB bus to the debugger via the DAP, enabling memory reads/writes.

EmbeddedPathashala — Embedded Systems Programming on ARM Cortex-M3/M4

💬 Need help or career guidance? Ask us!Contact Us →

2 Comments

Leave a Reply

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

×

🚀 BLE on Linux (BlueZ)

Hands-on course. Only ₹399

Enroll Now →