STM32H7 + VS Code + OpenOCD + STLink 完整配置教程

It works.

目录

  1. 前置要求
  2. 工具安装
  3. 项目配置文件
  4. 配置步骤
  5. 验证测试
  6. 常见问题
  7. 使用方法

前置要求

硬件要求

  • STM32H7 开发板(本教程以 STM32H743ZIT6 为例)
  • STLink V2/V3 调试器
  • USB 数据线

软件要求

  • Windows 10/11
  • VS Code 最新版本
  • Git Bash(可选,用于命令行操作)

工具安装

1. ARM GCC 工具链

作用:编译 STM32 C/C++ 代码

推荐版本:gcc-arm-none-eabi

安装路径示例D:/program/gcc-arm-none-eabi/

安装步骤

  1. 下载 ARM GCC 工具链
  2. 解压到指定目录(避免中文路径)
  3. 记录 bin 目录路径,例如:D:/program/gcc-arm-none-eabi/bin

验证安装

bash 复制代码
# 打开命令行,测试
D:/program/gcc-arm-none-eabi/bin/arm-none-eabi-gcc.exe --version

2. OpenOCD

作用:调试服务器,连接 GDB 和硬件调试器

推荐版本:xPack OpenOCD

安装路径示例D:/program/xpack-openocd/

安装步骤

  1. 下载 xPack OpenOCD
  2. 解压到指定目录(避免中文路径)
  3. 记录 bin 目录路径,例如:D:/program/xpack-openocd/bin

验证安装

bash 复制代码
# 打开命令行,测试
D:/program/xpack-openocd/bin/openocd.exe --version

3. CMake 和 Ninja

作用:构建系统

安装步骤

  1. 下载并安装 CMake:https://cmake.org/download/
  2. 下载 Ninja:https://github.com/ninja-build/ninja/releases
  3. 将 CMake 和 Ninja 添加到系统 PATH

验证安装

bash 复制代码
cmake --version
ninja --version

4. VS Code 扩展

打开 VS Code,安装以下扩展:

必需扩展:
  1. Cortex-Debug (marus25.cortex-debug)

    • 用于 ARM 调试
    • 扩展 ID: marus25.cortex-debug
  2. C/C++ (ms-vscode.cpptools)

    • C/C++ 语言支持
    • 扩展 ID: ms-vscode.cpptools
  3. CMake Tools (ms-vscode.cmake-tools)

    • CMake 集成
    • 扩展 ID: ms-vscode.cmake-tools
推荐扩展:
  1. Task Buttons (spencerwmiles.vscode-task-buttons)

    • 在状态栏显示任务快捷按钮
    • 扩展 ID: spencerwmiles.vscode-task-buttons
  2. clangd (llvm-vs-code-extensions.vscode-clangd)

    • 代码智能提示和补全
    • 扩展 ID: llvm-vs-code-extensions.vscode-clangd

作用:Windows 识别 STLink 调试器

安装步骤

  1. 下载 STLink 驱动程序
  2. 安装驱动
  3. 连接 STLink,确保 Windows 设备管理器中能看到设备

验证

  • 打开设备管理器
  • 应该能看到 "STMicroelectronics STLink dongle" 或类似设备

项目配置文件

目录结构

复制代码
项目根目录/
├── .vscode/
│   ├── launch.json          # 调试配置
│   ├── tasks.json           # 任务配置
│   └── settings.json        # 工作区设置
├── build/                   # 编译输出目录
│   └── Debug/
│       ├── h7zti6.elf      # ELF 可执行文件
│       └── h7zti6.bin      # 二进制文件
├── Core/                    # STM32CubeMX 生成的核心代码
├── Drivers/                 # HAL 驱动
├── openocd.cfg             # OpenOCD 配置(调试用)
├── openocd_flash.cfg       # OpenOCD 配置(烧录用)
├── CMakeLists.txt          # CMake 配置
├── CMakePresets.json       # CMake 预设
└── STM32H743XX_FLASH.ld    # 链接脚本

配置文件 1: .vscode/launch.json

作用:配置 VS Code 调试器

完整内容

json 复制代码
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "调试 STM32H7 (ST-Link)",
            "type": "cortex-debug",
            "request": "launch",
            "servertype": "openocd",
            "cwd": "${workspaceFolder}",
            "executable": "${workspaceFolder}/build/Debug/h7zti6.elf",
            "configFiles": ["${workspaceFolder}/openocd.cfg"],
            "toolchainPrefix": "arm-none-eabi",
            "armToolchainPath": "D:/program/gcc-arm-none-eabi/bin",
            "openOCDPath": "D:/program/xpack-openocd/bin/openocd.exe",
            "svdFile": "",
            "runToEntryPoint": "main",
            "showDevDebugOutput": "raw",
            "postRestartCommands": ["break main", "continue"]
        },
        {
            "name": "附加调试 STM32H7 (ST-Link)",
            "type": "cortex-debug",
            "request": "attach",
            "servertype": "openocd",
            "cwd": "${workspaceFolder}",
            "executable": "${workspaceFolder}/build/Debug/h7zti6.elf",
            "configFiles": ["${workspaceFolder}/openocd.cfg"],
            "toolchainPrefix": "arm-none-eabi",
            "armToolchainPath": "D:/program/gcc-arm-none-eabi/bin",
            "openOCDPath": "D:/program/xpack-openocd/bin/openocd.exe",
            "svdFile": "",
            "showDevDebugOutput": "raw"
        }
    ]
}

需要修改的部分

  1. executable: 修改为你的 ELF 文件名(替换 h7zti6.elf
  2. armToolchainPath: 修改为你的 ARM GCC 路径
  3. openOCDPath: 修改为你的 OpenOCD 路径

配置文件 2: .vscode/tasks.json

作用:配置编译、烧录、擦除等任务

完整内容

json 复制代码
{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "🔨 编译项目",
			"type": "shell",
			"command": "cmake",
			"args": [
				"--build",
				"build/Debug",
				"--config",
				"Debug"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": false,
				"panel": "shared"
			},
			"problemMatcher": [
				"$gcc"
			],
			"detail": "编译 Debug 版本",
			"dependsOn": "配置 CMake"
		},
		{
			"label": "配置 CMake",
			"type": "shell",
			"command": "cmake",
			"args": [
				"--preset",
				"Debug"
			],
			"group": "build",
			"presentation": {
				"echo": true,
				"reveal": "silent",
				"panel": "shared"
			},
			"detail": "配置 CMake Debug 构建"
		},
		{
			"label": "🔥 擦除Flash (ST-Link)",
			"type": "shell",
			"command": "D:/program/xpack-openocd/bin/openocd.exe",
			"args": [
				"-f",
				"${workspaceFolder}/openocd_flash.cfg",
				"-c",
				"init; reset halt; flash erase_address 0x08000000 0x100000; shutdown"
			],
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "使用 ST-Link 擦除 STM32H7 Flash Bank1 (1MB)"
		},
		{
			"label": "🔥 擦除整个Flash (ST-Link)",
			"type": "shell",
			"command": "D:/program/xpack-openocd/bin/openocd.exe",
			"args": [
				"-f",
				"${workspaceFolder}/openocd_flash.cfg",
				"-c",
				"init; reset halt; stm32h7x mass_erase 0; shutdown"
			],
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "使用 ST-Link 擦除 STM32H7 整个 Flash (2MB,包括 Bank1+Bank2)"
		},
		{
			"label": "⬇️ 烧录程序 (ST-Link)",
			"type": "shell",
			"command": "D:/program/xpack-openocd/bin/openocd.exe",
			"args": [
				"-f",
				"${workspaceFolder}/openocd_flash.cfg",
				"-c",
				"init; reset halt; flash write_image erase build/Debug/h7zti6.bin 0x08000000; verify_image build/Debug/h7zti6.bin 0x08000000; reset run; shutdown"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "使用 ST-Link 烧录二进制文件到 STM32H7"
		},
		{
			"label": "⬇️ 烧录程序-ELF (ST-Link)",
			"type": "shell",
			"command": "D:/program/xpack-openocd/bin/openocd.exe",
			"args": [
				"-f",
				"${workspaceFolder}/openocd_flash.cfg",
				"-c",
				"program build/Debug/h7zti6.elf verify reset exit"
			],
			"options": {
				"cwd": "${workspaceFolder}"
			},
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "使用 ST-Link 烧录 ELF 文件到 STM32H7(推荐)"
		},
		{
			"label": "🔧 检测连接 (ST-Link)",
			"type": "shell",
			"command": "D:/program/xpack-openocd/bin/openocd.exe",
			"args": [
				"-f",
				"${workspaceFolder}/openocd_flash.cfg",
				"-c",
				"init; targets; reset halt; mdw 0x1FF1E800 3; shutdown"
			],
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "使用 ST-Link 检测 OpenOCD 连接状态并读取 STM32H7 芯片 ID"
		},
		{
			"label": "🚀 编译+烧录 (ST-Link)",
			"dependsOrder": "sequence",
			"dependsOn": [
				"🔨 编译项目",
				"⬇️ 烧录程序-ELF (ST-Link)"
			],
			"group": {
				"kind": "test",
				"isDefault": true
			},
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "完整流程:编译+烧录 ELF(推荐)"
		},
		{
			"label": "🚀 编译+擦除+烧录 (ST-Link)",
			"dependsOrder": "sequence",
			"dependsOn": [
				"🔨 编译项目",
				"🔥 擦除Flash (ST-Link)",
				"⬇️ 烧录程序 (ST-Link)"
			],
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "完整流程:编译+擦除+烧录 BIN"
		},
		{
			"label": "🔄 擦除+烧录 (ST-Link)",
			"dependsOrder": "sequence",
			"dependsOn": [
				"🔥 擦除Flash (ST-Link)",
				"⬇️ 烧录程序 (ST-Link)"
			],
			"group": "test",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"focus": true,
				"panel": "new",
				"clear": true
			},
			"detail": "使用 ST-Link 擦除 Flash 后烧录现有程序"
		},
		{
			"label": "🧹 清理编译",
			"type": "shell",
			"command": "cmake",
			"args": [
				"--build",
				"build/Debug",
				"--target",
				"clean"
			],
			"group": "build",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"panel": "shared"
			},
			"detail": "清理编译产物"
		},
		{
			"label": "🔄 重新配置 CMake",
			"type": "shell",
			"command": "rm",
			"args": [
				"-rf",
				"build"
			],
			"group": "build",
			"presentation": {
				"echo": true,
				"reveal": "always",
				"panel": "shared"
			},
			"detail": "删除 build 目录,强制重新配置"
		}
	]
}

需要修改的部分

  1. 所有 "command": "D:/program/xpack-openocd/bin/openocd.exe" - 修改为你的 OpenOCD 路径
  2. 所有包含 h7zti6.binh7zti6.elf 的地方 - 修改为你的项目名称

配置文件 3: .vscode/settings.json

作用:工作区设置和任务快捷按钮

完整内容

json 复制代码
{
    "files.associations": {
        "*.h": "c",
        "*.c": "c"
    },

    // clangd 配置
    "clangd.arguments": [
        "--compile-commands-dir=./",
        "--clang-tidy",
        "--completion-style=detailed",
        "--header-insertion=iwyu",
        "--background-index",
        "--suggest-missing-includes"
    ],
    "clangd.fallbackFlags": ["-std=gnu11", "-Wall"],

    // 编辑器配置
    "editor.semanticHighlighting.enabled": true,
    "editor.suggest.snippetsPreventQuickSuggestions": false,
    "C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",

    // Task Buttons 扩展配置
    "VsCodeTaskButtons.showCounter": true,
    "VsCodeTaskButtons.tasks": [
        {
            "label": "$(gear) STM32H7 开发",
            "tasks": [
                {
                    "label": "🔨 编译项目",
                    "task": "🔨 编译项目",
                    "description": "编译 Debug 版本的 STM32H7 项目"
                },
                {
                    "label": "🚀 编译+烧录 (推荐)",
                    "task": "🚀 编译+烧录 (ST-Link)",
                    "description": "完整流程:编译+烧录 ELF(推荐)"
                },
                {
                    "label": "⬇️ 烧录程序-ELF",
                    "task": "⬇️ 烧录程序-ELF (ST-Link)",
                    "description": "使用 ST-Link 烧录 ELF 文件到 STM32H7"
                },
                {
                    "label": "⬇️ 烧录程序-BIN",
                    "task": "⬇️ 烧录程序 (ST-Link)",
                    "description": "使用 ST-Link 烧录二进制文件到 STM32H7"
                },
                {
                    "label": "🔥 擦除Flash (Bank1)",
                    "task": "🔥 擦除Flash (ST-Link)",
                    "description": "使用 ST-Link 擦除 STM32H7 Flash Bank1 (1MB)"
                },
                {
                    "label": "🔥 擦除整个Flash",
                    "task": "🔥 擦除整个Flash (ST-Link)",
                    "description": "使用 ST-Link 擦除 STM32H7 整个 Flash (2MB)"
                },
                {
                    "label": "🔄 擦除+烧录",
                    "task": "🔄 擦除+烧录 (ST-Link)",
                    "description": "使用 ST-Link 擦除Flash后烧录现有程序"
                },
                {
                    "label": "🔧 检测连接",
                    "task": "🔧 检测连接 (ST-Link)",
                    "description": "使用 ST-Link 检测 OpenOCD 连接状态"
                },
                {
                    "label": "🧹 清理编译",
                    "task": "🧹 清理编译",
                    "description": "清理编译产物"
                },
                {
                    "label": "🔄 重新配置",
                    "task": "🔄 重新配置 CMake",
                    "description": "删除 build 目录,强制重新配置"
                }
            ],
            "tooltip": "STM32H7 开发任务菜单"
        },
        {
            "label": "🚀 编译+烧录",
            "task": "🚀 编译+烧录 (ST-Link)",
            "tooltip": "快速编译并烧录程序到 STM32H7",
            "color": "default"
        }
    ],

    // CMake Tools 配置
    "cmake.configureOnOpen": false,
    "cmake.buildDirectory": "${workspaceFolder}/build/Debug"
}

配置文件 4: openocd.cfg

作用:OpenOCD 调试配置(不含 init 命令,用于 VS Code 调试)

完整内容

tcl 复制代码
# OpenOCD 配置文件 - STM32H743ZIT6 (ST-Link)
# 接口配置 - ST-Link (自动检测版本)
source [find interface/stlink.cfg]

# 目标芯片配置 - STM32H7x 系列
source [find target/stm32h7x.cfg]

# 适配器速度设置 (kHz)
adapter speed 4000

# 复位配置
reset_config srst_only srst_nogate

需要修改的部分

  • source [find target/stm32h7x.cfg] - 根据你的芯片型号修改
    • STM32H7: stm32h7x.cfg
    • STM32F1: stm32f1x.cfg
    • STM32F4: stm32f4x.cfg
    • STM32F7: stm32f7x.cfg
    • 等等

配置文件 5: openocd_flash.cfg

作用:OpenOCD 烧录配置(含 init 命令,用于命令行烧录)

完整内容

tcl 复制代码
# OpenOCD 配置文件 - STM32H743ZIT6 (ST-Link) - 用于烧录
# 接口配置 - ST-Link (自动检测版本)
source [find interface/stlink.cfg]

# 目标芯片配置 - STM32H7x 系列
source [find target/stm32h7x.cfg]

# 适配器速度设置 (kHz)
adapter speed 4000

# 复位配置
reset_config srst_only srst_nogate

# 初始化
init

重要说明

  • openocd.cfg(调试用):不含 init 命令
  • openocd_flash.cfg(烧录用):包含 init 命令
  • 两个文件的区别仅在于最后是否有 init 命令

配置步骤

步骤 1: 安装所有工具

按照 工具安装 章节,完成以下安装:

  1. ✅ ARM GCC 工具链
  2. ✅ OpenOCD
  3. ✅ CMake 和 Ninja
  4. ✅ VS Code 扩展
  5. ✅ STLink 驱动

步骤 2: 创建/打开 STM32 项目

如果是新项目:

  1. 使用 STM32CubeMX 创建项目
  2. 配置时钟、外设等
  3. 生成代码,选择 CMake 作为工具链

如果是现有项目:

  1. 直接打开项目文件夹

步骤 3: 创建 .vscode 目录

在项目根目录创建 .vscode 文件夹:

bash 复制代码
mkdir .vscode

步骤 4: 创建配置文件

在项目根目录和 .vscode 目录中创建以下文件:

根目录:

  • openocd.cfg
  • openocd_flash.cfg

.vscode 目录:

  • launch.json
  • tasks.json
  • settings.json

复制上述配置文件的内容到对应文件中。

步骤 5: 修改配置文件中的路径

必须修改的地方:

  1. launch.json:

    json 复制代码
    "executable": "${workspaceFolder}/build/Debug/你的项目名.elf",
    "armToolchainPath": "你的ARM GCC路径/bin",
    "openOCDPath": "你的OpenOCD路径/bin/openocd.exe"
  2. tasks.json:

    • 所有 "command": "D:/program/xpack-openocd/bin/openocd.exe" 改为你的路径
    • 所有 h7zti6.binh7zti6.elf 改为你的项目名
  3. openocd.cfgopenocd_flash.cfg:

    • 根据芯片型号修改 source [find target/stm32xxxx.cfg]

步骤 6: 检查 CMake 配置

确保项目根目录有以下文件:

  • CMakeLists.txt
  • CMakePresets.json(可选,但推荐)

如果使用 STM32CubeMX 生成,这些文件应该已经存在。

步骤 7: 首次编译

  1. 打开 VS Code
  2. 打开项目文件夹
  3. Ctrl+Shift+B 执行默认编译任务
  4. 或运行任务:🔨 编译项目

首次编译会自动配置 CMake。


验证测试

  1. 连接 STLink 到电脑和开发板
  2. 给开发板上电
  3. 在 VS Code 中按 Ctrl+Shift+P
  4. 输入 "Tasks: Run Task"
  5. 选择 🔧 检测连接 (ST-Link)

预期结果

复制代码
Info : STLINK V2J43S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.199483
Info : [stm32h7x.cpu0] Cortex-M7 r1p1 processor detected
...
Info : Device: STM32H74x/75x
Info : flash size probed value 2048k

测试 2: 编译项目

运行任务:🔨 编译项目

预期结果

  • 编译成功
  • build/Debug/ 目录生成 .elf.bin 文件

测试 3: 烧录程序

运行任务:🚀 编译+烧录 (ST-Link)

预期结果

复制代码
Info : Device: STM32H74x/75x
Info : flash size probed value 2048k
...
Info : Padding image section 0 at 0x08000298 with 8 bytes
shutdown command invoked

测试 4: 调试程序

  1. F5 启动调试
  2. 或点击左侧调试图标,选择 "调试 STM32H7 (ST-Link)"

预期结果

  • 调试工具栏出现
  • 程序停在 main 函数
  • 可以设置断点、单步执行等

常见问题

问题 1: 找不到 openocd 命令

错误信息

复制代码
'openocd' 不是内部或外部命令

解决方案

  1. 检查 tasks.json 中的 OpenOCD 路径是否正确
  2. 使用绝对路径:D:/program/xpack-openocd/bin/openocd.exe
  3. 确保路径中没有中文字符

问题 2: libusb_open() failed with LIBUSB_ERROR_ACCESS

错误信息

复制代码
Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
Error: open failed

可能原因

  1. STLink 正被其他程序占用(STM32CubeIDE、Keil、STLink Utility 等)
  2. 驱动问题
  3. 权限问题

解决方案

  1. 关闭所有可能占用 STLink 的程序
  2. 重新插拔 STLink USB 线
  3. 等待 3-5 秒后重试
  4. 以管理员身份运行 VS Code
  5. 重新安装 STLink 驱动

问题 3: No flash at address 0x08100000

错误信息

复制代码
Error: No flash at address 0x08100000

原因

STM32H7 双 Bank 模式,每个 Bank 1MB,擦除范围超出了单个 Bank

解决方案

  • 使用 🔥 擦除Flash (ST-Link) 擦除单个 Bank (1MB)
  • 或使用 🔥 擦除整个Flash (ST-Link) 擦除整个芯片 (2MB)

问题 4: OpenOCD: GDB Server Quit Unexpectedly

错误信息

复制代码
OpenOCD: GDB Server Quit Unexpectedly

可能原因

  1. openocd.cfg 包含了 init 命令(调试时不应包含)
  2. STLink 被占用

解决方案

  1. 确保 openocd.cfg(调试用)不含 init 命令
  2. 确保 openocd_flash.cfg(烧录用)包含 init 命令
  3. 检查 STLink 连接

问题 5: 编译错误 - 找不到工具链

错误信息

复制代码
CMake Error: CMake was unable to find a build program

解决方案

  1. 确保 CMake 和 Ninja 已安装并在 PATH 中
  2. 检查 cmake/gcc-arm-none-eabi.cmake 中的工具链路径
  3. 重新运行 配置 CMake 任务

问题 6: 调试时找不到符号表

错误信息

复制代码
No symbol table is loaded

解决方案

  1. 确保编译时使用了 Debug 模式(包含调试符号)
  2. 检查 launch.json 中的 executable 路径是否正确
  3. 确保 ELF 文件存在:build/Debug/项目名.elf

使用方法

日常开发工作流

方法 1: 使用快捷键
  1. 编译 :按 Ctrl+Shift+B
  2. 烧录 :运行任务 🚀 编译+烧录 (ST-Link)
  3. 调试 :按 F5
方法 2: 使用任务菜单
  1. Ctrl+Shift+P
  2. 输入 "Tasks: Run Task"
  3. 选择需要的任务
方法 3: 使用状态栏按钮(需要 Task Buttons 扩展)
  • 点击齿轮图标 $(gear) STM32H7 开发
  • 选择任务

调试操作

启动调试

  • F5
  • 或点击左侧调试图标,点击绿色播放按钮

调试操作

  • F5 - 继续运行
  • F10 - 单步跨越(Step Over)
  • F11 - 单步进入(Step Into)
  • Shift+F11 - 单步跳出(Step Out)
  • Ctrl+Shift+F5 - 重启调试
  • Shift+F5 - 停止调试

断点操作

  • 点击行号左侧设置/取消断点
  • 右键断点可设置条件断点

查看变量

  • 鼠标悬停在变量上
  • 查看左侧"变量"面板
  • 在"监视"面板添加表达式

烧录操作

快速烧录(推荐):

复制代码
运行任务:🚀 编译+烧录 (ST-Link)

仅烧录(不编译):

复制代码
运行任务:⬇️ 烧录程序-ELF (ST-Link)

完整擦除后烧录(遇到问题时使用):

复制代码
运行任务:🚀 编译+擦除+烧录 (ST-Link)

适配其他 STM32 芯片

STM32F1 系列

openocd.cfg 修改

tcl 复制代码
source [find target/stm32f1x.cfg]
adapter speed 1000  # F1 速度较慢

擦除 Flash

bash 复制代码
# STM32F103RET6 (512KB)
flash erase_address 0x08000000 0x80000

STM32F4 系列

openocd.cfg 修改

tcl 复制代码
source [find target/stm32f4x.cfg]
adapter speed 2000

擦除 Flash

bash 复制代码
# STM32F407VGT6 (1MB)
flash erase_address 0x08000000 0x100000

STM32G0/G4 系列

openocd.cfg 修改

tcl 复制代码
source [find target/stm32g0x.cfg]  # 或 stm32g4x.cfg
adapter speed 2000

芯片 ID 地址对照表

不同系列的芯片 ID 读取地址不同,用于 🔧 检测连接 任务:

系列 芯片 ID 地址
STM32F1 0x1FFFF7E0
STM32F4 0x1FFF7A10
STM32F7 0x1FF0F420
STM32H7 0x1FF1E800
STM32G0 0x1FFF7590
STM32G4 0x1FFF7590
STM32L4 0x1FFF7590

Flash 容量对照表

不同芯片的 Flash 大小不同,用于擦除任务:

芯片型号 Flash 大小 Bank 配置 擦除大小(十六进制)
STM32F103C8T6 64KB 单 Bank 0x10000
STM32F103RET6 512KB 单 Bank 0x80000
STM32F407VGT6 1MB 单 Bank 0x100000
STM32F746ZGT6 1MB 单 Bank 0x100000
STM32H743ZIT6 2MB 双 Bank (1MB+1MB) 0x100000 (单Bank)
STM32H750VBT6 128KB 单 Bank 0x20000

附录:完整工具链路径示例

示例 1: 标准安装路径

json 复制代码
"armToolchainPath": "D:/program/gcc-arm-none-eabi/bin"
"openOCDPath": "D:/program/xpack-openocd/bin/openocd.exe"

示例 2: 使用 STM32CubeIDE 自带工具链

json 复制代码
"armToolchainPath": "C:/ST/STM32CubeIDE_1.12.0/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.100.202210260954/tools/bin"
"openOCDPath": "C:/ST/STM32CubeIDE_1.12.0/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.openocd.win32_2.0.100.202109301221/tools/bin/openocd.exe"

示例 3: 自定义路径

json 复制代码
"armToolchainPath": "C:/Tools/ARM/gcc-arm-none-eabi-10.3-2021.10/bin"
"openOCDPath": "C:/Tools/OpenOCD/bin/openocd.exe"

总结检查清单

配置完成后,请逐项检查:

工具安装

  • ARM GCC 工具链已安装
  • OpenOCD 已安装
  • CMake 和 Ninja 已安装
  • STLink 驱动已安装
  • VS Code 扩展已安装(Cortex-Debug、C/C++、CMake Tools)

配置文件

  • .vscode/launch.json 已创建并修改路径
  • .vscode/tasks.json 已创建并修改路径
  • .vscode/settings.json 已创建
  • openocd.cfg 已创建(不含 init)
  • openocd_flash.cfg 已创建(含 init)

路径修改

  • launch.json 中的 executable 已修改为项目名
  • launch.json 中的 armToolchainPath 已修改
  • launch.json 中的 openOCDPath 已修改
  • tasks.json 中所有 OpenOCD 路径已修改
  • tasks.json 中所有 ELF/BIN 文件名已修改
  • openocd.cfg 中的芯片型号已修改

功能测试

  • 检测连接成功(显示芯片信息)
  • 编译成功(生成 ELF 和 BIN 文件)
  • 烧录成功(程序写入 Flash)
  • 调试成功(程序停在 main 函数)

参考资源


文档版本 :1.0
最后更新 :2026-01-20
适用芯片 :STM32H743ZIT6(可适配其他 STM32 系列)
测试环境:Windows 11, VS Code 1.85+, OpenOCD 0.12.0

相关推荐
飞来客isdn2 小时前
关于单片机与上位机串口通信的问题
单片机·嵌入式硬件·mcu
为何创造硅基生物2 小时前
STM32 串口的中断,空闲中断DMA
stm32·单片机·嵌入式硬件
youcans_2 小时前
【动手学STM32G4】(5)STM32G431之ADC采样
stm32·单片机·嵌入式硬件·adc
一丁_3 小时前
瞬态抑制二极管和稳压二极管在电路中的应用
单片机·嵌入式硬件
代码游侠3 小时前
嵌入式开发——ARM Cortex-A7内核和i.MX6处理器相关的底层头文件
arm开发·笔记·嵌入式硬件·学习·架构
VekiSon4 小时前
ARM架构——用汇编语言点亮 LED
汇编·arm开发·嵌入式硬件
炸膛坦客4 小时前
FreeRTOS 学习:(二十四)任务状态与信息查询 API 函数
stm32·操作系统·freertos
流浪_彩虹5 小时前
Bluejay电调固件电机参数分析
单片机
Y1rong5 小时前
STM32之GPIO
stm32·单片机·嵌入式硬件