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 with cargo run

nanoflash run is a runner for Cargo. It writes the firmware that Cargo built to the board, then shows what the firmware writes to its serial port, as nanoflash monitor does.

Set up the runner

  1. Write a board manifest and a layout. The manifest needs no image: the runner supplies the ELF file. The layout needs one partition with the role firmware.

  2. Name the board in the Cargo.toml of the firmware:

    # Cargo.toml
    [package.metadata.nanoflash]
    board = "boards/nano33ble.toml"     # the board manifest, relative to this Cargo.toml
    # board-identifier = "arduino-nano-33-ble"   # the board of the manifest, when it has several
    
  3. Add the runner to .cargo/config.toml:

    # .cargo/config.toml
    [target.thumbv7em-none-eabihf]
    runner = "nanoflash run"
    

    runner = "nanoflash run --board boards/nano33ble.toml" also works, with a path relative to the directory of cargo run.

Run

$ cargo run --release
flashed arduino-nano-33-ble (nrf52840, 1 MiB)
  0x10000  application  64 KiB
verified
hello from the sketch

Cargo gives the ELF file to the runner. The arguments after -- in cargo run -- ... are options of nanoflash run:

  • -- --no-monitor only flashes. Use it when the serial port of the application speaks a binary protocol, not text: the monitor would show garbage and keep the port open.
  • -- --baud-rate 9600 sets the baud rate of the serial output. --flash-baud-rate sets the baud rate of flashing, with the exceptions that Espressif lists.
  • -- --raw sends each key as you type it. The command checks for a terminal before it flashes.
  • -- --erase-all, -- --no-verify, -- --port <port> and -- --progress <format> have the meaning that they have for flash and monitor.
  • -- --json prints the monitor as JSON lines, and the first line is the event flashed with the report of the flash.

When the ELF file has a defmt table, the runner decodes the defmt frames. See Decode defmt logs.

Make sure the monitor finds the application

flash resets the board into its application, and the monitor opens the application-devices of the board and waits up to 30 s for it. When the board starts in its bootloader and the manifest lists no application-devices, the monitor waits for the bootloader identity, which never comes back, and fails after 30 s. List the application-devices of the board.

A firmware with its own USB identity (not the identity of the Arduino sketch) needs it in the list too. An Arduino board with a 1200-baud touch shows the entries for the Nano 33 BLE and how to add that identity.

Use cargo nanoflash

The archives and the installation from the source give two commands: nanoflash and cargo-nanoflash. Cargo runs the second one as a subcommand, so cargo nanoflash <arguments> does the same as nanoflash <arguments>, with the same output and the same exit code:

$ cargo nanoflash flash
$ cargo nanoflash monitor --baud-rate 9600
$ cargo nanoflash layout check

cargo nanoflash flash only flashes. It does not build the firmware: cargo run builds with the target, the profile and the features that Cargo knows, and hands the ELF file to the runner. To flash without cargo run, build first, and name the built ELF file as an image of the board manifest, for example ../target/thumbv7em-none-eabihf/release/firmware:

$ cargo build --release && cargo nanoflash flash