diff --git a/.claude/skills/load/SKILL.md b/.claude/skills/load/SKILL.md index 05843cd..94f1b6e 100644 --- a/.claude/skills/load/SKILL.md +++ b/.claude/skills/load/SKILL.md @@ -1,11 +1,11 @@ --- name: load -description: Build firmware, load it onto the Pico, and reboot. Use when the user says "load", "flash", "deploy", "push to pico", or after making changes to picomap.cpp or include/ headers that need testing on hardware. +description: Build firmware, load it onto the Pico, and reboot. Use when the user says "load", "flash", "deploy", "push to pico", or after making changes to firmware/ files that need testing on hardware. user-invocable: true --- -Run `go run ./cmd/load/` from the project root. This builds the firmware, loads it onto the Pico, and reboots. +Run `go run ./cmd/load/` from the project root. This builds both firmware targets, loads picomap onto the first device and picomap_test onto the second, and reboots both. -If cmake needs reconfiguring (e.g. after CMakeLists.txt changes), run `cmake -B build` first. +After loading, run `go run ./cmd/info/` to verify both devices are responding. After modifying the load command itself (cmd/load/, lib/wire/, lib/picoserial/), run it twice: once to load the firmware, once to verify the load process still works end-to-end. diff --git a/cmd/info/main.go b/cmd/info/main.go index 3a627a5..d573d7e 100644 --- a/cmd/info/main.go +++ b/cmd/info/main.go @@ -3,11 +3,18 @@ package main import ( "fmt" "os" + "sync" "time" "github.com/theater/picomap/lib/client" ) +type deviceResult struct { + dev string + info *client.ResponseInfo + err error +} + func main() { if err := run(); err != nil { fmt.Fprintf(os.Stderr, "error: %v\n", err) @@ -21,27 +28,42 @@ func run() error { return err } if len(devs) == 0 { - return fmt.Errorf("no device found") - } - dev := devs[0] - - fmt.Printf("Device: %s\n", dev) - c, err := client.NewSerial(dev, 2*time.Second) - if err != nil { - return err - } - info, err := c.Info() - c.Close() - if err != nil { - return err + return fmt.Errorf("no devices found") } - fmt.Printf("Board ID: %02X%02X%02X%02X%02X%02X%02X%02X\n", - info.BoardID[0], info.BoardID[1], info.BoardID[2], info.BoardID[3], - info.BoardID[4], info.BoardID[5], info.BoardID[6], info.BoardID[7]) - fmt.Printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", - info.MAC[0], info.MAC[1], info.MAC[2], - info.MAC[3], info.MAC[4], info.MAC[5]) + results := make([]deviceResult, len(devs)) + var wg sync.WaitGroup + for i, dev := range devs { + wg.Add(1) + go func() { + defer wg.Done() + results[i].dev = dev + c, err := client.NewSerial(dev, 2*time.Second) + if err != nil { + results[i].err = err + return + } + info, err := c.Info() + c.Close() + results[i].info = info + results[i].err = err + }() + } + wg.Wait() + + for _, r := range results { + fmt.Printf("Device: %s\n", r.dev) + if r.err != nil { + fmt.Fprintf(os.Stderr, " error: %v\n", r.err) + continue + } + fmt.Printf(" Board ID: %02X%02X%02X%02X%02X%02X%02X%02X\n", + r.info.BoardID[0], r.info.BoardID[1], r.info.BoardID[2], r.info.BoardID[3], + r.info.BoardID[4], r.info.BoardID[5], r.info.BoardID[6], r.info.BoardID[7]) + fmt.Printf(" MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", + r.info.MAC[0], r.info.MAC[1], r.info.MAC[2], + r.info.MAC[3], r.info.MAC[4], r.info.MAC[5]) + } return nil } diff --git a/cmd/load/main.go b/cmd/load/main.go index 9fb1acc..53ac69c 100644 --- a/cmd/load/main.go +++ b/cmd/load/main.go @@ -5,6 +5,7 @@ import ( "os" "os/exec" "path/filepath" + "sync" "time" "github.com/theater/picomap/lib/client" @@ -25,7 +26,7 @@ func main() { } } -func run(buildDir string) error { +func build(buildDir string) error { fmt.Println("Configuring...") cmake := exec.Command("cmake", "-S", filepath.Join(filepath.Dir(buildDir)), "-B", buildDir) cmake.Stdout = os.Stdout @@ -41,36 +42,116 @@ func run(buildDir string) error { if err := cmd.Run(); err != nil { return fmt.Errorf("build failed: %w", err) } + return nil +} + +func boardSerial(id [8]byte) string { + return fmt.Sprintf("%02X%02X%02X%02X%02X%02X%02X%02X", + id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7]) +} + +func run(buildDir string) error { + if err := build(buildDir); err != nil { + return err + } devs, err := client.ListSerial() if err != nil { return err } - if len(devs) > 0 { - dev := devs[0] - fmt.Printf("Sending PICOBOOT request to %s...\n", dev) - c, err := client.NewSerial(dev, 2*time.Second) + if len(devs) < 2 { + return fmt.Errorf("expected 2 devices, found %d", len(devs)) + } + + serials := make([]string, 2) + errs := make([]error, 2) + + var wg sync.WaitGroup + for i := range 2 { + wg.Add(1) + go func() { + defer wg.Done() + c, err := client.NewSerial(devs[i], 2*time.Second) + if err != nil { + errs[i] = err + return + } + info, err := c.Info() + c.Close() + if err != nil { + errs[i] = err + return + } + serials[i] = boardSerial(info.BoardID) + }() + } + wg.Wait() + for i, err := range errs { + if err != nil { + return fmt.Errorf("info %s: %w", devs[i], err) + } + } + + fmt.Println("Sending PICOBOOT requests...") + for i := range 2 { + wg.Add(1) + go func() { + defer wg.Done() + c, err := client.NewSerial(devs[i], 2*time.Second) + if err != nil { + errs[i] = err + return + } + err = c.PICOBOOT() + c.Close() + if err != nil { + errs[i] = fmt.Errorf("PICOBOOT %s: %w", devs[i], err) + } + }() + } + wg.Wait() + for _, err := range errs { if err != nil { return err } - err = c.PICOBOOT() - c.Close() - if err != nil { - fmt.Fprintf(os.Stderr, "warning: PICOBOOT request failed: %v\n", err) - } else { - fmt.Println("Device confirmed reboot into PICOBOOT mode.") - } - time.Sleep(2 * time.Second) } - uf2 := filepath.Join(buildDir, "picomap.uf2") + time.Sleep(2 * time.Second) + + uf2s := []string{ + filepath.Join(buildDir, "picomap.uf2"), + filepath.Join(buildDir, "picomap_test.uf2"), + } + fmt.Println("Loading firmware...") - if err := picotool.Load(uf2); err != nil { - return err + for i := range 2 { + wg.Add(1) + go func() { + defer wg.Done() + errs[i] = picotool.Load(uf2s[i], serials[i]) + }() + } + wg.Wait() + for i, err := range errs { + if err != nil { + return fmt.Errorf("load %s: %w", serials[i], err) + } } fmt.Println("Rebooting...") - _ = picotool.Reboot() + 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/firmware/CMakeLists.txt b/firmware/CMakeLists.txt index a6f9adc..fedb3fd 100644 --- a/firmware/CMakeLists.txt +++ b/firmware/CMakeLists.txt @@ -9,26 +9,29 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 23) pico_sdk_init() -add_executable(picomap - main.cpp - dhcp.cpp - net.cpp - tusb_config.cpp +set(LIB_SOURCES + lib/dhcp.cpp + lib/net.cpp + lib/tusb_config.cpp w6300/w6300.cpp ) -target_include_directories(picomap PRIVATE - include - w6300 -) +set(LIB_DEPS pico_stdlib pico_rand tinyusb_device tinyusb_board hardware_pio hardware_spi hardware_dma hardware_clocks) +add_executable(picomap firmware.cpp ${LIB_SOURCES}) +target_include_directories(picomap PRIVATE include w6300) target_compile_options(picomap PRIVATE -Wall -Wextra -Wno-unused-parameter) - pico_generate_pio_header(picomap ${CMAKE_CURRENT_LIST_DIR}/w6300/qspi.pio) - pico_enable_stdio_usb(picomap 0) pico_enable_stdio_uart(picomap 0) - pico_add_extra_outputs(picomap) +target_link_libraries(picomap ${LIB_DEPS}) -target_link_libraries(picomap pico_stdlib pico_rand tinyusb_device tinyusb_board hardware_pio hardware_spi hardware_dma hardware_clocks) +add_executable(picomap_test test.cpp ${LIB_SOURCES}) +target_include_directories(picomap_test PRIVATE include w6300) +target_compile_options(picomap_test PRIVATE -Wall -Wextra -Wno-unused-parameter) +pico_generate_pio_header(picomap_test ${CMAKE_CURRENT_LIST_DIR}/w6300/qspi.pio) +pico_enable_stdio_usb(picomap_test 0) +pico_enable_stdio_uart(picomap_test 0) +pico_add_extra_outputs(picomap_test) +target_link_libraries(picomap_test ${LIB_DEPS}) diff --git a/firmware/main.cpp b/firmware/firmware.cpp similarity index 100% rename from firmware/main.cpp rename to firmware/firmware.cpp diff --git a/firmware/dhcp.cpp b/firmware/lib/dhcp.cpp similarity index 100% rename from firmware/dhcp.cpp rename to firmware/lib/dhcp.cpp diff --git a/firmware/net.cpp b/firmware/lib/net.cpp similarity index 100% rename from firmware/net.cpp rename to firmware/lib/net.cpp diff --git a/firmware/tusb_config.cpp b/firmware/lib/tusb_config.cpp similarity index 100% rename from firmware/tusb_config.cpp rename to firmware/lib/tusb_config.cpp diff --git a/firmware/test.cpp b/firmware/test.cpp new file mode 100644 index 0000000..3a109b2 --- /dev/null +++ b/firmware/test.cpp @@ -0,0 +1,58 @@ +#include "pico/stdlib.h" +#include "pico/bootrom.h" +#include "pico/unique_id.h" +#include "tusb.h" +#include "wire.h" +#include "usb_cdc.h" +#include "net.h" +#include "w6300.h" + +static usb_cdc usb; + +int main() { + tusb_init(); + net_init(); + + static static_vector rx_buf; + + while (true) { + tud_task(); + + usb.drain(); + + while (tud_cdc_available()) { + uint8_t byte; + if (tud_cdc_read(&byte, 1) != 1) break; + + rx_buf.push_back(byte); + + auto msg = try_decode(rx_buf); + if (!msg) { + if (rx_buf.full()) rx_buf.clear(); + continue; + } + + rx_buf.clear(); + + switch (msg->type_id) { + case RequestPICOBOOT::ext_id: + usb.send(encode_response(msg->message_id, ResponsePICOBOOT{})); + sleep_ms(100); + reset_usb_boot(0, 1); + break; + case RequestInfo::ext_id: { + ResponseInfo resp; + pico_unique_board_id_t uid; + pico_get_unique_board_id(&uid); + std::copy(uid.id, uid.id + 8, resp.board_id.begin()); + auto ninfo = w6300::get_net_info(); + resp.mac = ninfo.mac; + usb.send(encode_response(msg->message_id, resp)); + break; + } + } + } + + __wfi(); + } +} diff --git a/lib/picotool/picotool.go b/lib/picotool/picotool.go index 0777e4a..f9c38b4 100644 --- a/lib/picotool/picotool.go +++ b/lib/picotool/picotool.go @@ -5,8 +5,8 @@ import ( "os/exec" ) -func Load(uf2Path string) error { - cmd := exec.Command("picotool", "load", "-f", uf2Path) +func Load(uf2Path string, serial string) error { + cmd := exec.Command("picotool", "load", uf2Path, "--ser", serial) out, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("picotool load: %w\n%s", err, out) @@ -14,8 +14,8 @@ func Load(uf2Path string) error { return nil } -func Reboot() error { - cmd := exec.Command("picotool", "reboot") +func Reboot(serial string) error { + cmd := exec.Command("picotool", "reboot", "--ser", serial) out, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("picotool reboot: %w\n%s", err, out)