The Rust programming language is making its way into the embedded world, and getting more and more popular and not only at the Lucerne University. With Rust, the probe-rs is one of the popular debug choices, as it nicely comes with cargo. On the hardware side, the NXP MCU-Link is $15 debug probe hardware I use for many targets. Why not using the MCU-Link with probe-rs and Rust?
Outline
With Visual Studio Code and Rust for embedded, probe-rs is probably the most popular choice. While it works well with cortex-debug too, I really like the probe-rs features and cli support to flash the target, or that it supports SEGGER RTT.
Installation
probe-rs installation is very simple and easy, just run
cargo install probe-rs --features cli
For the MCU-Link debug probe, install the latest LinkServer software and have the probe firmware updated as CMSIS-DAP debug probe. See LinkServer for Microcontrollers and New MCU-Link Debug Probe from NXP about updating the firmware. I’m using in this article MCU-LINK_CMSIS-DAP_V3_133.s19.
Usage with probe-rs
With
probe-rs list
it should list the NXP MCU-Link debug probe:
[0]: MCU-LINK (r0FF) CMSIS-DAP V3.133 (VID: 1fc9, PID: 0143, Serial: GNENQG0AQTDWK, CmsisDap)
Using cargo
To flash the application using cargo, use probe-rs in .cargo/config.toml
, for example with:
runner = "probe-rs run --chip RP2040 --protocol swd"
With this I can use:
cargo run
to flash and run the application:
Running probe-rs run --chip RP2040 --protocol swd target\thumbv6m-none-eabi\debug\rp2040-project-template
Erasing ✔ [00:00:00] [#####################################################################] 12.00 KiB/12.00 KiB @ 75.96 KiB/s (eta 0s ) Programming ✔ [00:00:00] [#####################################################################] 12.00 KiB/12.00 KiB @ 58.35 KiB/s (eta 0s ) Finished in 0.384s
INFO Program start
└─ rp2040_project_template::__cortex_m_rt_main @ src\main.rs:27
INFO on!
└─ rp2040_project_template::__cortex_m_rt_main @ src\main.rs:67
INFO off!
Debugging with VS Code
Below is an example launch configuration, both for probe-rs
and cortex-debug
:
// The format of this file is specified in https://probe.rs/docs/tools/vscode/#start-a-debug-session-with-minimum-configuration
{
"version": "0.2.0",
"configurations": [
{
"preLaunchTask": "rust: cargo build",
"type": "probe-rs-debug MCU-Link",
"request": "launch",
"name": "myProject",
"cwd": "${workspaceFolder}",
"chip": "rp2040",
"connectUnderReset": false,
"speed": 4000,
"runtimeExecutable": "probe-rs",
"runtimeArgs": [
"dap-server"
],
"flashingConfig": {
"flashingEnabled": true,
"resetAfterFlashing": true,
"haltAfterReset": true,
},
"coreConfigs": [
{
"coreIndex": 0,
"programBinary": "target/thumbv6m-none-eabi/debug/rp2040-project-template",
"chip": "RP2040",
"rttEnabled": true,
"options": {
"env": {
"DEFMT_LOG": "debug"
}
},
}
],
"consoleLogLevel": "Info", //Error, Warn, Info, Debug, Trace
"wireProtocol": "Swd"
},
{
"name": "MCU-Link cortex-debug",
"type": "cortex-debug",
"request": "launch",
"servertype": "openocd",
"serverpath": "${env:OPENOCD_PATH}/openocd",
"serverArgs": [
"-c adapter speed 5000",
"-c set USE_CORE 0",
],
"cwd": "${workspaceRoot}",
"executable": "${workspaceFolder}/target/thumbv6m-none-eabi/debug/project",
"armToolchainPath": "${env:PICO_TOOLCHAIN_PATH}",
"device": "RP2040",
"configFiles": [
"interface/cmsis-dap.cfg",
"target/rp2040.cfg"
],
"runToEntryPoint": "main",
"postLaunchCommands": [
"monitor arm semihosting enable",
],
"svdFile": "${env:PICO_SDK_PATH}/src/rp2040/hardware_regs/rp2040.svd",
"rttConfig": {
"enabled": false,
"address": "auto",
"decoders": [
{
"label": "",
"port": 0,
"type": "console"
}
]
},
"showDevDebugOutput": "none",
}
]
}
With this, I can debug Rust applications with the NXP MCU-Link and probe-rs in VS Code:
Trouble Shooting
If you get something like this:
ERROR nusb::platform::windows_winusb::enumeration: Failed to get DeviceInterfaceGUIDs from registry: The system cannot find the file specified. (os error 2)
Error: Failed to open the debug probe.
Check the USB driver/devices e.g. with the USB Driver Tool. It should look like this:
Otherwise, re-installing the driver with the above tool usually solves the problem.
Summary
The NXP MCU-Link debug probe not only works for C/C++ applications, but can be easily used for Rust based applications. For C/C++ applications I’m mostly using the cortex-debug VS Code extension. Probe-rs nicely works with a Rust environment, and with the NXP MCU-Link I have a universal, fast and inexpensive debug probe, working with extensions like cppdbg, cortex-debug and now probe-rs.
Happy rusting 🙂
As always Erich, great tutorial.
I think it would also be useful if you can delve over the Embassy framework:
https://github.com/embassy-rs/embassy
I really think readers would benefit for going deep with Embassy since that’s Rust best for embedded systems.
Sergio.
LikeLiked by 1 person
Question ! The Rust compiler for C has been written.
LikeLiked by 1 person
One can easily interface with C or C++: https://docs.rust-embedded.org/book/interoperability/c-with-rust.html
LikeLike
Hi Sergio,
have you used embassy with a device not directly supported with it? I wanted to use it with NXP Kinetis and LPC devices, but they are not listed?
LikeLike
No Erich. I only used it with RP2040 which is already supported. I just interacted with it briefly and not for my main line of work in which I use iMX RT devices.
LikeLiked by 1 person
Ok, thanks for the update. I see how I could use it with the LPC series then.
LikeLike