From f4a6e0373bd7b72509aefb1423cec67bbdb18eaa Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 7 Apr 2026 20:19:35 +0200 Subject: [PATCH] main: update espflasher and auto-use best available flashing options on esp32 Signed-off-by: deadprogram --- go.mod | 2 +- go.sum | 4 ++-- main.go | 31 +++++++++++++++++++++---------- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 035527189..d18a92d9b 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( golang.org/x/sys v0.30.0 golang.org/x/tools v0.30.0 gopkg.in/yaml.v2 v2.4.0 - tinygo.org/x/espflasher v0.4.0 + tinygo.org/x/espflasher v0.5.0 tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74 ) diff --git a/go.sum b/go.sum index a3b398191..f223a4bf1 100644 --- a/go.sum +++ b/go.sum @@ -58,7 +58,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -tinygo.org/x/espflasher v0.4.0 h1:0N+Ei+0qT/wGkGIoMaY2g+oI519VA5G4kUVUYHedTv8= -tinygo.org/x/espflasher v0.4.0/go.mod h1:a3hRV9EETPUkfPE6P14p4A6jKKth+oD5gtQz3nmij+8= +tinygo.org/x/espflasher v0.5.0 h1:aLYv6ldFw/Bxj36e4DaEqjGMUwx+LvI7xdapqz49LQ8= +tinygo.org/x/espflasher v0.5.0/go.mod h1:a3hRV9EETPUkfPE6P14p4A6jKKth+oD5gtQz3nmij+8= tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74 h1:ovavgTdIBWCH8YWlcfq9gkpoyT1+IxMKSn+Df27QwE8= tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0= diff --git a/main.go b/main.go index 5ae4765bf..f0d7bd46c 100644 --- a/main.go +++ b/main.go @@ -1094,10 +1094,15 @@ const ( ) func flashBinUsingEsp32(port, resetMode, tmppath string, options *compileopts.Options) error { - var opts *espflasher.FlasherOptions + opts := espflasher.DefaultOptions() + opts.Compress = true + opts.Logger = &espflasher.StdoutLogger{W: os.Stdout} + if options.BaudRate != 0 { + opts.FlashBaudRate = options.BaudRate + } + // On Windows, we have to explicitly specify the reset mode to use USB JTAG. if runtime.GOOS == "windows" && resetMode == jtagReset { - opts = espflasher.DefaultOptions() opts.ResetMode = espflasher.ResetUSBJTAG } @@ -1108,8 +1113,6 @@ func flashBinUsingEsp32(port, resetMode, tmppath string, options *compileopts.Op defer flasher.Close() chipName := flasher.ChipName() - fmt.Printf("Connected to %s\n", chipName) - offset := uint32(0x0) if chipName == "ESP32" { offset = 0x1000 @@ -1121,18 +1124,26 @@ func flashBinUsingEsp32(port, resetMode, tmppath string, options *compileopts.Op return err } + if err := flasher.EraseFlash(); err != nil { + return fmt.Errorf("erase failed: %v", err) + } + + progress := func(current, total int) { + pct := float64(current) / float64(total) * 100 + bar := int(pct / 2) + fmt.Printf("\r[%-50s] %6.1f%%", strings.Repeat("#", bar)+strings.Repeat(".", 50-bar), pct) + if current >= total { + fmt.Println() + } + } + // Flash with progress reporting - err = flasher.FlashImage(data, offset, func(current, total int) { - fmt.Printf("\rFlashing: %d/%d bytes (%.0f%%)", current, total, - float64(current)/float64(total)*100) - }) + err = flasher.FlashImage(data, offset, progress) if err != nil { return err } fmt.Println() - time.Sleep(time.Second) - // Reset the device to run the new firmware flasher.Reset()