使用VSCode开发STM32:调试技巧实现

vscode开发STM32(三)—调试篇

文章目录

  • vscode开发STM32(三)—调试篇
  • 前提条件
  • 配置调试
  • 配置JLink使用`JLinkGDB`进行调试
  • 配置stlink使用openOCD进行调试
  • 完整的launch文件内容
  • 前提条件

    安装Cortex-Debug插件

    安装OpenOCD
    安装JLink驱动

    配置调试

    通过vscode左侧的调试和运行按钮选择生成launch文件

    配置JLink使用JLinkGDB进行调试

    将如下内容添加到launch文件合适的位置

    {
                "name": "Cortex Debug-jlink",
                "cwd": "${workspaceRoot}/",
                "executable": "${workspaceFolder}/build/${workspaceFolderBasename}.elf",
                "request": "launch",
                "type": "cortex-debug",
                "servertype": "jlink", //要选择的GDB server
                "device": "STM32F405RG",
                "interface": "swd",
                "runToEntryPoint": "main",
                "showDevDebugTimestamps": true,
                // "svdFile": "${workspaceRoot}/STM32F103.svd",
                // "preLaunchTask": "build",
                // "postDebugTask": "run"
            }
    

    修改该内容使之适配自己的工程:

    1. executable参数为要执行的目标文件
    2. devices为当前使用的mcu型号
    3. svdFile参数指定对应mcu的.svd文件位置,用于调试的时候查看寄存器的内容。
    4. preLaunchTask(调试前执行任务)和postDebugTask(调试结束后执行任务)根据需要添加,实际指的是task.json文件中定义好的任务。

    配置stlink使用openOCD进行调试

    将如下内容复制到对应的launch文件中

    {
                "name": "Cortex Debug-stlink",
                "cwd": "${workspaceRoot}/",
                "executable": "${workspaceFolder}/build/${workspaceFolderBasename}.elf",
                "request": "launch",
                "type": "cortex-debug",
                "servertype": "openocd", //要选择的GDB server
                "device": "STM32F405RG", //
                "interface": "swd",
                "configFiles": [
                    // "${workspaceRoot}/openocd.cfg"
                    "interface/stlink-v2.cfg",
                    "target/stm32f4x.cfg",
                ],
                "runToEntryPoint": "main",
                "showDevDebugTimestamps": true,
                // "svdFile": "${workspaceRoot}/STM32F103.svd",
                "preLaunchTask": "build",
                "postDebugTask": "run"
            }
    

    需要修改的位置和上面基本一致。

    完整的launch文件内容

    {
        // 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": [  
            {
                "name": "Cortex Debug-jlink",
                "cwd": "${workspaceRoot}/",
                "executable": "${workspaceFolder}/build/${workspaceFolderBasename}.elf",
                "request": "launch",
                "type": "cortex-debug",
                "servertype": "jlink", //要选择的GDB server
                "device": "STM32F405RG",
                "interface": "swd",
                "runToEntryPoint": "main",
                "showDevDebugTimestamps": true,
                // "svdFile": "${workspaceRoot}/STM32F103.svd",
                // "preLaunchTask": "build",
                // "postDebugTask": "run"
            },
            {
                "name": "Cortex Debug-stlink",
                "cwd": "${workspaceRoot}/",
                "executable": "${workspaceFolder}/build/${workspaceFolderBasename}.elf",
                "request": "launch",
                "type": "cortex-debug",
                "servertype": "openocd", //要选择的GDB server
                "device": "STM32F405RG", //
                "interface": "swd",
                "configFiles": [
                    // "${workspaceRoot}/openocd.cfg"
                    "interface/stlink-v2.cfg",
                    "target/stm32f4x.cfg",
                ],
                "runToEntryPoint": "main",
                "showDevDebugTimestamps": true,
                // "svdFile": "${workspaceRoot}/STM32F103.svd",
                "preLaunchTask": "build",
                "postDebugTask": "run"
            }
        ]
    }
    

    在调试时可以通过调试启动三角右侧的下拉框进行选择使用哪种方式进行调试。

    物联沃分享整理
    物联沃-IOTWORD物联网 » 使用VSCode开发STM32:调试技巧实现

    发表评论