Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 nanoflash command. 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:

  1. touches the port of the sketch at 1200 baud, so that the board restarts into its bootloader (2341:005a);
  2. writes the application at 0x10000 through SAM-BA and checks it with CRC-16;
  3. restarts the board into the application;
  4. 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 build feature turns the layout into Rust constants and a memory.x.
  • nanoflash run is the Cargo runner: cargo run builds, flashes and monitors.

Next steps