vscode搭建STM32开发环境的详细过程

目录需要安装的软件vscodemakeopenocdarm-none-eabistm32CubeMX配置开发环境配置编译下载功能配置调试功能

需要安装的软件

vscode

必装插件:

C/C++:用于提供高亮显示和代码补全 Cortex-Debug:用于提供调试配置

make

make工具可以直接下载xPack项目提供的windows-build-tools工具里面带了make工具。

Release xPack Windows Build Tools v4.2.1-2 · xpack-dev-tools/windows-build-tools-xpack (github.com)

openocd

arm-none-eabi

stm32CubeMX

上述软件具体的安装教程网上有很多详细的介绍资料,这里就不详细介绍了。需要注意的是记得将make,openocd,arm-none-eabi等可执行程序的路径添加到环境变量中

以下章节的内容都是根据stm32CubeMX生成的vscode_stm32f411 Makefile工程为例子来进行讲解的。

配置开发环境

实际上就是打造代码的编辑环境,实现类似于keil中编辑代码和代码补全等功能。在通过vscode打开vscode_stm32f411文件夹后,其实已经具备了编辑和代码补全功能(前提是必装的插件已经安装好),只是会有很多报错的波浪线,这时候便需配置c_cpp_properties.json文件来解决源文件的各种报错提示:

如果提示**uint32_t是未定义的类型**在defines下添加__GNUC__

c_cpp_properties.json文件:

{    "configurations": [        {            "name": "Win32",            "includePath": [                "${workspaceFolder}/**"            ],            "defines": [                "_DEBUG",                "UNICODE",                "_UNICODE",                "USE_HAL_DRIVER", //                "STM32F411xE", //                "__GNUC__" //            ],            // "compilerPath": "C:\\Program Files\\LLVM\\bin\\clang.exe",            "compilerPath": "C:/Program Files (x86)/GNU Tools Arm Embedded/7 2018-q2-update/bin/arm-none-eabi-gcc.exe",            "cStandard": "c17",            "cppStandard": "c++14",            // "intelliSenseMode": "windows-clang-x64"            "intelliSenseMode": "gcc-arm"        }    ],    "version": 4}

配置编译下载功能

新建task.json文件

{    // See https://go.microsoft.com/fwlink/?LinkId=733558    // for the documentation about the tasks.json format    "version": "2.0.0",    "tasks": [        {            "label": "build",            "type": "shell",            "command": "make",            "args": [              "-j4"            ],            "group": { //group用于将当前任务设置为默认的build任务,可以直接通过Ctrl+Shift+B直接执行              "kind": "build",              "isDefault": true            },            "problemMatcher":[              "$gcc"            ]        },        {          "label": "clean",          "type": "shell",          "command": "make",          "args": [            "clean"          ]        },        {            "label": "flash - ST-Link", //用于执行makefile文件中实现的下载指令            "type": "shell",            "command": "make flash",            "problemMatcher": []        },        {          "label": "download", //下载并运行          "type": "shell",          "command": "openocd",          "args": [            "-f",            "interface/stlink-v2.cfg",            "-f",            "target/stm32f4x.cfg",            "-c",            "program build/vscode_stm32f411.elf verify reset exit" //TODO:这里的下载文件的路径不能够用${workspaceFolder}来指定          ],          "dependsOn":"build", //download任务的依赖任务,即download任务执行前会先执行build任务        },        {          "label": "reset", //复位程序          "type": "shell",          "command":"openocd",          "args": [            "-f",            "interface/stlink-v2.cfg",            "-f",            "target/stm32f4x.cfg",            "-c init",            "-c reset",            "-c exit",          ],          "problemMatcher": []        },        {          "label": "halt", //挂起程序          "type": "shell",          "command":"openocd",          "args": [            "-f",            "interface/stlink-v2.cfg",            "-f",            "target/stm32f4x.cfg",            "-c init",            "-c halt",            "-c exit",          ],          "problemMatcher": []        },        {          "label": "run", //运行程序          "type": "shell",          "command":"openocd",          "args": [            "-f",            "interface/stlink-v2.cfg",            "-f",            "target/stm32f4x.cfg",            "-c init",            "-c resume",            "-c exit",          ],          "problemMatcher": []        },    ]}

build任务用于编译工程(实质上是执行makefile文件 make)

clean任务用于清除编译生成的中间文件(实质是执行makefile文件中的 make clean)

flash - ST-Link任务用于下载代码到STM32芯片中,这里需要在makefile中添加flash伪目标,伪目标flash实现如下:

#flash the stm32OPENOCD := openocd -f interface/stlink-v2.cfg -f target/stm32f4x_stlink.cfgFIRMWARE = $(BUILD_DIR)/vscode_stm32f411.elfflash:$(OPENOCD) -c init \-c 'reset halt' \-c 'flash write_image erase $(FIRMWARE)' \-c 'reset run' \-c exit

download任务用于下载代码到STM32芯片中,这里是完全在tasks.json文件中实现的(通过openocd实现下载目标文件)

reset任务用于复位目标板程序

halt任务用于挂起目标板程序

run任务用于运行挂起的目标程序

配置调试功能

添加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": [          {            "name": "Cortex Debug",            "cwd": "${workspaceRoot}",            "executable": "${workspaceRoot}/build/vscode_stm32f411.elf",            "request": "launch",            "type": "cortex-debug",            "servertype": "openocd",            "device": "STM32F411xx",            "interface": "swd",            "configFiles": [                "${workspaceRoot}/openocd.cfg"            ],            "runToMain": true,            "showDevDebugTimestamps": true,            "svdFile": "${workspaceRoot}/STM32F411xx.svd", //需要查看外设寄存器的值必须指定该svd文件        }    ]}

工作空间目录下添加openocd.cfg文件,文件内容如下:

source [find interface/stlink-v2.cfg]source [find target/stm32f4x_stlink.cfg]

到此出已经可以执行F5经行调试了。

注意:这里必须执行make指令后才能进行调试,否则不能够正常调试

​ 为了确保每次执行调试时工程都是最新编译过的,可以在launch.json文件中添加"preLaunchTask": "build"的配置。preLaunchTask表示调试前执行的任务,build是指task.json文件中标签为build的任务(注意launch.json文件中的任务名字必须和task.json文件中的标签名一致)。

​ 为了确保每次调试结束后目标板上的程序还能继续运行,可以在launch.json文件中添加"postDebugTask": "run"的配置,这样可以在调试结束后执行task.json文件中的run任务以确保目标板上的程序还能继续运行。

完整的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": [          {            "name": "Cortex Debug",            "cwd": "${workspaceRoot}",            "executable": "${workspaceRoot}/build/vscode_stm32f411.elf",            "request": "launch",            "type": "cortex-debug",            "servertype": "openocd", //要选择的GDB server            // "device": "STM32F411xx", //            "interface": "swd",            "configFiles": [                // "${workspaceRoot}/openocd.cfg"                "interface/stlink-v2.cfg",                "target/stm32f4x.cfg"            ],            "runToMain": true,            "showDevDebugTimestamps": true,            "svdFile": "${workspaceRoot}/STM32F411xx.svd",            "preLaunchTask": "build", //调试之前运行的任务(调试之前一定要确保工程被编译过)            "postDebugTask": "run", //调试结束后运行程序,没有的化会导致程序调试结束后处于挂起状态        }    ]}

细心的同学可能会注意到,这里的launch.json文件和上面的该文件在configFiles位置处也有一些区别:

采用这里的这种写法可以不用在工作文件夹目录下新建openocd.cfg文件,不过这种方式在命令行中直接输入openocd便会报错。

小知识点:在终端中启动openocd时,会自动在当前目录下寻找openocd.cfg的文件作为配置文件

到此这篇关于vscode搭建STM32开发环境的详细过程的文章就介绍到这了,更多相关vscode搭建STM32开发环境内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

代替你主持夕阳的葬礼。

vscode搭建STM32开发环境的详细过程

相关文章:

你感兴趣的文章:

标签云: