🛠 Développer en Rust embarqué avec nrf52840-mdk-rs
The context
The community BSP nrf-rs/nrf52840-mdk-rs (dual-licensed MIT/Apache-2.0) provides board support for the embedded Rust ecosystem: it re-exports nrf52840-hal and exposes the pins of the 18-pin double header — the RGB LED and the user button are ordinary P0 pins (see the board's pinout diagram for the exact assignment). Repository warning: the embedded-hal ecosystem moves quickly, so take the canonical version of the examples from the crate's examples/ directory.
Step 1 — Toolchain
rustup target add thumbv7em-none-eabihf # Cortex-M4F avec FPU
cargo install cargo-binutils # pour produire l'hex via rust-objcopy
Step 2 — The blinky project
Adapted from the crate's examples — check the current API in examples/:
//! examples/blinky.rs — nrf52840-mdk-rs
#![no_std]
#![no_main]
use embedded_hal::digital::OutputPin;
use nrf52840_mdk::hal;
use panic_halt as _;
#[cortex_m_rt::entry]
fn main -> ! {
let p = hal::nb::new(
nrf52840_mdk::hal::pac::Peripherals::take.unwrap,
hal::InitialPeripherals::default,
);
// Broche P0 ordinaire de l'en-tête — l'affectation LED vient du pinout
let mut led = p.pins.p0_13.into_push_pull_output(hal::port::typestate::PushPull);
loop {
led.set_high.ok;
cortex_m::asm::nop; // du vrai code utilise un timer
led.set_low.ok;
cortex_m::asm::nop;
}
}
cargo build --release --example blinky --target thumbv7em-none-eabihf
Step 3 — Flashing: the tricky part
The BSP's README is explicit: its maintainer never managed to get the onboard probe working with the OpenOCD provided by Ubuntu 18.04, and works with an external J-Link. Two reliable strategies:
- Without external hardware: convert the ELF to hex and use DAPLink drag-and-drop, which is foolproof:
rust-objcopy --output-target=hexfile target/thumbv7em-none-eabihf/release/example-blinky \
blinky.hex
# déposer blinky.hex sur le lecteur DAPLINK, puis bouton Boot/Reset
- With a J-Link: wire the SWD header according to the README table (3V3 / RST / SWDIO / SWCLK / TDO / GND) — this is also the wiring used by PlatformIO with
upload_protocol = jlink.
Step 4 — Debugging
With the J-Link: use probe-rs or cargo embed for hot loading, GDB, and inspection of the Cortex-M4F registers. Without an external probe, the DAPLink's CDC port remains available for serial logging.
What the BSP provides
- PAC + HAL nRF52840 pre-wired for the board (pins, board features);
- a clean
cortex_m_rtentry point forno_std; - reference UART/blinky examples to get started with GPIO, timers, and serial before tackling BLE (which remains the domain of C stacks or dedicated crates).