usb: add hiddev and write to flash

This commit is contained in:
Dien-Nhung Nguyen-Phu
2024-07-11 21:23:07 +07:00
parent 4f3b6f4283
commit 49fc27a23e
5 changed files with 274 additions and 0 deletions

View File

@@ -94,6 +94,40 @@ void ble_start()
legacy_registerService();
}
static void usb_receive(uint8_t *buf, uint16_t len)
{
static uint16_t rx_len, data_len;
static uint8_t *data;
PRINT("dump first 8 bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n",
buf[0], buf[1], buf[2], buf[3],
buf[4], buf[5], buf[6], buf[7]);
if (rx_len == 0) {
if (memcmp(buf, "wang", 5))
return;
int init_len = len > LEGACY_HEADER_SIZE ? len : sizeof(data_legacy_t);
init_len += MAX_PACKET_SIZE;
data = malloc(init_len);
}
memcpy(data + rx_len, buf, len);
rx_len += len;
if (!data_len) {
data_legacy_t *d = (data_legacy_t *)data;
uint16_t n = bigendian16_sum(d->sizes, 8);
data_len = LEGACY_HEADER_SIZE + LED_ROWS * n;
data = realloc(data, data_len);
}
if ((rx_len > LEGACY_HEADER_SIZE) && rx_len >= data_len) {
data_flatSave(data, data_len);
SYS_ResetExecute();
}
}
void handle_mode_transition()
{
static int prev_mode;
@@ -141,6 +175,7 @@ int main()
debug_init();
PRINT("\nDebug console is on UART%d\n", DEBUG);
hiddev_onWrite(usb_receive);
usb_start();
led_init();