1. 硬件准备与连接
1.1 所需硬件
- STM32F103C8T6 最小系统板
- ST-LINK v2.1 调试器
- 连接线(杜邦线)
1.2 硬件连接
ST-LINK v2.1 ↔ STM32F103C8T6 连接方式:
| ST-LINK v2.1 引脚 | STM32F103C8T6 引脚 | 功能说明 | 
|---|---|---|
| SWDIO | PA13 | 数据线 | 
| SWCLK | PA14 | 时钟线 | 
| GND | GND | 共地 | 
| 3.3V (可选) | 3.3V (可选) | 供电 | 
连接注意事项:
- 确保目标板的 BOOT0 引脚已接地(低电平)
- 如目标板已有独立供电,可不连接 3.3V 引脚以避免电源冲突
- 使用短而优质的连接线,确保连接牢固
2. 软件环境安装与配置
2.1 安装编译工具链
sudo apt update
sudo apt install build-essential git
sudo apt install arm-none-eabi-gcc arm-none-eabi-binutils arm-none-eabi-newlib
2.2 安装调试工具
sudo apt install gdb-multiarch openocd
2.3 安装配置 Vimspector
- 在 - ~/.vimrc中添加插件配置(以 vim-plug 为例):- call plug#begin('~/.vim/plugged') Plug 'puremourning/vimspector' call plug#end()
- 安装插件: - :PlugInstall
- 安装调试适配器: - :VimspectorInstall --enable-c- 或使用 Python 脚本安装: - # 进入 vimspector 目录,通常在你的插件管理路径下 cd ~/.vim/plugged/vimspector # 路径请根据实际情况修改 ./install_gadget.py --enable-c
- 配置 Vimspector 快捷键(添加到 - ~/.vimrc):- nmap <Leader>dd <Plug>VimspectorContinue nmap <Leader>dx <Plug>VimspectorStop nmap <Leader>dR <Plug>VimspectorRestart nmap <Leader>dp <Plug>VimspectorPause nmap <Leader>db <Plug>VimspectorToggleBreakpoint nmap <Leader>dc <Plug>VimspectorToggleConditionalBreakpoint nmap <Leader>dn <Plug>VimspectorStepOver nmap <Leader>di <Plug>VimspectorStepInto nmap <Leader>do <Plug>VimspectorStepOut nmap <Leader>dr <Plug>VimspectorRunToCursor
3. Vimspector 调试配置
3.1 创建 .vimspector.json 文件
在项目根目录创建 .vimspector.json 文件:
{
  "configurations": {
    "Launch STM32": {
      "adapter": "vscode-cpptools",
      "configuration": {
        "request": "launch",
        "type": "cppdbg",
        "program": "${workspaceRoot}/build/software.elf", // 确保此路径正确
        "cwd": "${workspaceRoot}",
        "targetArchitecture": "arm",
        "MIMode": "gdb",
        "MIDebuggerPath": "gdb-multiarch",
        "stopOnEntry": true,
        "stopAtConnect": true,
        "serverAddress": "localhost",
        "serverPort": 3333,
        "serverType": "openocd",
        "setupCommands": [
          {
            "description": "Specify executable file", // 明确指定可执行文件
            "text": "file ${workspaceRoot}/build/software.elf",
            "ignoreFailures": false
          },
          {
            "description": "Connect to OpenOCD",
            "text": "target extended-remote :3333"
          },
          {
            "description": "Reset and halt",
            "text": "monitor reset halt",
            "ignoreFailures": true
          },
          {
            "description": "Load program",
            "text": "load",
            "ignoreFailures": false
          }
        ],
        "launchCompleteCommand": "exec-continue"
      }
    }
  }
}
3.2 工作区路径说明
- ${workspaceRoot}表示 Vim 的当前工作目录
- 使用 :pwd命令可查看当前工作目录
- 使用 :cd /path/to/project可切换工作目录
4. 调试流程
4.1 启动 OpenOCD
打开终端,运行以下命令:
sudo openocd -f interface/stlink.cfg -f target/stm32f1x.cfg
4.2 启动 Vim 并打开项目
cd /path/to/project
vim src/main.c
4.3 启动调试会话
在 Vim 中执行:
<Leader>dd