🛠 Développer en Rust embarqué avec nrf52840-mdk-rs

nRF52840-MDK (boards.json id: nrf52840-mdk) · avance · Le BSP communautaire nrf52840-mdk-rs apporte Rust sans std sur la carte : hal, pins, et un chemin de flash fiable malgré les soucis OpenOCD.

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:

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials