全网最快无繁琐配置VScode中DAPLink/ST-Link一键仅下载/调试STM32

发布时间:2026/8/15 22:01:00
全网最快无繁琐配置VScode中DAPLink/ST-Link一键仅下载/调试STM32 ST官方推出了STM32CubeIDE for Visual Studio Code插件仅支持ST-LInk的调试没有仅下载功能也完全不支持DAPLink的调试和下载本文带你一次性解决。一、自行安装VScode、STM32CubeMXSTM32CubeIDE for Visual Studio Code插件ST-Link调试Cortex-Debug插件DAPLink调试及查看RTOS任务OpenOCDReleases · xpack-dev-tools/openocd-xpackArm GNU 工具链然后把Arm GNU 工具链和OpenOCD分别解压在英文目录里并在系统环境变量中添加OpenOCD的bin文件夹路径如图二、导入工程STM32CubeMX生成一个CMake工程然后在VScode打开这个文件夹之后右下角会提醒你是否配置为STM32工程点是没截上这个图过了一会上面选择Debug最后点击左下角齿轮编译显示“退出代码为 0”即为成功。三、文件配置打开左侧的.vscode\settings.json如图所示添加两行路径注意圈起来的地方有没有英文逗号cortex-debug.armToolchainPath: 替换为你解压的Arm GNU 工具链路径/bin, cortex-debug.openocdPath: 替换为你解压的OpenOCD路径/bin/openocd.exe然后如图右键新建两个文件分别为launch.json用于调试和tasks.json用于仅下载和flash.ps1(下载的具体实现包括自动识别芯片等)成品把下面这段代码粘到tasks.json里{ version: 2.0.0, tasks: [ { label: Flash (DAP-Link), type: shell, command: powershell, args: [ -ExecutionPolicy, Bypass, -File, ${workspaceFolder}/.vscode/flash.ps1, -Probe, daplink ], problemMatcher: [] }, { label: Flash (ST-LINK), type: shell, command: powershell, args: [ -ExecutionPolicy, Bypass, -File, ${workspaceFolder}/.vscode/flash.ps1, -Probe, stlink ], problemMatcher: [] } ] }把下面这段粘到launch.json里{ version: 0.2.0, configurations: [ { type: stlinkgdbtarget, request: launch, name: STM32Cube: Launch ST-Link GDB Server, origin: snippet, cwd: ${workspaceFolder}, preBuild: ${command:st-stm32-ide-debug-launch.build}, runEntry: main, imagesAndSymbols: [ { imageFileName: ${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1} } ] }, { name: DAPLink Debug, type: cortex-debug, request: launch, servertype: openocd, cwd: ${workspaceFolder}, executable: ${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}, configFiles: [ interface/cmsis-dap.cfg, ${input:mcuTarget} ], serverArgs: [ -c, adapter speed 500 ], runToEntryPoint: main, device: STM32H743ZIT6 } ], inputs: [ { id: mcuTarget, type: pickString, description: Select the openocd target config for your MCU family, options: [ target/stm32h7x.cfg, target/stm32f4x.cfg, target/stm32f3x.cfg, target/stm32f1x.cfg, target/stm32f0x.cfg, target/stm32g0x.cfg, target/stm32g4x.cfg, target/stm32l4x.cfg, target/stm32l5x.cfg, target/stm32u5x.cfg, target/stm32wbx.cfg, target/stm32c0x.cfg ], default: target/stm32h7x.cfg } ] }最后一步粘贴flash.ps1的东西# flash.ps1 - Flash the built .elf via openocd, auto-detecting the target config. # - Reads MCU family from the projects .ioc (Mcu.Name...) # - Maps family to the openocd target/*.cfg automatically # - Auto-finds the .elf under build/ # Usage (from .vscode tasks or terminal): # powershell -ExecutionPolicy Bypass -File .vscode/flash.ps1 -Probe daplink # powershell -ExecutionPolicy Bypass -File .vscode/flash.ps1 -Probe stlink param( [ValidateSet(daplink,stlink)] [string]$Probe daplink, [int]$Speed 1000 ) $ErrorActionPreference Stop # ---- project root: parent of .vscode if we are inside .vscode ---- $proj $PSScriptRoot if ((Split-Path $PSScriptRoot -Leaf) -eq .vscode) { $proj Split-Path $PSScriptRoot -Parent } # ---- 1) read MCU from .ioc ---- $ioc Get-ChildItem -Path $proj -Filter *.ioc -File | Select-Object -First 1 $mcu UNKNOWN if ($ioc) { $line (Get-Content $ioc.FullName | Where-Object { $_ -match ^Mcu\.Name } | Select-Object -First 1) if ($line) { $mcu ($line -split , 2)[1].Trim() } } Write-Host MCU: $mcu # ---- 2) map MCU family - openocd target cfg ---- $targetCfg switch -Regex ($mcu) { ^STM32H7 { stm32h7x.cfg } ^STM32F4 { stm32f4x.cfg } ^STM32F3 { stm32f3x.cfg } ^STM32F1 { stm32f1x.cfg } ^STM32F0 { stm32f0x.cfg } ^STM32G0 { stm32g0x.cfg } ^STM32G4 { stm32g4x.cfg } ^STM32L0 { stm32l0x.cfg } ^STM32L1 { stm32l1x.cfg } ^STM32L4 { stm32l4x.cfg } ^STM32L5 { stm32l5x.cfg } ^STM32U5 { stm32u5x.cfg } ^STM32WB { stm32wbx.cfg } ^STM32C0 { stm32c0x.cfg } default { stm32h7x.cfg } } $interface if ($Probe -eq stlink) { interface/stlink.cfg } else { interface/cmsis-dap.cfg } # ---- 3) find the .elf ---- $elf Get-ChildItem -Path (Join-Path $proj build) -Filter *.elf -File -Recurse -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $elf) { Write-Host [ERROR] no .elf found under build/; exit 1 } Write-Host Probe: $Probe Target: $targetCfg SWD speed: ${Speed}kHz Write-Host ELF: $($elf.FullName) # ---- 4) run openocd (cd into the elf dir so the filename needs no path/backslash) ---- Push-Location $elf.DirectoryName try { openocd -f $interface -f target/$targetCfg -c adapter speed $Speed -c program $($elf.Name) verify reset exit $code $LASTEXITCODE } finally { Pop-Location } exit $code四、开始使用最关心的下载来了点击最上面的框再点击“运行任务task”最后选择下载器然后是调试方法第五步在中间选择自己的芯片首先点击左侧的调试然后选择调试器最后点击绿色的播放按钮选择自己的芯片即可进入调试未来如果开发其它工程将.VScode文件夹复制到新工程即可有用的话不妨点个赞