windows上VScode开发STM32
一、编译环境安装
使用Cygwin结合VScode进行编译。
1.1、Cygwin安装
下载网址:https://cygwin.com/install.html
- 双击启动steup-x86_64.exe。选择下一步
- 选择从互联网安装,然后下一步(注意可能需要连到比较慢的网络上加载)
- 选择安装目录
- 选择必要的软件包
- 设置环境变量
1.2、arm-none-eabi-gcc安装
- gcc-arm-none-eabi-10.3下载链接 gcc-arm-none-eabi-10.3 downoads
双击gcc-arm-none-eabi-10.3-2021.10-win32.exe,选择安装路径一直点击下一步至完成即可添加环境变量 - 添加环境变量,一般安装完已经自动添加了环境变量
二、配置Debug及编译烧录任务
Vscode可以通过json文件自定义调试任务,结合ST-link/j-link+openocd可完成MCU的debug及自定义运行任务编译及烧录
2.1、安装openocd
- openocd下载地址:openocd 下载
选择下载最新的更新
- 解压下载的openocd放置指定位置
- 添加openocd环境变量
2.2 编译及烧录任务配置
若为新建项目,需找到自己PC的Openocd安装路径下找到对应MCU的配置文件,如:从D:\OpenOCD-20230712-0.12.0\share\openocd\scripts\target目录下将stm32xxx.cfg(这个文件看当前工作芯片型号,F4则stm32f4x.cfg,G4则stm32g4x.cfg,F1则stm32f1x.cfg)文件拷贝到工程目录下;从D:OpenOCD-20230712-0.12.0\share\openocd\scripts\interface目录下将stlink.cfg文件拷贝到工程目录下。
- 组合键shift+ctrl+p,进入c/c++配置UI
将c_cpp_properties.json内容配置如下:
{
"configurations":[
{
"name":"cygwin-gcc-x64",
"includePath":[
"${workspaceFolder}/**"
],
"defines":[
"_DEBUG",
"UNICODE",
"_UNICODE",
"USE_HAL_DRIVER"
"STM32G473xx" //这个芯片看自身使用情况而定
],
"compilerPath": "D:\\cygwin64\\bin\\gcc.exe",
"cStandard":"c17",
"intellisenseMode":"windows-gcc-arm",
"cppstandard":"c++14"
}
],
"version":4
}
- task配置
在终端->运行任务->添加配置任务,将创建一个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 -j12", //根据自己的PC性能选择线程数
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
],
"group": "build"
},
{
"label": "clean",
"type": "shell",
"command": "make clean",
"options": {
"cwd": "${workspaceFolder}"
},
"args": [
],
"group": "build"
},
{
"label": "download",
"type": "shell",
"command": "openocd",
"args": [
"-f",
"stlink.cfg",
"-f",
"stm32g4x.cfg", // 根据自身芯片设置
"-c",
"program firmware/build/target.elf verify reset exit" // target.elf更改为自己的工程编译文件名称
],
"group": "build"
}
]
}
- TASK RUNNER插件安装
安装插件完之后重启VScode
重启VScode后可以看到编译、清除、烧录选项
2.3 Debug配置
- 配置launch.json
添加一个launch.json脚本
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceFolder}",
"executable": "./build/xxxxx.elf",
"name": "Cortex Debug",
"request": "launch",
"type": "cortex-debug",
"showDevDebugOutput": false,
"servertype": "openocd",
"configFiles": [
"stlink.cfg",
"stm32g4x.cfg"
]
}
]
}
- debug运行
点击左边运行与调试,运行Cortex Debug