第 4 節

插件使用教程

0瀏覽次數0訪問次數--跳出率--平均停留

CubeMX安装FW

Click Help

Select Manage embedded software packages, and check the first and latest firmware for STM32F1, F4, and H7.

Click install.

可以不用登录账号选do not ask me again

Then wait for the download and installation to finish.

Just download it.

VScode安装插件

Then open VSCode and enter the following command in the terminal:

code

Then you can configure a separate environment for the CubeIDE plugin to use, avoiding conflicts with the default environment.

alt text

进行一些设置,自己按习惯来,不懂的暂时按我的来也行

alt text

Select STM32

alt text

Then install some plugins.

Find the installation of the STM32CubeIDE for Visual Studio Code plugin below.

alt text

On the right, this prompt appears asking you to select and install (requires a reliable scientific network).

alt text

Next, some environment installations will be performed.

alt text

You can also install some other plugins, such as Codex and others. These are up to you.

alt text

OpenOCD使用事项

选择debugger和mcu型号

OpenOCD 一般通过 .cfg 配置文件确定:使用什么调试器 + 连接什么 MCU

openocd -f interface/调试器.cfg -f target/芯片系列.cfg

第一个参数:看你用什么调试器

|调试器|OpenOCD 配置|Explanation|

|---|---|---| |ST-Link V2 / V2-1 / V3|"interface/stlink.cfg"|STM32 最常见|

|DAPLink|"interface/cmsis-dap.cfg"|DAPLink 实现 CMSIS-DAP|

|CMSIS-DAP|"interface/cmsis-dap.cfg"|很多第三方调试器使用|

|J-Link|"interface/jlink.cfg"|SEGGER J-Link| |FTDI 类 JTAG/SWD|"interface/ftdi/xxx.cfg"|根据具体硬件选择|

第二个参数:看 STM32 哪个系列

|STM32 series|常见型号示例|OpenOCD Target|

|---|---|---| |STM32C0|C011、C031、C071|"target/stm32c0x.cfg"| |STM32F0|F030、F072|"target/stm32f0x.cfg"| |STM32F1|F103C8、F103RC|"target/stm32f1x.cfg"| |STM32F2|F205、F207|"target/stm32f2x.cfg"| |STM32F3|F303、F334|"target/stm32f3x.cfg"| |STM32F4|F405、F407、F429、F446|"target/stm32f4x.cfg"| |STM32F7|F746、F767|"target/stm32f7x.cfg"| |STM32G0|G030、G070、G0B1|"target/stm32g0x.cfg"| |STM32G4|G431、G474|"target/stm32g4x.cfg"| |STM32H5|H503、H563、H573|"target/stm32h5x.cfg"| |STM32H7|H743、H750、H745、H747|"target/stm32h7x.cfg"| |STM32H7RS|H7R3、H7S3|"target/stm32h7rsx.cfg"| |STM32L0|L031、L073|"target/stm32l0.cfg"| |STM32L1|L151、L152|"target/stm32l1.cfg"| |STM32L4 / L4+|L432、L476、L496、L4R5|"target/stm32l4x.cfg"| |STM32L5|L552、L562|"target/stm32l5x.cfg"| |STM32N6|N657 等|"target/stm32n6x.cfg"|

|STM32U0|U031、U073|"target/stm32u0x.cfg"| |STM32U3|U385 等|"target/stm32u3x.cfg"|

|STM32U5|U575、U585、U5A5|"target/stm32u5x.cfg"| |STM32WB|WB55 等|"target/stm32wbx.cfg"|

|STM32WBA2|WBA2xx|"target/stm32wba2x.cfg"| |STM32WBA5|WBA5xx|"target/stm32wba5x.cfg"| |STM32WBA6|WBA6xx|"target/stm32wba6x.cfg"| |STM32WL|WL55、WLE5|"target/stm32wlx.cfg"| |STM32C5|C53、C54、C55、C56、C59、C5A|见下方说明|

STM32C5 要特别注意:

现在不要把 C5 写死成target/stm32c5x.cfg,

原因是 STM32C5 的 OpenOCD 支持非常新。2026 年 7 月才有 stm32c5x.cfg 支持补丁提交到 OpenOCD Gerrit;补丁里的文件名确实就是target/stm32c5x.cfg。

但是现在还没完全发行到OpenOCD,需要等一段时间。

测试设备连接

我们用的stm32f103c8和st-link:

所以用

openocd -f interface/stlink.cfg -f target/stm32f1x.cfg

可以插上之后测试一波:

照上图所示,已经连接成功了。

几个关键日志分别说明:

Info : STLINK V2J48S7 ...
→ OpenOCD 正常识别 ST-Link。

Info : Target voltage: 3.150483
→ 目标板供电正常,约 3.15 V。

Info : SWD DPIDR 0x1ba01477
→ SWD 通信已经建立。

Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected
→ 已经真正识别并连接 MCU 核心。

Info : target has 6 breakpoints, 4 watchpoints
→ Cortex-M3 的硬件断点/数据观察点正常。

Info : Examination succeed
→ Target 初始化成功。

最后:
Info : [stm32f1x.cpu] starting gdb server on 3333
Info : Listening on port 3333 for gdb connections
→ OpenOCD 的 GDB Server 已经正式启动,可以给 VS Code / STM32Cube 调试器连接了。

Project Creation and Testing

Create a project using CubeMX

Click the button to select a microcontroller.

Search for the corresponding chip, and double-click the chip option.

Perform some configuration. The following are all very basic things that you definitely already know before watching this video.

Open any IO for testing, such as the LED's GPIO.

FreeRTOS also needs to be configured.

这里也打开。

These folders also need to be configured properly. Finally, select CMake for Toolchain and choose GCC for the compiler (it's normal not to have the compiler selection option in version 6.14.1 and earlier).

Configure and compile the project.

在工程文件夹打开终端(或者直接在此处打开VScode也行)

alt text

code .

After opening VSCode, remember to switch to the STM32 configuration.

alt text

Select Yes here to configure the CMake preset.

alt text

Generally, choose Debug.

alt text

可能谈一个很长的对话框,是这个STM32Cube Core提示的,你直接点yes开始下载(这是自动部署各种环境的)

反正右下角是STM32Cube Core提示让你安装的,你安就完了,每个版本提示可能不一样,总之安装就完了。

等上面的环境部署完毕后,

找个C语言的代码文件打开,然后右下角会提示安装一个C/C++插件,这个可以安装也可以不安装,他也带代码提示,但是他的代码提示对比自带的clangd简直是弱爆了,如果你是新手,你不会设置代码提示,建议按我下面的操作来,直接别装这个C/C++插件。

默认用的是clangd代码提示

You can test the code suggestions to see how powerful they are.

alt text

For compilation, either of these two "build" options in the image will work.

alt text

Porting author tungchiahui's standard C/C++ project template

Use the git clone command to clone the repository: https://github.com/tungchiahui/STM32HAL_CMake_CPP_Template

git clone https://github.com/tungchiahui/STM32HAL_CMake_CPP_Template.git

Copy all files and folders (except .git) from the repository into our STM32 project directory.

alt text

main.c:

这里面加上如下:

#include "cpp_interface.h"

在main函数里找合适位置加上如下函数:

一般裸机的话放在while(1)上面。

RTOS放在main()里仅靠while(1)的那几个RTOS相关的开启函数的上面。

cpp_main();

Then, go to the top-level CMakeLists.txt and add this line to reference our own CMakeLists.txt.

# Add USER generated sources
add_subdirectory(cmake/user)

在最顶上,也要把编程语言的标准改一下(这一步在README.md里写了)

# Setup compiler settings
set(CMAKE_C_STANDARD 11 CACHE STRING "C language standard")
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)

set(CMAKE_CXX_STANDARD 20 CACHE STRING "C++ language standard")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)

Then open the applications folder, and create led_task.cpp and led_task.h in the Src and Inc folders respectively, with the following contents:

led_task.cpp:

#include "led_task.h"
#include "cmsis_os.h"
#include "stm32f1xx_hal.h" 

int32_t t;

extern "C"
void StartDefaultTask(void *argument)
{
  for(;;)
  {
    HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
    ++t;
    osDelay(500);
  }
}

led_task.h:

#ifndef LED_TASK_H
#define LED_TASK_H

#include "cpp_interface.h"


#endif

Then open CMakeLists.txt in the cmake/user folder and add the newly created led_task.cpp to it.

Detailed explanation (optional): The cmake/stm32cubemx under CMakeLists.txt here is managed by CubeMX. If you regenerate code with CubeMX, the contents of this file will be overwritten. However, the CMakeLists.txt at the workspace root will not be overwritten, and it provides some areas for adding source and header files. But this can make the file too cluttered. So we chose to create a new user folder, set up a CMakeLists.txt inside it, and then use the top-level CMakeLists.txt to load this sub-CMakeLists.txt. This sub-CMakeLists.txt makes it easier for us to modify, and the file structure is clearer. (You don't need to create these yourself—I've already added them to the template, and they were copied over when you copied the template above.)

Add the cpp file as shown in the image below.

Great success, let's try compiling once. As shown in the image below, all the newly added files have been compiled.

alt text

Download the program to the board

下载之前首先要先配置:

如果你不是stlink和jlink,接着往下看

按下图的来点击,你看看你是什么debugger,你就选哪个。

如果这里看不到对应设备,遇到权限问题,请查看Linux 下 USB 权限问题。

可以更新下STlink的驱动:

First, install the jlink-gdbserver bundle, as shown in the image below:

alt text

使用openocd服务驱动原生gdb调试器

当你要使用除了ST-Link和J-Link以外的调试器时:

先点调试,让他生成launch.json:

openOCD接管选STM32Cube: STM32 Launch GDB:

然后选openocd:

过一会儿,肯定会调试失败,然后提示你open launch.json

然后再点STM32Cube: STM32 Launch GDB:

launch.json内容如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "stgdbtarget",
            "request": "launch",
            "name": "STM32Cube: Launch Generic GDB Server",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "program": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
            "gdb": "${command:st-stm32-ide-debug-launch.get-gdb-executable}",
            "deviceName": "${command:st-stm32-ide-debug-launch.get-device-name}",
            "deviceCore": "${command:st-stm32-ide-debug-launch.get-core-name}",
            "deviceTrustzone": "${command:st-stm32-ide-debug-launch.get-trustzone-status}",
            "serverExe": "",
            "serverParameters": [],
            "serverHost": "localhost",
            "serverPort": "",
            "serverCwd": "",
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "imageOffset": "",
                    "symbolFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "symbolOffset": ""
                }
            ]
        },
        {
            "type": "stgdbtarget",
            "request": "attach",
            "name": "STM32Cube: Launch GDB Client",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "program": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
            "gdb": "${command:st-stm32-ide-debug-launch.get-gdb-executable}",
            "serverHost": "localhost",
            "serverPort": "${command:st-stm32-ide-debug-launch.get-server-port}",
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "imageOffset": "",
                    "symbolFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "symbolOffset": ""
                }
            ]
        }
    ]
}

我们需要改哪里呢?

主要是:

  • serverExe : 就填二进制程序名 openocd或者openocd.exe
  • serverHost :OpenOCD GDB Server 默认监听 localhost,一般不用改
  • serverPort :OpenOCD 默认 GDB 端口 为 3333
  • serverCwd : 填serverExe这个二进制程序在哪个目录底下
  • 添加liveWatch参数(重要)
  1. serverExe:

Linux填openocd

Windows填openocd.exe

  1. serverParameters按下面这个格式来:

那俩-f的参数由openocd的两个-f参数可知:

  • interface/stlink.cfg
  • target/stm32f1x.cfg

但是除了这俩,还需要一些参数:

            "serverParameters": [                
                "-f",
                "interface/stlink.cfg",

                "-c",
                "transport select swd",

                "-f",
                "target/stm32f1x.cfg",

                "-c",
                "$_TARGETNAME configure -gdb-max-connections 2"],

这里建议按上面这个顺序排布参数:先探针 → 再通信协议 → 再 MCU → 最后修改这个 MCU target 的参数

这里的-c transport select xxx是选协议:

|实际调试方式|配置|

|---|---| |STM32 常用 SWD|transport select swd|

|JTAG|transport select jtag|

咱们一般都是swd,所以选transport select swd。

而这个-c $_TARGETNAME configure -gdb-max-connections 2是允许这个 OpenOCD target 同时接受最多 2 个 GDB 客户端连接。

正常调试已经占了一个:

OpenOCD :3333
    ↑
    └── GDB #1
         普通 Debug

咱们还需要用到Live Watch,所以把参数设置为2之后:

                  ┌─ GDB #1:普通 Debug
STM32 ← OpenOCD ──┤
                  └─ GDB #2:Live Watch
  1. serverCwd:

用终端命令直接查找下:

# Linux
which openocd

# Windows
where.exe openocd

Linux如上图是/usr/bin/openocd,所以serverCwd填/usr/bin。

Windows如上图是C:\xpack-openocd-0.12.0-7\bin\openocd.exe,所以serverCwd填C:\xpack-openocd-0.12.0-7\bin。

但是不能直接填C:\xpack-openocd-0.12.0-7\bin,因为在json中\是转义字符,所以傻逼Windows应该填C:/xpack-openocd-0.12.0-7/bin或者双反斜杠C:\\xpack-openocd-0.12.0-7\\bin。

  1. 添加liveWatch参数

在serverCwd后面,runEntry前面加上:

            "liveWatch": {
                "enabled": true,
                "samplesPerSecond": "4"
            },

最后:

Linux:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "stgdbtarget",
            "request": "launch",
            "name": "STM32Cube: Launch Generic GDB Server",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "program": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
            "gdb": "${command:st-stm32-ide-debug-launch.get-gdb-executable}",
            "deviceName": "${command:st-stm32-ide-debug-launch.get-device-name}",
            "deviceCore": "${command:st-stm32-ide-debug-launch.get-core-name}",
            "deviceTrustzone": "${command:st-stm32-ide-debug-launch.get-trustzone-status}",
            "serverExe": "openocd",
            "serverParameters": [                
                "-f",
                "interface/stlink.cfg",

                "-c",
                "transport select swd",

                "-f",
                "target/stm32f1x.cfg",

                "-c",
                "$_TARGETNAME configure -gdb-max-connections 2"],
            "serverHost": "localhost",
            "serverPort": "3333",
            "serverCwd": "/usr/bin",
            "liveWatch": {
                "enabled": true,
                "samplesPerSecond": "4"
            },
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "imageOffset": "",
                    "symbolFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "symbolOffset": ""
                }
            ]
        },
        {
            "type": "stgdbtarget",
            "request": "attach",
            "name": "STM32Cube: Launch GDB Client",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "program": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
            "gdb": "${command:st-stm32-ide-debug-launch.get-gdb-executable}",
            "serverHost": "localhost",
            "serverPort": "${command:st-stm32-ide-debug-launch.get-server-port}",
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "imageOffset": "",
                    "symbolFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "symbolOffset": ""
                }
            ]
        }
    ]
}

Windows:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "stgdbtarget",
            "request": "launch",
            "name": "STM32Cube: Launch Generic GDB Server",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "program": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
            "gdb": "${command:st-stm32-ide-debug-launch.get-gdb-executable}",
            "deviceName": "${command:st-stm32-ide-debug-launch.get-device-name}",
            "deviceCore": "${command:st-stm32-ide-debug-launch.get-core-name}",
            "deviceTrustzone": "${command:st-stm32-ide-debug-launch.get-trustzone-status}",
            "serverExe": "openocd.exe",
            "serverParameters": [                
                "-f",
                "interface/stlink.cfg",

                "-c",
                "transport select swd",

                "-f",
                "target/stm32f1x.cfg",

                "-c",
                "$_TARGETNAME configure -gdb-max-connections 2"],
            "serverHost": "localhost",
            "serverPort": "3333",
            "serverCwd": "C:/xpack-openocd-0.12.0-7/bin",
            "liveWatch": {
                "enabled": true,
                "samplesPerSecond": "4"
            },
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "imageOffset": "",
                    "symbolFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "symbolOffset": ""
                }
            ]
        },
        {
            "type": "stgdbtarget",
            "request": "attach",
            "name": "STM32Cube: Launch GDB Client",
            "origin": "snippet",
            "cwd": "${workspaceFolder}",
            "preBuild": "${command:st-stm32-ide-debug-launch.build}",
            "program": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
            "gdb": "${command:st-stm32-ide-debug-launch.get-gdb-executable}",
            "serverHost": "localhost",
            "serverPort": "${command:st-stm32-ide-debug-launch.get-server-port}",
            "runEntry": "main",
            "imagesAndSymbols": [
                {
                    "imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "imageOffset": "",
                    "symbolFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}",
                    "symbolOffset": ""
                }
            ]
        }
    ]
}

如何进入调试

显示1

如果你是下图这样,请按下面这样做:

如果不是,请往下滑看下面的显示2。

alt text

点完Run and Debug后,如下图:

  • ST-Link选STM32cube: STM32 Launch STLink GDB Server
  • J-Link选STM32Cube: STM32 LaunchJLink GDB Server
  • openOCD接管选STM32Cube: Launch Generic GDB Server

alt text

显示2

如果你是下图这样,请按下面这样做:

alt text

你显示上面这样,可能是因为你已经有生成的launch.json了。(这样直接点击就行)

alt text

进入调试

然后会出现这个条,他会下载程序到板子(仅J-link)

alt text

Then the program was successfully downloaded and entered Debug mode.

alt text

按照下图所示,把你要监视的变量输入到最顶上的框里,就可以加入到实时监视框里了。

结构体也可以:

更换调试器软件为cortex debug(可选,很没啥必要):

安装cortex debug插件

在VScode里搜索cortex debug并安装

进行launch.json的配置

新建.vscode/launch.json:

{
    "version": "0.2.0",

    "configurations": [
        {
            // 调试配置名称
            "name": "STM32 - OpenOCD - Cortex Debug",

            // Cortex-Debug 固定写法
            "type": "cortex-debug",

            // 下载程序并开始调试
            "request": "launch",

            // 工程工作目录
            "cwd": "${workspaceFolder}",

            // ★ 必改:编译生成的 ELF
            "executable": "${workspaceFolder}/build/Debug/TEST.elf",

            // 使用 OpenOCD
            "servertype": "openocd",

            // ★ OpenOCD 可执行文件
            // Linux 下用 which openocd 查询
            "serverpath": "/usr/bin/openocd",

            // ★ 根据调试器和 MCU 修改
            //
            // ST-Link:
            // interface/stlink.cfg
            //
            // STM32F1:
            // target/stm32f1x.cfg
            //
            // STM32F4 则是:
            // target/stm32f4x.cfg
            "configFiles": [
                "interface/stlink.cfg",
                "target/stm32f1x.cfg"
            ],

            // ★ ARM GNU Toolchain 的 bin 目录
            // 建议不要直接写 "~"
            // 用 ${env:HOME} 更稳
            "armToolchainPath": "${env:HOME}/.local/share/stm32cube/bundles/gnu-tools-for-stm32/14.3.1+st.2/bin",

            // 启动后运行到 main()
            "runToEntryPoint": "main",

            // ★ Cortex Live Watch
            // OpenOCD 下可用
            // samplesPerSecond 最大 20
            // 先用 4Hz 足够
            "liveWatch": {
                "enabled": true,
                "samplesPerSecond": 4
            }


            // ============================================================
            // 以下为可选配置
            // ============================================================


            // 如果 Cortex-Debug 找不到 OpenOCD,
            // Linux:
            // which openocd
            //
            // 一般 Fedora 是:
            // /usr/bin/openocd
            //
            // "serverpath": "/usr/bin/openocd",


            // 如果 OpenOCD 找不到 interface/*.cfg 或 target/*.cfg,
            // Fedora 一般脚本目录在:
            // /usr/share/openocd/scripts
            //
            // 可以显式增加:
            //
            // "searchDir": [
            //     "/usr/share/openocd/scripts"
            // ],


            // 调试 Cortex-Debug / OpenOCD 问题:
            //
            // "showDevDebugOutput": "raw"
        }
    ]
}

目前我们要知道三件事,configFiles,executable,armToolchainPath和serverpath:

  1. configFiles:

参考 openocd的两个-f参数

我们用的stm32f103c8和st-link:

所以"configFiles": ["interface/stlink.cfg","target/stm32f1x.cfg"]。

  1. executable:

用vscode的终端命令直接查找下,或者直接在VScode的文件管理器里找:

# Linux
find build -name "*.elf"
# Windows
gci build -Recurse -File -Filter "*.elf"

注意,Windows要用powershell,不可以用cmd。

如上图所示,在build/Debug/TEST.elf。

所以executable = ${workspaceFolder}/build/Debug/TEST.elf。

  1. armToolchainPath:

用vscode的终端命令直接查找下:

cube bundle show --project

这个输出说明,我在用gnu-tools-for-stm32@14.3.1+st.2

用命令找他在哪:

# Linux
find ~/.local/share/stm32cube/bundles/gnu-tools-for-stm32 \
  -type f -name "arm-none-eabi-gdb"
# Windows
gci "$env:LOCALAPPDATA\stm32cube\bundles\gnu-tools-for-stm32" `
  -Recurse -File -Filter "arm-none-eabi-gdb.exe"

注意,Windows要用powershell,不可以用cmd。

复制输出结果:

/home/tungchiahui/.local/share/stm32cube/bundles/gnu-tools-for-stm32/14.3.1+st.2/bin/arm-none-eabi-gdb

但是去掉最后的执行程序arm-none-eabi-gdb,只需要到bin即可:

/home/tungchiahui/.local/share/stm32cube/bundles/gnu-tools-for-stm32/14.3.1+st.2/bin

所以armToolchainPath = /home/tungchiahui/.local/share/stm32cube/bundles/gnu-tools-for-stm32/14.3.1+st.2/bin。

  1. serverpath:

用终端命令直接查找下:

# Linux
which openocd

而Windows,你自己装哪的你应该知道吧。。。

最终的launch.json:

{
    "version": "0.2.0",

    "configurations": [
        {
            // 调试配置名称
            "name": "STM32 - OpenOCD - Cortex Debug",

            // Cortex-Debug 固定写法
            "type": "cortex-debug",

            // 下载程序并开始调试
            "request": "launch",

            // 工程工作目录
            "cwd": "${workspaceFolder}",

            // ★ 必改:编译生成的 ELF
            "executable": "${workspaceFolder}/build/Debug/TEST.elf",

            // 使用 OpenOCD
            "servertype": "openocd",

            // ★ OpenOCD 可执行文件
            // Linux 下用 which openocd 查询
            "serverpath": "/usr/bin/openocd",

            // ★ 根据调试器和 MCU 修改
            //
            // ST-Link:
            // interface/stlink.cfg
            //
            // STM32F1:
            // target/stm32f1x.cfg
            //
            // STM32F4 则是:
            // target/stm32f4x.cfg
            "configFiles": [
                "interface/stlink.cfg",
                "target/stm32f1x.cfg"
            ],

            // ★ ARM GNU Toolchain 的 bin 目录
            // 建议不要直接写 "~"
            // 用 ${env:HOME} 更稳
            "armToolchainPath": "/home/tungchiahui/.local/share/stm32cube/bundles/gnu-tools-for-stm32/14.3.1+st.2/bin",

            // 启动后运行到 main()
            "runToEntryPoint": "main",

            // ★ Cortex Live Watch
            // OpenOCD 下可用
            // samplesPerSecond 最大 20
            // 先用 4Hz 足够
            "liveWatch": {
                "enabled": true,
                "samplesPerSecond": 4
            }
        }
    ]
}
Debug与LiveWatch

先编译

点击进入debug:

成功进入:

复制一个你程序里的某个全局变量,我这里是cmd_vel2:

填到cortex live watch,注意不是stm32cube live watch:

然后点运行看一下变量cmd_vel2

是不断在变化的:

Frequently Asked Questions

Linux下USB权限问题

如果你都正常识别出设备了,则没必要做这一步了

如果你遇到的话,你解决不了就让AI来解决,AI几秒钟就给你把事办了

如果调试器插上后能在 lsusb 中看到,但 VS Code 找不到设备,或者调试输出中出现下面的报错,通常是当前用户没有访问 USB 设备节点的权限:

libusb couldn't open USB device /dev/bus/usb/001/008, errno=13
libusb requires write access to USB device nodes
ST-Link enumeration failed

先在终端运行 lsusb,确认调试器已被系统识别。报错中的 /dev/bus/usb/001/008 只是示例,实际路径以你自己的输出为准。可以用 ls -l 查看该节点的权限:

lsusb
ls -l /dev/bus/usb/001/008

**使用 ST-Link:**先按上文点击插件中的 install ST-Link udev rules。如果仍然报权限错误,检查规则是否装到了系统中:

find /etc/udev/rules.d /usr/lib/udev/rules.d -maxdepth 1 -iname '*stlink*.rules' -print 2>/dev/null

如果没有找到规则,可以从 ST 官方的 STSW-LINK007 下载页获取安装包。解压后进入 AllPlatforms/StlinkRulesFilesForLinux,按其中的 readme.txt 安装与你的发行版对应的 udev 规则包。在这个目录中,Fedora 使用 .rpm 包,Ubuntu/Debian 使用 .deb 包:

# Fedora:只运行这一行
sudo dnf install ./st-stlink-udev-rules-*.rpm

# Ubuntu/Debian:只运行这一行
sudo apt install ./st-stlink-udev-rules-*.deb

如果下载的包名不同,以实际文件名和随包说明为准。ST 的发行说明也说明了 Linux 下需要安装相应的 ST-Link USB 访问规则。

**使用 J-Link:**上文安装的 jlink-gdbserver bundle 用于启动调试服务;USB 权限还需要 SEGGER 的 udev 规则。安装 SEGGER J-Link Software and Documentation Pack 中对应发行版的 .rpm 或 .deb 包时,会一并安装规则。如果使用解压版,则按照包内 README.txt,将 99-jlink.rules 复制到 /etc/udev/rules.d/。具体方法见 SEGGER 的 Linux 排查说明。

安装规则后,运行:

sudo udevadm control --reload-rules

然后拔下调试器再插回去,重新运行 lsusb,用新的总线号和设备号查看 /dev/bus/usb/... 节点。可以将实际路径代入下方命令,检查当前用户能否读写:

test -r /dev/bus/usb/001/008 && test -w /dev/bus/usb/001/008 && echo 'USB 设备节点可读写'

拔插后设备号可能变化,不要照抄示例中的 001/008。确认权限后,回到 VS Code 重新选择调试器。如果 lsusb 根本看不到调试器,应先检查 USB 线、接口和供电;这不是 udev 权限问题。

这里检查的是 /dev/bus/usb/...。如果另外还要打开板载虚拟串口 /dev/ttyACM*,那是串口设备的权限,需要分别排查。

音乐页