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 a board

Flash with a manifest

$ 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 checks every image before it erases anything, writes the images and the partition table, checks what it wrote, and resets the board into its application.

  • Port. nanoflash uses the only port that suits the board. When several ports suit it, pass --port.
  • Board. In a Cargo package with [package.metadata.nanoflash], --board is optional. When the manifest has several boards and the device cannot tell, pass --board-identifier.
  • Speed. --baud-rate changes the speed (460800 through a UART bridge by default). USB-Serial-JTAG boards and the ESP32-C2 stay at 115200 baud: see Espressif.
  • Verification. --no-verify skips the MD5, CRC-16 or read-back checks.
  • Scripts. --json prints one JSON document. The exit code is 0 on success, 1 for an input error, 2 for a device or connection error and 3 when the flash does not read back as written.

Find out what is attached

$ nanoflash detect --port /dev/ttyACM0
protocol espressif
chip esp32-s3
flash 8 MiB
usb 303a:1001
secure download mode no

Pass --protocol when the USB identity of the port does not tell, for example a Nano clone behind a CH340 bridge.

Flash an Espressif ELF file

An Espressif board can take an ELF file as the image of its application partition. The command turns it into an application image with espflash 4.7.

  • The partition must have the role firmware or update.
  • The image goes at a multiple of 0x10000, not below 0x10000.
  • The command writes a temporary file in the temporary directory of the system (TMPDIR on Unix) and removes it.

Other programs that use the library must provide the conversion themselves.

Flash an Arduino Nano 33 BLE or SAMD21 board

$ nanoflash flash --board hardware/nano33ble/manifest.toml
connecting
writing at 0x10000
verifying
resetting
flashed arduino-nano-33-ble (nrf52840, 1 MiB)
  0x10000  application  84 KiB
verified

nanoflash touches the port of the sketch at 1200 baud, waits for the port of the bootloader, writes and checks the images with CRC-16, then resets into the application. The bootloader at the start of the flash is never written.

If the touch does not start the bootloader, press the reset button of the board twice (the orange LED pulses) and pass the port of the bootloader.

The SAMD21 bootloader erases more than it writes. What flash erases describes it.

Flash an Arduino AVR board

Arduino’s AVR boards take the .hex file that the Arduino IDE builds.

$ nanoflash flash --board hardware/uno/manifest.toml
connecting
writing at 0x0
verifying
resetting
flashed arduino-uno-r3 (atmega328p, 32 KiB)
  0x0  application  300 B
verified
  • Uno, Nano, Pro Mini (stk500v1): a pulse on DTR resets them into Optiboot or the older ATmegaBOOT. nanoflash tries 115200 baud, then 57600. Pass --baud-rate 57600 for a Nano with the old bootloader to skip the first try.
  • Mega 2560 (stk500v2): its bootloader limits the order of the images. See What flash erases.
  • Leonardo, Micro, Pro Micro (avr109): a 1200-baud touch starts Caterina on its own port.

When the bootloader does not answer, press the reset button just before flashing: the bootloader of the Uno listens for only about a second after a reset. On the Leonardo, press reset once (the LED pulses) and pass the port of the bootloader within 8 seconds.

Pages are written whole and read back. The bootloader at the top of the flash is never written.

Flash a chip with more flash than the layout

A board whose layout has size = "detect" is checked against the minimum of the layout before connecting, then placed again for the flash that the chip reports:

$ nanoflash flash --board boards/esp32-s3-detect.toml
...
warning: the chip has 0x800000 bytes of flash, more than the minimum of 0x400000 of the layout, so partitions grew: "storage" from 0xe0000 to 0x4e0000 bytes; the bootloader image must be built for this flash size or with flash size detection
flashed esp32-s3-devkitc-detect (esp32-s3, 8 MiB)
  • Images and erase entries that name a partition follow it, and the partition table is generated for the reported size.
  • Images and erase entries given by offset, Intel HEX and ELF images and a manifest image of the partition table cannot follow the chip. They are refused when they touch a partition that moves, or for the table, when any partition grows or moves.
  • On an Espressif board, build the bootloader for this flash size or with flash size detection (one bootloader per flash size). The chip refuses a bootloader header that names more flash than it has.