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"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM