需要安裝的軟件
vscode
必裝插件:
- C/C++:用于提供高亮顯示和代碼補(bǔ)全
- Cortex-Debug:用于提供調(diào)試配置
make
make工具可以直接下載xPack項(xiàng)目提供的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)上有很多詳細(xì)的介紹資料,這里就不詳細(xì)介紹了。需要注意的是記得將make,openocd,arm-none-eabi等可執(zhí)行程序的路徑添加到環(huán)境變量中
以下章節(jié)的內(nèi)容都是根據(jù)stm32CubeMX生成的vscode_stm32f411 Makefile工程為例子來(lái)進(jìn)行講解的。
配置開(kāi)發(fā)環(huán)境
實(shí)際上就是打造代碼的編輯環(huán)境,實(shí)現(xiàn)類似于keil中編輯代碼和代碼補(bǔ)全等功能。在通過(guò)vscode打開(kāi)vscode_stm32f411文件夾后,其實(shí)已經(jīng)具備了編輯和代碼補(bǔ)全功能(前提是必裝的插件已經(jīng)安裝好),只是會(huì)有很多報(bào)錯(cuò)的波浪線,這時(shí)候便需配置c_cpp_properties.json
文件來(lái)解決源文件的各種報(bào)錯(cuò)提示:
如果提示**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用于將當(dāng)前任務(wù)設(shè)置為默認(rèn)的build任務(wù),可以直接通過(guò)Ctrl+Shift+B直接執(zhí)行 "kind" : "build" , "isDefault" : true }, "problemMatcher" :[ "$gcc" ] }, { "label" : "clean" , "type" : "shell" , "command" : "make" , "args" : [ "clean" ] }, { "label" : "flash - ST-Link" , //用于執(zhí)行makefile文件中實(shí)現(xiàn)的下載指令 "type" : "shell" , "command" : "make flash" , "problemMatcher" : [] }, { "label" : "download" , //下載并運(yùn)行 "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}來(lái)指定 ], "dependsOn" : "build" , //download任務(wù)的依賴任務(wù),即download任務(wù)執(zhí)行前會(huì)先執(zhí)行build任務(wù) }, { "label" : "reset" , //復(fù)位程序 "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" , //運(yùn)行程序 "type" : "shell" , "command" : "openocd" , "args" : [ "-f" , "interface/stlink-v2.cfg" , "-f" , "target/stm32f4x.cfg" , "-c init" , "-c resume" , "-c exit" , ], "problemMatcher" : [] }, ] } |
build
任務(wù)用于編譯工程(實(shí)質(zhì)上是執(zhí)行makefile文件 make)
clean
任務(wù)用于清除編譯生成的中間文件(實(shí)質(zhì)是執(zhí)行makefile文件中的 make clean)
flash - ST-Link
任務(wù)用于下載代碼到STM32芯片中,這里需要在makefile中添加flash偽目標(biāo),偽目標(biāo)flash實(shí)現(xiàn)如下:
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
任務(wù)用于下載代碼到STM32芯片中,這里是完全在tasks.json
文件中實(shí)現(xiàn)的(通過(guò)openocd實(shí)現(xiàn)下載目標(biāo)文件)
reset
任務(wù)用于復(fù)位目標(biāo)板程序
halt
任務(wù)用于掛起目標(biāo)板程序
run
任務(wù)用于運(yùn)行掛起的目標(biāo)程序
配置調(diào)試功能
添加launch.json
文件配置調(diào)試環(huán)境
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" , //需要查看外設(shè)寄存器的值必須指定該svd文件 } ] } |
工作空間目錄下添加openocd.cfg
文件,文件內(nèi)容如下:
1
2
3
|
source [find interface/stlink-v2.cfg] source [find target/stm32f4x_stlink.cfg] |
到此出已經(jīng)可以執(zhí)行F5經(jīng)行調(diào)試了。
注意:這里必須執(zhí)行make
指令后才能進(jìn)行調(diào)試,否則不能夠正常調(diào)試
? 為了確保每次執(zhí)行調(diào)試時(shí)工程都是最新編譯過(guò)的,可以在launch.json
文件中添加"preLaunchTask": "build"
的配置。preLaunchTask
表示調(diào)試前執(zhí)行的任務(wù),build是指task.json
文件中標(biāo)簽為build的任務(wù)(注意launch.json
文件中的任務(wù)名字必須和task.json
文件中的標(biāo)簽名一致)。
? 為了確保每次調(diào)試結(jié)束后目標(biāo)板上的程序還能繼續(xù)運(yùn)行,可以在launch.json
文件中添加"postDebugTask": "run"
的配置,這樣可以在調(diào)試結(jié)束后執(zhí)行task.json
文件中的run
任務(wù)以確保目標(biāo)板上的程序還能繼續(xù)運(yùn)行。
完整的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" , //調(diào)試之前運(yùn)行的任務(wù)(調(diào)試之前一定要確保工程被編譯過(guò)) "postDebugTask" : "run" , //調(diào)試結(jié)束后運(yùn)行程序,沒(méi)有的化會(huì)導(dǎo)致程序調(diào)試結(jié)束后處于掛起狀態(tài) } ] } |
細(xì)心的同學(xué)可能會(huì)注意到,這里的launch.json
文件和上面的該文件在configFiles
位置處也有一些區(qū)別:
采用這里的這種寫法可以不用在工作文件夾目錄下新建openocd.cfg
文件,不過(guò)這種方式在命令行中直接輸入openocd便會(huì)報(bào)錯(cuò)。
小知識(shí)點(diǎn):在終端中啟動(dòng)openocd
時(shí),會(huì)自動(dòng)在當(dāng)前目錄下尋找openocd.cfg
的文件作為配置文件
到此這篇關(guān)于vscode搭建STM32開(kāi)發(fā)環(huán)境的詳細(xì)過(guò)程的文章就介紹到這了,更多相關(guān)vscode搭建STM32開(kāi)發(fā)環(huán)境內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/xiaoyuanwuhui/article/details/116301416