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

Browser API

The npm package nanoflash exports a TypeScript API with no runtime dependency, and nanoflash/button registers the <nanoflash-button> element. Use the browser package shows how to use them.

Functions

ExportRole
isSupported(): booleanWhether the browser has Web Serial.
prepare(manifestUrl): Promise<Prepared>Reads the manifest and starts to load the WebAssembly module. Call it before the click.
detect({ protocol, port?, onProgress? }): Promise<ChipInformation>Tells what chip the device on a port is. Without port, asks the user to choose one.
checkLayout(text, images, name?, options?): Promise<LayoutReport>Checks a layout. images is a list of { partition, size }.
writeLayout(text, format, name?, options?): Promise<Uint8Array>Writes a table: "espressif", "mbr", "linker" or "constants".
loadBackend(protocol): Promise<BackendModule>Imports the WebAssembly module of a protocol.
NanoflashErrorThe class of every failure.
camelize, fromWasmHelpers that turn the values of the library into the types of the package.

protocol is "espressif", "samba", "stk500v1", "stk500v2" or "avr109". The three AVR protocols share the avr module.

Prepared

MemberValue
manifestUrlThe URL of the manifest. Paths in the manifest are resolved against it.
protocolsThe protocols of the boards.
filtersFilters for requestPort: the devices of the boards. Empty when any board lists none.
bootloaderFiltersThe devices of the boards in their bootloader (bootloader-devices).
promptEraseWhether to offer to erase the whole flash (prompt-erase, new_install_prompt_erase).
flash(options?): Promise<FlashReport>Asks the user to choose a port, filtered by the devices of the manifest, before its first await, then flashes.

FlashOptions

OptionValue
portThe port to use. Without it, the user is asked to choose one.
boardIdentifierThe board of the manifest to flash. Without it, the board that the chip is.
eraseAllErase the whole flash first.
verifyRead back and check what was written. Default true.
baudRateThe baud rate to flash at. USB-Serial-JTAG boards and the ESP32-C2 stay at 115200 baud (Espressif).
onProgress(event)Receives a ProgressEvent: stage (connecting, erasing, writing, verifying, resetting), offset, done and total in bytes.
onMessage(text)Receives the notes of the library, and the warnings about the flash size.

FlashReport

FieldValue
boardThe identifier of the board.
chipThe ChipInformation.
imagesThe images written: label, offset, size.
verifiedWhether the flash was read back and checked.
warningsA list of warnings, each with a kind and the message to show.

Warning kinds and their fields:

KindFields
partition-table-differspartition
partition-table-not-checkedpartition
not-verified-in-secure-download-mode
erases-to-endfrom, to, partitions
partitions-grewflashSize, minimumFlashSize, partitions (each name, sizeAtMinimum, size), protocol
partitions-movedflashSize, minimumFlashSize, partitions (each name, offsetAtMinimum, offset)
flash-size-not-detectedminimumFlashSize

See Errors and warnings for their meaning.

ChipInformation

FieldValue
protocolThe protocol.
chipThe chip, such as esp32-s3.
flashSizeThe flash size in bytes, or null when the chip does not tell.
secureDownloadModeWhether an Espressif chip is in secure download mode.
usb{ vendor, product, serialNumber }, or null.
writableFromThe lowest offset that the bootloader writes, when it protects the flash below it: 0x10000 on the Nano 33 BLE, 0x2000 on the SAMD21 boards. Nothing below it is written or erased.
writableToThe end of the flash that the bootloader writes, where an AVR bootloader starts: 0x7e00 on an Uno. Nothing at or above it is written.
erasesToEndWhether the bootloader erases from an offset to the end of flash (SAMD21).

LayoutReport

FieldValue
flashSizeThe flash size that the layout is resolved for.
minimumFlashSizeThe minimum of a layout with size = "detect", else null.
unallocatedThe bytes that no partition uses.
partitionsA list of PartitionReport.

PartitionReport has name, role, offset, end, size, imageSize, headroom, headroomPercent, and for a detect layout growsWithFlash and movesWithFlash (its size or offset depends on the chip), minimumSize (the minimum that the layout declares, 0 for remaining) and growth (the bytes above it).

LayoutOptions is the last argument of checkLayout and writeLayout. flashSize, a number of bytes or a text such as "16 MiB", resolves a layout with size = "detect" for that flash instead of its minimum. On a layout with a fixed size it is a layout error, and a size that does not parse is an invalid-argument error. checkLayout still checks the images against the partitions on the minimum flash. For a size above the minimum, writeLayout fails with a table error for linker and constants.

NanoflashError

FieldValue
kindWhat failed, such as no-sync, verification-failed, not-granted, disconnected.
errorClassinput, device or verification.
messageThe message to show.
detailsThe fields of the library, in camelCase.
duringWhat the library was doing, for errors of the port.
rawThe object that the library threw, as it was.

See Errors and warnings for every kind.

<nanoflash-button>

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

Importing nanoflash does not register the element. The manifest is a nanoflash or an esp-web-tools manifest, in JSON.

AttributeValue
manifestThe URL of the manifest.
labelThe text of the button. Default Install.
erase-allErase the whole flash first. Without it, the element offers a checkbox when the manifest asks for it.

The element asks for the port inside the click. It shows the progress, the notes of the library, the result with what was not verified and every warning, and the errors. A broken module asks the user to reload the page. In a browser without Web Serial, it says so and points to the nanoflash command. When the board comes back in its bootloader under another USB identity, it shows a second button, “Select the board in bootloader mode”, which asks for that port. Install stays available beside it, to start over. A manifest with boards of several protocols works too: the chosen port picks the board.

ItemValues
data-stateunsupported, loading, ready, connecting, erasing, writing, verifying, resetting, done, error, bootloader-permission.
Events (bubbling, composed)nanoflash-progress (detail: the progress event), nanoflash-done (the report), nanoflash-error (the error).
CSS custom properties--nanoflash-button-background, --nanoflash-button-color, --nanoflash-button-radius, --nanoflash-progress-color, --nanoflash-font.
Partsbutton, erase, progress, message, bootloader-button.

Bundlers

The WebAssembly modules ship in dist/wasm and are loaded relative to the module that imports them (new URL("...wasm", import.meta.url)). Each module (espressif, samba, and avr for the three AVR protocols) is a separate dynamic import, so a bundler loads only the module of the board that is flashed. If your bundler does not copy .wasm files that it finds this way, copy node_modules/nanoflash/dist/wasm next to the chunk.