From 49bbe1b29c32a6d5b60d3066dbeb81bc7b0c8434 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Mon, 6 Apr 2026 20:01:22 +0900 Subject: [PATCH] Split dispatch init/run, join IPv6 multicast discovery group in firmware --- firmware/firmware.cpp | 26 +++++++++++++++++++++++++- firmware/include/dispatch.h | 3 ++- firmware/lib/dispatch.cpp | 10 ++++++---- firmware/test.cpp | 3 ++- firmware/w6300/w6300.cpp | 4 ++++ firmware/w6300/w6300.h | 1 + 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/firmware/firmware.cpp b/firmware/firmware.cpp index be65a5e..fe01faf 100644 --- a/firmware/firmware.cpp +++ b/firmware/firmware.cpp @@ -1,5 +1,17 @@ #include "dispatch.h" #include "handlers.h" +#include "w6300.h" + +static constexpr uint16_t PICOMAP_DISCOVERY_PORT = 28777; + +static constexpr std::array picomap_discovery_ip = { + 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x70, 0x69, 0x63, 0x6f, 0x6d, 0x61, 0x70, 0x00, +}; + +static constexpr std::array picomap_discovery_mac = { + 0x33, 0x33, 0x6d, 0x61, 0x70, 0x00, +}; static constexpr handler_entry handlers[] = { {RequestPICOBOOT::ext_id, handle_picoboot}, @@ -7,5 +19,17 @@ static constexpr handler_entry handlers[] = { }; int main() { - dispatch(handlers); + dispatch_init(); + + auto sn = w6300::socket_id{1}; + w6300::set_socket_dest_mac(sn, picomap_discovery_mac); + w6300::ip_address addr = {}; + std::copy(picomap_discovery_ip.begin(), picomap_discovery_ip.end(), addr.ip.begin()); + addr.len = 16; + w6300::set_socket_dest_ip(sn, addr); + w6300::set_socket_dest_port(sn, w6300::port_num{PICOMAP_DISCOVERY_PORT}); + w6300::open_socket(sn, w6300::protocol::udp6, w6300::port_num{PICOMAP_DISCOVERY_PORT}, + w6300::sock_flag::multi_enable); + + dispatch_run(handlers); } diff --git a/firmware/include/dispatch.h b/firmware/include/dispatch.h index 24beddb..02816e5 100644 --- a/firmware/include/dispatch.h +++ b/firmware/include/dispatch.h @@ -8,4 +8,5 @@ struct handler_entry { void (*handle)(usb_cdc&, uint32_t); }; -[[noreturn]] void dispatch(std::span handlers); +void dispatch_init(); +[[noreturn]] void dispatch_run(std::span handlers); diff --git a/firmware/lib/dispatch.cpp b/firmware/lib/dispatch.cpp index 6b1a323..b5dff0b 100644 --- a/firmware/lib/dispatch.cpp +++ b/firmware/lib/dispatch.cpp @@ -6,15 +6,17 @@ #include "timer_queue.h" #include "net.h" -[[noreturn]] void dispatch(std::span handlers) { +void dispatch_init() { + tusb_init(); + net_init(); +} + +[[noreturn]] void dispatch_run(std::span handlers) { std::unordered_map handler_map; for (auto& entry : handlers) { handler_map[entry.type_id] = entry.handle; } - tusb_init(); - net_init(); - static usb_cdc usb; static timer_queue timers; static static_vector rx_buf; diff --git a/firmware/test.cpp b/firmware/test.cpp index be65a5e..276681f 100644 --- a/firmware/test.cpp +++ b/firmware/test.cpp @@ -7,5 +7,6 @@ static constexpr handler_entry handlers[] = { }; int main() { - dispatch(handlers); + dispatch_init(); + dispatch_run(handlers); } diff --git a/firmware/w6300/w6300.cpp b/firmware/w6300/w6300.cpp index 4b786b0..e0ce8a9 100644 --- a/firmware/w6300/w6300.cpp +++ b/firmware/w6300/w6300.cpp @@ -1730,6 +1730,10 @@ uint16_t get_socket_mss(socket_id sid) { return get_sn_mssr(static_cast(sid)); } +void set_socket_dest_mac(socket_id sid, const std::array& mac) { + set_sn_dhar(static_cast(sid), const_cast(mac.data())); +} + void set_socket_dest_ip(socket_id sid, const ip_address& addr) { uint8_t sn = static_cast(sid); if (addr.len == 16) set_sn_dip6r(sn, const_cast(addr.ip.data())); diff --git a/firmware/w6300/w6300.h b/firmware/w6300/w6300.h index 8d19586..c70baee 100644 --- a/firmware/w6300/w6300.h +++ b/firmware/w6300/w6300.h @@ -247,6 +247,7 @@ void set_socket_tos(socket_id sn, uint8_t tos); uint8_t get_socket_tos(socket_id sn); void set_socket_mss(socket_id sn, uint16_t mss); uint16_t get_socket_mss(socket_id sn); +void set_socket_dest_mac(socket_id sn, const std::array& mac); void set_socket_dest_ip(socket_id sn, const ip_address& addr); ip_address get_socket_dest_ip(socket_id sn); void set_socket_dest_port(socket_id sn, port_num port);