需要安裝的軟件
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
文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
{ "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
文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
{ // 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實現如下:
1
2
3
4
5
6
7
8
9
10
|
#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
文件配置調試環境
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
{ // 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
文件,文件內容如下:
1
2
3
|
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
文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
{ // 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
的文件作為配置文件
到此這篇關于vscode搭建STM32開發環境的詳細過程的文章就介紹到這了,更多相關vscode搭建STM32開發環境內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/xiaoyuanwuhui/article/details/116301416