一、前言
幾年前寫過一篇類似的文章,那時是用于調(diào)試rtthread系統(tǒng):使用 VSCode、arm-none-eabi-gdb、J-Link GDB Server 調(diào)試RTThread。
當(dāng)時有關(guān)vscode和gcc調(diào)試MCU的文章很少,所以只是寫了個大概。
最近又遇到了類似的問題,網(wǎng)上搜了一些資料,發(fā)現(xiàn)此方面的內(nèi)容已經(jīng)比較成熟了,借鑒了一些資料來完善自己這方面的知識。
參考資料:
1、GD32F307VC+WIN10+VSCODE+GCC+JLINK環(huán)境build。
2、stm32的調(diào)試工具:vscode下jlink或stlink下載及調(diào)試+設(shè)置快捷鍵
二、Makefile配置
基礎(chǔ)Makefile模板使用STM32CubeMX工具生成,為了節(jié)約篇幅,只介紹新增的部分。
- 2.1、使用jlink下載到sram并運行:
在Makefile中添加以下規(guī)則:
$(BUILD_DIR)/jlink_flash_file: $(BUILD_DIR)/$(TARGET).bin
@-rm -fR $@
@touch $@
@echo usb >> $@
@echo si 0 >> $@
@echo speed 50000 >> $@
@echo device $(DEVICE) >> $@
@echo JTAGConf -1,-1 >> $@
@echo h >> $@
@echo rx 1 >> $@
@echo loadbin $< $(LOADADDR) >> $@
@echo setpc `od -N4 -t x4 -j4 $(BUILD_DIR)/$(TARGET).bin | head -1 | sed -e 's/0000004//g' -e 's/ //g'| tr -d '\n'` >> $@
@echo g >> $@
@echo qc >> $@
jlink: $(BUILD_DIR)/jlink_flash_file
# on gitbash
"D:\Program Files (x86)\SEGGER\JLink\JLink.exe" -commanderscript $<
# on WSL
# /mnt/d/'Program Files (x86)'/SEGGER/JLink/JLink.exe -commanderscript $<
其中:
-
jlink_flash_file規(guī)則
:用于生成JLink.exe使用的配置腳本; -
DEVICE = CORTEX-M7
,MCU使用的是ARM Cortex-M7內(nèi)核; -
LOADADDR=0x20000000
,程序下載到sram的0x20000000處; -
setpc
,是jlink的內(nèi)置命令,用于設(shè)置PC指針的值,后邊的od -N4 -t x4 -j4 $(BUILD_DIR)/$(TARGET).bin | head -1 | sed -e 's/0000004//g' -e 's/ //g'| tr -d '\n'
是通過od命令從bin文件中讀取第4-7字節(jié)處的程序入口地址。
-
2.2、使用gdb調(diào)試:
在Makefile中添加以下規(guī)則:
JLinkGDBServer:
"D:\Program Files (x86)\SEGGER\JLink\JLinkGDBServer.exe" -select USB -device CORTEX-M7 -endian little \
-if JTAG -speed 50000 -noir -LocalhostOnly -nologtofile -port 2331 -SWOPort 2332 -TelnetPort 2333 &
$(BUILD_DIR)/debug.gdb: $(BUILD_DIR)/$(TARGET).bin
@-rm -fR $@
@touch $@
@echo target remote localhost:2331 >> $@
@echo monitor reset >> $@
@echo monitor halt >> $@
@echo load >> $@
@echo b main >> $@
@echo c >> $@
debug: $(BUILD_DIR)/debug.gdb JLinkGDBServer
$(PREFIX)gdb $(BUILD_DIR)/$(TARGET).elf --command=$(BUILD_DIR)/debug.gdb
其中:
-
JLinkGDBServer規(guī)則
:用于啟動JLinkGDBServer.exe; -
debug.gdb規(guī)則
:用于生成gdb的配置腳本; -
2.3、生成反匯編:
在Makefile中添加以下規(guī)則:
$(BUILD_DIR)/%.dis:$(BUILD_DIR)/%.elf
@-rm -fR $@
$(DUMP) --all-headers --demangle --disassemble $< > $@
其中,DUMP=arm-none-eabi-objdump
。
三、使用方法
前提條件:需要一個jlink仿真器,并安裝好jlink工具軟件,本人使用的是這個JLink-Windows工具。
- 3.1、程序下載到sram并運行
- 1、將jlink插到電腦上
- 2、使用vscode打開代碼,打開終端,使用git_bash,執(zhí)行:
make jlink
- 3.2、使用命令行g(shù)db調(diào)試程序
- 1、將jlink插到電腦上
- 2、使用vscode打開代碼,打開終端,使用git_bash,執(zhí)行:
make debug
四、vscode配置文件launch.json
參考自:使用VSCode編譯調(diào)試IAR的stm32工程。
對于習(xí)慣了使用keil、iar等IDE調(diào)試程序的人,可能不習(xí)慣上述的命令行g(shù)db調(diào)試。此時可以借助于vscode來實現(xiàn)類似于IDE調(diào)試的界面,只需配置vscode的launch.json即可。本人實際調(diào)試的配置如下:文章來源:http://www.zghlxwxcb.cn/news/detail-545771.html
{
// 使用 IntelliSense 了解相關(guān)屬性。
// 懸停以查看現(xiàn)有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "stm32f4", //程序名
"type": "cppdbg",
"request": "launch",
"targetArchitecture" : "arm", //arm架構(gòu)
"program": "${workspaceFolder}/stm32f4_project/build/stm32f4.elf", //編譯生成的elf文件
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Program Files\\gcc-arm-none-eabi-10.3-2021.10\\bin\\arm-none-eabi-gdb.exe", //gdb的安裝路徑
//"miDebuggerServerAddress": "localhost:2331",
"setupCommands" : [
{
"text" : "target remote localhost:2331" //連接目標(biāo)設(shè)備
},
{
"text" : "monitor reset" //復(fù)位目標(biāo)設(shè)備
},
{
"text" : "monitor halt" //停止目標(biāo)設(shè)備
},
{
"text" : "file E:/code/stm32f4_project/build/stm32f4.elf" //編譯生成的elf文件
},
{
"text" : "load" //加載elf文件
},
{
"text" : "b main" //在main函數(shù)打斷點
}
],
}
]
}
按上述配置后:文章來源地址http://www.zghlxwxcb.cn/news/detail-545771.html
- 1、先在git bash執(zhí)行下面的命令,打開JLinkGDBServer:
make JLinkGDBServer
- 2、點擊vscode的
Run->Start Debugging
,即可像IDE一樣在vscode中調(diào)試代碼了。
到了這里,關(guān)于vscode+gcc+jlink調(diào)試mcu程序(用于替代IDE)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!