Skip to content

Commit f4a6e03

Browse files
committed
main: update espflasher and auto-use best available flashing options on esp32
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent d274545 commit f4a6e03

3 files changed

Lines changed: 24 additions & 13 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
golang.org/x/sys v0.30.0
1919
golang.org/x/tools v0.30.0
2020
gopkg.in/yaml.v2 v2.4.0
21-
tinygo.org/x/espflasher v0.4.0
21+
tinygo.org/x/espflasher v0.5.0
2222
tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74
2323
)
2424

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
5858
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
5959
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6060
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
61-
tinygo.org/x/espflasher v0.4.0 h1:0N+Ei+0qT/wGkGIoMaY2g+oI519VA5G4kUVUYHedTv8=
62-
tinygo.org/x/espflasher v0.4.0/go.mod h1:a3hRV9EETPUkfPE6P14p4A6jKKth+oD5gtQz3nmij+8=
61+
tinygo.org/x/espflasher v0.5.0 h1:aLYv6ldFw/Bxj36e4DaEqjGMUwx+LvI7xdapqz49LQ8=
62+
tinygo.org/x/espflasher v0.5.0/go.mod h1:a3hRV9EETPUkfPE6P14p4A6jKKth+oD5gtQz3nmij+8=
6363
tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74 h1:ovavgTdIBWCH8YWlcfq9gkpoyT1+IxMKSn+Df27QwE8=
6464
tinygo.org/x/go-llvm v0.0.0-20250422114502-b8f170971e74/go.mod h1:GFbusT2VTA4I+l4j80b17KFK+6whv69Wtny5U+T8RR0=

main.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,10 +1094,15 @@ const (
10941094
)
10951095

10961096
func flashBinUsingEsp32(port, resetMode, tmppath string, options *compileopts.Options) error {
1097-
var opts *espflasher.FlasherOptions
1097+
opts := espflasher.DefaultOptions()
1098+
opts.Compress = true
1099+
opts.Logger = &espflasher.StdoutLogger{W: os.Stdout}
1100+
if options.BaudRate != 0 {
1101+
opts.FlashBaudRate = options.BaudRate
1102+
}
1103+
10981104
// On Windows, we have to explicitly specify the reset mode to use USB JTAG.
10991105
if runtime.GOOS == "windows" && resetMode == jtagReset {
1100-
opts = espflasher.DefaultOptions()
11011106
opts.ResetMode = espflasher.ResetUSBJTAG
11021107
}
11031108

@@ -1108,8 +1113,6 @@ func flashBinUsingEsp32(port, resetMode, tmppath string, options *compileopts.Op
11081113
defer flasher.Close()
11091114

11101115
chipName := flasher.ChipName()
1111-
fmt.Printf("Connected to %s\n", chipName)
1112-
11131116
offset := uint32(0x0)
11141117
if chipName == "ESP32" {
11151118
offset = 0x1000
@@ -1121,18 +1124,26 @@ func flashBinUsingEsp32(port, resetMode, tmppath string, options *compileopts.Op
11211124
return err
11221125
}
11231126

1127+
if err := flasher.EraseFlash(); err != nil {
1128+
return fmt.Errorf("erase failed: %v", err)
1129+
}
1130+
1131+
progress := func(current, total int) {
1132+
pct := float64(current) / float64(total) * 100
1133+
bar := int(pct / 2)
1134+
fmt.Printf("\r[%-50s] %6.1f%%", strings.Repeat("#", bar)+strings.Repeat(".", 50-bar), pct)
1135+
if current >= total {
1136+
fmt.Println()
1137+
}
1138+
}
1139+
11241140
// Flash with progress reporting
1125-
err = flasher.FlashImage(data, offset, func(current, total int) {
1126-
fmt.Printf("\rFlashing: %d/%d bytes (%.0f%%)", current, total,
1127-
float64(current)/float64(total)*100)
1128-
})
1141+
err = flasher.FlashImage(data, offset, progress)
11291142
if err != nil {
11301143
return err
11311144
}
11321145
fmt.Println()
11331146

1134-
time.Sleep(time.Second)
1135-
11361147
// Reset the device to run the new firmware
11371148
flasher.Reset()
11381149

0 commit comments

Comments
 (0)