A Go command-line tool and library for flashing firmware to Espressif ESP8266 and ESP32-family microcontrollers over serial (UART) or USB-JTAG/Serial connections.
- ESP8266
- ESP32
- ESP32-S2
- ESP32-S3
- ESP32-C2 (ESP8684)
- ESP32-C3
- ESP32-C5
- ESP32-C6
- ESP32-H2
- ESP32-P4 (rev 3.x)
The ESP32-P4 defaults to rev 3.x (ECO6). The legacy ECO2 silicon (rev 1.x, no longer sold) is selectable with
-chip esp32p4-rev1. P4 flashes in ROM mode — the RAM stub loads but does not answer flash commands — so the stub is skipped automatically and a finalFLASH_ENDis sent after the last image part.
You can download and install one of the prebuilt binaries for your operating system under "Releases" or install from source:
go install tinygo.org/x/espflasher@latestespflasher <command> [options] [args]
| Command | Description |
|---|---|
write-flash |
Write binary files to flash |
read-flash |
Read flash contents to a file |
verify-flash |
Compare flash contents with files using MD5 |
erase-flash |
Erase the entire flash |
erase-region |
Erase a flash region |
chip-id |
Print chip type, revision, features and MAC |
flash-id |
Print SPI flash manufacturer, device and size |
read-mac |
Print the base MAC address |
get-security-info |
Print security info from the ROM loader |
read-mem |
Read a 32 bit word from memory |
write-mem |
Write a 32 bit word to memory, with optional mask |
run |
Reset the chip to run the application |
version |
Print version |
help |
Show help for a command |
Command names match esptool. Underscore names like write_flash also work. Every device command takes -port, -baud, -chip, -reset, -after and -no-stub. Run espflasher help <command> to see all options.
# Write one or more files at given offsets
espflasher write-flash -port /dev/ttyUSB0 0x0 bootloader.bin 0x8000 partitions.bin 0x10000 app.bin
# Read the whole flash to a file
espflasher read-flash -port /dev/ttyUSB0 0x0 ALL backup.bin
# Check that flash matches a file
espflasher verify-flash -port /dev/ttyUSB0 0x10000 app.bin
# Erase all flash, or one region
espflasher erase-flash -port /dev/ttyUSB0
espflasher erase-region -port /dev/ttyUSB0 0x9000 0x6000
# Chip and flash info
espflasher chip-id -port /dev/ttyUSB0
espflasher flash-id -port /dev/ttyUSB0
espflasher read-mac -port /dev/ttyUSB0
espflasher get-security-info -port /dev/ttyUSB0
# Read and write registers
espflasher read-mem -port /dev/ttyUSB0 0x60008000
espflasher write-mem -port /dev/ttyUSB0 0x60008000 0x1 0x1
# Reset into the application
espflasher run -port /dev/ttyUSB0The original usage without a command still works:
# Flash a single binary
espflasher -port /dev/ttyUSB0 firmware.bin
# Flash with specific offset and chip
espflasher -port /dev/ttyUSB0 -offset 0x10000 -chip esp32s3 app.bin
# Flash via native USB-JTAG/Serial (ESP32-S3, ESP32-C3, etc.)
espflasher -port /dev/ttyACM0 -reset usb-jtag -chip esp32s3 firmware.bin
# Flash multiple images (bootloader + partitions + app)
espflasher -port /dev/ttyUSB0 \
-bootloader bootloader.bin \
-partitions partitions.bin \
-app application.bin
# Flash an ESP32-P4 (rev 3.x) over its native USB-JTAG/Serial port
espflasher -port /dev/ttyACM0 -reset usb-jtag -chip esp32p4 app.bingo get tinygo.org/x/espflasher/pkg/espflasherpackage main
import (
"fmt"
"log"
"os"
"tinygo.org/x/espflasher/pkg/espflasher"
)
func main() {
// Connect to the ESP device
flasher, err := espflasher.New("/dev/ttyUSB0", nil)
if err != nil {
log.Fatal(err)
}
defer flasher.Close()
fmt.Printf("Connected to %s\n", flasher.ChipName())
// Read the firmware binary
data, err := os.ReadFile("firmware.bin")
if err != nil {
log.Fatal(err)
}
// Flash with progress reporting
err = flasher.FlashImage(data, 0x0, func(current, total int) {
fmt.Printf("\rFlashing: %d/%d bytes (%.0f%%)", current, total,
float64(current)/float64(total)*100)
})
if err != nil {
log.Fatal(err)
}
fmt.Println()
// Reset the device to run the new firmware
flasher.Reset()
fmt.Println("Done!")
}- Auto-detection: Automatically identifies the connected ESP chip
- Compressed transfers: Uses zlib compression for significantly faster flashing
- Multi-image support: Flash bootloader, partition table, and application in one operation
- Progress callbacks: Monitor flash progress in real-time
- MD5 verification: Verifies written data integrity after flashing
- Flash readback: Read flash contents back with byte-accurate progress reporting
- GPIO access: Read, drive, and release GPIO pins over the ROM/stub connection on ESP32-S2, ESP32-C3, and ESP32-S3
- Connection hooks: Display connection-phase status updates or override serial port opening when integrating with monitors or custom transports
- Configurable: Customize baud rate, compression, reset mode, and more
- USB-JTAG/Serial: Native USB support for boards like ESP32-S3 and ESP32-C3 that expose a built-in USB-JTAG/Serial interface (typically
/dev/ttyACM0on Linux,cu.usbmodem*on macOS) - Stubs: Use stubs for higher-speed downloads and other advanced processor features
- Stub control: Opt out of loading the stub for register-level workflows that only need ROM functionality
- NVS support: Generate and parse ESP-IDF NVS (Non-Volatile Storage) partition images in pure Go, including lossless read-modify-write of unknown and chunked entries
The nvs package provides a pure-Go implementation for generating and parsing ESP-IDF NVS (Non-Volatile Storage) partition images in the v2 binary format.
It is suitable for read-modify-write workflows: ParseNVS preserves unknown entry types and ESP-IDF chunked blob entries so GenerateNVS can round-trip them without data loss.
go get tinygo.org/x/espflasher/pkg/nvsimport "tinygo.org/x/espflasher/pkg/nvs"
entries := []nvs.Entry{
{Namespace: "wifi", Key: "ssid", Type: "string", Value: "MyNetwork"},
{Namespace: "wifi", Key: "channel", Type: "u8", Value: uint8(6)},
{Namespace: "config", Key: "timeout", Type: "u16", Value: uint16(3000)},
{Namespace: "config", Key: "name", Type: "string", Value: "MyDevice"},
}
partition, err := nvs.GenerateNVS(entries, nvs.DefaultPartSize)
if err != nil {
log.Fatal(err)
}
// partition is a []byte ready to flash at the NVS partition offsetentries, err := nvs.ParseNVS(partitionData)
if err != nil {
log.Fatal(err)
}
for _, e := range entries {
fmt.Printf("[%s] %s = %v\n", e.Namespace, e.Key, e.Value)
}| Type | Go Value Type |
|---|---|
u8 |
uint8 (or int) |
u16 |
uint16 (or int) |
u32 |
uint32 (or int) |
i8 |
int8 (or int) |
i16 |
int16 (or int) |
i32 |
int32 (or int) |
string |
string |
blob |
[]byte |
The pkg/webusb package lets you flash boards from a browser by compiling to wasm with Go or TinyGo. It supports USB CDC-ACM devices (including the ESP native USB-Serial/JTAG and USB-OTG ports), CP210x and CH34x bridges.
// dev is a USBDevice from navigator.usb.requestDevice({filters: webusb.Filters()}).
// Call this from a goroutine, not directly inside a JS callback.
f, err := espflasher.New(webusb.PortName(dev), webusb.Options())See examples/webusb for a complete page.
| CLI Flag | Go Constant | Description |
|---|---|---|
default |
ResetDefault |
Classic DTR/RTS reset sequence. Works with most boards using a USB-to-UART bridge (CP2102, CH340, etc.). |
usb-jtag |
ResetUSBJTAG |
Reset sequence for boards with a native USB-JTAG/Serial interface (ESP32-S3, ESP32-C3, ESP32-C6, ESP32-H2, ESP32-P4). Use this when connected via /dev/ttyACM0 (Linux) or cu.usbmodem* (macOS). |
no-reset |
ResetNoReset |
Skip hardware reset entirely. Useful when the chip is already in bootloader mode or reset is handled externally. |
If the port is a program's USB-OTG CDC device with the Espressif VID (not the 303a:1001 USB-JTAG/Serial unit), every mode except no-reset first sends a 1200 baud touch. A program that supports it, such as TinyGo with -tags=usbotg on the ESP32-S3, restarts into download mode on USB-JTAG/Serial, and espflasher continues on that port.
// With default options (115200 baud, auto-detect, compressed)
flasher, err := espflasher.New("/dev/ttyUSB0", nil)
// With custom options
opts := espflasher.DefaultOptions()
opts.FlashBaudRate = 921600
opts.ChipType = espflasher.ChipESP32S3
opts.Logger = &espflasher.StdoutLogger{W: os.Stdout}
flasher, err := espflasher.New("/dev/ttyUSB0", opts)
// For boards with native USB-JTAG/Serial (ESP32-S3, ESP32-C3, etc.)
opts.ResetMode = espflasher.ResetUSBJTAG
flasher, err := espflasher.New("/dev/ttyACM0", opts)
// Surface connection phases and skip the stub for ROM-only workflows.
opts.ConnectStatus = func(phase espflasher.ConnectPhase, attempt, maxAttempts int, message string) {
fmt.Printf("[%s] %s\n", phase, message)
}
opts.SkipStub = true
flasher, err := espflasher.New("/dev/ttyUSB0", opts)
// Flash an ESP32-P4 (rev 3.x). P4 flashes in ROM mode, so the stub is skipped
// automatically; select the chip explicitly and use the USB-JTAG reset.
opts.ChipType = espflasher.ChipESP32P4Rev3
opts.ResetMode = espflasher.ResetUSBJTAG
flasher, err = espflasher.New("/dev/ttyACM0", opts)data, _ := os.ReadFile("firmware.bin")
err := flasher.FlashImage(data, 0x0, progressCallback)images := []espflasher.ImagePart{
{Data: bootloaderBin, Offset: 0x1000},
{Data: partitionsBin, Offset: 0x8000},
{Data: applicationBin, Offset: 0x10000},
}
err := flasher.FlashImages(images, progressCallback)// Erase entire flash
err := flasher.EraseFlash(nil)
// Erase a specific region (must be sector-aligned)
err := flasher.EraseRegion(0x10000, 0x100000, nil)
// Erase with progress: current/total are elapsed/estimated milliseconds
err := flasher.EraseFlash(func(current, total int) {
fmt.Printf("erasing... %dms / %dms\n", current, total)
})
// Read back flash with byte-accurate progress
data, err := flasher.ReadFlash(0x10000, 0x20000, func(current, total int) {
fmt.Printf("read %d / %d bytes\n", current, total)
})
// Ask the device to compute an MD5 for a flash region
sum, err := flasher.GetFlashMD5(0x10000, uint32(len(data)), func(current, total int) {
fmt.Printf("md5... %dms / %dms\n", current, total)
})
// Probe and temporarily drive a GPIO on supported chips
reserved, reason := flasher.GPIOReserved(2)
if reserved {
fmt.Printf("GPIO2 is reserved: %s\n", reason)
}
level, err := flasher.ReadGPIO(2)
err = flasher.SetGPIO(2, !level)
err = flasher.ReleaseGPIO(2)
// Read a hardware register
val, err := flasher.ReadRegister(0x3FF00050)
// Hard reset the device
flasher.Reset()The library is organized in layers:
| Layer | File(s) | Description |
|---|---|---|
| SLIP | pkg/espflasher/slip.go |
Serial Line Internet Protocol framing |
| Protocol | pkg/espflasher/protocol.go |
ROM bootloader command/response protocol |
| Chip | pkg/espflasher/chip.go, pkg/espflasher/target_*.go |
Per-target definitions and detection |
| Reset | pkg/espflasher/reset.go |
Hardware reset strategies |
| Flasher | pkg/espflasher/flasher.go |
High-level flash/verify/reset API |
| WebUSB | pkg/webusb/*.go |
serial.Port over the browser WebUSB API |
| NVS | pkg/nvs/*.go |
Generate and parse NVS partition images |
| CLI | main.go |
Command-line interface |
The flasher includes pre-compiled bootloader stubs from esp-flasher-stub releases. To update stubs:
- Edit
stubVersionintools/update-stubs.goto the desired release version - Run
go generate ./pkg/espflasher/...to download and embed the latest stubs - The
go:generatedirective inpkg/espflasher/stub.gowill invoketools/update-stubs.go
This library implements the ESP serial bootloader protocol as documented by Espressif's esptool. Key protocol features:
- SLIP framing (RFC 1055) over UART
- 8-byte command/response headers with opcodes
- XOR checksum verification
- Compressed flash writes via zlib deflate
- MD5 verification of flash contents
- SPI flash parameter configuration