赞
踩
对于嵌入式开发人员,想必最熟悉MDK5或者IAR的IDE开发了。MDK5的调试和仿真功能是其他IDE所不能比拟的,IAR我没用过,本文章只是兴趣指导,旨在搭建一套VSCode的嵌入式开发环境,了解从编译到调试的过程。当然,项目的合作开发还是MDK5的兼容性好。
用到的工具有:
1)VSCode:微软旗下的开源编辑器,需要搭配插件使用。
2)arm-none-eabi-gcc:开源的交叉编译工具链,PC平台和嵌入式平台交叉编译,将编译到调试的一整套过程链接起来。
3)Make工具:识别并执行Makefile文件。Make工具的安装可以参考这个链接:https://developer.aliyun.com/article/1113601
4)Makefile文件:自动化构建脚本,负责文件的编译顺序和编译规则等等功能(这里不细说,大家可自行探索)。
5)openocd工具:作为PC平台到嵌入式平台的软件接口调试工具,与JLINK/STlink/CMSIS-DAPlink硬件接口对应。
上述工具的下载安装方式以及环境变量设置自行搜索,网上的教程都很细。这里不作过多介绍,我们进入正题:
本文以STM32F401CCU6单片机作为示例,来进行演示。
前提:需要安装好对应的VSCode插件
一、工程架构
二、VSCode各个脚本文件
task.json,主要作用是使用XX名称,执行XX命令和XX参数。
//任务脚本 { //ctrl+shift+B "tasks": [ //编译 { "type": "shell", "label": "build", "command": "make -j 8", "args": [], "problemMatcher": [ "$gcc" ], "group": "build" }, //清除 { "type": "shell", "label": "clean", "command": "make clean", "args": [], "problemMatcher": [ "$gcc" ], "group": "build" }, //重编译 { "type": "shell", "label": "rebuild", "command": "make -j 8", "args": [], "problemMatcher": [ "$gcc" ], "group": "build", "dependsOn": [ //每次执行这个任务,会先执行clean任务,再执行build任务,这便是所谓的依赖。 "clean" ] }, //下载 { "type": "shell", "label": "flash", "command": "openocd", "args": [ "-f", // "interface/stlink.cfg", "interface/cmsis-dap.cfg", "-f", "target/stm32f4x.cfg", "-c", "program build/${workspaceRootFolderName}.elf verify reset exit" //将工程根目录名称作为可执行文件名称 ], /*command+args相当于主命令+子命令,也就是openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg -c "program Project.hex verify reset exit"的效果*/ "problemMatcher": [ "$gcc" ], "group": "build", "dependsOn": [ //每次执行这个任务,会先执行clean任务,再执行build任务,这便是所谓的依赖。 "build" ] }, { "type": "cppbuild", "label": "C/C++: arm-none-eabi-gcc.exe 生成活动文件", "command": "D:/Software/arm_riscv_develop_tools/arm-none-eabi-gcc/10 2021.10/bin/arm-none-eabi-gcc.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "-mcpu=cortex-m4", "-mthumb", "-mfpu=fpv4-sp-d16", "-mfloat-abi=hard" ], "options": { "cwd": "D:/Software/arm_riscv_develop_tools/arm-none-eabi-gcc/10 2021.10/bin" }, "problemMatcher": [ "$gcc" ], "group": "build", "detail": "编译器: D:/Software/arm_riscv_develop_tools/arm-none-eabi-gcc/10 2021.10/bin/arm-none-eabi-gcc.exe" } ], "version": "2.0.0" }
launch.json,主要作用是MCU调试,以及选择什么样的下载调试器。
//调试脚本 { "configurations": [ { "name": "Debug with OpenOCD", "cwd": "${workspaceRoot}", "executable": "./build/${workspaceRootFolderName}.elf", //将工程根目录名称作为可执行文件名称 "request": "launch", "type": "cortex-debug", "servertype": "openocd", "device": "STM32F401CCU6", "configFiles": [ // "interface/stlink.cfg", "interface/cmsis-dap.cfg", "target/stm32f4x.cfg" ], "svdFile": "./STM32F401.svd", //选择寄存器文件 "liveWatch": { "enabled": true, "samplesPerSecond": 4 }, "searchDir": [], "runToEntryPoint": "main", "showDevDebugOutput": "none", "preLaunchTask": "flash" //每次调试之前会先调用下载任务再调试 } ], "version": "2.0.0" }
settings.json,主要作用是设置一些快捷功能,以及配合插件Task Buttons来显示编译、重编译、下载、调试等UI按钮。
//UI和功能设置脚本
{
// 1秒后自动保存
"files.autoSave": "afterDelay",
// 开启 material icons
"workbench.iconTheme": "vscode-icons",
// theme主题设置
"workbench.colorTheme": "Dracula Theme",
// 粘贴的时候格式化
"editor.formatOnPaste": true,
// 保存的时候格式化
"editor.formatOnSave": true,
// 字体样式
// "editor.fontFamily": "'Fira Code', monospace",
// 字体大小
"editor.fontSize": 16,
// 字体宽度
// "editor.fontWeight": "bold",
"security.workspace.trust.enabled": false,
"VsCodeTaskButtons.showCounter": true,
"VsCodeTaskButtons.tasks": [
{
"label": "$(tools) Build",
"task": "build",
"tooltip": "声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/918870
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。