OpenOCD 终端使用指令大全
一、启动 OpenOCD
基本启动命令
# 通用格式
openocd -f interface/调试器.cfg -f target/目标芯片.cfg# STM32F4 + ST-Link 示例
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg# STM32F1 + ST-Link
openocd -f interface/stlink.cfg -f target/stm32f1x.cfg# STM32H7 + ST-Link
openocd -f interface/stlink.cfg -f target/stm32h7x.cfg# 使用 J-Link
openocd -f interface/jlink.cfg -f target/stm32f4x.cfg
指定端口启动
# 指定 GDB 端口(默认3333)
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "gdb_port 3333"# 指定 Telnet 端口(默认4444)
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c "telnet_port 4444"
自定义配置文件
# 使用自己的配置文件
openocd -f myconfig.cfg# 执行多个命令
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c init -c "reset halt"
二、连接 OpenOCD (Telnet)
OpenOCD 启动后,在另一个终端连接:
# 连接到 OpenOCD
telnet localhost 4444```三、OpenOCD 内部命令
连接后可以使用以下命令:
基本操作```powershell
# 初始化
> init# 复位芯片
> reset# 复位并暂停
> reset halt# 复位并运行
> reset run# 暂停执行
> halt# 继续运行
> resume# 退出 OpenOCD
> shutdown
内存操作
# 读取内存 (地址 数量)
> mdw 0x08000000 16 # 读16个字(32bit)
> mdh 0x08000000 32 # 读32个半字(16bit)
> mdb 0x08000000 64 # 读64个字节(8bit)# 写入内存
> mww 0x20000000 0x12345678 # 写字
> mwh 0x20000000 0x1234 # 写半字
> mwb 0x20000000 0x12 # 写字节
寄存器操作
# 读取所有寄存器
> reg# 读取指定寄存器
> reg r0
> reg pc
> reg sp# 写入寄存器
> reg r0 0x12345678
> reg pc 0x08000000
Flash 操作
# 擦除 Flash
> flash erase_sector 0 0 last # 擦除所有扇区
> flash erase_address 0x08000000 0x10000 # 擦除指定地址# 烧录程序
> flash write_image erase firmware.bin 0x08000000
> flash write_image erase firmware.hex
> flash write_image erase firmware.elf# 校验 Flash
> verify_image firmware.bin 0x08000000# Flash 信息
> flash info 0
> flash banks
断点和单步
# 设置断点
> bp 0x08000100 4 hw # 硬件断点
> bp 0x08000200 2 hw # 2字节断点# 删除断点
> rbp 0x08000100# 列出断点
> bp# 单步执行
> step # 单步(进入函数)
> stepi # 汇编级单步
目标信息
# 查看目标状态
> targets# CPU 信息
> target names
> target current# 芯片 ID
> stm32f4x.cpu mdb 0xE0042000 3 # 读设备ID
四、烧录程序的完整流程
方法一: 通过 Telnet
# 1. 启动 OpenOCD
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg# 2. 另一个终端连接
telnet localhost 4444# 3. 烧录
> reset halt
> flash write_image erase /path/to/firmware.bin 0x08000000
> reset run
> exit
方法二: 命令行直接烧录
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \-c init \-c "reset halt" \-c "flash write_image erase firmware.bin 0x08000000" \-c "reset run" \-c shutdown
方法三: 使用脚本文件
创建 flash.cfg:
init
reset halt
flash write_image erase firmware.bin 0x08000000
verify_image firmware.bin 0x08000000
reset run
shutdown
执行:
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -f flash.cfg
五、配合 GDB 调试
启动调试
# 1. 启动 OpenOCD (终端1)
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg# 2. 启动 GDB (终端2)
arm-none-eabi-gdb firmware.elf# 3. GDB 中连接 OpenOCD
(gdb) target remote localhost:3333
(gdb) monitor reset halt
(gdb) load
(gdb) continue
GDB 中的 monitor 命令
(gdb) monitor reset halt # 复位并暂停
(gdb) monitor reset run # 复位并运行
(gdb) monitor halt # 暂停
(gdb) monitor resume # 继续
(gdb) monitor reg # 查看寄存器
六、常用完整命令示例
快速烧录
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \-c "program firmware.bin 0x08000000 verify reset exit"
擦除整个 Flash
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \-c init \-c "reset halt" \-c "flash erase_sector 0 0 last" \-c "reset run" \-c shutdown
读取 Flash 内容
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg \-c init \-c "reset halt" \-c "dump_image readback.bin 0x08000000 0x10000" \-c shutdown
七、查看帮助
# OpenOCD 命令行帮助
openocd --help# Telnet 连接后查看帮助
> help
> help flash
> help reset
八、常见问题排查
# 检查配置文件位置
ls /opt/homebrew/share/openocd/scripts/# 测试连接
openocd -f interface/stlink.cfg -f target/stm32f4x.cfg -c init -c "reset halt" -c shutdown# 详细日志
openocd -d3 -f interface/stlink.cfg -f target/stm32f4x.cfg