rust+stm32+vscode搭建开发调试环境

1.安装rust

rust官网传送门

2.安装调试和烧写软件(二者选其一)

安装openocd传送门

安装probe-rs

3.安装stlink

安装stlink传送门

4.搭建gcc-arm-none-eabi编译环境

搭建gcc-arm-none-eabi编译环境

5.安装vscode

vscode官网传送门

6.安装相关插件

rust-analyzer:使用VSCode开发Rust必备

cortex-debug:基于openocd的调试和烧录插件

Debugger for probe-rs:基于probe-rs的调试和烧录插件

crates:提升编辑Cargo.toml的体验,辅助包管理

7.编写调试配置

7.1 添加构建任务和烧写任务

.vscode/task.json

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            /*
             * This is the default cargo build task,
             * but we need to provide a label for it,
             * so we can invoke it from the debug launcher.
             */
            "label": "Cargo Build (debug)",
            "type": "process",
            "command": "cargo",
            "args": ["build"],
            "problemMatcher": [
                "$rustc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Cargo Build (release)",
            "type": "process",
            "command": "cargo",
            "args": ["build", "--release"],
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build"
        },
        {
            "label": "Flash",
            "type": "shell",
            "command": "openocd -s /usr/local/share/openocd/scripts -f ${workspaceFolder}/openocd.cfg -c \"program target/thumbv7m-none-eabi/debug/led preverify verify reset exit\"",
            "dependsOn": [
                "Cargo Build (debug)"
            ],
            "group": "build"
        },
        {
            "label": "Cargo Build Examples (debug)",
            "type": "process",
            "command": "cargo",
            "args": ["build","--examples"],
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build"
        },
        {
            "label": "Cargo Build Examples (release)",
            "type": "process",
            "command": "cargo",
            "args": ["build","--examples", "--release"],
            "problemMatcher": [
                "$rustc"
            ],
            "group": "build"
        },
        {
            "label": "Cargo Clean",
            "type": "process",
            "command": "cargo",
            "args": ["clean"],
            "problemMatcher": [],
            "group": "build"
        },
    ]
}
7.2编写调试配置

以下是两种调试配置,一个是基于probe-rs的调试配置,一个是基于openocd的调试配置

probe-rs的优势是使用方便,缺点是功能不如openocd

openocd的优势是功能强大,缺点是配置相对繁琐,注意(部分版本有坑,建议使用最新版)

两种调试方式都需要安装对应的插件,openocd对应的插件是cortex-debug,probe-rs对应的插件是Debugger for probe-rs

{
    /* 
     * Requires the Rust Language Server (rust-analyzer) and Cortex-Debug extensions
     * https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer
     * https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug
     */
    "version": "0.2.0",
    "configurations": [
        {
            /* Configuration for the STM32F303 Discovery board */
            "type": "probe-rs-debug",
            "request": "launch",
            "name": "Debug (probe-rs)",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "Cargo Build (debug)",
            "chip": "STM32F103ZE",
            "runtimeExecutable": "probe-rs",
            "flashingConfig": {
                "flashingEnabled": true,
                "haltAfterReset": false,
            },
            "coreConfigs": [
                {
                    "coreIndex": 0,
                    "programBinary": "${workspaceFolder}/target/thumbv7m-none-eabi/debug/led",
                    "svdFile": "${workspaceFolder}/STM32F103.svd"
                }
            ],
        },
        {
            /* Configuration for the STM32F303 Discovery board */
            "type": "cortex-debug",
            "request": "launch",
            "name": "Debug (OpenOCD)",
            "servertype": "openocd",
            "cwd": "${workspaceRoot}",
            "preLaunchTask": "Flash",
            "runToEntryPoint": "main",
            "executable": "./target/thumbv7m-none-eabi/debug/led",
            /* Run `cargo build --example itm` and uncomment this line to run itm example */
            // "executable": "./target/thumbv7em-none-eabihf/debug/examples/itm",
            "device": "STM32F103ZE",
            "configFiles": [
                "${workspaceRoot}/openocd.cfg"
            ],
            "svdFile": "${workspaceRoot}/STM32F103.svd",
            "gdbPath": "/usr/local/gcc-arm-none-eabi/bin/arm-none-eabi-gdb",
            "showDevDebugOutput":"raw"
        }
    ]
}

  

8.带调试配置的demo项目

https://gitee.com/navysummer/stm32-rust-demo

  

 

posted @ 2024-04-30 18:09  navysummer_hardware  阅读(587)  评论(0编辑  收藏  举报