环境

版本

ESP-IDF:v4.2
Espressif IDF:v1.0.2
Python:v3.8.7

路径

下载工具包 esp-idf-tools-setup-offline-2.5.exe 安装
esp-idf路径

C:\Users\yjw\esp\esp-idf

.espressif路径

C:\Users\yjw\.espressif

Espressif IDF扩展设置:

Idf: Custom Extra Paths
导出到系统 PATH 环境变量的路径

C:\Users\yjw\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin;C:\Users\yjw\.espressif\tools\xtensa-esp32s2-elf\esp-2020r3-8.4.0\xtensa-esp32s2-elf\bin;C:\Users\yjw\.espressif\tools\esp32ulp-elf\2.28.51-esp-20191205\esp32ulp-elf-binutils\bin;C:\Users\yjw\.espressif\tools\esp32s2ulp-elf\2.28.51-esp-20191205\esp32s2ulp-elf-binutils\bin;C:\Users\yjw\.espressif\tools\cmake\3.16.4\bin;C:\Users\yjw\.espressif\tools\openocd-esp32\v0.10.0-esp32-20200709\openocd-esp32\bin;C:\Users\yjw\.espressif\tools\ninja\1.10.0;C:\Users\yjw\.espressif\tools\idf-exe\1.0.1;C:\Users\yjw\.espressif\tools\ccache\3.7;C:\Users\yjw\.espressif\tools\dfu-util\0.9\dfu-util-0.9-win64

Idf: Custom Extra Vars
导出到系统中的环境变量

{"OPENOCD_SCRIPTS":"C:\\Users\\yjw\\.espressif\\tools\\openocd-esp32\\v0.10.0-esp32-20200709/openocd-esp32/share/openocd/scripts","IDF_CCACHE_ENABLE":"1"}

Idf: Esp Idf Path Win
IDF_PATH 的路径

C:\Users\yjw\esp\esp-idf

Idf: Python Bin Path Win
项目构建使用的 Python 解释器的绝对路径

C:\Users\yjw\.espressif\python_env\idf4.2_py3.8_env\Scripts\python.exe

完整settings.json

{
    "idf.espIdfPathWin": "C:\\Users\\yjw\\esp\\esp-idf",
    "idf.pythonBinPathWin": "C:\\Users\\yjw\\.espressif\\python_env\\idf4.2_py3.8_env\\Scripts\\python.exe",
    "idf.customExtraPaths": "C:\\Users\\yjw\\.espressif\\tools\\xtensa-esp32-elf\\esp-2020r3-8.4.0\\xtensa-esp32-elf\\bin;C:\\Users\\yjw\\.espressif\\tools\\xtensa-esp32s2-elf\\esp-2020r3-8.4.0\\xtensa-esp32s2-elf\\bin;C:\\Users\\yjw\\.espressif\\tools\\esp32ulp-elf\\2.28.51-esp-20191205\\esp32ulp-elf-binutils\\bin;C:\\Users\\yjw\\.espressif\\tools\\esp32s2ulp-elf\\2.28.51-esp-20191205\\esp32s2ulp-elf-binutils\\bin;C:\\Users\\yjw\\.espressif\\tools\\cmake\\3.16.4\\bin;C:\\Users\\yjw\\.espressif\\tools\\openocd-esp32\\v0.10.0-esp32-20200709\\openocd-esp32\\bin;C:\\Users\\yjw\\.espressif\\tools\\ninja\\1.10.0;C:\\Users\\yjw\\.espressif\\tools\\idf-exe\\1.0.1;C:\\Users\\yjw\\.espressif\\tools\\ccache\\3.7;C:\\Users\\yjw\\.espressif\\tools\\dfu-util\\0.9\\dfu-util-0.9-win64",
    "idf.customExtraVars": "{\"OPENOCD_SCRIPTS\":\"C:\\\\Users\\\\yjw\\\\.espressif\\\\tools\\\\openocd-esp32\\\\v0.10.0-esp32-20200709/openocd-esp32/share/openocd/scripts\",\"IDF_CCACHE_ENABLE\":\"1\"}",
    "idf.openOcdConfigs": [
        "interface/ftdi/esp32_devkitj_v1.cfg",
        "board/esp32-wrover.cfg"
    ]
}

openOCD配置

插件的环境配置可以看到openOCD的配置使用的是esp32_devkitj_v1.cfgesp32-wrover.cfg,接下来根据自己的情况做简单更改。
打开路径C:\Users\yjw\.espressif\tools\openocd-esp32\v0.10.0-esp32-20200709\openocd-esp32\share\openocd\scripts里面有很多预设,根据自己需要选择,我这使用esp32-wrover-kit-3.3v.cfg
打开工程的settings.json文件,在.vscode里面,添加如下:

{
  "C_Cpp.intelliSenseEngine": "Tag Parser",
  "idf.portWin": "COM5",
  "idf.flashType": "UART",
  "idf.openOcdConfigs": [
    "board/esp32-wrover-kit-3.3v.cfg"
  ]
}

然后点击底部OpenOCD Server(stopped)启动OpenOCD,正常启动内容如下:

Open On-Chip Debugger  v0.10.0-esp32-20200709 (2020-07-09-08:54)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
Info : Configured 2 cores
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : ftdi: if you experience problems at higher adapter clocks, try the command "ftdi_tdo_sample_edge falling"
Info : clock speed 20000 kHz
Info : JTAG tap: esp32.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : JTAG tap: esp32.cpu1 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1)
Info : Target halted. CPU0: PC=0x400D39EA (active)
Info : Target halted. CPU1: PC=0x400E253E 
Info : Listening on port 3333 for gdb connections

这样OpenOCD启动完毕,接下来配置gdb。

GDB配置

安装使用Native Debug扩展。
打开工程的launch.json文件:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "espidf",
            "name": "Launch",
            "request": "launch",
        }
    ]
}

更改配置:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "gdb",
            "request": "launch",
            "name": "ESP",
            "target": "./build/${workspaceFolderBasename}.elf",
            "cwd": "${workspaceFolder}",
            "gdbpath": "C:/Users/yjw/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gdb",
            "autorun": 
            [
                "target remote :3333",
                "mon reset halt",
                "flushregs",
                "thb app_main",
                "c"
            ],
        }
    ]
}

之后按F5开始调试。到此结束撒花~~


以下为扩展:


环境变量重要参数

IDF_PATH路径:

C:\Users\yjw\esp\esp-idf

OPENOCD_SCRIPTS路径

C:\Users\yjw\.espressif\tools\openocd-esp32\v0.10.0-esp32-20200709\openocd-esp32\share\openocd\scripts

IDF_CCACHE_ENABLE的值:1
IDF_PYTHON_ENV_PATH的路径:

C:\Users\yjw\.espressif\python_env\idf4.2_py3.8_env

添加到PATH的环境变量:

C:\Users\yjw\esp\esp-idf\components\esptool_py\esptool
C:\Users\yjw\esp\esp-idf\components\app_update
C:\Users\yjw\esp\esp-idf\components\espcoredump
C:\Users\yjw\esp\esp-idf\components\partition_table
C:\Users\yjw\.espressif\tools\xtensa-esp32-elf\esp-2020r3-8.4.0\xtensa-esp32-elf\bin
C:\Users\yjw\.espressif\tools\xtensa-esp32s2-elf\esp-2020r3-8.4.0\xtensa-esp32s2-elf\bin
C:\Users\yjw\.espressif\tools\esp32ulp-elf\2.28.51-esp-20191205\esp32ulp-elf-binutils\bin
C:\Users\yjw\.espressif\tools\esp32s2ulp-elf\2.28.51-esp-20191205\esp32s2ulp-elf-binutils\bin
C:\Users\yjw\.espressif\tools\cmake\3.16.4\bin
C:\Users\yjw\.espressif\tools\openocd-esp32\v0.10.0-esp32-20200709\openocd-esp32\bin
C:\Users\yjw\.espressif\tools\ninja\1.10.0\
C:\Users\yjw\.espressif\tools\idf-exe\1.0.1\
C:\Users\yjw\.espressif\tools\ccache\3.7\
C:\Users\yjw\.espressif\tools\dfu-util\0.9\dfu-util-0.9-win64
C:\Users\yjw\.espressif\python_env\idf4.2_py3.8_env\Scripts
C:\Users\yjw\esp\esp-idf\tools

.vscode 默认

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "ESP-IDF",
            "compilerPath": "C:\\Users\\yjw\\.espressif\\tools\\xtensa-esp32-elf\\esp-2020r3-8.4.0\\xtensa-esp32-elf\\bin\\xtensa-esp32-elf-gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "includePath": [
                "${config:idf.espIdfPath}/components/**",
                "${config:idf.espIdfPathWin}/components/**",
                "${config:idf.espAdfPath}/components/**",
                "${config:idf.espAdfPathWin}/components/**",
                "${workspaceFolder}/**"
            ],
            "browse": {
                "path": [
                    "${config:idf.espIdfPath}/components",
                    "${config:idf.espIdfPathWin}/components",
                    "${config:idf.espAdfPath}/components/**",
                    "${config:idf.espAdfPathWin}/components/**",
                    "${workspaceFolder}"
                ],
                "limitSymbolsToIncludedHeaders": false
            }
        }
    ],
    "version": 4
}

launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "espidf",
            "name": "Launch",
            "request": "launch",
        }
    ]
}

settings.json

{
  "C_Cpp.intelliSenseEngine": "Tag Parser"
}

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build - Build project",
            "type": "shell",
            "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py build",
            "windows": {
                "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py build",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}"
                    }
                }
            },
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
                }
            },
            "problemMatcher": [
                {
                    "owner": "cpp",
                    "fileLocation": [
                        "relative",
                        "${workspaceFolder}"
                    ],
                    "pattern": {
                        "regexp": "^\\.\\.(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                },
                {
                    "owner": "cpp",
                    "fileLocation": "absolute",
                    "pattern": {
                        "regexp": "^[^\\.](.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Set ESP-IDF Target",
            "type": "shell",
            "command": "${command:espIdf.setTarget}",
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.*):(//d+):(//d+)://s+(warning|error)://s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "label": "Clean - Clean the project",
            "type": "shell",
            "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py fullclean",
            "windows": {
                "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py fullclean",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}"
                    }
                }
            },
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
                }
            },
            "problemMatcher": [
                {
                    "owner": "cpp",
                    "fileLocation": [
                        "relative",
                        "${workspaceFolder}"
                    ],
                    "pattern": {
                        "regexp": "^\\.\\.(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                },
                {
                    "owner": "cpp",
                    "fileLocation": "absolute",
                    "pattern": {
                        "regexp": "^[^\\.](.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            ]
        },
        {
            "label": "Flash - Flash the device",
            "type": "shell",
            "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py -p ${config:idf.port} -b ${config:idf.flashBaudRate} flash",
            "windows": {
                "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py flash -p ${config:idf.portWin} -b ${config:idf.flashBaudRate}",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}"
                    }
                }
            },
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
                }
            },
            "problemMatcher": [
                {
                    "owner": "cpp",
                    "fileLocation": [
                        "relative",
                        "${workspaceFolder}"
                    ],
                    "pattern": {
                        "regexp": "^\\.\\.(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                },
                {
                    "owner": "cpp",
                    "fileLocation": "absolute",
                    "pattern": {
                        "regexp": "^[^\\.](.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            ]
        },
        {
            "label": "Monitor: Start the monitor",
            "type": "shell",
            "command": "${config:idf.pythonBinPath} ${config:idf.espIdfPath}/tools/idf.py -p ${config:idf.port} monitor",
            "windows": {
                "command": "${config:idf.pythonBinPathWin} ${config:idf.espIdfPathWin}\\tools\\idf.py -p ${config:idf.portWin} monitor",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}"
                    }
                }
            },
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
                }
            },
            "problemMatcher": [
                {
                    "owner": "cpp",
                    "fileLocation": [
                        "relative",
                        "${workspaceFolder}"
                    ],
                    "pattern": {
                        "regexp": "^\\.\\.(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                },
                {
                    "owner": "cpp",
                    "fileLocation": "absolute",
                    "pattern": {
                        "regexp": "^[^\\.](.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                        "file": 1,
                        "line": 2,
                        "column": 3,
                        "severity": 4,
                        "message": 5
                    }
                }
            ],
            "dependsOn": "Flash - Flash the device"
        },
        {
            "label": "OpenOCD: Start openOCD",
            "type": "shell",
            "presentation": {
                "echo": true,
                "reveal": "never",
                "focus": false,
                "panel": "new"
            },
            "command": "openocd -s ${command:espIdf.getOpenOcdScriptValue} ${command:espIdf.getOpenOcdConfigs}",
            "windows": {
                "command": "openocd.exe -s ${command:espIdf.getOpenOcdScriptValue} ${command:espIdf.getOpenOcdConfigs}",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}"
                    }
                }
            },
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
                }
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": "absolute",
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        },
        {
            "label": "adapter",
            "type": "shell",
            "command": "${config:idf.pythonBinPath}",
            "isBackground": true,
            "options": {
                "env": {
                    "PATH": "${env:PATH}:${config:idf.customExtraPaths}",
                    "PYTHONPATH": "${command:espIdf.getExtensionPath}/esp_debug_adapter/debug_adapter"
                }
            },
            "problemMatcher": {
                "background": {
                    "beginsPattern": "\bDEBUG_ADAPTER_STARTED\b",
                    "endsPattern": "DEBUG_ADAPTER_READY2CONNECT",
                    "activeOnStart": true
                },
                "pattern": {
                    "regexp": "(\\d+)-(\\d+)-(\\d+)\\s(\\d+):(\\d+):(\\d+),(\\d+)\\s-(.+)\\s(ERROR)",
                    "file": 8,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 9
                }
            },
            "args": [
                "${command:espIdf.getExtensionPath}/esp_debug_adapter/debug_adapter_main.py",
                "-e",
                "${workspaceFolder}/build/${command:espIdf.getProjectName}.elf",
                "-s",
                "${command:espIdf.getOpenOcdScriptValue}",
                "-ip",
                "localhost",
                "-dn",
                "${config:idf.adapterTargetName}",
                "-om",
                "connect_to_instance"
            ],
            "windows": {
                "command": "${config:idf.pythonBinPathWin}",
                "options": {
                    "env": {
                        "PATH": "${env:PATH};${config:idf.customExtraPaths}",
                        "PYTHONPATH": "${command:espIdf.getExtensionPath}/esp_debug_adapter/debug_adapter"
                    }
                }
            }
        }
    ]
}
最后修改:2021 年 07 月 08 日
男宾一位~ 欢迎下次再来!