VS Code 搭建stm32开发环境
搭建准备
程序安装
1.下载并安装 vs code
https://code.visualstudio.com/
2.下载并安装 STM32CubeMX
https://www.st.com/content/st_com/zh/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html
支持最新的HAL库,工程代码配置与生成工具,支持生成IAR、Keil、STM32CubeIDE、Makefile等工程,这里使用其生成的Makefile工程。
3.下载并安装 Git for Windows
https://gitforwindows.org/
该工具集成有精简版的mingw,这里我们使用其bash终端和版本管理均非常有用。
4.下载并安装 arm-none-eabi-gcc
https://launchpad.net/gcc-arm-embedded/+download
编译器,GUN的arm的通用交叉编译链工具,基本上常用的arm处理器均支持;
安装时勾选设置全局环境变量以便于配置;
使用离线免安装包时,解压到合适的位置,在系统环境变量添加\bin目录,运行CMD或者Windows PowerShell,测试一下是否可用。命令:arm-none-eabi-gcc -v
5.下载并安装 mingw
https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/
MinGW 的全称是:Minimalist GNU on Windows 。它实际上是将经典的开源 C语言 编译器 GCC 移植到了 Windows 平台下,并且包含了 Win32API 和 MSYS,因此可以将源代码编译生成 Windows 下的可执行程序,又能如同在 Linux 平台下时,使用一些 Windows 不具备的开发工具。
一句话来概括:MinGW 就是 GCC 的 Windows 版本 。
其安装一般为在线安装,按网上步骤即可。
这里我们主要需要使用其 mingw32-make 功能.
VS Code插件搭建所需
安装开发所需基础插件(插件在 vs code 拓展栏搜索名称即可)
C/C++(必要)
增加了对C / C ++的语言支持,语法智能感知、加亮及调试功能
GBKtoUTF8
cortex-debug
工程示例
使用Cube-MX 生成Makefile工程
芯片选型、HAL版本、引脚配置、时钟树配置等,可参照下面博客,这个是用Keil来进行编译,所以只需要在最后一步修改成makefile就可以了
STM32CubeMX让我们的蓝色LED闪亮起来(胎教级包看包会不用懂原理)
工程管理中,选择生成makefile工程,然后点击 generate code即可
vs code配置
工程 .vscode json配置(3个)
c_cpp_properties.json
主要有添加include路径,编译器路径,宏定义等,设置好后索引、编译就跟keil一样方便;
打开工程 .vscode 下面的 c_cpp_properties.json 配置脚本,这个json不允许有注释,如果你自己编写了头文件又不在workspaceFolder下,路径也要加到includePath和browse里。设置如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:/Program Files (x86)/GNU Arm Embedded Toolchain/9 2020-q2-update/arm-none-eabi/include",
"${workspaceFolder}/Core/Inc",
"${workspaceFolder}/LWIP/App",
"${workspaceFolder}/LWIP/Target",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/system",
"${workspaceFolder}/Drivers/STM32F4xx_HAL_Driver/Inc",
"${workspaceFolder}/Drivers/STM32F4xx_HAL_Driver/Inc/Legacy",
"${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/include",
"${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2",
"${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/netif/ppp",
"${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32F4xx/Include",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip/apps",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip/priv",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/lwip/prot",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/netif",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix/arpa",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix/net",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/posix/sys",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/src/include/compat/stdc",
"${workspaceFolder}/Middlewares/Third_Party/LwIP/system/arch",
"${workspaceFolder}/Drivers/CMSIS/Include"
],
"defines": [
"USE_HAL_DRIVER",
"STM32F407xx",
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
所需要调试的文件的路径、调试时的CWD(工作路径)、调试器的路径及一些调试参数(程序启动参数等);
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${workspaceRoot}",
"executable": "./build/Control.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "jlink",
"device": "STM32F407ZG",
"interface": "swd",
"armToolchainPath": "C:/Program Files (x86)/GNU Arm Embedded Toolchain/9 2020-q2-update/bin/",
"preLaunchTask": "build",
"rtos": "FreeRTOS"
}
]
}
tasks.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 -j8",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
工程下打开终端,输入 make
工程编程会生成 .bin 文件,这个就是我们要烧录的目标文件。