diff options
| author | hathach <[email protected]> | 2026-06-11 20:14:31 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2026-06-11 20:14:31 +0700 |
| commit | 4f5be18911c4d5dd4c3b097cb4ff18e2b7bf2543 (patch) | |
| tree | 7215ed281d513583e23d3056231cc689dacd8eb0 /examples/host/bare_api/src/main.c | |
| parent | 264472e380bbca90aebabc6457fc53d372fcb66e (diff) | |
| parent | 629e805e08575398070d0a9ac2087a853c93d290 (diff) | |
Merge remote-tracking branch 'origin/master' into musb_ep0_race
# Conflicts:
# test/hil/hil_test.py
Diffstat (limited to 'examples/host/bare_api/src/main.c')
| -rw-r--r-- | examples/host/bare_api/src/main.c | 43 |
1 files changed, 31 insertions, 12 deletions
diff --git a/examples/host/bare_api/src/main.c b/examples/host/bare_api/src/main.c index 544f38102..679ce6f43 100644 --- a/examples/host/bare_api/src/main.c +++ b/examples/host/bare_api/src/main.c @@ -48,14 +48,19 @@ CFG_TUH_MEM_SECTION uint16_t temp_buf[128]; // temp buffer for string descriptor // MACRO CONSTANT TYPEDEF PROTYPES //--------------------------------------------------------------------+ void led_blinking_task(void); +void print_devinfo_task(void); static void print_utf16(uint16_t *temp_buf, size_t buf_len); -void print_device_descriptor(tuh_xfer_t* xfer); +static void print_one_device(uint8_t daddr); void parse_config_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg); uint8_t* get_hid_buf(uint8_t daddr); void free_hid_buf(uint8_t daddr); +// One flag per possible device address — set in tuh_mount_cb (host task) and +// cleared by print_devinfo_task (main loop) once the descriptors are printed. +static volatile bool need_devinfo[CFG_TUH_DEVICE_MAX + 1]; + /*------------- MAIN -------------*/ int main(void) { board_init(); @@ -74,38 +79,53 @@ int main(void) { while (1) { // tinyusb host task tuh_task(); + print_devinfo_task(); led_blinking_task(); } } /*------------- TinyUSB Callbacks -------------*/ -// Invoked when device is mounted (configured) +// Invoked when device is mounted (configured). Runs in the host task — keep +// it minimal. The descriptor fetching/printing happens in print_devinfo_task() +// below where the sync helpers are safe (different context). void tuh_mount_cb(uint8_t daddr) { printf("Device attached, address = %d\r\n", daddr); - - // Get Device Descriptor - // TODO: invoking control transfer now has issue with mounting hub with multiple devices attached, fix later - tuh_descriptor_get_device(daddr, &desc_device, 18, print_device_descriptor, 0); + if (daddr < TU_ARRAY_SIZE(need_devinfo)) { + need_devinfo[daddr] = true; + } } /// Invoked when device is unmounted (bus reset/unplugged) void tuh_umount_cb(uint8_t daddr) { printf("Device removed, address = %d\r\n", daddr); + if (daddr < TU_ARRAY_SIZE(need_devinfo)) { + need_devinfo[daddr] = false; + } free_hid_buf(daddr); } //--------------------------------------------------------------------+ -// Device Descriptor +// Print device info task — serialises descriptor fetching across all +// mounted devices via sync helpers. Sync calls are safe here because this +// runs in the main loop (outside any host-stack callback context). //--------------------------------------------------------------------+ -void print_device_descriptor(tuh_xfer_t *xfer) { - if (XFER_RESULT_SUCCESS != xfer->result) { +void print_devinfo_task(void) { + for (uint8_t daddr = 1; daddr < TU_ARRAY_SIZE(need_devinfo); daddr++) { + if (need_devinfo[daddr]) { + need_devinfo[daddr] = false; + print_one_device(daddr); + } + } +} + +static void print_one_device(uint8_t daddr) { + // Get Device Descriptor + if (XFER_RESULT_SUCCESS != tuh_descriptor_get_device_sync(daddr, &desc_device, 18)) { printf("Failed to get device descriptor\r\n"); return; } - uint8_t const daddr = xfer->daddr; - printf("Device %u: ID %04x:%04x\r\n", daddr, desc_device.idVendor, desc_device.idProduct); printf("Device Descriptor:\r\n"); printf(" bLength %u\r\n" , desc_device.bLength); @@ -119,7 +139,6 @@ void print_device_descriptor(tuh_xfer_t *xfer) { printf(" idProduct 0x%04x\r\n" , desc_device.idProduct); printf(" bcdDevice %04x\r\n" , desc_device.bcdDevice); - // Get String descriptor using Sync API printf(" iManufacturer %u ", desc_device.iManufacturer); if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, temp_buf, sizeof(temp_buf))) { print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf)); |
