下面就给大家分享一下 VSCode 搭建 STM32 开发环境的一些常规且使用的功能。
01
需要的软件和工具VS Code下载地址:https://code.visualstudio.com/
LLVM下载地址:http://releases.llvm.org/download.html
Git下载地址:https://gitforwindows.org/
gcc-arm-none-eabi-5\_4-2016q3-20160926-win32.exe (md5)这里是一个2016年的,我目前也是用的这个,好像还有其他的新版的下载,GNU Arm Embedded Toolchain。正常安装后,记得需要将软件的安装目录下的bin文件夹设置进入环境变量PATH中。arm-none-eabi-gcc下载地址:https://launchpad.net/gcc-arm-embedded/+download
OpenOCD下载地址:http://gnutoolchains.com/arm-eabi/openocd/
STM32CubeMX链接:http://www.stm32cube.com/
02
搭建编译环境

help -> updater settings ->Connection Parameters设置好网络后,才能下载固件包。(2)生成工程。选择MakeFile选项。注意: 4.18.0以下的版本是没有Makefile这个选项的。最后下载一个中间版本,因为5.0以上的版本界面很乱。我选择的是 4.27.0.



.ioc文件是STM32Cube的工程文件,Inc和Src是供用户修改的源码,Driver里是STM32和ARM CMSIS的库,最好不要修改。不过,如果你要采用标准库开发的话,就修改为标准库的Driver.


"terminal.integrated.shell.windows": "D:\Program Files\Git\bin\bash.exe", "terminal.external.windowsExec": "D:\Program Files\Git\bin\bash.exe" 然后按Ctrl+` 就可以打开终端,看到Bash了

c\_cpp\_properties.json文件的配置文件。我们在当前目录的.vscode文件夹下创建c\_cpp\_properties.json配置文件,用来告诉VS Code我们定义的宏与文件的路径。{ "configurations": [ { "name": "Win32", "browse": { "path": [ "${workspaceFolder}/", "${workspaceFolder}/Drivers/CMSIS", "${workspaceFolder}/Drivers/FWlib/inc", "${workspaceFolder}/Drivers/CMSIS/startup", "${workspaceFolder}/User/inc", "${workspaceFolder}/User", "${workspaceFolder}/ThirdParty/crclib/include" ], "limitSymbolsToIncludedHeaders": true }, "includePath": [ "${workspaceFolder}/", "${workspaceFolder}/", "${workspaceFolder}/Drivers/CMSIS", "${workspaceFolder}/Drivers/FWlib/inc", "${workspaceFolder}/Drivers/CMSIS/startup", "${workspaceFolder}/User/inc", "${workspaceFolder}/User", "${workspaceFolder}/ThirdParty/crclib/include" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE", "__CC_ARM", "USE_STDPERIPH_DRIVER", "STM32F10X_HD" ], "compilerPath": "C:\Program Files\LLVM\bin\clang-format.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "clang-x64" } ], "version": 4 } 注意:如果提示
variable “uint32\_t” is not a type name不是一个type类型。则需要 添加宏定义\_\_CC\_ARM,如果没有该宏定义,则uint32\_t类型会报错。并且结构体中使用了uint32\_t定义的成员,也会补全不了。.c文件 经过 arm-none-eabi-gcc 编译成 .o文件.s文件 经过 arm-none-eabi-as 编译成 .o文件.o文件 和 .a文件 经过 arm-none-eabi-ld 链接成 .elf文件.elf文件 经过 arm-none-eabi-objcopy 和 arm-none-eabi-objdump 转换成 hex文件/dis文件arm-none-eabi-gdb 使用 .elf文件 进行debug



03
布局

openocd –f interface/stlink.cfg –f target/stm32f4.cfg 【注】配置仿真器的参数必须在配置目标MCU的参数之前,否则将报错。如果我们不带参数启动,openocd就会自动查找当前目录下有没有名为openocd.cfg的文件,并把它作为配置文件来启动。因此,我们就在当前工程下创建一个名为openocd.cfg的文件。
我们选择使用ST-link,SWD接口,目标芯片为stm32f1x。 (PS:这里注释掉了SWD接口,如果采用Jlink 则需要SWD接口)。这样,我们连好板子上好电,直接在终端里敲openocd,即可启动。
openocd运行时,这个shell终端就被占用了,我们一会要新开一个终端。

target remote localhost:3333





{ "version": "0.2.0", "configurations": [ { "name": "ARM Debug", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/Build/STM32F103RC_Template.elf", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:\Program Files (x86)\GNU Tools ARM Embedded\5.4 2016q3\bin\arm-none-eabi-gdb.exe", "targetArchitecture": "arm", "setupCommands": [ { "description": "选择调试文件(.elf)到gdb", "text": "file E:/VScode/STM32_VSCode/stm32f103_temp/Build/STM32F103RC_Template.elf", "ignoreFailures": false }, { "description": "连接GDB Server", "text": "target remote localhost:3333", "ignoreFailures": false }, { "description": "Reset MCU", "text": "monitor reset", "ignoreFailures": false }, { "description": "Halt", "text": "monitor halt", "ignoreFailures": false }, { "description":"下载代码到MCU", "text": "load" , "ignoreFailures": false } ], "preLaunchTask": "build", } ] } 可以看到,setupCommands里面就是我们之前试用gdb时操作过的流程:连接GDB Server——reset——halt——下载代码。而preLaunchTask中是我们之前在tasks.json中配置的build任务。它可以让我们每次调试时都先编译一遍。保存后,随便打几个断点,按下F5,就可以快乐调试了。可以看到功能还是很齐全的。


审核编辑 :李倩
全部0条评论
快来发表一下你的评论吧 !