需要安装的软件

 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 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文件配置调试环境

{
    // 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开发环境内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!