From 1fa1b2076c27b86e1e0e058982af5e1d993569dc Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 6 Apr 2026 09:19:26 +0900 Subject: [PATCH] Poll for BOOTSEL readiness instead of fixed sleep, load with -x --- cmd/load/main.go | 29 +++++++++++++---------------- lib/picotool/picotool.go | 15 ++++++++++++++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/cmd/load/main.go b/cmd/load/main.go index 53ac69c..69a5865 100644 --- a/cmd/load/main.go +++ b/cmd/load/main.go @@ -116,7 +116,19 @@ func run(buildDir string) error { } } - time.Sleep(2 * time.Second) + for i := range 2 { + wg.Add(1) + go func() { + defer wg.Done() + errs[i] = picotool.WaitForBootsel(serials[i], 10*time.Second) + }() + } + wg.Wait() + for i, err := range errs { + if err != nil { + return fmt.Errorf("wait %s: %w", serials[i], err) + } + } uf2s := []string{ filepath.Join(buildDir, "picomap.uf2"), @@ -138,21 +150,6 @@ func run(buildDir string) error { } } - fmt.Println("Rebooting...") - for i := range 2 { - wg.Add(1) - go func() { - defer wg.Done() - errs[i] = picotool.Reboot(serials[i]) - }() - } - wg.Wait() - for i, err := range errs { - if err != nil { - return fmt.Errorf("reboot %s: %w", serials[i], err) - } - } - fmt.Println("Done.") return nil } diff --git a/lib/picotool/picotool.go b/lib/picotool/picotool.go index f9c38b4..92c98a5 100644 --- a/lib/picotool/picotool.go +++ b/lib/picotool/picotool.go @@ -3,10 +3,23 @@ package picotool import ( "fmt" "os/exec" + "time" ) +func WaitForBootsel(serial string, timeout time.Duration) error { + deadline := time.Now().Add(timeout) + for time.Now().Before(deadline) { + cmd := exec.Command("picotool", "info", "--ser", serial) + if err := cmd.Run(); err == nil { + return nil + } + time.Sleep(100 * time.Millisecond) + } + return fmt.Errorf("device %s not found in BOOTSEL after %v", serial, timeout) +} + func Load(uf2Path string, serial string) error { - cmd := exec.Command("picotool", "load", uf2Path, "--ser", serial) + cmd := exec.Command("picotool", "load", uf2Path, "-x", "--ser", serial) out, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("picotool load: %w\n%s", err, out)