仿真器概述
2026-07-28
STLINKv2
ST-LINK/V2 是ST官方推出的调试器,专用于STM8和STM32系列微控制器。它通过SWIM(STM8)和SWD/JTAG(STM32)接口与目标芯片通信,提供下载、调试和仿真功能。
ST-LINK/V2 驱动的主要作用是确保PC能够识别连接的调试器,并通过驱动程序支持的接口与微控制器进行有效的通信。它包括了基本的硬件识别、数据传输、固件更新等功能。
驱动程序设计用于跨平台兼容性,主要支持Windows操作系统。然而,对于一些对Linux和macOS系统有需求的开发者,也有社区支持的非官方驱动,可以实现类似的功能。
stlink-dap.cfg
在OpenOCD安装目录里可找到。openocd 安装;
# SPDX-License-Identifier: GPL-2.0-or-later
#
# STMicroelectronics ST-LINK/V1, ST-LINK/V2, ST-LINK/V2-1, STLINK-V3 in-circuit
# debugger/programmer
#
# This new interface driver creates a ST-Link wrapper for ARM-DAP named "dapdirect"
# Old ST-LINK/V1 and ST-LINK/V2 pre version V2J24 don't support "dapdirect"
#
# SWIM transport is natively supported
#
adapter driver st-link
st-link vid_pid 0x0483 0x3744 0x0483 0x3748 0x0483 0x374b 0x0483 0x374d 0x0483 0x374e 0x0483 0x374f 0x0483 0x3752 0x0483 0x3753 0x0483 0x3754
# transport select dapdirect_jtag
# transport select dapdirect_swd
# transport select swim
# Optionally specify the serial number of usb device
# e.g.
# adapter serial "\xaa\xbc\x6e\x06\x50\x75\xff\x55\x17\x42\x19\x3f"使用方式
在工程搭建实例中,需要修改.vscode/tasks.json及.vscode/launch.json来选中目标调试器。
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
// 清理编译输出目录
"label": "clean",
"type": "shell",
"command": "rm * -r",
"problemMatcher": []
},
{
// 生成编译配置文件
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
"-G",
"MinGW Makefiles",
".."
],
"dependsOn": [
"clean" // 生成编译配置文件前先清理编译输出目录
],
"problemMatcher": []
},
{
// 编译生成可执行文件
"label": "make",
"type": "shell",
"command": "make ", // 单线程编译
// "command": "make -j32", // 多线程编译,指定编译线程数为32
// "command": "make -j${env:NUMBER_OF_PROCESSORS}", // 多线程编译,指定编译线程数为CPU核心数
// 添加CTRL+SHIFT+B快捷键,执行make任务
"group": {
"kind": "build",
"isDefault": true
}
},
{
// 重新生成编译配置文件并编译生成可执行文件
"label": "rebuild",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
],
"problemMatcher": []
},
{
// 下载可执行文件到目标芯片
"type": "shell",
"label": "download",
"command": "openocd",
"args": [
"-f",
"../stlink-dap.cfg", // 烧录时 STLINKV2 仿真器配置文件路径
// "../cmsis-dap.cfg", // 烧录时 DAP-Link 仿真器配置文件路径
// "../jlink.cfg", // 烧录时 J-Link 仿真器配置文件路径
"-f",
"../stm32f1x.cfg", // 目标芯片配置文件路径
"-c",
"init",
"-c",
"halt",
"-c",
"program app.bin 0x8000000", // 下载可执行文件到目标芯片的起始地址
"-c",
"reset",
"-c",
"shutdown"
],
"group": "build",
"dependsOn": "make",
"problemMatcher": []
}
]
}{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug STM32",
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/build/app.elf", // 可执行文件路径
"request": "launch",
"type": "cortex-debug", // 调试类型
"runToEntryPoint": "main", // 运行到入口点
// "runToEntryPoint": "SystemInit",
"servertype": "openocd", // 调试服务器类型
"configFiles": [
"${workspaceFolder}/stlink-dap.cfg", // 仿真时 STLINKV2 仿真器配置文件路径
// "${workspaceFolder}/cmsis-dap.cfg", // 仿真时 DAP-Link 仿真器配置文件路径
// "${workspaceFolder}/jlink.cfg", // 仿真时 J-Link 仿真器配置文件路径
"${workspaceFolder}/stm32f1x.cfg", // 目标芯片配置文件路径
],
// "showDevDebugOutput": "raw", // 显示调试输出
"svdFile": "${workspaceFolder}/STM32F103xx.svd", // 寄存器描述文件路径
"preLaunchTask": "make"
}
]
}常见问题
1. 提示目标电压低
Open On-Chip Debugger 0.12.0 (2023-01-14-23:37)
Licensed under GNU GPL v2
For bug reports, read
http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "dapdirect_swd". To override use 'transport select <transport>'.
Info : STLINK V2J41S7 (API v2) VID:PID 0483:3748
Info : Target voltage: 0.000000
Error: target voltage may be too low for reliable debugging
Error: init mode failed (unable to connect to the target)解决方法:
ST-LINK/V2 通过1-2引脚检测目标板的电压,当检测到电压低于3.3V时会报错。短接仿真器3.3V电源到1-2引脚即可解决。
2. 提示固件版本过低
Info : STLINK V2J24 ...
Warn : Communication to stlink failed, firmware version too low解决方法:
使用 ST-Link Utility 或 STM32CubeProgrammer 更新 ST-LINK/V2 固件至最新版本。
引脚定义
ST-LINK/V2 标准 20-pin(JTAG/SWD 混合接口)引脚定义:
| 引脚 | 信号 | 说明 |
|---|---|---|
| 1 | VCC | 目标板参考电压(3.3V) |
| 2 | SWDIO | 数据线 |
| 3 | GND | 地 |
| 4 | SWCLK | 时钟线 |
| 5 | GND | 地 |
| 6 | SWO | 可选:串行数据输出线 |
| 7 | TMS | JTAG模式选择 |
| 8 | TCK | JTAG时钟 |
| 9 | GND | 地 |
| 10 | TDO | JTAG数据输出 |
| 11 | nRESET | 目标板复位 |
| 12 | TDI | JTAG数据输入 |
| 13~20 | NC | 未连接 |
提示:使用 SWD 模式调试时,仅需连接 VCC、GND、SWDIO、SWCLK 四根线即可正常工作。
DAP-Link
DAP-Link(CMSIS-DAP)是ARM官方推出的开源调试器方案,基于ARM Cortex微控制器调试接口标准(CMSIS-DAP)实现。与ST-LINK不同,DAP-Link采用通用ARM调试协议,支持所有ARM Cortex系列芯片(包括STM32、GD32、AIR32、NXP、Nordic等),具有更好的跨平台和跨厂商兼容性。
DAP-Link的核心优势在于其开源特性和免驱动设计。大多数主流操作系统(Windows 10/11、Linux、macOS)均可免驱识别,即插即用。开发者也可以使用任意带有USB接口的ARM开发板(如NUCLEO板上的ST-LINK/V2-1、树莓派Pico等)自行制作DAP-Link调试器。
cmsis-dap.cfg
在OpenOCD安装目录里可找到。openocd 安装;
# SPDX-License-Identifier: GPL-2.0-or-later
#
# ARM CMSIS-DAP compliant adapter
#
# This is the generic CMSIS-DAP adapter configuration file.
# It can be used with any CMSIS-DAP compliant adapter (DAP-Link, Daplink, etc.)
#
adapter driver cmsis-dap
# transport select cmsis-dap
# transport select swd
# transport select jtag
# Adapter clock in kHz
# adapter speed 1000
# Optionally specify the serial number of usb device
# e.g.
# cmsis-dap serial 12345678901234567890使用方式
在工程搭建实例中,将 ST-LINK 替换为 DAP-Link 时,只需将配置文件切换为 cmsis-dap.cfg,修改.vscode/tasks.json及.vscode/launch.json中的配置路径即可。
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
// 清理编译输出目录
"label": "clean",
"type": "shell",
"command": "rm * -r",
"problemMatcher": []
},
{
// 生成编译配置文件
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
"-G",
"MinGW Makefiles",
".."
],
"dependsOn": [
"clean" // 生成编译配置文件前先清理编译输出目录
],
"problemMatcher": []
},
{
// 编译生成可执行文件
"label": "make",
"type": "shell",
"command": "make ", // 单线程编译
// "command": "make -j32", // 多线程编译,指定编译线程数为32
// "command": "make -j${env:NUMBER_OF_PROCESSORS}", // 多线程编译,指定编译线程数为CPU核心数
// 添加CTRL+SHIFT+B快捷键,执行make任务
"group": {
"kind": "build",
"isDefault": true
}
},
{
// 重新生成编译配置文件并编译生成可执行文件
"label": "rebuild",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
],
"problemMatcher": []
},
{
// 下载可执行文件到目标芯片
"type": "shell",
"label": "download",
"command": "openocd",
"args": [
"-f",
// "../stlink-dap.cfg", // 烧录时 STLINKV2 仿真器配置文件路径
"../cmsis-dap.cfg", // 烧录时 DAP-Link 仿真器配置文件路径
// "../jlink.cfg", // 烧录时 J-Link 仿真器配置文件路径
"-f",
"../stm32f1x.cfg", // 目标芯片配置文件路径
"-c",
"init",
"-c",
"halt",
"-c",
"program app.bin 0x8000000", // 下载可执行文件到目标芯片的起始地址
"-c",
"reset",
"-c",
"shutdown"
],
"group": "build",
"dependsOn": "make",
"problemMatcher": []
}
]
}{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug STM32",
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/build/app.elf", // 可执行文件路径
"request": "launch",
"type": "cortex-debug", // 调试类型
"runToEntryPoint": "main", // 运行到入口点
// "runToEntryPoint": "SystemInit",
"servertype": "openocd", // 调试服务器类型
"configFiles": [
// "${workspaceFolder}/stlink-dap.cfg", // 仿真时 STLINKV2 仿真器配置文件路径
"${workspaceFolder}/cmsis-dap.cfg", // 仿真时 DAP-Link 仿真器配置文件路径
// "${workspaceFolder}/jlink.cfg", // 仿真时 J-Link 仿真器配置文件路径
"${workspaceFolder}/stm32f1x.cfg", // 目标芯片配置文件路径
],
// "showDevDebugOutput": "raw", // 显示调试输出
"svdFile": "${workspaceFolder}/STM32F103xx.svd", // 寄存器描述文件路径
"preLaunchTask": "make"
}
]
}常见问题
1. 无法识别 DAP-Link 设备
Error: unable to find CMSIS-DAP device
Error: init failed解决方法:
- 检查USB连接线是否正常,建议使用高质量数据线(部分充电线缺少数据通道)
- 检查设备管理器中是否存在未知设备,尝试重新安装驱动
- 部分DAP-Link(如使用PY32F003制作的版本)需要手动安装驱动,可使用 Zadig 工具将驱动替换为 WinUSB
- 若使用自制 DAP-Link,检查固件是否正确烧录
2. 连接不稳定或断开
Error: DAP operation failed (error during a DAP transfer)
Info : Communication failure, lost connection with target解决方法:
- 降低SWD时钟频率,在
cmsis-dap.cfg中设置adapter speed 100(100kHz)或adapter speed 10 - 缩短SWD线缆长度,建议不超过20cm
- 增加上拉电阻,SWDIO和SWCLK分别接10kΩ上拉到3.3V
- 检查目标板供电是否稳定
3. 提示无法进入调试模式
Error: target not halted
Error: timed out while waiting for target halted解决方法:
- 检查目标芯片复位电路是否正常
- 尝试手动复位目标板后再次连接
- 检查目标芯片是否被读保护(如STM32的RDP等级不为0),如有需要先解除读保护
- 确认目标芯片SWD接口引脚未被复用为GPIO
DAP-Link 自制方案
引脚定义
DAP-Link 标准 10-pin(Cortex Debug Connector)引脚定义:
| 引脚 | 信号 | 说明 |
|---|---|---|
| 1 | VCC | 目标板参考电压 |
| 2 | SWDIO | 数据线 |
| 3 | GND | 地 |
| 4 | SWCLK | 时钟线 |
| 5 | GND | 地 |
| 6 | SWO | 可选:串行数据输出线(调试日志) |
| 7 | KEY | 防反插(空脚) |
| 8 | NC | 未连接 |
| 9 | GND | 地 |
| 10 | nRESET | 目标板复位 |
提示:大部分 DAP-Link 仅需连接 VCC、GND、SWDIO、SWCLK 四根线即可正常工作,SWO 用于
ITM调试日志输出,nRESET 为可选。
J-Link
J-Link 是 SEGGER 公司推出的专业级调试器,支持几乎所有 ARM Cortex 系列芯片,以及 RISC-V 等架构。凭借其高速下载、稳定调试和丰富的软件生态,J-Link 被广泛认为是业界标杆级的调试器。
J-Link 的核心优势在于其极快的下载速度和强大的调试功能。配合 SEGGER 提供的 J-Link Commander、J-Flash、Ozone 等工具链,可实现高效的开发调试体验。J-Link 支持 JTAG 和 SWD 两种调试协议,并提供虚拟串口(VCOM)功能。
J-Link 提供多个版本:J-Link BASE(基础版)、J-Link PLUS(增强版)、J-Link ULTRA+(高速版)以及 J-Link PRO(远程调试版),以及价格低廉的 J-Link EDU(教育版)和 J-Link OB(板载版)。
jlink.cfg
在OpenOCD安装目录里可找到。openocd 安装;
注意: jlink 没有默认模式,需要手动选择模式。
# SPDX-License-Identifier: GPL-2.0-or-later
#
# SEGGER J-Link adapter
#
# This is the generic J-Link adapter configuration file.
# It can be used with any J-Link model (BASE, PLUS, ULTRA+, PRO, EDU, OB)
#
adapter driver jlink
# transport select jtag
transport select swd # 使用SWD模式
# Adapter clock in kHz
# adapter speed 1000
# Optionally specify the serial number of usb device
# e.g.
# jlink serial 123456789
# For J-Link OB (on-board) with specific VID/PID
# jlink vid_pid 0x1366 0x0105 0x1366 0x0101使用方式
在工程搭建实例中,将 ST-LINK 替换为 J-Link 时,只需将配置文件切换为 jlink.cfg,修改.vscode/tasks.json及.vscode/launch.json中的配置路径即可。
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
// 清理编译输出目录
"label": "clean",
"type": "shell",
"command": "rm * -r",
"problemMatcher": []
},
{
// 生成编译配置文件
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
"-G",
"MinGW Makefiles",
".."
],
"dependsOn": [
"clean" // 生成编译配置文件前先清理编译输出目录
],
"problemMatcher": []
},
{
// 编译生成可执行文件
"label": "make",
"type": "shell",
"command": "make ", // 单线程编译
// "command": "make -j32", // 多线程编译,指定编译线程数为32
// "command": "make -j${env:NUMBER_OF_PROCESSORS}", // 多线程编译,指定编译线程数为CPU核心数
// 添加CTRL+SHIFT+B快捷键,执行make任务
"group": {
"kind": "build",
"isDefault": true
}
},
{
// 重新生成编译配置文件并编译生成可执行文件
"label": "rebuild",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
],
"problemMatcher": []
},
{
// 下载可执行文件到目标芯片
"type": "shell",
"label": "download",
"command": "openocd",
"args": [
"-f",
// "../stlink-dap.cfg", // 烧录时 STLINKV2 仿真器配置文件路径
// "../cmsis-dap.cfg", // 烧录时 DAP-Link 仿真器配置文件路径
"../jlink.cfg", // 烧录时 J-Link 仿真器配置文件路径
"-f",
"../stm32f1x.cfg", // 目标芯片配置文件路径
"-c",
"init",
"-c",
"halt",
"-c",
"program app.bin 0x8000000", // 下载可执行文件到目标芯片的起始地址
"-c",
"reset",
"-c",
"shutdown"
],
"group": "build",
"dependsOn": "make",
"problemMatcher": []
}
]
}{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug STM32",
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/build/app.elf", // 可执行文件路径
"request": "launch",
"type": "cortex-debug", // 调试类型
"runToEntryPoint": "main", // 运行到入口点
// "runToEntryPoint": "SystemInit",
"servertype": "openocd", // 调试服务器类型
"configFiles": [
// "${workspaceFolder}/stlink-dap.cfg", // 仿真时 STLINKV2 仿真器配置文件路径
// "${workspaceFolder}/cmsis-dap.cfg", // 仿真时 DAP-Link 仿真器配置文件路径
"${workspaceFolder}/jlink.cfg", // 仿真时 J-Link 仿真器配置文件路径
"${workspaceFolder}/stm32f1x.cfg", // 目标芯片配置文件路径
],
// "showDevDebugOutput": "raw", // 显示调试输出
"svdFile": "${workspaceFolder}/STM32F103xx.svd", // 寄存器描述文件路径
"preLaunchTask": "make"
}
]
}常见问题
1. 无法识别 J-Link 设备
Error: unable to find J-Link device
Error: init failed解决方法:
- 检查USB连接线是否正常
- 部分 J-Link 克隆版可能无法被最新版驱动识别,可尝试使用 SEGGER 提供的 J-Link Commander 工具检查固件版本
- 若使用 J-Link OB(板载版),需在配置文件中添加
jlink vid_pid 0x1366 0x0105 - 检查设备管理器中 J-Link 驱动是否正常安装,解决openocd无法识别jlink的问题
- 根本原因 :简单来说就是需要把 j-link 原来的驱动更换为 WinUSB 驱动才可以被 openocd 识别。
- 影响 :转换成 openocd 可以识别的 WinUSB 驱动之后,j-scope,j-flash 等软件都无法使用,也无法在 KEIL 中使用 j-link 烧录和调试程序了。
- 还原驱动的方法 :安装J-Link驱动后,
win + x-> 设备管理器->右键点击调试器设备->更新驱动程序->自动搜索驱动程序。 - 查看当前驱动 :
win + x-> 设备管理器->右键点击调试器设备->属性。提供商是SEGGER代表 J-Link 驱动,libwdi代表 WinUSB 驱动。
2. 提示盗版克隆
SEGGER J-Link Commander (CLI)
*** J-Link clone detected ***解决方法:
- 使用正版 J-Link 设备
- 部分旧版本 J-Link 固件可降级使用,但不建议
- 可考虑使用 DAP-Link 作为替代方案
3. 连接不稳定或断开
Error: J-Link connection failed
Info : Communication failure解决方法:
- 降低SWD/JTAG时钟频率,在
jlink.cfg中设置adapter speed 100(100kHz) - 缩短线缆长度,建议不超过20cm
- 检查目标板供电是否稳定
- 检查SWDIO和SWCLK是否接10kΩ上拉电阻到3.3V
4. 提示无法进入调试模式
Error: target not halted
Error: timed out while waiting for target halted解决方法:
- 检查目标芯片复位电路是否正常
- 尝试手动复位目标板后再次连接
- 检查目标芯片是否被读保护
- 确认目标芯片SWD/JTAG接口引脚未被复用为GPIO
引脚定义
J-Link 标准 20-pin(ARM JTAG/SWD 混合接口)引脚定义:
| 引脚 | 信号 | 说明 |
|---|---|---|
| 1 | VTref | 目标板参考电压 |
| 2 | SWDIO/TMS | 数据线 / JTAG模式选择 |
| 3 | GND | 地 |
| 4 | SWCLK/TCK | 时钟线 / JTAG时钟 |
| 5 | GND | 地 |
| 6 | SWO/TDO | 可选:串行数据输出 / JTAG数据输出 |
| 7 | NC/TDI | JTAG数据输入(SWD模式未使用) |
| 8 | NC/TRST | JTAG复位(SWD模式未使用) |
| 9 | GND | 地 |
| 10 | nRESET | 目标板复位 |
| 11~20 | NC | 未连接 |
提示:使用 SWD 模式调试时,仅需连接 VTref、GND、SWDIO、SWCLK 四根线即可正常工作。J-Link 的 VTref 引脚用于检测目标板电压,不可悬空。