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 | |
| 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')
| -rw-r--r-- | examples/host/bare_api/only.txt | 1 | ||||
| -rw-r--r-- | examples/host/bare_api/src/main.c | 43 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid/only.txt | 1 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid/src/cdc_app.c | 6 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid_freertos/only.txt | 1 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid_freertos/src/cdc_app.c | 10 | ||||
| -rw-r--r-- | examples/host/device_info/only.txt | 1 | ||||
| -rw-r--r-- | examples/host/device_info/src/main.c | 71 | ||||
| -rw-r--r-- | examples/host/midi2_host/src/main.c | 4 | ||||
| -rw-r--r-- | examples/host/midi_rx/only.txt | 1 | ||||
| -rw-r--r-- | examples/host/msc_file_explorer/only.txt | 1 | ||||
| -rw-r--r-- | examples/host/msc_file_explorer_freertos/skip.txt | 1 |
12 files changed, 101 insertions, 40 deletions
diff --git a/examples/host/bare_api/only.txt b/examples/host/bare_api/only.txt index 1ddfc2b5c..a2ff93be5 100644 --- a/examples/host/bare_api/only.txt +++ b/examples/host/bare_api/only.txt @@ -20,6 +20,7 @@ mcu:RP2040 mcu:RW61X mcu:RX65X mcu:STM32C0 +mcu:STM32C5 mcu:STM32F4 mcu:STM32F7 mcu:STM32G0 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)); diff --git a/examples/host/cdc_msc_hid/only.txt b/examples/host/cdc_msc_hid/only.txt index 1ddfc2b5c..a2ff93be5 100644 --- a/examples/host/cdc_msc_hid/only.txt +++ b/examples/host/cdc_msc_hid/only.txt @@ -20,6 +20,7 @@ mcu:RP2040 mcu:RW61X mcu:RX65X mcu:STM32C0 +mcu:STM32C5 mcu:STM32F4 mcu:STM32F7 mcu:STM32G0 diff --git a/examples/host/cdc_msc_hid/src/cdc_app.c b/examples/host/cdc_msc_hid/src/cdc_app.c index 20033981e..e6c190715 100644 --- a/examples/host/cdc_msc_hid/src/cdc_app.c +++ b/examples/host/cdc_msc_hid/src/cdc_app.c @@ -95,7 +95,6 @@ void tuh_cdc_mount_cb(uint8_t idx) { printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.desc.bInterfaceNumber); -#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM // If CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined, line coding will be set by tinyusb stack // while eneumerating new cdc device cdc_line_coding_t line_coding = {0}; @@ -103,11 +102,6 @@ void tuh_cdc_mount_cb(uint8_t idx) { printf(" Baudrate: %" PRIu32 ", Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits); printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity, line_coding.data_bits); } -#else - // Set Line Coding upon mounted - cdc_line_coding_t new_line_coding = { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }; - tuh_cdc_set_line_coding(idx, &new_line_coding, NULL, 0); -#endif } // Invoked when a device with CDC interface is unmounted diff --git a/examples/host/cdc_msc_hid_freertos/only.txt b/examples/host/cdc_msc_hid_freertos/only.txt index 753fa7cd3..4ab8a906e 100644 --- a/examples/host/cdc_msc_hid_freertos/only.txt +++ b/examples/host/cdc_msc_hid_freertos/only.txt @@ -16,6 +16,7 @@ mcu:MSP432E4 mcu:RW61X mcu:RX65X mcu:STM32C0 +mcu:STM32C5 mcu:STM32F4 mcu:STM32F7 mcu:STM32G0 diff --git a/examples/host/cdc_msc_hid_freertos/src/cdc_app.c b/examples/host/cdc_msc_hid_freertos/src/cdc_app.c index 30baacaac..9fad775e9 100644 --- a/examples/host/cdc_msc_hid_freertos/src/cdc_app.c +++ b/examples/host/cdc_msc_hid_freertos/src/cdc_app.c @@ -29,16 +29,16 @@ #include "app.h" #ifdef ESP_PLATFORM - #define CDC_STACK_SZIE 2048 + #define CDC_STACK_SIZE 2048 #else - #define CDC_STACK_SZIE (3*configMINIMAL_STACK_SIZE/2) + #define CDC_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) #endif //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ #if configSUPPORT_STATIC_ALLOCATION -StackType_t cdc_stack[CDC_STACK_SZIE]; +StackType_t cdc_stack[CDC_STACK_SIZE]; StaticTask_t cdc_taskdef; #endif @@ -46,9 +46,9 @@ static void cdc_app_task(void* param); void cdc_app_init(void) { #if configSUPPORT_STATIC_ALLOCATION - (void) xTaskCreateStatic(cdc_app_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, cdc_stack, &cdc_taskdef); + (void) xTaskCreateStatic(cdc_app_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES-2, cdc_stack, &cdc_taskdef); #else - (void) xTaskCreate(cdc_app_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, NULL); + (void) xTaskCreate(cdc_app_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES-2, NULL); #endif } diff --git a/examples/host/device_info/only.txt b/examples/host/device_info/only.txt index 742935dcf..4c2cb0f35 100644 --- a/examples/host/device_info/only.txt +++ b/examples/host/device_info/only.txt @@ -21,6 +21,7 @@ mcu:RP2040 mcu:RW61X mcu:RX65X mcu:STM32C0 +mcu:STM32C5 mcu:STM32F4 mcu:STM32F7 mcu:STM32G0 diff --git a/examples/host/device_info/src/main.c b/examples/host/device_info/src/main.c index b0e38dd6b..f32ed1a3e 100644 --- a/examples/host/device_info/src/main.c +++ b/examples/host/device_info/src/main.c @@ -72,8 +72,14 @@ CFG_TUH_MEM_SECTION struct { } desc; void led_blinking_task(void* param); +void print_devinfo_task(void* param); static void print_utf16(uint16_t* temp_buf, size_t buf_len); +// One flag per possible device address — set by tuh_mount_cb (host task) and +// cleared by print_devinfo_task (separate task / main loop) once the device's +// descriptor info has been printed. +static volatile bool need_devinfo[CFG_TUH_DEVICE_MAX + 1]; + #if CFG_TUSB_OS == OPT_OS_FREERTOS void init_freertos_task(void); #endif @@ -103,6 +109,7 @@ int main(void) { init_tinyusb(); while (1) { tuh_task(); // tinyusb host task + print_devinfo_task(NULL); led_blinking_task(NULL); } #endif @@ -110,10 +117,34 @@ int main(void) { /*------------- TinyUSB Callbacks -------------*/ -// Invoked when device is mounted (configured) +// Invoked when device is mounted (configured). Runs in the host task — keep +// it minimal. The actual descriptor fetching/printing happens in +// print_devinfo_task() below, which runs in a different context (main loop +// on OS_NONE / dedicated task on RTOS) where the sync helpers are safe. void tuh_mount_cb(uint8_t daddr) { blink_interval_ms = BLINK_MOUNTED; + 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) { + blink_interval_ms = BLINK_NOT_MOUNTED; + if (daddr < TU_ARRAY_SIZE(need_devinfo)) { + need_devinfo[daddr] = false; + } + printf("Device removed, address = %d\r\n", daddr); +} + +//--------------------------------------------------------------------+ +// Print device info task — serialises descriptor fetching across all +// mounted devices using the sync helpers. Sync calls are safe here because +// this task runs outside the host-task callback context (main loop on +// OS_NONE / dedicated FreeRTOS task on RTOS). +//--------------------------------------------------------------------+ + +static void print_one_device(uint8_t daddr) { // Get Device Descriptor uint8_t xfer_result = tuh_descriptor_get_device_sync(daddr, &desc.device, 18); if (XFER_RESULT_SUCCESS != xfer_result) { @@ -129,9 +160,8 @@ void tuh_mount_cb(uint8_t daddr) { } if (XFER_RESULT_SUCCESS != xfer_result) { uint16_t* serial = (uint16_t*)(uintptr_t) desc.serial; - serial[0] = (uint16_t)((TUSB_DESC_STRING << 8) | (2 * 1 + 2)); - serial[1] = '0'; // simply 0 + serial[1] = '0'; serial[2] = 0; } print_utf16((uint16_t*)(uintptr_t) desc.serial, sizeof(desc.serial)/2); @@ -149,12 +179,9 @@ void tuh_mount_cb(uint8_t daddr) { 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 (desc.device.iManufacturer != 0) { - xfer_result = tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, desc.buf, sizeof(desc.buf)); - if (XFER_RESULT_SUCCESS == xfer_result) { + if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(daddr, LANGUAGE_ID, desc.buf, sizeof(desc.buf))) { print_utf16((uint16_t*)(uintptr_t) desc.buf, sizeof(desc.buf)/2); } } @@ -162,22 +189,33 @@ void tuh_mount_cb(uint8_t daddr) { printf(" iProduct %u ", desc.device.iProduct); if (desc.device.iProduct != 0) { - xfer_result = tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, desc.buf, sizeof(desc.buf)); - if (XFER_RESULT_SUCCESS == xfer_result) { + if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(daddr, LANGUAGE_ID, desc.buf, sizeof(desc.buf))) { print_utf16((uint16_t*)(uintptr_t) desc.buf, sizeof(desc.buf)/2); } } printf("\r\n"); printf(" iSerialNumber %u ", desc.device.iSerialNumber); - printf("%s\r\n", (char*)desc.serial); // serial is already to UTF-8 + printf("%s\r\n", (char*)desc.serial); // serial is already UTF-8 printf(" bNumConfigurations %u\r\n", desc.device.bNumConfigurations); } -// Invoked when device is unmounted (bus reset/unplugged) -void tuh_umount_cb(uint8_t daddr) { - blink_interval_ms = BLINK_NOT_MOUNTED; - printf("Device removed, address = %d\r\n", daddr); +void print_devinfo_task(void* param) { + (void) param; + +#if CFG_TUSB_OS == OPT_OS_FREERTOS + while (1) { +#endif + for (uint8_t daddr = 1; daddr < TU_ARRAY_SIZE(need_devinfo); daddr++) { + if (need_devinfo[daddr]) { + need_devinfo[daddr] = false; + print_one_device(daddr); + } + } +#if CFG_TUSB_OS == OPT_OS_FREERTOS + vTaskDelay(pdMS_TO_TICKS(10)); + } +#endif } //--------------------------------------------------------------------+ @@ -278,6 +316,9 @@ StaticTask_t blinky_taskdef; StackType_t usb_stack[USB_STACK_SIZE]; StaticTask_t usb_taskdef; + +StackType_t devinfo_stack[USB_STACK_SIZE]; +StaticTask_t devinfo_taskdef; #endif #ifdef ESP_PLATFORM @@ -299,9 +340,11 @@ void init_freertos_task(void) { #if configSUPPORT_STATIC_ALLOCATION xTaskCreateStatic(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, blinky_stack, &blinky_taskdef); xTaskCreateStatic(usb_host_task, "usbh", USB_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_stack, &usb_taskdef); + xTaskCreateStatic(print_devinfo_task, "devinfo", USB_STACK_SIZE, NULL, configMAX_PRIORITIES-2, devinfo_stack, &devinfo_taskdef); #else xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL); xTaskCreate(usb_host_task, "usbh", USB_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL); + xTaskCreate(print_devinfo_task, "devinfo", USB_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL); #endif // only start scheduler for non-espressif mcu diff --git a/examples/host/midi2_host/src/main.c b/examples/host/midi2_host/src/main.c index 63b08318c..d81e77a33 100644 --- a/examples/host/midi2_host/src/main.c +++ b/examples/host/midi2_host/src/main.c @@ -35,7 +35,7 @@ // State //--------------------------------------------------------------------+ -static uint8_t midi2_idx = 0xFF; +TU_ATTR_UNUSED static uint8_t midi2_idx = 0xFF; //--------------------------------------------------------------------+ // UMP printer - shows MT and word(s) in hex; decodes Channel Voice @@ -160,6 +160,4 @@ int main(void) { while (1) { tuh_task(); } - - return 0; } diff --git a/examples/host/midi_rx/only.txt b/examples/host/midi_rx/only.txt index c71aacd87..65ef8fac9 100644 --- a/examples/host/midi_rx/only.txt +++ b/examples/host/midi_rx/only.txt @@ -23,6 +23,7 @@ mcu:RP2040 mcu:RW61X mcu:RX65X mcu:STM32C0 +mcu:STM32C5 mcu:STM32F4 mcu:STM32F7 mcu:STM32G0 diff --git a/examples/host/msc_file_explorer/only.txt b/examples/host/msc_file_explorer/only.txt index 1ddfc2b5c..a2ff93be5 100644 --- a/examples/host/msc_file_explorer/only.txt +++ b/examples/host/msc_file_explorer/only.txt @@ -20,6 +20,7 @@ mcu:RP2040 mcu:RW61X mcu:RX65X mcu:STM32C0 +mcu:STM32C5 mcu:STM32F4 mcu:STM32F7 mcu:STM32G0 diff --git a/examples/host/msc_file_explorer_freertos/skip.txt b/examples/host/msc_file_explorer_freertos/skip.txt index f0be07d25..a8c9bea2a 100644 --- a/examples/host/msc_file_explorer_freertos/skip.txt +++ b/examples/host/msc_file_explorer_freertos/skip.txt @@ -1,3 +1,4 @@ mcu:CH32F20X board:lpcxpresso54114 mcu:FT90X +board:stm32h7s3nucleo |
