VSCode+arm-none-eabi+openocd+msys实现STM32在线调试

VSCode是一个非常好用的工具,首先免费,个人和商用都免费。这就解决了很多问题,因为现在国内对版权越来越重视了。所以告别那些破解的keil等工具吧,涉及版权问题不说,还有可能有病毒,木马等风险。
现在来总结一下VSCode如何实现在线调试STM32代码的方法。先总结Windows平台,后面再来总结Mac OS平台。

##1、安装VSCode
这个不用说了
##2、安装windows-arm-none-eabi扩展
这是一个交叉编译工具链,使用这个工具将代码编译成可以在STM32上运行的文件。
直接在VSCode的Extension窗口中,搜索windows-arm-none-eabi,安装,截至我目前的情况是,需要安装0.1.2版本,0.1.6版本编译在最后copy的时候会出错。
方法是,先安装搜索到的版本,安装完成后,点设置按钮,选择 Install Another Version。就可以选择旧版本安装。
image.png
##3、安装openocd
https://gnutoolchains.com/arm-eabi/openocd/ 下载最新版本,解压备用。
##4、安装msys
msys是为了提供shell运行环境,避免Makefile不能执行。
msys是MinGW的一部分,先下载MinGW安装器,安装完成后运行。选择MSYS,然后选择右边最上面的msys-base-bin,选中后会自动选中其他一系列的package,然后应用修改。
image.png
安装完成后,会在C:\MinGW\msys\1.0\bin看到安装这些文件。主要看有没有rm,mkdir这些。
##5、配置环境变量
打开“高级系统设置”–“环境变量”,找到用户变量里的“path”,“编辑”,分别添加上面安装的三个工具的路径。
image.png
##6、添加VSCode调试配置文件
切换到调试窗口,添加launch.json配置文件。
image.png
选择C++(GDB/LLDB)
image.png
选择arm-none-eabi-gcc.exe build and debug active file
image.png
自动创建了一个launch.json文件,有一些默认的配置
image.png
修改部分参数

{
    // 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": "可以改成自己的项目名",
            "cwd": "${workspaceRoot}",
            "request": "launch", 
            "type": "cppdbg",
            "program": "${workspaceRoot}/build/xxx.elf",  // 改成自己的项目生成的elf文件路径
            "stopAtEntry": false,
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Users\\xxx\\.vscode\\extensions\\metalcode-eu.windows-arm-none-eabi-0.1.2\\bin\\arm-none-eabi-gdb.exe",   // 改成arm-none-eabi-gdb.exe的路径
            "targetArchitecture": "arm",   // 目标架构,改成arm
            "preLaunchTask": "build",  // 启动调试之前要执行的任务,这里配置的编译任务,每次调试之前执行一次编译
            "setupCommands": [ // 进入GDB以后,自动执行的一些命令  
                {
                    "description": "连接GDB Server",
                    "text": "target remote localhost:3333",
                    "ignoreFailures": false
                },
                {
                    "description": "选择调试文件(.elf)到gdb",
                    "text": "file D:/xxx/build/xxx.elf",
                    "ignoreFailures": false
                },
                {
                    "description": "Halt",
                    "text": "mon halt",
                    "ignoreFailures": false
                },
                {
                    "description": "Reset MCU",
                    "text": "mon reset init",
                    "ignoreFailures": false
                },
                {
                    "description": "下载代码到MCU",
                    "text": "load",
                    "ignoreFailures": false
                }
            ]
        }
    ]
}

到这里,launch.json就配置完成了,下面配置task。task主要有2个,一个编译,一个是启动openocd。
Ctrl+Shift+P,打开VSCode的命令行,输入configure task,选择Tasks:Configure Task – 回车 – Create task.json file form template – 回车 – Others – 回车。
image.png
image.png
image.png
自动创建一个task.json文件。
image.png
修改配置文件

{
    // 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",
        },
        {
            "label": "run openocd",
            "type": "shell",
           "command": "openocd -f interface/stlink.cfg -f target/stm32f1x.cfg"  // 这里注意选择和你板子相匹配的配置文件
        }
    ]
}

可以在openocd目录下的share->openocd->scripts下找到interface和target目录,里面有配置文件可以选择。
image.png
到此,所有工作都完成了。
注意,需要先运行启动openocd的任务,然后切换到调试窗口,点击运行即可自动编译,然后烧写,然后运行到断点。
image.png
#####到此实现VSCode+arm-none-eabi+openocd+msys的在线STM32调试.

该文同时发布在我的简书:https://www.jianshu.com/p/ca26b2227a58

发布了17 篇原创文章 · 获赞 10 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/markinstephen/article/details/105265016