Windows 平台安装 Cortex-M 单片机 Rust 开发工具,配置 VS Code,编译、调试单片机程序。
安装 Rust
在 Rust 本地开发工具的基础上添加组件,以支持 Cortex-M 单片机。
这部分主要参考 Rust 离线文档/doc/rust/html/embedded-book/intro/install.html
。
首先需要安装 MinGW-w64 GCC 工具链。编译单片机程序本身不需要 MinGW-w64,但是在后面的安装步骤中,安装 cargo-binutils 时需要进行本地编译。
一般是先安装 MSYS2,建议添加国内镜像源,再通过pacman
安装 MinGW-w64
pacman -S mingw-w64-x86_64-toolchain
将 MinGW-w64 bin
目录路径(例如msys64\mingw64\bin
)加入PATH
环境变量。
执行下列命令,为 Rust 增加 ARM Cortex-M 编译目标
rustup target add <target>
其中<target>
有
thumbv6m-none-eabi
- Cortex-M0/M0+/M1thumbv7m-none-eabi
- Cortex-M3thumbv7em-none-eabi
- Cortex-M4/M7,无 FPUthumbv7em-none-eabihf
- Cortex-M4/M7,有 FPU
依次执行下列命令,完成 Rust 安装
cargo install cargo-binutils
rustup component add llvm-tools-preview
cargo install cargo-generate
下载并安装以下工具。这些工具都提供压缩包,解压即可
将各个工具的 bin
目录加入PATH
环境变量。
至此 Rust 安装完成。我通过下面这样的一个批处理程序设置环境变量,并启动 Windows 命令行(cmd.exe):
@echo off
set PATH=%windir%\System32
set PATH=D:\devel\rust\msys64\mingw64\bin;%PATH%
set PATH=D:\devel\arm\tools\xpack-arm-none-eabi-gcc-11.3.1-1.1\bin;%PATH%
set PATH=D:\devel\arm\tools\xpack-openocd-0.11.0-5\bin;%PATH%
set RUSTUP_HOME=D:\devel\rust\.rustup
set CARGO_HOME=D:\devel\rust\.cargo
set PATH=%CARGO_HOME%\bin;%PATH%
start cmd.exe
VS Code 新建和编译工程
这部分参考离线文档 /doc/rust/html/embedded-book/start/qemu.html
。
Rust 提供了一个工程模板,github.com/rust-embedd… 。考虑到国内网络访问不和谐,我下载完整仓库 zip 包(cortex-m-quickstart-master.zip
)备用。
要创建工程,将cortex-m-quickstart-master.zip
解压,将文件夹名称改成工程名,例如start
,加载到 VS Code 中。
强烈建议认真阅读工程目录中的 README.md
和 .vscode/README.md
文件。
接下来需要对工程目录下的一些文件进行修改。
修改Cargo.toml
文件,将其中的占位符进行如下修改:
{{authors}}
修改为作者姓名{{project-name}}
修改为工程名,例如start
修改.cargo/config.toml
文件,主要是配置默认的编译目标。我的开发板是 BBC Micro:bit v2,处理器是 nRF52833,其核心是 Cortex-M4F,因此,在[build]
小节下选择target = "thumbv7em-none-eabihf"
,例如
[build]
# Pick ONE of these default compilation targets
# target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
#target = "thumbv7m-none-eabi" # Cortex-M3
# target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
...
修改 memory.x
文件,此文件大体上等同于 GCC ld 的 linker script,定义目标单片机的存储器布局。对于 nRF52833,查阅其 datasheet(nRF52833_PS_v1.5.pdf)中 “Memory map”一节,将 memory.x
文件相应修改如下:
MEMORY
{
/* NOTE 1 K = 1 KiBi = 1024 bytes */
/* TODO Adjust these memory regions to match your device memory layout */
/* These values correspond to the LM3S6965, one of the few devices QEMU can emulate */
FLASH : ORIGIN = 0x00000000, LENGTH = 512K
RAM : ORIGIN = 0x20000000, LENGTH = 128K
}
运行 cargo build
进行编译,生成的 ELF 程序为target\thumbv7em-none-eabihf\debug\start
。
VS Code 调试
在 VS Code 内进行调试需安装 “Cortex-Debug” 扩展。
修改.vscode/launch.json
文件。此文件定义调试配置,默认已存在 2 项,复制其中 Debug (OpenOCD)
配置,依样画葫芦修改:
{
/* Configuration for BBC micro:bit v2 */
"type": "cortex-debug",
"request": "launch",
"name": "Debug (Micro:bit v2)",
"servertype": "openocd",
"cwd": "${workspaceRoot}",
"preLaunchTask": "Cargo Build (debug)",
"runToEntryPoint": "main",
"executable": "./target/thumbv7em-none-eabihf/debug/start",
/* Run `cargo build --example itm` and uncomment this line to run itm example */
// "executable": "./target/thumbv7em-none-eabihf/debug/examples/itm",
"device": "nrf52833",
"configFiles": [
"interface/cmsis-dap.cfg",
"target/nrf52.cfg"
],
"svdFile": "${workspaceRoot}/.vscode/nrf52833.svd",
"swoConfig": {
"enabled": true,
"cpuFrequency": 8000000,
"swoFrequency": 2000000,
"source": "probe",
"decoders": [
{ "type": "console", "label": "ITM", "port": 0 }
]
}
}
主要的修改处有:
"name": "Debug (Micro:bit v2)",
- 配置的名称"executable": "./target/thumbv7em-none-eabihf/debug/start",
- 编译出的程序文件名"device": "nrf52833",
- 目前我任意修改"configFiles":[...]
- 指定 OpenOCD 配置文件名称,具体取决于单片机/开发板"svdFile":...
- 单片机的 SVD 文件
OpenOCD 配置文件取决于具体的开发板。对于 Micro:bit v2,应指定 interface/cmsis-dap.cfg
和 target/nrf52.cfg
这两个文件。
nRF52833 的 SVD 文件可从 nRF5_SDK 中找到(相对路径modules\nrfx\mdk\nrf52833.svd
),将其复制到 .vscode
目录内。
切换到 VS Code 调试界面(Run and Debug),选择前面所创建的配置(Debug (Micro:bit v2)
),启动调试。GDB 会自动将程序烧写到单片机。程序启动后,将自动暂停到 src/main.rs
中的 main()
函数的开头,如下图:
当然,在启动调试前,别忘了把开发板连接到电脑。对于 Micro:bit,通过 USB 数据线连接即可。Micro:bit 板载了调试器(CMSIS-DAP),同时提供 USB 虚拟串口,开发调试极其方便。
工程根目录下的openocd.cfg
和openocd.gdb
主要是用于在命令行中进行 GDB 调试。如果像上面利用 VS Code GUI 调试界面,则所有配置都在 .vscode/launch.json
文件中进行,故,这两个配置文件可置之不理。