MCU免費開發環境
一般芯片廠家會提供各種開發IDE方案,通常其中就包括其自家的集成IDE,如:
意法半導體 STM32CubeIDE
NXP Codewarrior
TI CCS
另外也可以用eclipse、VS studio、VS code等搭建開發環境
VS Code 搭建stm32開發環境
1.搭建准備
程序安裝
1.下載並安裝 vs code
2.下載並安裝 STM32CubeMX
支持最新的HAL庫,工程代碼配置與生成工具,支持生成IAR、Keil、STM32CubeIDE、Makefile等工程,這里使用其生成的Makefile工程。
3.下載並安裝 Git for Windows
該工具集成有精簡版的mingw,這里我們使用其bash終端和版本管理均非常有用。
4.下載並安裝 arm-none-eabi-gcc
編譯器,GUN的arm的通用交叉編譯鏈工具,基本上常用的arm處理器均支持;
安裝時勾選設置全局環境變量以便於配置;
使用離線免安裝包時,解壓到合適的位置,在系統環境變量添加\bin目錄,運行CMD或者Windows PowerShell,測試一下是否可用。命令:arm-none-eabi-gcc -v
5.下載並安裝 mingw
MinGW 的全稱是:Minimalist GNU on Windows 。它實際上是將經典的開源 C語言 編譯器 GCC 移植到了 Windows 平台下,並且包含了 Win32API 和 MSYS,因此可以將源代碼編譯生成 Windows 下的可執行程序,又能如同在 Linux 平台下時,使用一些 Windows 不具備的開發工具。
一句話來概括:MinGW 就是 GCC 的 Windows 版本 。
其安裝一般為在線安裝,按網上步驟即可。
這里我們主要需要使用其 mingw32-make 功能.
- 離線安裝
如果由於環境不能在線安裝,可安裝其離線安裝包
MinGW-W64 GCC-8.1.0 x86_64-win32-seh
下載壓縮文件並解壓到合適的位置,在系統環境變量添加\bin目錄,運行CMD或者Windows PowerShell,測試一下是否可用。命令:gcc -v
同時為方便使用,復制 mingw32-make.exe 一份為 make.exe,這樣后面編譯程序使用 make 即可。
6.安裝mysy2
shell 命令行開發環境,可用於替代 git-bash、cmd、power shell,功能相對更完善。
安裝之后,在vscode中配置 settings.json--"terminal.integrated.shell.windows": "C:\msys64\msys2_shell.cmd", 詳見下節。
7.下載並安裝(可選) OpenOCD for Windows
8.Jlink、ST-Link驅動
9.STM32CubeProg 用於stm32下載程序
VS Code插件搭建所需
安裝開發所需基礎插件(插件在 vs code 拓展欄搜索名稱即可)
-
C/C++(必要)
增加了對C / C ++的語言支持,語法智能感知、加亮及調試功能
-
GBKtoUTF8
-
cortex-debug
2.工程示例
2.1 使用Cube-MX 生成Makefile工程
1.芯片選型、HAL版本、引腳配置、時鍾樹配置等,可參照下面博客
https://www.cnblogs.com/silencehuan/p/10904048.html
2.工程管理中,選擇生成makefile工程,然后點擊 generate code即可
2.2 vs code配置
默認情況下,工程下是不含.vscode的文件夾的,修改編輯用戶或工程settings.json文件時會自動創建;
user--settings.json文件參考修改如下(包含配置終端和一些格式等設置):
{
"C_Cpp.updateChannel": "Insiders",
"http.proxySupport": "off",
"workbench.iconTheme": "vscode-icons", //取消左側自動聚焦
"explorer.autoReveal": false,
"[c]": {
"editor.defaultFormatter": "ms-vscode.cpptools" //默認格式化工具
},
"[h]": {
"editor.defaultFormatter": "ms-vscode.cpptools" //默認格式化工具
},
"editor.formatOnSave": true, //文件保存時自動格式化
"editor.formatOnPaste": true, //代碼粘貼時自動格式化
"editor.formatOnType": true, //自動格式化鍵入行
// "terminal.integrated.shell.windows": "D:\\Program Files\\Git\\git-bash.exe",
"terminal.integrated.shell.windows": "C:\\msys64\\msys2_shell.cmd",
"terminal.integrated.shellArgs.windows": [
"-defterm",
"-mingw64",
"-no-start",
"-here",
"-use-full-path" //使用系統環境變量
],
"terminal.external.windowsExec": "D:\\Program Files\\Git\\git-bash.exe"
}
或者在菜單中設置 File--Preferences--Settings--Features--Terminal
2.3 工程makefile
1.實際開發需要熟悉 makefile
工程后面添加的文件程序需要由makefile來組織編譯;
打開工程makefile,編譯工具指定,默認 PREFIX = arm-none-eabi- ,如果已設置環境變量則無需修改,否則需添加(實際絕對路徑路徑) GCC_PATH = D:\gcc-arm-none-eabi-5_4-2016q3-20160926-win32\bin
PREFIX = arm-none-eabi-
# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
# either it can be added to the PATH environment variable.
ifdef GCC_PATH
CC = $(GCC_PATH)/$(PREFIX)gcc
AS = $(GCC_PATH)/$(PREFIX)gcc -x assembler-with-cpp
CP = $(GCC_PATH)/$(PREFIX)objcopy
SZ = $(GCC_PATH)/$(PREFIX)size
else
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
endif
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
2.4 工程 .vscode json配置(4個)
1.c_cpp_properties.json
主要有添加include路徑,編譯器路徑,宏定義等,設置好后索引、編譯就跟keil一樣方便;
打開工程 .vscode 下面的 c_cpp_properties.json 配置腳本,這個json不允許有注釋,如果你自己編寫了頭文件又不在workspaceFolder下,路徑也要加到includePath和browse里。設置如下:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}Drivers/STM32F4xx_HAL_Driver/Inc",
"${workspaceFolder}Drivers/STM32F4xx_HAL_Driver/Inc/Legacy",
"${workspaceFolder}Drivers/CMSIS/Device/ST/STM32F4xx/Include",
"${workspaceFolder}Drivers/CMSIS/Include",
"${workspaceFolder}Drivers/CMSIS/Include",
"D:/gcc-arm-none-eabi-5_4-2016q3-20160926-win32/arm-none-eabi/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"USE_HAL_DRIVER",
"STM32F407xx"
],
"compilerPath": "D:\\gcc-arm-none-eabi-5_4-2016q3-20160926-win32\\bin\\arm-none-eabi-gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x86"
}
],
"version": 4
}
2.launch.json
所需要調試的文件的路徑、調試時的CWD(工作路徑)、調試器的路徑及一些調試參數(程序啟動參數等);
{
// 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": [
{
"cwd": "${workspaceRoot}",
"executable": "./bin/HAL_Test.elf",
"name": "stm32 Debug",
"request": "launch",
"type": "cortex-debug",
"servertype": "stutil",
"device": "STM32F407ZG",
"preLaunchTask": "編譯並下載",
"postDebugTask": "復位設備"
}
]
}
3.tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "編譯",
"type": "shell",
"command": "make -j6",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "編譯並下載",
"type": "shell",
"command": "make -j6 && make update",
"problemMatcher": []
},
{
"label": "重新編譯",
"type": "shell",
"command": "make clean && make -j6",
"problemMatcher": []
},
{
"label": "復位設備",
"type": "shell",
"command": "STM32_Programmer_CLI -c port=SWD -hardRst",
"problemMatcher": []
}
]
}
4.工程下打開終端,輸入 make
工程編程會生成 .bin 文件,這個就是我們要燒錄的目標文件。
5.使用 Jlink 的 Jflash 工具燒錄
6.使用 JLink GDB server 調試,調試方法如同Linux下面的GDB,主要使用命令行
3. vs code 配置
1.取消文件自動定位到側邊欄
當我在右側點擊某個文件時,左側會自動定位到該文件所在位置,這點特別煩,尤其在項目目錄很長的時候。
在用戶 settings.json 中修改
"explorer.autoReveal": false
2.設置默認終端
File--Preferences--Settings-- 中打開用戶 setting.json文件,修改如下:
"terminal.integrated.shell.windows": "D:\\Program Files\\Git\\bin\\bash.exe",
"terminal.external.windowsExec": "D:\\Program Files\\Git\\bin\\bash.exe",
2.使用插件推薦(根據需求選擇)
插件 | 功能 |
---|---|
C/C++ | C / C ++的語言支持,語法智能感知、加亮及調試功能,當然需要系統安裝arm-none-eabi-gcc編譯器 |
Include Autocomplete | 頭文件自動匹配 |
Code Runner | 代碼一鍵運行 |
Cortex Debug | 提供jlink、stlink等調試接口功能 |
filesize | 顯示文件大小 |
Python | Python的語言支持,語法智能感知、加亮及調試功能,需要系統安裝python |
Git History | 查看版本歷史及比較 |
GitLens | 代碼中顯示提交信息、日志查看方便,同時提供操作圖標 |
GBKtoUTF8 | |
ARM | arm匯編語言支持 |
Bracket Pair Colorizer | 彩虹花括號,程序邏輯范圍查看方便 |
DeviceTree | 設備樹語法支持 |
vscode-icons | 文件圖標,可快速查看文件類型 |
PlatformIO IDE |