在VSCode中配置ESP-IDF开发环境

第一步:安装 VSCode  C/C++ 扩展
    1.在应用商店里搜索 C++  
    2.安装C/C++ IntelliSense, debugging, and code browsing
第二步:安装 VSCode ESP-IDF 扩展
    1.在应用商店里搜索 Espressif
    2.安装 Develop and debug applications for Espressif ESP32, ESP32-S2 chips with ESP-IDF (带乐鑫图标)
第三步:配置ESP-IDF 扩展
    1.按 F1 或Ctrl+Shift+P 打开命令面板
    2.输入 ESP-IDF: Configure ESP-IDF extension
      加载初始设置时间较长,耐心等待
    3.根据实际情况选择不同的配置方式
      Express: IDF 版本 配置Python环境,扩展将安装IDF
      Advanced: 手动配置已安装的开发环境
      Using Existing Setup : 使用扩展检测出的环境
    配置完成后显示:All settings have been configured. You can close this window.
第四步:创建项目模板,检测设置
    1.按 F1 或Ctrl+Shift+P 打开命令面板 输入ESP-IDF:Create project 或按Ctrl+E+C
    2.选择工程创建目录
    3.选择模板类型
第五部:设置工作区的json文件
    在settings.json文件中添加以下内容
    "terminal.integrated.shell.windows": "cmd.exe",
    "terminal.integrated.shellArgs.windows": ["/k", "c:\\esp\\esp-idf\\export.bat"],
    "terminal.integrated.shell.linux": "/bin/bash",
    "terminal.integrated.shellArgs.linux": ["--init-file", "~/esp/esp-idf/export.sh", "-i"],
    "terminal.integrated.shell.osx": "/bin/bash",
    "terminal.integrated.shellArgs.osx": ["--init-file", "~/esp/esp-idf/export.sh", "-i"],
    "files.associations": {
    "*.md": "markdown",
    "*.mdx": "tdx",
    "stdio.h": "c"
  },
  可以在VSCode 终端中正常使用idf的各种命令。
第六步:编译工程
    可以使用ESP扩展下的各个按钮完成项目的串口选择、项目配置、Full Clearn、编译、下载、监视
    也可以使用命令行方式:
    1.按Ctrl+` (~按键的位置)打开终端(第一次运行时扩展会有提示,选择允许 其实质就是运行~/esp/esp-idf/export.sh)
    2.选择终止终端
    3.重新打开终端 会看到export.sh运行的结果
    Go to the project directory and run:
              idf.py build
    4.运行各种idf命令
第七部:下载程序并监测程序运行
    1. 按Select Device Port 按钮 或运行 ESP-IDF:Device configuration命令
    按提示选择/dev/ttyUSB1作为下载口
      2.编译完成后,可使用下载按钮进行程序下载。此时会出现提示:
    PermissionError: [Errno 13] Permission denied: '/dev/ttyUSB1'
    原因:
    $ ls -l /dev/ttyUSB*
    crw-rw---- 1 root dialout 188, 0 2月   3 11:21 /dev/ttyUSB0
    crw-rw---- 1 root dialout 188, 1 2月   3 11:21 /dev/ttyUSB1
    发现ttyUSB* 设备属于root用户 dialout 用户组,当前用户不属于dialout用户组
    解决方案:
      (1).执行以下命令
      $sudo chmod 666 /dev/ttyUSB*
      修改ttyUSB设备权限为其它用户可读可写。
      缺点:电脑重启后,又会出现这种问题,还要重新设置
      (2).为了能让普通用户也能使用串口,可以增加udev规则来实现
      $sudo vim /etc/udev/rules.d/70-ttyusb.rules
      增加如下内容:
      KERNEL=="ttyUSB[0-9]*",MODE="0666"
      保存,重新插入USB转串口,普通用户就能搞定了
      缺点:该方法会让所有的普通用户都具有访问这些串口设备的权限,存在一定的安全隐患
      (3).将目标用户加入dialout用户组,可以使用以下任意一个命令
      $sudo usermod -aG dialout <目标用户名>
      或:
      sudo gpasswd --add <目标用户名> dialout
      重启系统即可

第八部:跟踪调试程序
    1.按 OpenOCD Server 按钮 输出提示:
    ❌ Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
    ❌ Error: no device foun
    按照 https://sourceforge.net/p/openocd/code/ci/master/tree/README 文档解释做如下操作:
      (1). 将~/.espressif/tools/openocd-esp32/v0.10.0-esp32-20200709/openocd-esp32/share/openocd/contrib/
      目录下的 60-openocd.rules 拷贝至  /etc/udev/rules.d/ 目录下
      (2).确保当前用户属于 plugdev 用户组。 提示:运行 groups 命令可以查看当前用户所属的用户组
      (3).重启系统
    2.配置Debug 环境
    参考:https://github.com/espressif/vscode-esp-idf-extension/blob/master/docs/DEBUGGING.md
        注:该文档中的模板有坑。
    问题:
    使用 ESP-IDF Debug Adapter 配置时出现如下提示:
    pygdbmi.gdbcontroller.NoGdbProcessError: gdb process has already finished with return code: 127
    按照 esp_debug_adapter 说明文档 在~/.vscode/extensions/espressif.esp-idf-extension-0.6.1/esp_debug_adapter/ 目录下
    $pip install -r requirements.txt
    问题依然存在 暂无解决思路

    使用 Microsoft C/C++ extension to debug 配置时出现如下提示:
     error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
    实质是系统中没有python2.7的库,解决:
    $ sudo apt update
    $ sudo apt upgrade
    $ sudo apt install libpython2.7

    问题解决
    同时也解决了使用 ESP-IDF Debug Adapter 配置时出现的问题 故是否需要pip install …… 不能确定

    在使用 Microsoft C/C++ extension to debug 配置时 会提示出现异常,不用理会可正常跟踪调试。
    
    有时会提示
    Error: couldn’t bind tcl to socket: Address already in use
    则证明 刚刚启动的 进程未被终止。
    解决办法:
    a).查看当前活动进程
      netstat为显示网络相关信息 a(all:默认显示所有,如果加了其他选项此项不生效) n(number:以数字形式显示) t(仅仅显示tcp连接),p(process:显示该项是由哪个程序建立起来的)
      $ sudo netstat -antp
    b). 强制杀死它(假设进程号为3560,-9为强制杀死)
      $ sudo kill -9 3560
  Debug正常运行时,状态栏由蓝色变为棕色。  

  附:scode的各个json文件
  c_cpp_properties.json
  ======================================
  {
  "configurations": [
    {
      "name": "ESP-IDF",
      "compilerPath": "${default}",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "includePath": [
        "${config:idf.espIdfPath}/components/**",
        "${config:idf.espIdfPathWin}/components/**",
        "${workspaceFolder}/**"
      ],
      "browse": {
        "path": [
          "${config:idf.espIdfPath}/components",
          "${config:idf.espIdfPathWin}/components",
          "${workspaceFolder}"
        ],
        "limitSymbolsToIncludedHeaders": false
      }
    }
  ],
  "version": 4
}

settings.json
  ======================================
{
  "terminal.integrated.shell.windows": "cmd.exe",
  "terminal.integrated.shellArgs.windows": ["/k", "c:\\esp\\esp-idf\\export.bat"],
  "terminal.integrated.shell.linux": "/bin/bash",
  "terminal.integrated.shellArgs.linux": ["--init-file", "~/esp/esp-idf/export.sh", "-i"],
  "terminal.integrated.shell.osx": "/bin/bash",
  "terminal.integrated.shellArgs.osx": ["--init-file", "~/esp/esp-idf/export.sh", "-i"],
  "files.associations": {
    "*.md": "markdown",
    "*.mdx": "tdx",
    "stdio.h": "c"
  },
  "C_Cpp.clang_format_style": "Visual Studio",
  "editor.formatOnSave": false,
  "[cpp]": {
    "editor.quickSuggestions": true
  },
  "[c]": {
    "editor.quickSuggestions": true
  },
  "C_Cpp.intelliSenseEngine": "Tag Parser",
  //配置下载接口
  "idf.port": "/dev/ttyUSB1",
  //配置下载方式
  "idf.flashType": "UART",
  //openOcd配置,根据开发板确定
  "idf.openOcdConfigs": [
    //新版建议用“board/XXX” 配置
    "interface/ftdi/esp32_devkitj_v1.cfg",
    "target/esp32.cfg"
  ]
}

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": "ESP_Debug",
            "request": "launch",
            "debugPort": 43474,
            "logLevel": 2,
            //模板中有坑的地方,模板内容为 "mode": "manual",
            //这样不能自动启动 Debug Adapter
            "mode": "auto",
            "initGdbCommands": [
                "target remote :3333",
                "symbol-file ${workspaceFolder}/build/${command:espIdf.getProjectName}.elf",
                "set remote hardware-watchpoint-limit 2",
                "mon reset halt",
                "flushregs",
                "thb app_main",
                "c"
            ],
            "env": {
                "PATH": "${env:PATH}:${config:idf.customExtraPaths}"
            }
        },
        {
            "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": "-enable-pretty-printing",
                  "ignoreFailures": true },
                { "text": "file '${workspaceFolder}/build/${command:espIdf.getProjectName}.elf'"},
                { "text": "target remote :3333" },
                { "text": "set remote hardware-watchpoint-limit 2"},
                { "text": "mon reset halt" },
                { "text": "thb app_main" },
                { "text": "flushregs" }
                //{ "text": "c"}
            ],
            "externalConsole": false,
            "logging": {
                "engineLogging": true
            }
        }
    ]
}

 

tasks.json 这个文用系统生成的即可 略
  ======================================

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值