1.引言
嵌入式系統(tǒng)的開發(fā)通常需要使用調(diào)試工具來驗證和調(diào)試代碼铜异。ST-Link是STMicroelectronics提供的一種常用調(diào)試和編程工具,用于與STM32微控制器系列進行通信辅搬。本文將介紹如何在Visual Studio Code(VSCode)中配置并使用ST-Link來調(diào)試嵌入式系統(tǒng)。
2.安裝必要軟件脖旱、工具和插件
2.1.安裝vscode;
沒什么要說的
2.2.安裝cortex-debug插件伞辛;
在VSCode中烂翰,插件是擴展功能的關(guān)鍵。為了與ST-Link一起使用蚤氏,我們需要安裝一個支持ARM嵌入式系統(tǒng)的插件甘耿。打開VSCode,點擊左側(cè)的擴展圖標(biāo)(或使用快捷鍵Ctrl+Shift+X)竿滨,然后在搜索框中輸入“Cortex-Debug”并安裝該插件佳恬。
2.3.安裝MingW64;
安裝完成記得把安裝路勁加入到環(huán)境變量。
C:\mingw64\bin
進入路徑C:\mingw64\bin于游。復(fù)制mingw32-make.exe毁葱,并改名為make.exe
2.4.安裝git;
沒什么說的
2.5.安裝arm-none-eabi-gcc
安裝完成后記得將路徑加入到環(huán)境變量
C:\Program Files (x86)\GNU Arm Embedded Toolchain\10 2020-q4-major\bin
2.6.安裝 ST-LINK GDB server
st-util是ST-LINK GDB server。st-util需要通過編譯官方源碼得到贰剥。github上找到源碼倾剿,https://github.com/stlink-org/stlink
通過git bash命令下載源碼。
下載完成后蚌成,通過下圖所示方式編譯STLINK源碼前痘。
編譯完成后在路徑stlink\build-mingw\bin
下找到如下圖所示文件。st-util
就是本次需要的GDB Server工具担忧。
為了便于管理芹缔,可以把目錄bin整體復(fù)制到C盤下。
3.配置launch.json文件
{
// 使用 IntelliSense 了解相關(guān)屬性瓶盛。
// 懸停以查看現(xiàn)有屬性的描述最欠。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${workspaceFolder}",
"executable": "./build/tmr2104.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "stutil",
"serverpath": "C:/Program Files (x86)/STMicroelectronics/bin/st-util.exe",
"interface": "swd",
"device": "STM32G431CB",
"v1": false,
"runToEntryPoint": "main",
//.svd可以在keil官網(wǎng)去下載STM32G0xx_DFP
"svdFile": "G:/SVD/STM32G431xx.svd",
"showDevDebugOutput": "both"
}
]
}
如果需要在在線調(diào)試期間惩猫,查看MCU寄存器數(shù)據(jù)芝硬,需加入。svd文件轧房。"svdFile": "G:/SVD/STM32G431xx.svd",
拌阴。svd文件可以去keil官網(wǎng)下載。
4.用cubemx創(chuàng)建一個makefile工程
創(chuàng)建完成工程后锯厢,用vscode打開皮官。在命令行輸入make指令編譯功能脯倒。如下如所示:
5.連接硬件
將ST-Link調(diào)試器連接到你的目標(biāo)STM32開發(fā)板上实辑,并確保硬件連接正確。
6.開始調(diào)試
通過點擊VSCode中的調(diào)試按鈕或使用快捷鍵F5啟動調(diào)試會話藻丢。如果一切設(shè)置正確剪撬,VSCode將連接到ST-Link調(diào)試器并開始執(zhí)行你的代碼。
[2023-11-26T03:18:40.572Z] SERVER CONSOLE DEBUG: onBackendConnect: gdb-server session connected. You can switch to "DEBUG CONSOLE" to see GDB interactions.
"C:/Program Files (x86)/STMicroelectronics/bin/st-util.exe" -p 50000 --no-reset
st-util 1.7.0-338-g509d60e
2023-11-26T11:18:40 INFO common.c: STM32G43x_G44x: 32 KiB SRAM, 128 KiB flash in at least 2 KiB pages.
2023-11-26T11:18:40 INFO gdb-server.c: Listening at *:50000...
2023-11-26T11:18:42 INFO common.c: STM32G43x_G44x: 32 KiB SRAM, 128 KiB flash in at least 2 KiB pages.
2023-11-26T11:18:42 INFO gdb-server.c: Found 6 hw breakpoint registers
2023-11-26T11:18:42 INFO gdb-server.c: GDB connected.
2023-11-26T11:18:42 INFO gdb-server.c: Found 6 hw breakpoint registers
2023-11-26T11:18:42 INFO common.c: STM32G43x_G44x: 32 KiB SRAM, 128 KiB flash in at least 2 KiB pages.
2023-11-26T11:18:42 INFO gdb-server.c: flash_erase: block 08000000 -> 7800
2023-11-26T11:18:42 INFO gdb-server.c: flash_erase: page 08000000
2023-11-26T11:18:42 INFO gdb-server.c: flash_erase: page 08000800
2023-11-26T11:18:42 INFO gdb-server.c: flash_erase: page 08001000
2023-11-26T11:18:42 INFO gdb-server.c: flash_erase: page 08001800
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08002000
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08002800
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08003000
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08003800
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08004000
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08004800
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08005000
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08005800
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08006000
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08006800
2023-11-26T11:18:43 INFO gdb-server.c: flash_erase: page 08007000
2023-11-26T11:18:43 INFO flash_loader.c: Starting Flash write for WB/G0/G4/L5/U5/H5/C0
2023-11-26T11:18:43 INFO gdb-server.c: flash_do: block 08000000 -> 7800
2023-11-26T11:18:43 INFO gdb-server.c: flash_do: page 08000000
1/1 pages written
2023-11-26T11:18:43 INFO gdb-server.c: flash_do: page 08000800
1/1 pages written
2023-11-26T11:18:43 INFO gdb-server.c: flash_do: page 08001000
1/1 pages written
2023-11-26T11:18:44 INFO gdb-server.c: flash_do: page 08001800
1/1 pages written
2023-11-26T11:18:44 INFO gdb-server.c: flash_do: page 08002000
1/1 pages written
2023-11-26T11:18:44 INFO gdb-server.c: flash_do: page 08002800
1/1 pages written
2023-11-26T11:18:44 INFO gdb-server.c: flash_do: page 08003000
1/1 pages written
2023-11-26T11:18:45 INFO gdb-server.c: flash_do: page 08003800
1/1 pages written
2023-11-26T11:18:45 INFO gdb-server.c: flash_do: page 08004000
1/1 pages written
2023-11-26T11:18:45 INFO gdb-server.c: flash_do: page 08004800
1/1 pages written
2023-11-26T11:18:45 INFO gdb-server.c: flash_do: page 08005000
1/1 pages written
2023-11-26T11:18:45 INFO gdb-server.c: flash_do: page 08005800
1/1 pages written
2023-11-26T11:18:46 INFO gdb-server.c: flash_do: page 08006000
1/1 pages written
2023-11-26T11:18:46 INFO gdb-server.c: flash_do: page 08006800
1/1 pages written
2023-11-26T11:18:46 INFO gdb-server.c: flash_do: page 08007000
1/1 pages written
2023-11-26T11:18:46 INFO gdb-server.c: Found 6 hw breakpoint registers
7.結(jié)論
通過這些步驟悠反,你現(xiàn)在應(yīng)該能夠在VSCode中成功配置并使用ST-Link調(diào)試你的嵌入式系統(tǒng)残黑。這種集成式的開發(fā)環(huán)境可以提高開發(fā)效率馍佑,并幫助你更輕松地診斷和修復(fù)代碼中的問題。