概述
esp32有些情况下只是看打印信息无法快速定位问题,于是需要调试仿真,本文章使用esp32s3通过vscode实现单步调试,因为esp32-s3系列可以直接通过芯片的usb接口直接进行调试,可以不需要外部的调试器。
环境搭建
- 首先使用vscode创建一个esp32 idf工程,本文使用esp32例程。
- 修改.vscode/launch.json内容文件,文件内容参考esp32官方文档,笔者写这篇文章的时候,ESP-IDF编程指南中给出的github主分支已经失效(https://github.com/espressif/vscode-esp-idf-extension/blob/master/docs/DEBUGGING.md),于是参考其他分支文档(https://github.com/espressif/vscode-esp-idf-extension/blob/unit-tests-test-coverage/docs/DEBUGGING.md)
- 快速定位到上述文档中的 Use Microsoft C/C++ extension to debug,并将内容复制到.vscode/launch.json 中,并删除原有内容
{
"version": "0.2.0",
"configurations": [
{
"name": "GDB",
"type": "cppdbg",
"request": "launch",
"MIMode": "gdb",
"miDebuggerPath": "${command:espIdf.getXtensaGdb}",
"program": "${workspaceFolder}/build/${command:espIdf.getProjectName}.elf",
"windows": {
"program": "${workspaceFolder}\\build\\${command:espIdf.getProjectName}.elf"
},
"cwd": "${workspaceFolder}",
"environment": [{ "name": "PATH", "value": "${config:idf.customExtraPaths}" }],
"setupCommands": [
{ "text": "target remote :3333" },
{ "text": "set remote hardware-watchpoint-limit 2"},
{ "text": "mon reset halt" },
{ "text": "thb app_main" },
{ "text": "flushregs" }
],
"externalConsole": false,
"logging": {
"engineLogging": true
}
}
]
}
4. 芯片选择esp32s3,并设置idf.openOcdConfigs
选择esp32s3
设置idf.openOcdConfigs
idf.openOcdConfigs 也可以手动修改,打开.vscode/settings.json,定位到 “idf.openOcdConfigs”,将其内容修改为 “board/esp32s3-builtin.cfg”
"board/esp32s3-builtin.cfg",
5. 编译工程代码并通过串口下载程序,将esp32串口接入电脑,编译、下载程序并打开监控器
6. 程序下载完后,将esp32 USB接入电脑,并选择对应的COM口,并启动GBD调试,如果出现以下弹窗,需要修改.vscode/launch.json内容
修改点如下
"miDebuggerPath": "${command:espIdf.getToolchainGdb}",
7. 修改.vscode/launch.json文件后再次仿真出现这个弹框,OpenOCD Server 没有开启导致
8. 打开 OpenOCD Server 并再次仿真,可以看到程序停留在断点处
注意事项
- 在仿真过程中,如果代码有改动需要编译下载后再进行仿真
- 如果jtag下载不稳定,可以使用串口下载程序,然后再使用usb jtag进行仿真调试
- 使用usb jtag仿真调试没有串口打印那么流畅