VSCode+OpenOCD+STM32CubeMX开发与调试STM32单片机环境

需要的东西

需要的软件:

名称 备注 下载地址
gcc-arm-none-eabi 编译代码 https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
make 管理代码编译顺序 http://gnuwin32.sourceforge.net/packages/make.htm http://www.equation.com/servlet/equation.cmd?fa=make
openocd 烧录与调试 https://gnutoolchains.com/arm-eabi/openocd/
vscode 编辑代码 https://code.visualstudio.com/
stlink驱动 或者其他调试器驱动,取决于你的调试器型号 www.st.com/en/development-tools/stsw-link009.html
stm32cubemx 用来生成stm32初始工程 https://www.st.com/zh/development-tools/stm32cubemx.html

前面三个需要以配置环境变量的形式安装,后面的有安装包.

VSCode插件

名称 备注
C/C++ 编译代码
Cortex-Debug 配合OpenOCD调试与烧录

需要的硬件:

STM32开发板 STLINK(或其他usb转jtag工具)

新建工程需要添加的文件

VSCode配置文件(放在.vscode目录):

launch.json

{
	"version": "0.2.0",
	"configurations": [
		{//本地调试
			"name": "Local Debug",
			"cwd": "${workspaceRoot}",
			"executable": "./build/${workspaceRootFolderName}.elf",
			"request": "launch",
			"type": "cortex-debug",
			"preLaunchTask": "build",
			"servertype": "openocd",
			//"device": "STM32H750VB", //这个不是很重要,写不写应该无所谓
			"configFiles": [
				"openocd.cfg"
			]
		},
		{ //另一种调试方案,需要用StartOCD单独打开OCD.调试的时候不会进入startup_xxx.s文件,支持通过网络调试
			//有一个美中不足之处:需要在装载文件的地方手工指定全路径
			"name": "Remote Debug",
			"type": "cppdbg",
			"request": "launch",
			"miDebuggerPath": "arm-none-eabi-gdb.exe",
			"targetArchitecture": "arm",
			"program": "${workspaceFolder}/build/${workspaceRootFolderName}.elf",
			"preLaunchTask": "build",
			"setupCommands": [
				{
					"description": "装载文件",
					"text": "file 'E:/projects/Folder/${workspaceRootFolderName}/build/${workspaceRootFolderName}.elf'",
					//"ignoreFailures": true   //忽略异常
				},
				{
					"text": "target remote localhost:3333"
				},
				{
					"text": "monitor reset"
				},
				{
					"text": "monitor halt"
				},
				{
					"text": "load"
				}
			],
			"launchCompleteCommand": "None",
			"externalConsole": true,
			"cwd": "${workspaceFolder}"
		}
	]
}

tasks.json

{
	// 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 -j8"
		},
		{
			"label": "startocd",
			"type": "shell",
			"command": "cmd StartOCD.bat"
		}
	]
}

Openocd配置文件(放在工程根目录)

openocd.cfg

# 需要根据jtag工具修改
source [find interface/stlink.cfg]
# 需要根据要调试的芯片修改
source [find target/stm32h7x.cfg]
# use hardware reset, connect under reset
# connect_assert_srst needed if low power mode application running (WFI...)
# reset_config srst_only srst_nogate connect_assert_srst
reset_config none

StartOCD.bat

clear
openocd -f openocd.cfg -c init -c "reset halt" 
::一些配置以及烧录语句,根据情况使用:
::-c "flash write_image erase E:/projects/Folder/STM32H7Template/build/STM32H7Template.bin 0x8000000"
::-c "stm32h7x unlock 0"
原文地址:https://www.cnblogs.com/DragonStart/p/13152031.html