文章目錄
- 需要安裝的軟體
- vscode
- make
- openocd
- arm-none-eabi
- stm32CubeMX
- 配置開發環境
- 配置編譯下載功能
- 配置除錯功能
需要安裝的軟體
vscode
必裝插件:
- C/C++:用于提供高亮顯示和代碼補全
- Cortex-Debug:用于提供除錯配置
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
上述軟體具體的安裝教程網上有很多詳細的介紹資料,這里就不詳細介紹了,需要注意的是記得將make,openocd,arm-none-eabi等可執行程式的路徑添加到環境變數中
以下章節的內容都是根據stm32CubeMX生成的vscode_stm32f411 Makefile工程為例子來進行講解的,
配置開發環境
實際上就是打造代碼的編輯環境,實作類似于keil中編輯代碼和代碼補全等功能,在通過vscode打開vscode_stm32f411檔案夾后,其實已經具備了編輯和代碼補全功能(前提是必裝的插件已經安裝好),只是會有很多報錯的波浪線,這時候便需配置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直接執行
"kind": "build",
"isDefault": true
},
"problemMatcher":[
"$gcc"
]
},
{
"label": "clean",
"type": "shell",
"command": "make",
"args": [
"clean"
]
},
{
"label": "flash - ST-Link", //用于執行makefile檔案中實作的下載指令
"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任務執行前會先執行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任務用于編譯工程(實質上是執行makefile檔案 make) -
clean任務用于清除編譯生成的中間檔案(實質是執行makefile檔案中的 make clean) -
flash - ST-Link任務用于下載代碼到STM32芯片中,這里需要在makefile中添加flash偽目標,偽目標flash實作如下:#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檔案中實作的(通過openocd實作下載目標檔案) -
reset任務用于復位目標板程式 -
halt任務用于掛起目標板程式 -
run任務用于運行掛起的目標程式
配置除錯功能
添加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",
"device": "STM32F411xx",
"interface": "swd",
"configFiles": [
"${workspaceRoot}/openocd.cfg"
],
"runToMain": true,
"showDevDebugTimestamps": true,
"svdFile": "${workspaceRoot}/STM32F411xx.svd", //需要查看外設暫存器的值必須指定該svd檔案
}
]
}
作業空間目錄下添加openocd.cfg檔案,檔案內容如下:
source [find interface/stlink-v2.cfg]
source [find target/stm32f4x_stlink.cfg]
到此出已經可以執行F5經行除錯了,
注意:這里必須執行make指令后才能進行除錯,否則不能夠正常除錯
? 為了確保每次執行除錯時工程都是最新編譯過的,可以在launch.json檔案中添加"preLaunchTask": "build"的配置,preLaunchTask表示除錯前執行的任務,build是指task.json檔案中標簽為build的任務(注意launch.json檔案中的任務名字必須和task.json檔案中的標簽名一致),
? 為了確保每次除錯結束后目標板上的程式還能繼續運行,可以在launch.json檔案中添加"postDebugTask": "run"的配置,這樣可以在除錯結束后執行task.json檔案中的run任務以確保目標板上的程式還能繼續運行,
完整的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", //除錯之前運行的任務(除錯之前一定要確保工程被編譯過)
"postDebugTask": "run", //除錯結束后運行程式,沒有的化會導致程式除錯結束后處于掛起狀態
}
]
}
細心的同學可能會注意到,這里的launch.json檔案和上面的該檔案在configFiles位置處也有一些區別:
采用這里的這種寫法可以不用在作業檔案夾目錄下新建openocd.cfg檔案,不過這種方式在命令列中直接輸入openocd便會報錯,
- 小知識點:在終端中啟動
openocd時,會自動在當前目錄下尋找openocd.cfg的檔案作為組態檔
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/282373.html
標籤:其他
