当前位置:首页 > 通信资讯 > 正文

vscode开发stm32并支持c(vscode编写stm32)

目录
  • 需要安装的软件
    • vscode
    • make
    • openocd
    • arm-none-eabi
    • stm32CubeMX
  • 配置开发环境
    • 配置编译下载功能
      • 配置调试功能

        需要安装的软件

        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文件:

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 { "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文件

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 { // 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实现如下:

        ?
        1 2 3 4 5 6 7 8 9 10 #flash the stm32 OPENOCD := openocd -f interface/stlink-v2.cfg -f target/stm32f4x_stlink.cfg FIRMWARE = $(BUILD_DIR)/vscode_stm32f411.elf flash: $(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文件配置调试环境

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 { // 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文件,文件内容如下:

        ?
        1 2 3 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文件如下:

        ?
        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 { // 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开发环境内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

        原文链接:https://blog.csdn.net/xiaoyuanwuhui/article/details/116301416

        如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

        为您推荐:

        发表评论

        ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。