vscode搭建STM32開發(fā)環(huán)境的詳細過程
需要安裝的軟件
vscode
必裝插件:
- C/C++:用于提供高亮顯示和代碼補全
- Cortex-Debug:用于提供調(diào)試配置
make
make工具可以直接下載xPack項目提供的windows-build-tools工具里面帶了make工具。
Release xPack Windows Build Tools v4.2.1-2 · xpack-dev-tools/windows-build-tools-xpack (github.com)
openocd
arm-none-eabi
stm32CubeMX
上述軟件具體的安裝教程網(wǎng)上有很多詳細的介紹資料,這里就不詳細介紹了。需要注意的是記得將make,openocd,arm-none-eabi等可執(zhí)行程序的路徑添加到環(huán)境變量中
以下章節(jié)的內(nèi)容都是根據(jù)stm32CubeMX生成的vscode_stm32f411 Makefile工程為例子來進行講解的。
配置開發(fā)環(huán)境
實際上就是打造代碼的編輯環(huán)境,實現(xiàn)類似于keil中編輯代碼和代碼補全等功能。在通過vscode打開vscode_stm32f411文件夾后,其實已經(jīng)具備了編輯和代碼補全功能(前提是必裝的插件已經(jīng)安裝好),只是會有很多報錯的波浪線,這時候便需配置c_cpp_properties.json
文件來解決源文件的各種報錯提示:
如果提示**uint32_t
是未定義的類型**在defines
下添加__GNUC__
c_cpp_properties.json
文件:
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE", "USE_HAL_DRIVER", // "STM32F411xE", // "__GNUC__" // ], // "compilerPath": "C:\\Program Files\\LLVM\\bin\\clang.exe", "compilerPath": "C:/Program Files (x86)/GNU Tools Arm Embedded/7 2018-q2-update/bin/arm-none-eabi-gcc.exe", "cStandard": "c17", "cppStandard": "c++14", // "intelliSenseMode": "windows-clang-x64" "intelliSenseMode": "gcc-arm" } ], "version": 4 }
配置編譯下載功能
新建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", "args": [ "-j4" ], "group": { //group用于將當前任務設置為默認的build任務,可以直接通過Ctrl+Shift+B直接執(zhí)行 "kind": "build", "isDefault": true }, "problemMatcher":[ "$gcc" ] }, { "label": "clean", "type": "shell", "command": "make", "args": [ "clean" ] }, { "label": "flash - ST-Link", //用于執(zhí)行makefile文件中實現(xiàn)的下載指令 "type": "shell", "command": "make flash", "problemMatcher": [] }, { "label": "download", //下載并運行 "type": "shell", "command": "openocd", "args": [ "-f", "interface/stlink-v2.cfg", "-f", "target/stm32f4x.cfg", "-c", "program build/vscode_stm32f411.elf verify reset exit" //TODO:這里的下載文件的路徑不能夠用${workspaceFolder}來指定 ], "dependsOn":"build", //download任務的依賴任務,即download任務執(zhí)行前會先執(zhí)行build任務 }, { "label": "reset", //復位程序 "type": "shell", "command":"openocd", "args": [ "-f", "interface/stlink-v2.cfg", "-f", "target/stm32f4x.cfg", "-c init", "-c reset", "-c exit", ], "problemMatcher": [] }, { "label": "halt", //掛起程序 "type": "shell", "command":"openocd", "args": [ "-f", "interface/stlink-v2.cfg", "-f", "target/stm32f4x.cfg", "-c init", "-c halt", "-c exit", ], "problemMatcher": [] }, { "label": "run", //運行程序 "type": "shell", "command":"openocd", "args": [ "-f", "interface/stlink-v2.cfg", "-f", "target/stm32f4x.cfg", "-c init", "-c resume", "-c exit", ], "problemMatcher": [] }, ] }
build
任務用于編譯工程(實質(zhì)上是執(zhí)行makefile文件 make)
clean
任務用于清除編譯生成的中間文件(實質(zhì)是執(zhí)行makefile文件中的 make clean)
flash - ST-Link
任務用于下載代碼到STM32芯片中,這里需要在makefile中添加flash偽目標,偽目標flash實現(xiàn)如下:
#flash the stm32 OPENOCD := openocd -f interface/stlink-v2.cfg -f target/stm32f4x_stlink.cfg FIRMWARE = $(BUILD_DIR)/vscode_stm32f411.elf flash: $(OPENOCD) -c init \ -c 'reset halt' \ -c 'flash write_image erase $(FIRMWARE)' \ -c 'reset run' \ -c exit
download
任務用于下載代碼到STM32芯片中,這里是完全在tasks.json
文件中實現(xiàn)的(通過openocd實現(xiàn)下載目標文件)
reset
任務用于復位目標板程序
halt
任務用于掛起目標板程序
run
任務用于運行掛起的目標程序
配置調(diào)試功能
添加launch.json
文件配置調(diào)試環(huán)境
{ // 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": [ { "name": "Cortex Debug", "cwd": "${workspaceRoot}", "executable": "${workspaceRoot}/build/vscode_stm32f411.elf", "request": "launch", "type": "cortex-debug", "servertype": "openocd", "device": "STM32F411xx", "interface": "swd", "configFiles": [ "${workspaceRoot}/openocd.cfg" ], "runToMain": true, "showDevDebugTimestamps": true, "svdFile": "${workspaceRoot}/STM32F411xx.svd", //需要查看外設寄存器的值必須指定該svd文件 } ] }
工作空間目錄下添加openocd.cfg
文件,文件內(nèi)容如下:
source [find interface/stlink-v2.cfg] source [find target/stm32f4x_stlink.cfg]
到此出已經(jīng)可以執(zhí)行F5經(jīng)行調(diào)試了。
注意:這里必須執(zhí)行make
指令后才能進行調(diào)試,否則不能夠正常調(diào)試
為了確保每次執(zhí)行調(diào)試時工程都是最新編譯過的,可以在launch.json
文件中添加"preLaunchTask": "build"
的配置。preLaunchTask
表示調(diào)試前執(zhí)行的任務,build是指task.json
文件中標簽為build的任務(注意launch.json
文件中的任務名字必須和task.json
文件中的標簽名一致)。
為了確保每次調(diào)試結束后目標板上的程序還能繼續(xù)運行,可以在launch.json
文件中添加"postDebugTask": "run"
的配置,這樣可以在調(diào)試結束后執(zhí)行task.json
文件中的run
任務以確保目標板上的程序還能繼續(xù)運行。
完整的launch.json
文件如下:
{ // 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": [ { "name": "Cortex Debug", "cwd": "${workspaceRoot}", "executable": "${workspaceRoot}/build/vscode_stm32f411.elf", "request": "launch", "type": "cortex-debug", "servertype": "openocd", //要選擇的GDB server // "device": "STM32F411xx", // "interface": "swd", "configFiles": [ // "${workspaceRoot}/openocd.cfg" "interface/stlink-v2.cfg", "target/stm32f4x.cfg" ], "runToMain": true, "showDevDebugTimestamps": true, "svdFile": "${workspaceRoot}/STM32F411xx.svd", "preLaunchTask": "build", //調(diào)試之前運行的任務(調(diào)試之前一定要確保工程被編譯過) "postDebugTask": "run", //調(diào)試結束后運行程序,沒有的化會導致程序調(diào)試結束后處于掛起狀態(tài) } ] }
細心的同學可能會注意到,這里的launch.json
文件和上面的該文件在configFiles
位置處也有一些區(qū)別:
采用這里的這種寫法可以不用在工作文件夾目錄下新建openocd.cfg
文件,不過這種方式在命令行中直接輸入openocd便會報錯。
小知識點:在終端中啟動openocd
時,會自動在當前目錄下尋找openocd.cfg
的文件作為配置文件
到此這篇關于vscode搭建STM32開發(fā)環(huán)境的詳細過程的文章就介紹到這了,更多相關vscode搭建STM32開發(fā)環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++中用new創(chuàng)建二維數(shù)組和指針數(shù)組實例代碼
這篇文章主要介紹了C++中用new創(chuàng)建二維數(shù)組和指針數(shù)組實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-03-03C++中關于委派(Delegates)的實現(xiàn)示例
這篇文章主要介紹了C++中關于委派(Delegates)的實現(xiàn)示例,針對C++11的一些新特性進行講解,需要的朋友可以參考下2015-07-07