🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →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.
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.
OpenOCD
GDB Client
Protocol converter:
USB ↔ SWD/JTAG
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
GDB server (3333)
Telnet server (4444)
Flash driver
USB driver
USB ↔ SWD converter
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
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.
Debug Port
reads/writes memory
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
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
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

2 Comments