stm32笔记[3]-OpenOCD调试
保命声明:笔者代码能力有限,若行文中有错漏之处欢迎大家指出。
openocd介绍
[https://openocd.org]
OpenOCD, the Open On-Chip Debugger (片上调试器).
macOS+STM32CubeIDE+openocd+cmsis-dap(dap-link)
[https://my.oschina.net/u/2963604/blog/4701978]
配置OpenOCD
[https://blog.csdn.net/lone5moon/article/details/124940297]
brew install openocd
或者[https://openocd.org]手动下载安装.
cfg目录在/usr/local/Cellar/open-ocd/0.11.0/share/openocd/scripts,
Using CMSIS-DAPv2 interface 1 with wrong class 10 subclass 0 or protocol 0
如果没有显式指定 CMSIS-DAP 版本的话会优先尝试 V2 接口,如果失败再尝试 V1 接口
[https://blog.csdn.net/luckly_wang/article/details/127364184]
修改cmsis-dap.cfg
#
# ARM CMSIS-DAP compliant adapter
#
# http://www.keil.com/support/man/docs/dapdebug/
#
adapter driver cmsis-dap
# 使用CMSIS_DAP V1接口
cmsis_dap_backend hid
# Optionally specify the serial number of CMSIS-DAP usb device.
#cmsis_dap_serial 02200201E6661E601B98E3B9
然后在STM32CubeIDE配置GDB调试模式,External Tools Configurations
等效为如下命令:
openocd -f /Users/workspace/Desktop/Blue_bridge_embeded/files/cmsis-dap.cfg -f /Users/workspace/Desktop/Blue_bridge_embeded/files/stm32g4x.cfg
配置arm-gcc-none-eabi
[https://developer.arm.com/downloads/-/gnu-rm]
下载[https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-mac.tar.bz2?rev=58ed196feb7b4ada8288ea521fa87ad5&hash=62C9BE56E5F15D7C2D98F48BFCF2E839D7933597]
解压,添加到环境变量:
export PATH=$PATH:/Users/workspace/Desktop/Blue_bridge_embeded/tools/gcc-arm-none-eabi-10.3-2021.10/bin
#测试
arm-none-eabi-gdb
#输入q退出
配置GDB调试:
在Debug处设置,别找错地方了
设置断点(可选):
开始调试
build代码
然后先启动openocd,
然后启动gdb调试,运行到断点(图片)
进阶:macOS+OpenOCD+arm-none-eabi-gdb进行调试
以调试模式重新编译
修改Debug/Makefile
添加-ggdb3以调试模式编译
# Tool invocations
proj_screen1.elf: $(OBJS) $(USER_OBJS) /Users/workspace/Desktop/Blue_bridge_embeded/proj_screen1/STM32G431RBTX_FLASH.ld makefile objects.list $(OPTIONAL_TOOL_DEPS)
arm-none-eabi-gcc -o "proj_screen1.elf" @"objects.list" $(USER_OBJS) $(LIBS) -ggdb3 -mcpu=cortex-m4 -T"/Users/workspace/Desktop/Blue_bridge_embeded/proj_screen1/STM32G431RBTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="proj_screen1.map" -Wl,--gc-sections -static --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
@echo 'Finished building target: $@'
@echo ' '
然后,
#使用CubeIDE自带的交叉编译器
export PATH=$PATH:/Applications/STM32CubeIDE.app/Contents/Eclipse/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.9-2020-q2-update.macos64_2.0.0.202105311346/tools/bin
cd /Users/workspace/Desktop/Blue_bridge_embeded/proj_screen1/Debug
#清理上次编译
make clean
#加了-ggdb3编译才能用gdb调试
#编译和链接
make -j3 all
启动OpenOCD
cd /Users/workspace/Desktop/Blue_bridge_embeded
openocd -f /Users/workspace/Desktop/Blue_bridge_embeded/files/cmsis-dap.cfg -f /Users/workspace/Desktop/Blue_bridge_embeded/files/stm32g4x.cfg
连接成功:
启动arm-none-eabi-gdb
gdbserver在目标系统中运行,gdb则在宿主机上运行。CMSIS-DAP自带gdbserver,bin文件不包含调试信息,elf文件包含调试信息。
#设置环境变量
export PATH=$PATH:/Users/workspace/Desktop/Blue_bridge_embeded/tools/gcc-arm-none-eabi-10.3-2021.10/bin
#进入GDB界面
arm-none-eabi-gdb /Users/workspace/Desktop/Blue_bridge_embeded/proj_screen1/Debug/proj_screen1.elf
#进入gdb命令行
target extended-remote localhost:3333
按一下开发板上的reset键,
gdb命令
[https://zhuanlan.zhihu.com/p/357360607]
[https://www.gnu.org/software/gdb/]
[https://sourceware.org/gdb/current/onlinedocs/gdb.pdf]
- l : 列出所有源代码
- break main :在main处打断点
- step : 单步执行
- quit : 退出
- run : 执行到第一个断点
- continue : 继续执行,直到下一个断点
- info : 获取信息
- dump : 复制内存到文件
调试
l 203
info line main.c:206
print TextColor
info address Main_loop
info symbol 0x8000e98