diff options
| author | hathach <[email protected]> | 2026-06-11 17:20:15 +0700 |
|---|---|---|
| committer | hathach <[email protected]> | 2026-06-11 17:20:15 +0700 |
| commit | 5145b67f7946763ca04954f5494bfa88eec2acad (patch) | |
| tree | 9130540204c2404d532613adab01e88f4e918daa /examples | |
| parent | f52d9c7b27dc0234fb4738762a8a6da01817e2bf (diff) | |
| parent | 0244a4f12e019406a4b73c75cd96f8efa096bbdc (diff) | |
Merge remote-tracking branch 'origin/master' into stm32c5
# Conflicts:
# README.rst
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/device/audio_4_channel_mic_freertos/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | examples/device/audio_test_freertos/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | examples/device/board_test/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | examples/device/board_test/src/main.c | 26 | ||||
| -rw-r--r-- | examples/device/cdc_msc_freertos/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | examples/device/cdc_msc_freertos/src/main.c | 2 | ||||
| -rw-r--r-- | examples/device/hid_composite_freertos/CMakeLists.txt | 5 | ||||
| -rw-r--r-- | examples/device/midi2_device/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | examples/device/midi2_device/src/main.c | 2 | ||||
| -rw-r--r-- | examples/device/msc_dual_lun/src/main.c | 2 | ||||
| -rw-r--r-- | examples/dual/dynamic_switch/src/main.c | 159 | ||||
| -rw-r--r-- | examples/dual/host_info_to_device_cdc/src/main.c | 6 | ||||
| -rw-r--r-- | examples/host/bare_api/src/main.c | 43 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid/src/cdc_app.c | 6 | ||||
| -rw-r--r-- | examples/host/cdc_msc_hid_freertos/src/cdc_app.c | 10 | ||||
| -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/msc_file_explorer_freertos/skip.txt | 1 |
18 files changed, 230 insertions, 131 deletions
diff --git a/examples/device/audio_4_channel_mic_freertos/CMakeLists.txt b/examples/device/audio_4_channel_mic_freertos/CMakeLists.txt index d43a72e58..66ef19fbc 100644 --- a/examples/device/audio_4_channel_mic_freertos/CMakeLists.txt +++ b/examples/device/audio_4_channel_mic_freertos/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) +# Need to set Espressif defaults before project() is called +if(FAMILY STREQUAL "espressif") + list(APPEND SDKCONFIG_DEFAULTS "${CMAKE_CURRENT_LIST_DIR}/sdkconfig.defaults") +endif() + project(audio_4_channel_mic_freertos C CXX ASM) # Checks this example is valid for the family and initializes the project diff --git a/examples/device/audio_test_freertos/CMakeLists.txt b/examples/device/audio_test_freertos/CMakeLists.txt index 71d65eccc..a39e56822 100644 --- a/examples/device/audio_test_freertos/CMakeLists.txt +++ b/examples/device/audio_test_freertos/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) +# Need to set Espressif defaults before project() is called +if(FAMILY STREQUAL "espressif") + list(APPEND SDKCONFIG_DEFAULTS "${CMAKE_CURRENT_LIST_DIR}/sdkconfig.defaults") +endif() + project(audio_test_freertos C CXX ASM) # Checks this example is valid for the family and initializes the project diff --git a/examples/device/board_test/CMakeLists.txt b/examples/device/board_test/CMakeLists.txt index bd7b8e0ca..f14d72c08 100644 --- a/examples/device/board_test/CMakeLists.txt +++ b/examples/device/board_test/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) +# Need to set Espressif defaults before project() is called +if(FAMILY STREQUAL "espressif") + list(APPEND SDKCONFIG_DEFAULTS "${CMAKE_CURRENT_LIST_DIR}/sdkconfig.defaults") +endif() + project(board_test C CXX ASM) # Checks this example is valid for the family and initializes the project diff --git a/examples/device/board_test/src/main.c b/examples/device/board_test/src/main.c index 71e7e1da7..96dc1bd30 100644 --- a/examples/device/board_test/src/main.c +++ b/examples/device/board_test/src/main.c @@ -54,6 +54,19 @@ void tusb_time_delay_ms_api(uint32_t ms) { // //--------------------------------------------------------------------+ +// CI_BUILD (defined for all CI builds, see hw/bsp/family_support.cmake) skips the +// blink/echo loop below: after HIL tests, this firmware is flashed to park the +// board in a quiet, low-power idle state (no USB, LED, or UART activity). +#ifdef CI_BUILD +int main(void) { + while (1) { + #if defined(ESP_PLATFORM) + vTaskDelay(portMAX_DELAY); + #endif + } +} + +#else // Task parameter type: ULONG for ThreadX, void* for FreeRTOS and noos #if CFG_TUSB_OS == OPT_OS_THREADX #define RTOS_PARAM ULONG @@ -122,12 +135,6 @@ int main(void) { return 0; } -#ifdef ESP_PLATFORM -void app_main(void) { - main(); -} -#endif - //--------------------------------------------------------------------+ // FreeRTOS //--------------------------------------------------------------------+ @@ -173,3 +180,10 @@ void tx_application_define(void *first_unused_memory) { 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START); } #endif +#endif // CI_BUILD + +#ifdef ESP_PLATFORM +void app_main(void) { + main(); +} +#endif diff --git a/examples/device/cdc_msc_freertos/CMakeLists.txt b/examples/device/cdc_msc_freertos/CMakeLists.txt index 429000427..1eafd529a 100644 --- a/examples/device/cdc_msc_freertos/CMakeLists.txt +++ b/examples/device/cdc_msc_freertos/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) +# Need to set Espressif defaults before project() is called +if(FAMILY STREQUAL "espressif") + list(APPEND SDKCONFIG_DEFAULTS "${CMAKE_CURRENT_LIST_DIR}/sdkconfig.defaults") +endif() + project(cdc_msc_freertos C CXX ASM) # Checks this example is valid for the family and initializes the project diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c index f2f71d089..4a920c90b 100644 --- a/examples/device/cdc_msc_freertos/src/main.c +++ b/examples/device/cdc_msc_freertos/src/main.c @@ -88,7 +88,7 @@ int main(void) { #else xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL); xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL); - xTaskCreate(cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES - 2, NULL); + xTaskCreate(cdc_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL); #endif #ifndef ESP_PLATFORM diff --git a/examples/device/hid_composite_freertos/CMakeLists.txt b/examples/device/hid_composite_freertos/CMakeLists.txt index b52373011..2081a7782 100644 --- a/examples/device/hid_composite_freertos/CMakeLists.txt +++ b/examples/device/hid_composite_freertos/CMakeLists.txt @@ -2,6 +2,11 @@ cmake_minimum_required(VERSION 3.20) include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake) +# Need to set Espressif defaults before project() is called +if(FAMILY STREQUAL "espressif") + list(APPEND SDKCONFIG_DEFAULTS "${CMAKE_CURRENT_LIST_DIR}/sdkconfig.defaults") +endif() + project(hid_composite_freertos C CXX ASM) # Checks this example is valid for the family and initializes the project diff --git a/examples/device/midi2_device/CMakeLists.txt b/examples/device/midi2_device/CMakeLists.txt index 295af6550..f1fe09db2 100644 --- a/examples/device/midi2_device/CMakeLists.txt +++ b/examples/device/midi2_device/CMakeLists.txt @@ -30,4 +30,6 @@ target_include_directories(${PROJECT_NAME} PUBLIC family_configure_device_example(${PROJECT_NAME} noos) # Suppress pre-existing warning in usbd.c (uint8_t comparison always true/false) -target_compile_options(${PROJECT_NAME} PRIVATE -Wno-type-limits) +if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") + target_compile_options(${PROJECT_NAME} PRIVATE -Wno-type-limits) +endif() diff --git a/examples/device/midi2_device/src/main.c b/examples/device/midi2_device/src/main.c index 62741ac41..ce052a20b 100644 --- a/examples/device/midi2_device/src/main.c +++ b/examples/device/midi2_device/src/main.c @@ -715,6 +715,4 @@ int main(void) { } } } - - return 0; } diff --git a/examples/device/msc_dual_lun/src/main.c b/examples/device/msc_dual_lun/src/main.c index a4ade6f9b..1d764f12c 100644 --- a/examples/device/msc_dual_lun/src/main.c +++ b/examples/device/msc_dual_lun/src/main.c @@ -71,7 +71,7 @@ static void usb_device_init(void) { board_init_after_tusb(); } -#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO +#if CFG_TUSB_OS_HAS_SCHEDULER static void usb_device_task(RTOS_PARAM param) { (void) param; usb_device_init(); diff --git a/examples/dual/dynamic_switch/src/main.c b/examples/dual/dynamic_switch/src/main.c index c9ad2a835..f67cd885c 100644 --- a/examples/dual/dynamic_switch/src/main.c +++ b/examples/dual/dynamic_switch/src/main.c @@ -79,6 +79,9 @@ StaticTask_t usb_taskdef; StackType_t cdc_stack[CDC_STACK_SIZE]; StaticTask_t cdc_taskdef; + +StackType_t devinfo_stack[USBH_STACK_SIZE]; +StaticTask_t devinfo_taskdef; #endif #endif @@ -87,14 +90,13 @@ static tusb_role_t current_role = TUSB_ROLE_DEVICE; #if CFG_TUSB_OS == OPT_OS_FREERTOS static void usb_task(void *param); -void led_blinking_task(void *param); -void cdc_task(void *params); -#else -void led_blinking_task(void); -void cdc_task(void); #endif +void led_blinking_task(void *param); +void cdc_task(void *param); +void print_devinfo_task(void *param); + void usb_mode_switch(void); -static void print_device_info(uint8_t daddr); +static void print_one_device(uint8_t daddr); static void print_utf16(uint16_t* temp_buf, size_t buf_len); // Declare buffer for USB transfer @@ -124,11 +126,13 @@ int main(void) { xTaskCreateStatic(usb_task, "usb", USBD_STACK_SIZE > USBH_STACK_SIZE ? USBD_STACK_SIZE : USBH_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_stack, &usb_taskdef); xTaskCreateStatic(cdc_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, cdc_stack, &cdc_taskdef); + xTaskCreateStatic(print_devinfo_task, "devinfo", USBH_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, devinfo_stack, &devinfo_taskdef); #else xTaskCreate(led_blinking_task, "blinky", BLINKY_STACK_SIZE, NULL, 1, NULL); xTaskCreate(usb_task, "usb", USBD_STACK_SIZE > USBH_STACK_SIZE ? USBD_STACK_SIZE : USBH_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, NULL); xTaskCreate(cdc_task, "cdc", CDC_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL); + xTaskCreate(print_devinfo_task, "devinfo", USBH_STACK_SIZE, NULL, configMAX_PRIORITIES - 2, NULL); #endif #ifndef ESP_PLATFORM @@ -162,12 +166,13 @@ int main(void) { // Process USB tasks based on current mode if (current_role == TUSB_ROLE_DEVICE) { tud_task(); - cdc_task(); + cdc_task(NULL); } else { tuh_task(); + print_devinfo_task(NULL); } - led_blinking_task(); + led_blinking_task(NULL); } #endif } @@ -227,21 +232,28 @@ static void usb_task(void *param) { void usb_mode_switch(void) { printf("\r\n--- Switching USB mode ---\r\n"); - // Deinitialize current mode - if (current_role == TUSB_ROLE_DEVICE) { + // Snapshot then clear current_role BEFORE tusb_deinit() so concurrent + // tasks (cdc_task / print_devinfo_task on RTOS) see the role-change + // boundary and exit cleanly instead of calling host/device APIs against + // a deinitialised stack. + const tusb_role_t prev_role = current_role; + current_role = TUSB_ROLE_INVALID; + + if (prev_role == TUSB_ROLE_DEVICE) { printf("Stopping DEVICE mode...\r\n"); - tusb_deinit(BOARD_RHPORT); } else { printf("Stopping HOST mode...\r\n"); - tusb_deinit(BOARD_RHPORT); } + tusb_deinit(BOARD_RHPORT); #if CFG_TUSB_OS == OPT_OS_FREERTOS vTaskDelay(pdMS_TO_TICKS(100)); // Small delay for clean transition #else - tusb_time_delay_ms_api(100); // Small delay for clean transition -#endif // Switch to the other mode - if (current_role == TUSB_ROLE_DEVICE) { + tusb_time_delay_ms_api(100); +#endif + + // Switch to the other mode + if (prev_role == TUSB_ROLE_DEVICE) { printf("Starting HOST mode...\r\n"); tusb_rhport_init_t host_init = { .role = TUSB_ROLE_HOST, @@ -267,61 +279,29 @@ void usb_mode_switch(void) { // Device Mode: CDC Task //--------------------------------------------------------------------+ +void cdc_task(void *param) { + (void) param; #if CFG_TUSB_OS == OPT_OS_FREERTOS -void cdc_task(void *params) { - (void) params; - - // RTOS forever loop while (1) { - // Only process CDC when in device mode +#endif + // Only touch device-CDC APIs while we're in device mode. After + // usb_mode_switch() sets current_role to INVALID and tusb_deinit() runs, + // calling tud_cdc_write_flush() here would hit a deinit'd device stack. if (current_role == TUSB_ROLE_DEVICE) { - // Connected and there are data available - while (tud_cdc_available()) { + if (tud_cdc_available()) { uint8_t buf[64]; - - // Read data - uint32_t count = tud_cdc_read(buf, sizeof(buf)); - - // Echo back - tud_cdc_write(buf, count); - - // Add newline for carriage return - for (uint32_t i = 0; i < count; i++) { - if (buf[i] == '\r') { - tud_cdc_write_char('\n'); - break; - } + const uint32_t count = tud_cdc_read(buf, sizeof(buf)); + if (count) { + tud_cdc_write(buf, count); } } - tud_cdc_write_flush(); } - +#if CFG_TUSB_OS == OPT_OS_FREERTOS vTaskDelay(pdMS_TO_TICKS(10)); } -} -#else -void cdc_task(void) { - // Connected and there are data available - if (tud_cdc_available()) { - uint8_t buf[64]; - - // Read data - uint32_t count = tud_cdc_read(buf, sizeof(buf)); - - // Echo back - for (uint32_t i = 0; i < count; i++) { - tud_cdc_write_char(buf[i]); - - if (buf[i] == '\r') { - tud_cdc_write_char('\n'); - } - } - - tud_cdc_write_flush(); - } -} #endif +} //--------------------------------------------------------------------+ // Device Callbacks @@ -356,24 +336,56 @@ void tud_resume_cb(void) { // Host Callbacks //--------------------------------------------------------------------+ -// Invoked when device is mounted (configured) +// One flag per possible device address — set by tuh_mount_cb (host task) and +// cleared by print_devinfo_task once the device's descriptors are printed. +static volatile bool need_devinfo[CFG_TUH_DEVICE_MAX + 1]; + +// Invoked when device is mounted (configured). Runs in the host task — keep +// minimal; descriptor fetching happens in print_devinfo_task (different +// context so sync helpers are safe). void tuh_mount_cb(uint8_t daddr) { printf("[HOST] Device attached, address = %d\r\n", daddr); blink_interval_ms = BLINK_MOUNTED; - print_device_info(daddr); + if (daddr < TU_ARRAY_SIZE(need_devinfo)) { + need_devinfo[daddr] = true; + } } // Invoked when device is unmounted (unplugged) void tuh_umount_cb(uint8_t daddr) { printf("[HOST] Device removed, address = %d\r\n", daddr); blink_interval_ms = BLINK_NOT_MOUNTED; + if (daddr < TU_ARRAY_SIZE(need_devinfo)) { + need_devinfo[daddr] = false; + } } //--------------------------------------------------------------------+ -// Host Device Info +// Host Device Info — serialises descriptor fetching across all mounted +// devices using sync helpers. Safe to call from main loop (OS_NONE) or a +// dedicated task (FreeRTOS) — but NOT from a host-stack callback. //--------------------------------------------------------------------+ -static void print_device_info(uint8_t daddr) { +void print_devinfo_task(void *param) { + (void) param; +#if CFG_TUSB_OS == OPT_OS_FREERTOS + while (1) { +#endif + if (current_role == TUSB_ROLE_HOST) { + 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 +} + +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) { @@ -467,30 +479,23 @@ static void print_utf16(uint16_t* temp_buf, size_t buf_len) { // Blinking Task //--------------------------------------------------------------------+ -#if CFG_TUSB_OS == OPT_OS_FREERTOS void led_blinking_task(void *param) { (void) param; + static uint32_t start_ms = 0; static bool led_state = false; - - // RTOS forever loop +#if CFG_TUSB_OS == OPT_OS_FREERTOS while (1) { - board_led_write(led_state); - led_state = 1 - led_state; // toggle vTaskDelay(pdMS_TO_TICKS(blink_interval_ms)); + start_ms += blink_interval_ms; + board_led_write(led_state); + led_state = 1 - led_state; } -} #else -void led_blinking_task(void) { - static uint32_t start_ms = 0; - static bool led_state = false; - - // Blink every interval ms if (tusb_time_millis_api() - start_ms < blink_interval_ms) { return; // not enough time } start_ms += blink_interval_ms; - board_led_write(led_state); - led_state = 1 - led_state; // toggle -} + led_state = 1 - led_state; #endif +} diff --git a/examples/dual/host_info_to_device_cdc/src/main.c b/examples/dual/host_info_to_device_cdc/src/main.c index cf3430464..5186f91dc 100644 --- a/examples/dual/host_info_to_device_cdc/src/main.c +++ b/examples/dual/host_info_to_device_cdc/src/main.c @@ -130,7 +130,7 @@ static void main_task(void* param) { led_blinking_task(); // preempted RTOS run device/host stack in its own task -#if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO +#if CFG_TUSB_OS_HAS_SCHEDULER == 0 tud_task(); // tinyusb device task tuh_task(); // tinyusb host task #endif @@ -140,7 +140,7 @@ static void main_task(void* param) { int main(void) { board_init(); -#if CFG_TUSB_OS == OPT_OS_NONE || CFG_TUSB_OS == OPT_OS_PICO +#if CFG_TUSB_OS_HAS_SCHEDULER == 0 printf("TinyUSB Host Information -> Device CDC Example\r\n"); usb_device_init(); @@ -156,7 +156,7 @@ int main(void) { return 0; } -#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO +#if CFG_TUSB_OS_HAS_SCHEDULER // USB Device Driver task for RTOS static void usb_device_task(void *param) { (void) param; 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/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/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/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/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 |
