Openocd Jtag

mohitmishra786/low-level-dev-skills/skills/embedded/openocd-jtag

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

OpenOCD skill for embedded hardware debugging. Use when configuring OpenOCD for JTAG or SWD targets, flashing firmware, connecting GDB to bare-metal targets, setting hardware watchpoints, or using J-Link with OpenOCD. Activates on queries about OpenOCD, JTAG, SWD, GDB remote target, flash programming, hardware breakpoints, J-Link, or connecting GDB to an MCU.

AI 產生的概覽

指導設定 OpenOCD 以偵錯 JTAG/SWD 目標、燒錄韌體,並連接 GDB 進行裸機偵錯。

功能
此技能提供設定 OpenOCD 以連接 JTAG 與 SWD 目標的說明,包含 CMSIS-DAP、ST-Link、J-Link 及常見 MCU 的介面與目標設定檔。內容涵蓋透過 OpenOCD telnet、GDB 或腳本模式燒錄韌體、將 GDB 連接到 OpenOCD 遠端目標,以及設定硬體斷點與監看點。它也列出常用 OpenOCD 指令、常見錯誤與修復對照表,以及 J-Link 與 J-Link GDB Server 的設定。
適用情境
適用於為 JTAG 或 SWD 偵錯設定 OpenOCD、將韌體燒錄到微控制器、把 GDB 連接到裸機目標、設定硬體監看點,或設定 J-Link 與 CMSIS-DAP 轉接器。也可用於排查 OpenOCD 連線與燒錄錯誤。
執行需求
僅為說明文件,不附帶指令碼。需要 OpenOCD、適用於目標平台的 GDB(例如 arm-none-eabi-gdb)、硬體偵錯轉接器(如 CMSIS-DAP、ST-Link 或 J-Link),以及對應的 OpenOCD 介面與目標設定檔。

OpenOCD / JTAG Debugging

Purpose

Guide agents through configuring OpenOCD for JTAG and SWD targets, flashing firmware to microcontrollers, attaching GDB for bare-metal debugging, setting hardware watchpoints, and configuring J-Link and CMSIS-DAP adapters.

Triggers

  • "How do I connect GDB to my MCU with OpenOCD?"
  • "How do I flash firmware using OpenOCD?"
  • "How do I set up J-Link with OpenOCD?"
  • "What's the difference between JTAG and SWD?"
  • "How do I set a hardware watchpoint in GDB?"
  • "OpenOCD says 'Error: unable to find JTAG device' — how do I fix it?"

Workflow

1. JTAG vs SWD

FeatureJTAGSWD (Serial Wire Debug)
Pins4+ (TCK, TMS, TDI, TDO, TRST)2 (SWCLK, SWDIO)
Multi-targetYes (daisy chain)No (one target)
SpeedUp to 30 MHzUp to 10 MHz
AvailabilityFull JTAG: Cortex-A/R, RISC-VSWD: Cortex-M only
Cable costMore complexSimpler 2-wire

Most Cortex-M microcontrollers support both. Use SWD when pin count is limited.

2. OpenOCD configuration

tcl
# openocd.cfg — CMSIS-DAP (ST-Link v2, DAPLink)source [find interface/cmsis-dap.cfg]source [find target/stm32f4x.cfg]adapter speed 4000              # kHz
# For SWD explicitlytransport select swd
# J-Link adaptersource [find interface/jlink.cfg]jlink serial 123456789          # optional: select by serialsource [find target/nrf52.cfg]adapter speed 8000
bash
# Run OpenOCD (keeps running, serves GDB on port 3333)openocd -f openocd.cfg
# Common interface config filesls $(openocd --help 2>&1 | grep "scripts" | head -1)/interface/# cmsis-dap.cfg, jlink.cfg, ftdi/olimex-arm-usb-ocd.cfg, stlink.cfg ...
# Common target config filesls $(openocd --help 2>&1 | grep "scripts" | head -1)/target/# stm32f4x.cfg, nrf52.cfg, esp32.cfg, rp2040.cfg, at91sam4s.cfg ...

3. Connecting GDB

bash
# In terminal 1: start OpenOCDopenocd -f openocd.cfg
# In terminal 2: start GDBarm-none-eabi-gdb firmware.elf
# GDB commands(gdb) target extended-remote :3333   # connect to OpenOCD(gdb) monitor reset halt              # reset and halt target(gdb) load                            # flash ELF to target(gdb) monitor reset init             # re-initialize after flash(gdb) break main                      # set software breakpoint(gdb) continue
# One-liner for quick debuggingarm-none-eabi-gdb -ex "target extended-remote :3333" \                  -ex "monitor reset halt" \                  -ex "load" \                  -ex "break main" \                  -ex "continue" \                  firmware.elf

4. Flashing firmware

bash
# Flash via OpenOCD telnet interfacetelnet localhost 4444
# OpenOCD telnet commands> reset halt> program firmware.elf verify reset> exit
# Or via GDB(gdb) monitor flash write_image erase firmware.bin 0x08000000(gdb) monitor reset run
# Flash only (no debug) — script modeopenocd -f openocd.cfg \        -c "program firmware.elf verify reset exit"
# For raw binaryopenocd -f openocd.cfg \        -c "program firmware.bin 0x08000000 verify reset exit"

5. Hardware breakpoints and watchpoints

Software breakpoints (break) patch instruction memory with trap instructions — they don't work in flash-execute-in-place without debug registers. Use hardware breakpoints instead:

bash
# Hardware breakpoint (uses debug register, limited count: 4-8 on Cortex-M)(gdb) hbreak function_name(gdb) hbreak *0x08001234
# Hardware watchpoint — triggers on memory read/write(gdb) watch global_variable         # write watchpoint(gdb) rwatch some_buffer            # read watchpoint(gdb) awatch sensor_value           # read OR write watchpoint
# List breakpoints/watchpoints(gdb) info breakpoints
# Cortex-M typically has: 4–8 hardware breakpoints, 2–4 watchpoints

6. OpenOCD commands reference

bash
# Via telnet (port 4444) or GDB monitormonitor reset halt          # reset and hold at reset vectormonitor reset init          # reset and run init scriptsmonitor reset run           # reset and run freelymonitor halt                # halt running targetmonitor resume              # resume executionmonitor mdw 0x20000000      # memory display word at addressmonitor mww 0x40021000 0x1  # memory write wordmonitor reg r0              # read registermonitor arm disassemble 0x08000000 16  # disassemble 16 instructions
# Flash operationsmonitor flash listmonitor flash erase_sector 0 0 0   # erase sector 0monitor flash write_bank 0 firmware.bin 0

7. Common errors

ErrorCauseFix
unable to find JTAG deviceWrong interface, cable, or powerCheck USB connection; power target; verify interface config
JTAG scan chain interrogation failedWrong target config or bad SWD modeMatch config to exact MCU; try transport select swd
flash 'stm32f4x' is not supportedWrong targetCheck MCU part number; use correct .cfg
Error: timed out while waiting for target haltedTarget running, not haltedmonitor halt; check BOOT pins
Cannot access memory at addressMMU, memory protection, or wrong addressCheck MPU config; use correct flash/RAM address
Warn: target not examined yetOpenOCD not connected to targetCheck power and connections

8. J-Link configuration

tcl
# openocd.cfg for J-Linksource [find interface/jlink.cfg]jlink usb 0          # use first J-Link foundtransport select swd  # or jtagadapter speed 4000
source [find target/stm32l4x.cfg]

J-Link GDB Server (alternative to OpenOCD):

bash
# Segger's own GDB serverJLinkGDBServer -if SWD -device STM32L476RG -port 3333 &arm-none-eabi-gdb -ex "target remote :3333" firmware.elf

Related skills

  • Use skills/embedded/freertos for FreeRTOS-aware debugging with OpenOCD
  • Use skills/embedded/zephyr for Zephyr's west debug integration
  • Use skills/debuggers/gdb for GDB session management
  • Use skills/embedded/linker-scripts for understanding memory map for flash addresses

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/embedded/openocd-jtag提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架