Flash and monitor a Nano 33 BLE with cargo run
In this tutorial, you build the example firmware of the repository for the Arduino Nano 33 BLE.
Its memory layout comes from a nanoflash layout file, and cargo run flashes it through the SAM-BA
bootloader and opens the serial monitor. You write no memory.x and call no objcopy or bossac.
What you need
-
An Arduino Nano 33 BLE (nRF52840) and a USB cable.
-
Rust 1.95 or later with the target
thumbv7em-none-eabihf:$ rustup target add thumbv7em-none-eabihf -
The
nanoflashcommand. Install it from crates.io (see Install nanoflash for the other ways):$ cargo install --locked nanoflash-cli -
A clone of the repository:
$ git clone https://github.com/AlixANNERAUD/nanoflash $ cd nanoflash/examples/nano33ble
1. Look at the board files
The example is a package of its own, with its own [workspace], Cargo.lock and target
directory, because it builds for a microcontroller.
board/layout.toml describes the flash: Arduino’s bootloader in the first 64 KiB, the application
after it, from 0x10000.
[flash]
size = "1 MiB"
base-address = 0x0
[[partition]]
name = "bootloader"
role = "bootloader"
offset = 0x0
size = "64 KiB"
[[partition]]
name = "application"
role = "firmware"
size = "remaining"
board/manifest.toml describes the board. It lists no image: the runner supplies the ELF file.
[[board]]
identifier = "arduino-nano-33-ble"
name = "Arduino Nano 33 BLE"
protocol = "samba"
chip = "nrf52840"
layout = "layout.toml"
reset = "touch-1200"
application-devices = [{ vendor = 0x2341, product = 0x805a }]
bootloader-devices = [{ vendor = 0x2341, product = 0x005a }]
Cargo.toml names the board in [package.metadata.nanoflash], so every nanoflash command in this
directory knows it.
2. Look at how the firmware gets its layout
build.rs reads the layout of the board of the package, adds the RAM, and writes the constants and
the whole memory.x that cortex-m-rt includes:
fn main() {
nanoflash::build::Build::package()
.memory("RAM", 0x2000_0000, 256 * 1024)
.generate()
.unwrap_or_else(|error| panic!("{error}"));
println!("cargo:rustc-link-arg-bins=-Tlink.x");
}
src/main.rs includes the constants:
#![allow(unused)]
fn main() {
mod layout {
include!(concat!(env!("OUT_DIR"), "/nanoflash_layout.rs"));
}
static APPLICATION_SIZE: u32 = layout::application::SIZE;
}
The firmware does nothing else: it loops forever.
3. Build
$ cargo build --release
The build links with the generated memory.x. Check that the ELF file fits its partition:
$ nanoflash layout check --image application=target/thumbv7em-none-eabihf/release/nanoflash-example-nano33ble
The application line shows the size of the firmware and the headroom left. Without a layout file,
layout check uses the layout of the board of the package.
4. Flash and monitor
.cargo/config.toml sets the target and the runner:
[build]
target = "thumbv7em-none-eabihf"
[target.thumbv7em-none-eabihf]
runner = "nanoflash run"
Connect the board and run:
$ cargo run --release
Cargo builds the firmware and hands the ELF file to nanoflash run. The runner:
- touches the port of the sketch at 1200 baud, so that the board restarts into its bootloader
(
2341:005a); - writes the application at
0x10000through SAM-BA and checks it with CRC-16; - restarts the board into the application;
- opens the serial monitor on the device of the application.
The example prints nothing, so the monitor stays open and empty. Press Ctrl-C to quit.
If the touch does not start the bootloader, press the reset button of the board twice and run the
command again. To flash only, run cargo run --release -- --no-monitor.
What you learned
[package.metadata.nanoflash]names the board once, for the build script and for the command.- The
buildfeature turns the layout into Rust constants and amemory.x. nanoflash runis the Cargo runner:cargo runbuilds, flashes and monitors.
Next steps
- Flash and monitor with cargo run in your own firmware.
- Decode defmt logs.
- Generate memory.x and layout constants.