vscode+gcc+jlink调试mcu程序(用于替代IDE)

一、前言

几年前写过一篇类似的文章,那时是用于调试rtthread系统:使用 VSCode、arm-none-eabi-gdb、J-Link GDB Server 调试RTThread

当时有关vscode和gcc调试MCU的文章很少,所以只是写了个大概。
最近又遇到了类似的问题,网上搜了一些资料,发现此方面的内容已经比较成熟了,借鉴了一些资料来完善自己这方面的知识。

参考资料:
1、GD32F307VC+WIN10+VSCODE+GCC+JLINK环境build
2、stm32的调试工具:vscode下jlink或stlink下载及调试+设置快捷键

二、Makefile配置

基础Makefile模板使用STM32CubeMX工具生成,为了节约篇幅,只介绍新增的部分。

  • 2.1、使用jlink下载到sram并运行:
    在Makefile中添加以下规则:
$(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规则:用于生成JLink.exe使用的配置脚本;

  • DEVICE = CORTEX-M7,MCU使用的是ARM Cortex-M7内核;

  • LOADADDR=0x20000000,程序下载到sram的0x20000000处;

  • setpc ,是jlink的内置命令,用于设置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字节处的程序入口地址。

  • 2.2、使用gdb调试:
    在Makefile中添加以下规则:

    扫描二维码关注公众号,回复: 16647309 查看本文章
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规则:用于启动JLinkGDBServer.exe;

  • debug.gdb规则:用于生成gdb的配置脚本;

  • 2.3、生成反汇编:
    在Makefile中添加以下规则:

$(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,执行:
    make jlink
    
  • 3.2、使用命令行gdb调试程序
    • 1、将jlink插到电脑上
    • 2、使用vscode打开代码,打开终端,使用git_bash,执行:
    make debug
    

四、vscode配置文件launch.json

参考自:使用VSCode编译调试IAR的stm32工程

对于习惯了使用keil、iar等IDE调试程序的人,可能不习惯上述的命令行gdb调试。此时可以借助于vscode来实现类似于IDE调试的界面,只需配置vscode的launch.json即可。本人实际调试的配置如下:

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "stm32f4", //程序名
            "type": "cppdbg",
            "request": "launch",
            "targetArchitecture" : "arm", //arm架构
            "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" //连接目标设备
                },
                {
    
    
                    "text" : "monitor reset" //复位目标设备
                },
                {
    
    
                    "text" : "monitor halt" //停止目标设备
                },
                {
    
    
                    "text" : "file E:/code/stm32f4_project/build/stm32f4.elf" //编译生成的elf文件
                },
                {
    
    
                    "text" : "load" //加载elf文件
                },
                {
    
    
                    "text" : "b main" //在main函数打断点
                }
            ],
        }
    ]
}

按上述配置后:

  • 1、先在git bash执行下面的命令,打开JLinkGDBServer:
    make JLinkGDBServer
    
  • 2、点击vscode的Run->Start Debugging,即可像IDE一样在vscode中调试代码了。

猜你喜欢

转载自blog.csdn.net/weixin_40837318/article/details/131641985