Add dispatch loop, copy_to_ram, retry picotool load without sleep

This commit is contained in:
Ian Gulliver
2026-04-06 17:36:41 +09:00
parent b197f0bfa7
commit 00b960d81d
7 changed files with 87 additions and 116 deletions

View File

@@ -10,6 +10,7 @@ set(CMAKE_CXX_STANDARD 23)
pico_sdk_init()
set(LIB_SOURCES
lib/dispatch.cpp
lib/handlers.cpp
lib/net.cpp
lib/tusb_config.cpp
@@ -24,6 +25,7 @@ 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_set_binary_type(picomap copy_to_ram)
pico_add_extra_outputs(picomap)
target_link_libraries(picomap ${LIB_DEPS})
@@ -33,5 +35,6 @@ 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_set_binary_type(picomap_test copy_to_ram)
pico_add_extra_outputs(picomap_test)
target_link_libraries(picomap_test ${LIB_DEPS})

View File

@@ -1,51 +1,11 @@
#include "pico/stdlib.h"
#include "tusb.h"
#include "wire.h"
#include "usb_cdc.h"
#include "timer_queue.h"
#include "dispatch.h"
#include "handlers.h"
#include "net.h"
static usb_cdc usb;
static timer_queue timers;
static constexpr handler_entry handlers[] = {
{RequestPICOBOOT::ext_id, handle_picoboot},
{RequestInfo::ext_id, handle_info},
};
int main() {
tusb_init();
net_init();
static static_vector<uint8_t, 256> rx_buf;
while (true) {
tud_task();
usb.drain();
timers.run();
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:
handle_picoboot(usb, msg->message_id);
break;
case RequestInfo::ext_id:
handle_info(usb, msg->message_id);
break;
}
}
__wfi();
}
dispatch(handlers);
}

View File

@@ -0,0 +1,11 @@
#pragma once
#include <cstdint>
#include <span>
#include "usb_cdc.h"
struct handler_entry {
int8_t type_id;
void (*handle)(usb_cdc&, uint32_t);
};
[[noreturn]] void dispatch(std::span<const handler_entry> handlers);

50
firmware/lib/dispatch.cpp Normal file
View File

@@ -0,0 +1,50 @@
#include "dispatch.h"
#include <unordered_map>
#include "pico/stdlib.h"
#include "tusb.h"
#include "wire.h"
#include "timer_queue.h"
#include "net.h"
[[noreturn]] void dispatch(std::span<const handler_entry> handlers) {
std::unordered_map<int8_t, void (*)(usb_cdc&, uint32_t)> 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<uint8_t, 256> rx_buf;
while (true) {
tud_task();
usb.drain();
timers.run();
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();
auto it = handler_map.find(msg->type_id);
if (it != handler_map.end()) {
it->second(usb, msg->message_id);
}
}
__wfi();
}
}

View File

@@ -1,47 +1,11 @@
#include "pico/stdlib.h"
#include "tusb.h"
#include "wire.h"
#include "usb_cdc.h"
#include "dispatch.h"
#include "handlers.h"
#include "net.h"
static usb_cdc usb;
static constexpr handler_entry handlers[] = {
{RequestPICOBOOT::ext_id, handle_picoboot},
{RequestInfo::ext_id, handle_info},
};
int main() {
tusb_init();
net_init();
static static_vector<uint8_t, 256> 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:
handle_picoboot(usb, msg->message_id);
break;
case RequestInfo::ext_id:
handle_info(usb, msg->message_id);
break;
}
}
__wfi();
}
dispatch(handlers);
}