Use the browser package
The npm package nanoflash flashes boards from a web page over Web Serial. It is the Rust library
compiled to WebAssembly, behind a TypeScript API with no runtime dependency. Every export is in
Browser API.
Before you start
- Web Serial exists in Chromium-based browsers only (Chrome, Edge, Opera), on a secure origin
(
https:orlocalhost).isSupported()tells whether the browser has it. - The browser reads JSON manifests: nanoflash’s (the keys of the TOML manifest, with
boardas an array) or an esp-web-tools manifest. - Install the package with
npm install nanoflash, or build it from the repository withmise run web.
Add a button with no code
<script type="module">import "nanoflash/button";</script>
<nanoflash-button manifest="firmware/manifest.json" label="Install"></nanoflash-button>
The element handles the port chooser, the progress, the errors and the second permission of Arduino boards. Style it with CSS custom properties and parts. The tutorial Add a flash button to a web page walks through it.
Flash from your own code
import { isSupported, prepare, NanoflashError } from "nanoflash";
if (!isSupported()) showMessage("Use a Chromium based browser, or the command line.");
// Before the click: reads the manifest and starts loading the WebAssembly module.
const prepared = await prepare("/boards/esp32-s3.json");
if (prepared.promptErase) showEraseCheckbox();
button.addEventListener("click", async () => {
try {
const report = await prepared.flash({
eraseAll: checkbox.checked,
onProgress: ({ stage, done, total }) => showProgress(stage, done, total),
onMessage: (text) => log(text),
});
showMessage(`Flashed ${report.board} (${report.chip.chip}), verified: ${report.verified}`);
} catch (error) {
if (error instanceof NanoflashError) showMessage(`${error.kind} (${error.errorClass}): ${error.message}`);
else throw error;
}
});
- Call
preparebefore the click. It reads the manifest and starts to load the module. - Call
prepared.flashstraight from the click handler. A browser shows the port chooser only during a user gesture, and anawaitmay end it.prepared.flashasks for the port before its firstawait. - Show
report.warnings. Each warning has akindand themessageto show.onMessagealso receives the three warnings about the flash size.
A manifest may mix protocols. The USB identity of the chosen port, matched against the
application-devices and bootloader-devices of each board, picks the boards and so the module to
load. With no devices listed, the boards that a serial bridge suits are candidates. A port that
suits no board fails with no-matching-board, and one that suits boards of several protocols with
several-protocols: give boardIdentifier.
Arduino boards and the bootloader permission
An Arduino board that runs a sketch is reset into its SAM-BA bootloader by opening its port at 1200
baud and clearing DTR. The bootloader then appears as another USB device (the Nano 33 BLE goes from
2341:805a to 2341:005a), which the page has no permission for the first time.
flashwaits 15 s for the bootloader among the granted ports, then fails withnot-granted.- From a second click, ask for that port with
navigator.serial.requestPort({ filters: prepared.bootloaderFilters })(thebootloader-devicesof the manifest), and callprepared.flash({ port })with it. The component does this with its second button. - The browser keeps the grant, so the next flash finds the bootloader by itself.
A board already in its bootloader (double tap on reset) is flashed as it is.
The Leonardo, Micro and Pro Micro (AVR109) do the same: the touch, then Caterina on its own USB
identity (2341:8036 becomes 2341:0036 on a Leonardo). Caterina goes back to the sketch 8 s after
the touch, so flash waits 5 s for it before it fails with not-granted. For the first grant,
press the reset button of the board once and choose its port while the LED pulses. The bootloader
permission text of the component says so when the manifest has an avr109 board.
The Uno, Nano, Pro Mini and Mega 2560 keep their port: a pulse on DTR resets them into their bootloader, through the USB serial chip of the board or a serial bridge.
Detect a chip
import { detect } from "nanoflash";
const chip = await detect({ protocol: "espressif" });
console.log(chip.chip, chip.flashSize);
protocol names the protocol, and so the module to load.
Check a layout and write its table
import { checkLayout, writeLayout } from "nanoflash";
const text = await (await fetch("/layouts/esp32-s3-8mb.toml")).text();
const report = await checkLayout(text, [{ partition: "application", size: 1_048_576 }]);
const table: Uint8Array = await writeLayout(text, "espressif"); // or "mbr", "linker", "constants"
For a layout with size = "detect", pass flashSize to see it on a larger chip:
const report = await checkLayout(text, [], "layout.toml", { flashSize: "16 MiB" });
const table = await writeLayout(text, "espressif", "layout.toml", { flashSize: 16_777_216 });
Handle errors
Every failure is a NanoflashError with a kind and an errorClass.
no-port-chosen: the user closed the port chooser.port-request-refused: the browser did not show the chooser. A page in an iframe needsallow="serial", and a permissions policy may block Web Serial.not-granted: the board came back as a device that the page has no permission for.module-broken: a WebAssembly module trapped. Ask the user to reload the page.
Errors and warnings lists every kind.
Bundle the package
Each module is a separate dynamic import, loaded relative to the module that imports it. If your
bundler does not copy the .wasm files that it finds this way, copy
node_modules/nanoflash/dist/wasm next to the chunk.