You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WSL2下VS Code用pyocd-gdbserver调试nRF52时退出码1问题咨询

pyocd完全可以替代Segger JLink GDB Server调试nRF52平台,它原生支持JLink调试器,只要配置正确就能实现烧录、调试等完整功能。

解决pyocd-gdbserver退出码1的步骤

1. 先获取详细错误信息

VS Code的日志仅显示退出码,无法定位具体问题,先在WSL2终端手动运行pyocd-gdbserver:

pyocd-gdbserver --target <你的nRF52芯片型号>  # 例如nrf52840、nrf52832

根据终端输出的错误提示针对性解决,常见问题包括USB权限不足、设备未被WSL2识别、缺少目标芯片支持包等。

2. 确保WSL2能访问JLink调试器

nRF52的JLink是USB设备,WSL2需要手动挂载才能访问:

  • Windows侧安装usbipd-win:
    winget install usbipd-win
    
  • 列出USB设备,找到JLink的总线ID:
    usbipd list
    
  • 绑定JLink设备:
    usbipd bind --busid <总线ID>
    
  • WSL2中挂载设备:
    sudo usbip attach --remote=localhost --busid <总线ID>
    
  • 验证设备是否被识别:
    lsusb
    
    应该能看到Segger的设备(Vendor ID: 1366,Product ID: 0101)。

3. 修复pyocd的权限或依赖问题

  • 权限问题:如果终端运行pyocd需要sudo才能启动,配置udev规则赋予普通用户权限:

    1. 创建规则文件:
      sudo nano /etc/udev/rules.d/99-jlink.rules
      
    2. 写入以下内容:
      SUBSYSTEM=="usb", ATTR{idVendor}=="1366", ATTR{idProduct}=="0101", MODE="0666", GROUP="plugdev"
      
    3. 重新加载规则并更新设备权限:
      sudo udevadm control --reload-rules && sudo udevadm trigger
      
    4. 将当前用户加入plugdev组:
      sudo usermod -aG plugdev $USER
      
    5. 重启WSL2后生效。
  • 缺少目标支持包:如果提示找不到目标芯片,安装Nordic的pyocd目标包:

    pip install pyocd-target-nordic
    

4. 调整VS Code的launch.json配置

修改现有配置,适配pyocd的参数和命令:

"configurations": [
    {
        "name": "C++ Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceRoot}/build/zephyr/zephyr.elf",
        "args": [],
        "stopAtEntry": true,
        "cwd": "${workspaceRoot}",
        "environment": [],
        "externalConsole": false,
        // 添加目标芯片参数,替换为你的nRF52型号
        "debugServerArgs": "--target nrf52840", 
        "serverLaunchTimeout": 20000,
        "filterStderr": true,
        "filterStdout": false,
        // 匹配pyocd的启动成功提示
        "serverStarted": "GDB server started",
        "setupCommands": [
            { "text": "-target-select remote localhost:3333", "description": "connect to target", "ignoreFailures": false },
            { "text": "-file-exec-and-symbols ${workspaceRoot}/build/zephyr/zephyr.elf", "description": "load file", "ignoreFailures": false},
            { "text": "-interpreter-exec console \"monitor endian little\"", "ignoreFailures": false },
            { "text": "-interpreter-exec console \"monitor reset\"", "ignoreFailures": false },
            { "text": "-interpreter-exec console \"monitor halt\"", "ignoreFailures": false },
            // pyocd的半主机命令格式不同,去掉arm前缀
            { "text": "-interpreter-exec console \"monitor semihosting enable\"", "ignoreFailures": false },
            { "text": "-target-download", "description": "flash target", "ignoreFailures": false }
        ],
        "logging": {
            "moduleLoad": true,
            "trace": true,
            "engineLogging": true,
            "programOutput": true,
            "exceptions": true
        },
        "linux": {
            "MIMode": "gdb",
            "MIDebuggerPath": "/usr/bin/gdb",
            "debugServerPath": "pyocd-gdbserver"
        }
    }
]

关键修改点:

  • debugServerArgs添加--target指定芯片型号
  • 调整serverStarted的匹配字符串,适配pyocd的启动提示
  • 修改半主机启用命令,去掉arm前缀,适配pyocd的monitor命令格式

5. 重新测试调试会话

完成上述配置后,再次启动VS Code的调试会话,如果还是有问题,查看VS Code的调试控制台输出,或者再次手动运行pyocd-gdbserver确认是否能正常启动。

内容的提问来源于stack exchange,提问作者embdev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 04:11:14