Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2026, TinyGo
Copyright The TinyGo Authors. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# espflash
# espflasher

[![Test](https://github.com/tinygo-org/espflash/actions/workflows/test.yml/badge.svg)](https://github.com/tinygo-org/espflash/actions/workflows/test.yml)
[![Test](https://github.com/tinygo-org/espflasher/actions/workflows/test.yml/badge.svg)](https://github.com/tinygo-org/espflasher/actions/workflows/test.yml)

A Go command-line tool and library for flashing firmware to Espressif ESP8266 and ESP32-family microcontrollers over a serial (UART) connection.

Expand All @@ -22,37 +22,37 @@ A Go command-line tool and library for flashing firmware to Espressif ESP8266 an
You can download and install one of the prebuilt binaries for your operating system under "Releases" or install from source:

```bash
go install tinygo.org/x/espflash@latest
go install tinygo.org/x/espflasher@latest
```

### CLI Usage

```bash
# Install
go install tinygo.org/x/espflash@latest
go install tinygo.org/x/espflasher@latest

# Flash a single binary
espflash -port /dev/ttyUSB0 firmware.bin
espflasher -port /dev/ttyUSB0 firmware.bin

# Flash with specific offset and chip
espflash -port /dev/ttyUSB0 -offset 0x10000 -chip esp32s3 app.bin
espflasher -port /dev/ttyUSB0 -offset 0x10000 -chip esp32s3 app.bin

# Flash multiple images (bootloader + partitions + app)
espflash -port /dev/ttyUSB0 \
espflasher -port /dev/ttyUSB0 \
-bootloader bootloader.bin \
-partitions partitions.bin \
-app application.bin

# Erase flash before writing
espflash -port /dev/ttyUSB0 -erase-all firmware.bin
espflasher -port /dev/ttyUSB0 -erase-all firmware.bin
```

## Library

### Installation

```bash
go get tinygo.org/x/espflash/pkg/espflash
go get tinygo.org/x/espflasher/pkg/espflasher
```

### Quick Start
Expand All @@ -65,12 +65,12 @@ import (
"log"
"os"

"tinygo.org/x/espflash/pkg/espflash"
"tinygo.org/x/espflasher/pkg/espflasher"
)

func main() {
// Connect to the ESP device
flasher, err := espflash.NewFlasher("/dev/ttyUSB0", nil)
flasher, err := espflasher.NewFlasher("/dev/ttyUSB0", nil)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -115,14 +115,14 @@ func main() {

```go
// With default options (115200 baud, auto-detect, compressed)
flasher, err := espflash.NewFlasher("/dev/ttyUSB0", nil)
flasher, err := espflasher.NewFlasher("/dev/ttyUSB0", nil)

// With custom options
opts := espflash.DefaultOptions()
opts := espflasher.DefaultOptions()
opts.FlashBaudRate = 921600
opts.ChipType = espflash.ChipESP32S3
opts.Logger = &espflash.StdoutLogger{W: os.Stdout}
flasher, err := espflash.NewFlasher("/dev/ttyUSB0", opts)
opts.ChipType = espflasher.ChipESP32S3
opts.Logger = &espflasher.StdoutLogger{W: os.Stdout}
flasher, err := espflasher.NewFlasher("/dev/ttyUSB0", opts)
```

### Flashing a Single Binary
Expand All @@ -135,7 +135,7 @@ err := flasher.FlashImage(data, 0x0, progressCallback)
### Flashing Multiple Images

```go
images := []espflash.ImagePart{
images := []espflasher.ImagePart{
{Data: bootloaderBin, Offset: 0x1000},
{Data: partitionsBin, Offset: 0x8000},
{Data: applicationBin, Offset: 0x10000},
Expand Down Expand Up @@ -165,11 +165,11 @@ The library is organized in layers:

| Layer | File(s) | Description |
|-------|---------|-------------|
| SLIP | `pkg/espflash/slip.go` | Serial Line Internet Protocol framing |
| Protocol | `pkg/espflash/protocol.go` | ROM bootloader command/response protocol |
| Chip | `pkg/espflash/chip.go`, `pkg/espflash/target_*.go` | Per-target definitions and detection |
| Reset | `pkg/espflash/reset.go` | Hardware reset strategies |
| Flasher | `pkg/espflash/flasher.go` | High-level flash/verify/reset API |
| 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 |
| CLI | `main.go` | Command-line interface |

## Protocol Reference
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module tinygo.org/x/espflash
module tinygo.org/x/espflasher

go 1.22

Expand Down
56 changes: 28 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Command espflash flashes firmware to Espressif ESP8266 and ESP32-family
// Command espflasher flashes firmware to Espressif ESP8266 and ESP32-family
// microcontrollers over a serial (UART) connection.
//
// Install:
//
// go install tinygo.org/x/espflash@latest
// go install tinygo.org/x/espflasher@latest
//
// Usage:
//
// espflash -port /dev/ttyUSB0 -offset 0x0 firmware.bin
// espflash -port /dev/ttyUSB0 -bootloader bootloader.bin -partitions partitions.bin -app app.bin
// espflasher -port /dev/ttyUSB0 -offset 0x0 firmware.bin
// espflasher -port /dev/ttyUSB0 -bootloader bootloader.bin -partitions partitions.bin -app app.bin
package main

import (
Expand All @@ -19,7 +19,7 @@ import (
"strconv"
"strings"

"tinygo.org/x/espflash/pkg/espflash"
"tinygo.org/x/espflasher/pkg/espflasher"
)

func main() {
Expand Down Expand Up @@ -64,7 +64,7 @@ func main() {
flag.Parse()

if *version {
fmt.Printf("espflash %s\n", espflash.Version)
fmt.Printf("espflasher %s\n", espflasher.Version)
return
}

Expand All @@ -89,27 +89,27 @@ func main() {
os.Exit(1)
}

opts := espflash.DefaultOptions()
opts := espflasher.DefaultOptions()
opts.FlashBaudRate = *baud
opts.Compress = !*noCompress
opts.Logger = &espflash.StdoutLogger{W: os.Stdout}
opts.Logger = &espflasher.StdoutLogger{W: os.Stdout}
opts.ChipType = parseChipType(*chip)
opts.FlashMode = *flashMode
opts.FlashFreq = *flashFreq
opts.FlashSize = *flashSize
switch strings.ToLower(*resetMode) {
case "default":
opts.ResetMode = espflash.ResetDefault
opts.ResetMode = espflasher.ResetDefault
case "no-reset":
opts.ResetMode = espflash.ResetNoReset
opts.ResetMode = espflasher.ResetNoReset
case "usb-jtag":
opts.ResetMode = espflash.ResetUSBJTAG
opts.ResetMode = espflasher.ResetUSBJTAG
default:
log.Fatalf("Unknown reset mode: %s", *resetMode)
}

fmt.Printf("Connecting to %s...\n", *port)
flasher, err := espflash.NewFlasher(*port, opts)
flasher, err := espflasher.New(*port, opts)
if err != nil {
log.Fatalf("Failed to connect: %v", err)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func main() {
log.Fatalf("Flash failed: %v", err)
}
} else {
var images []espflash.ImagePart
var images []espflasher.ImagePart

if *bootloader != "" {
data, err := os.ReadFile(*bootloader)
Expand All @@ -160,7 +160,7 @@ func main() {
// Use chip-specific default
off = 0x1000 // Most common default
}
images = append(images, espflash.ImagePart{Data: data, Offset: off})
images = append(images, espflasher.ImagePart{Data: data, Offset: off})
fmt.Printf("Bootloader: %s (%d bytes) at 0x%08X\n", *bootloader, len(data), off)
}

Expand All @@ -170,7 +170,7 @@ func main() {
log.Fatalf("Read partitions: %v", err)
}
off := parseHex(*partitionsOffset)
images = append(images, espflash.ImagePart{Data: data, Offset: off})
images = append(images, espflasher.ImagePart{Data: data, Offset: off})
fmt.Printf("Partitions: %s (%d bytes) at 0x%08X\n", *partitions, len(data), off)
}

Expand All @@ -180,7 +180,7 @@ func main() {
log.Fatalf("Read app: %v", err)
}
off := parseHex(*appOffset)
images = append(images, espflash.ImagePart{Data: data, Offset: off})
images = append(images, espflasher.ImagePart{Data: data, Offset: off})
fmt.Printf("App: %s (%d bytes) at 0x%08X\n", *app, len(data), off)
}

Expand All @@ -207,37 +207,37 @@ func parseHex(s string) uint32 {
return uint32(val)
}

func parseChipType(s string) espflash.ChipType {
func parseChipType(s string) espflasher.ChipType {
switch strings.ToLower(s) {
case "auto":
return espflash.ChipAuto
return espflasher.ChipAuto
case "esp8266":
return espflash.ChipESP8266
return espflasher.ChipESP8266
case "esp32":
return espflash.ChipESP32
return espflasher.ChipESP32
case "esp32s2", "esp32-s2":
return espflash.ChipESP32S2
return espflasher.ChipESP32S2
case "esp32s3", "esp32-s3":
return espflash.ChipESP32S3
return espflasher.ChipESP32S3
case "esp32c2", "esp32-c2":
return espflash.ChipESP32C2
return espflasher.ChipESP32C2
case "esp32c3", "esp32-c3":
return espflash.ChipESP32C3
return espflasher.ChipESP32C3
case "esp32c6", "esp32-c6":
return espflash.ChipESP32C6
return espflasher.ChipESP32C6
case "esp32h2", "esp32-h2":
return espflash.ChipESP32H2
return espflasher.ChipESP32H2
default:
log.Fatalf("Unknown chip type: %s", s)
return espflash.ChipAuto
return espflasher.ChipAuto
}
}

// reorderArgs moves positional arguments (non-flag args that aren't values of
// a flag) to the end of os.Args so that Go's flag package can parse all flags
// regardless of where they appear on the command line. This lets users write:
//
// espflash -port COM3 firmware.bin -fm dout
// espflasher -port COM3 firmware.bin -fm dout
//
// instead of requiring all flags before the positional argument.
func reorderArgs() {
Expand Down
4 changes: 0 additions & 4 deletions pkg/espflash/version.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/espflash/chip.go → pkg/espflasher/chip.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion pkg/espflash/chip_test.go → pkg/espflasher/chip_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"testing"
Expand Down
6 changes: 3 additions & 3 deletions pkg/espflash/doc.go → pkg/espflasher/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package espflash provides a Go library for flashing firmware to Espressif
// Package espflasher provides a Go library for flashing firmware to Espressif
// ESP8266 and ESP32-family microcontrollers over a serial (UART) connection.
// It implements the serial bootloader protocol used by the ESP ROM bootloader,
// supporting the following chip families:
Expand All @@ -16,7 +16,7 @@
//
// To flash a .bin file to a connected ESP device:
//
// flasher, err := espflash.NewFlasher("/dev/ttyUSB0", nil)
// flasher, err := espflasher.NewFlasher("/dev/ttyUSB0", nil)
// if err != nil {
// log.Fatal(err)
// }
Expand All @@ -41,4 +41,4 @@
// The protocol uses SLIP framing over serial UART. Commands are sent as
// request packets with an opcode, and the device responds with status.
// Flash writes can optionally use zlib-compressed data for faster transfers.
package espflash
package espflasher
2 changes: 1 addition & 1 deletion pkg/espflash/errors.go → pkg/espflasher/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import "fmt"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"strings"
Expand Down
10 changes: 5 additions & 5 deletions pkg/espflash/flasher.go → pkg/espflasher/flasher.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"bytes"
Expand Down Expand Up @@ -96,7 +96,7 @@ type Flasher struct {
portStr string
}

// NewFlasher creates a new Flasher connected to the given serial port.
// New creates a new Flasher connected to the given serial port.
//
// It opens the serial port, enters the bootloader, syncs with the device,
// and detects the chip type. On success, the Flasher is ready for flash
Expand All @@ -106,12 +106,12 @@ type Flasher struct {
//
// Example:
//
// f, err := espflash.NewFlasher("/dev/ttyUSB0", nil)
// f, err := espflasher.New("/dev/ttyUSB0", nil)
// if err != nil {
// log.Fatal(err)
// }
// defer f.Close()
func NewFlasher(portName string, opts *FlasherOptions) (*Flasher, error) {
func New(portName string, opts *FlasherOptions) (*Flasher, error) {
if opts == nil {
opts = DefaultOptions()
}
Expand Down Expand Up @@ -355,7 +355,7 @@ func (f *Flasher) FlashImage(data []byte, offset uint32, progress ProgressFunc)
//
// Example:
//
// images := []espflash.ImagePart{
// images := []espflasher.ImagePart{
// {Data: bootloader, Offset: 0x1000},
// {Data: partTable, Offset: 0x8000},
// {Data: app, Offset: 0x10000},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/espflash/image.go → pkg/espflasher/image.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"crypto/sha256"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"crypto/sha256"
Expand Down
2 changes: 1 addition & 1 deletion pkg/espflash/protocol.go → pkg/espflasher/protocol.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package espflash
package espflasher

import (
"encoding/binary"
Expand Down
Loading