nanoflash 0.1.1

Lay out the flash of ESP32 and Arduino boards and write firmware to them through their bootloaders, natively or in the browser over Web Serial.
Documentation

nanoflash flashes ESP32, ESP32-S3, ESP32-C3 and the other Espressif chips, the Arduino Uno, Nano, Pro Mini, Mega 2560 and Leonardo, the Arduino Nano 33 BLE and SAMD21 boards from one command. It is an alternative to esptool, bossac and avrdude, written in Rust: the same engine runs in your terminal, as a Cargo runner behind cargo run, in a serial monitor with defmt decoding, and in the browser as a Web Serial flasher. A TOML layout file describes the partition table once: nanoflash computes the offsets, checks them, and writes the Espressif partition table, an MBR, linker regions or Rust constants from it.

It is for firmware developers who flash boards of several vendors, Rust embedded developers who want cargo run to flash and monitor, and projects that ship firmware to users and want a flash button on a web page. It replaces a different flashing tool and partition format per vendor with one tool and one format.

Why nanoflash

  • One tool for three families. The Espressif ROM loader, SAM-BA with the Arduino extensions of bossac, and the STK500v1, STK500v2 and AVR109 bootloaders of Arduino's AVR boards.
  • Nothing erased before every check passes. Digests, alignment, overlap, partition sizes, flash size and the limits of the bootloader are checked before the first erase, then again against the flash that the chip reports.
  • One layout file as the source of truth. The command, the build.rs of your firmware and the browser read the same file. Partitions can grow with the flash that the chip reports.
  • Made for Rust firmware. cargo run builds, flashes and monitors. ELF files are accepted as they are. The build feature writes your memory.x from the layout.
  • The same engine in the browser. The library compiles to WebAssembly: an npm package, a <nanoflash-button> web component and a hosted flasher page.
  • Made for CI. No prompts, --json output, exit codes that tell an input error from a device error and a failed verification.
  • Binary, Intel HEX and ELF images. Existing esp-web-tools manifests work unchanged.

Quick start

Install the command from crates.io. cargo binstall downloads the binary of the release for your host. cargo install builds it with Rust 1.95 or later:

$ cargo binstall nanoflash-cli
$ cargo install --locked nanoflash-cli

Alternatively, download the Linux, macOS or Windows binaries of GitHub Releases, or build the command from the repository with cargo install --locked --git https://github.com/AlixANNERAUD/nanoflash nanoflash-cli. The install guide shows how to check the archives.

Describe the flash of the board in a layout file. It gives the flash size and the partitions. nanoflash checks their alignment, overlap and size, and places a partition without an offset after the one before it:

# layouts/esp32-s3-8mb.toml
[flash]
size = "8 MiB"

[[partition]]
name = "bootloader"
role = "bootloader"
offset = 0x0
size = "32 KiB"

[[partition]]
name = "partition-table"
role = "partition-table"
offset = 0x8000
size = "4 KiB"

[[partition]]
name = "application"
role = "firmware"
alignment = "64 KiB"   # no offset: the first 64 KiB boundary after the table, 0x10000
size = "3 MiB"

Describe the board in a manifest that names its layout and images:

# boards/esp32-s3.toml
[[board]]
identifier = "esp32-s3-devkitc"
protocol = "espressif"
chip = "esp32-s3"
layout = "../layouts/esp32-s3-8mb.toml"
application-devices = [{ vendor = 0x303a, product = 0x1001 }]

[[board.image]]
partition = "bootloader"
path = "../build/bootloader.bin"

[[board.image]]
partition = "application"
path = "../build/application.bin"

Then flash it:

$ nanoflash flash --board boards/esp32-s3.toml
connecting
writing at 0x0
writing at 0x8000
writing at 0x10000
verifying
resetting
flashed esp32-s3-devkitc (esp32-s3, 8 MiB)
  0x0  bootloader  21 KiB
  0x8000  partition-table  3 KiB
  0x10000  application  812 KiB
verified

nanoflash found the port, generated the partition table from the layout, wrote and verified every image. nanoflash monitor shows the serial output, and runner = "nanoflash run" in .cargo/config.toml makes cargo run do both. The first tutorial walks through a full example.

Supported boards

Family Boards and chips Protocol
Espressif ESP32, ESP32-S2, ESP32-S3, ESP32-C2, ESP32-C3, ESP32-C5, ESP32-C6, ESP32-H2, ESP32-P4 ROM loader
Arduino SAM-BA Nano 33 BLE (nRF52840), SAMD21 boards such as the MKR WiFi 1010 SAM-BA
Arduino AVR Uno, Nano, Pro Mini (ATmega328P) STK500v1
Arduino AVR Mega 2560 (ATmega2560) STK500v2
Arduino AVR Leonardo, Micro, Pro Micro (ATmega32U4) AVR109

USB DFU, UF2, CMSIS-DAP and Nordic serial DFU come next. See supported boards and protocols.

Flash from the browser

The flasher page flashes a board from a manifest URL or from files on your computer, in Chrome, Edge or Opera, with nothing to install. To put a flash button on your own page:

<script type="module">import "nanoflash/button";</script>
<nanoflash-button manifest="firmware/manifest.json" label="Install"></nanoflash-button>

See use the browser package.

Documentation

The full guide lives at https://alixanneraud.github.io/nanoflash/:

Its source is in docs/src.

Status

Layout checks, the table writers and flashing work natively and in the browser. The monitor and the Cargo runner work in the command. Flashing is tested against recorded transcripts, Espressif's QEMU, arduino/BOSSA's emulated SAM-BA bootloader and the Arduino bootloaders emulated by simavr. The release workflow attaches the binaries of the command to its GitHub releases. The crates nanoflash and nanoflash-cli are on crates.io, and the browser package nanoflash is on npm.

Contributing

Issues and pull requests are welcome. Commits follow Conventional Commits. See develop and contribute and AGENTS.md.

Licence

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.