From f2681b64cbeb13f48ff7614cd1cff3823cc8d0a8 Mon Sep 17 00:00:00 2001 From: Roman Leonov Date: Thu, 28 Aug 2025 21:07:57 +0200 Subject: [PATCH] feat(usb_device): Update examples to esp_tinyusb v2.0.0 --- .../tusb_console/main/idf_component.yml | 3 +- .../tusb_console/main/tusb_console_main.c | 26 ++-- .../device/tusb_hid/main/idf_component.yml | 3 +- .../tusb_hid/main/tusb_hid_example_main.c | 16 +-- .../device/tusb_midi/main/idf_component.yml | 3 +- .../device/tusb_midi/main/tusb_midi_main.c | 16 +-- .../device/tusb_msc/main/idf_component.yml | 3 +- .../usb/device/tusb_msc/main/tusb_msc_main.c | 131 +++++++++++++----- .../tusb_serial_device/main/idf_component.yml | 3 +- .../main/tusb_serial_device_main.c | 19 +-- 10 files changed, 137 insertions(+), 86 deletions(-) diff --git a/examples/peripherals/usb/device/tusb_console/main/idf_component.yml b/examples/peripherals/usb/device/tusb_console/main/idf_component.yml index ff67b5da1e..f15f3aa5cb 100644 --- a/examples/peripherals/usb/device/tusb_console/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_console/main/idf_component.yml @@ -1,4 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_tinyusb: "^1" + espressif/esp_tinyusb: + version: "^2.0.0" idf: "^5.0" diff --git a/examples/peripherals/usb/device/tusb_console/main/tusb_console_main.c b/examples/peripherals/usb/device/tusb_console/main/tusb_console_main.c index efb91ba6f0..4c9c864286 100644 --- a/examples/peripherals/usb/device/tusb_console/main/tusb_console_main.c +++ b/examples/peripherals/usb/device/tusb_console/main/tusb_console_main.c @@ -1,13 +1,12 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ // DESCRIPTION: -// This example contains minimal code to make ESP32-S2 based device -// recognizable by USB-host devices as a USB Serial Device printing output from -// the application. +// This example contains minimal code to make a USB device, recognizable by USB-host as +// a USB Serial Device printing output from the application. #include #include @@ -17,8 +16,9 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "tinyusb.h" -#include "tusb_cdc_acm.h" -#include "tusb_console.h" +#include "tinyusb_default_config.h" +#include "tinyusb_cdc_acm.h" +#include "tinyusb_console.h" #include "sdkconfig.h" static const char *TAG = "example"; @@ -28,17 +28,11 @@ void app_main(void) /* Setting TinyUSB up */ ESP_LOGI(TAG, "USB initialization"); - const tinyusb_config_t tusb_cfg = { - .device_descriptor = NULL, - .string_descriptor = NULL, - .external_phy = false, // In the most cases you need to use a `false` value - .configuration_descriptor = NULL, - }; - + const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); tinyusb_config_cdcacm_t acm_cfg = { 0 }; // the configuration uses default values - ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + ESP_ERROR_CHECK(tinyusb_cdcacm_init(&acm_cfg)); ESP_LOGI(TAG, "USB initialization DONE"); while (1) { @@ -49,13 +43,13 @@ void app_main(void) fprintf(stderr, "example: print -> stderr\n"); vTaskDelay(1000 / portTICK_PERIOD_MS); - esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb + ESP_ERROR_CHECK(tinyusb_console_init(TINYUSB_CDC_ACM_0)); // log to usb ESP_LOGI(TAG, "log -> USB"); vTaskDelay(1000 / portTICK_PERIOD_MS); fprintf(stdout, "example: print -> stdout\n"); vTaskDelay(1000 / portTICK_PERIOD_MS); fprintf(stderr, "example: print -> stderr\n"); vTaskDelay(1000 / portTICK_PERIOD_MS); - esp_tusb_deinit_console(TINYUSB_CDC_ACM_0); // log to uart + ESP_ERROR_CHECK(tinyusb_console_deinit(TINYUSB_CDC_ACM_0)); // log to uart } } diff --git a/examples/peripherals/usb/device/tusb_hid/main/idf_component.yml b/examples/peripherals/usb/device/tusb_hid/main/idf_component.yml index b60893c81e..f15f3aa5cb 100644 --- a/examples/peripherals/usb/device/tusb_hid/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_hid/main/idf_component.yml @@ -1,4 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_tinyusb: "^1.1" + espressif/esp_tinyusb: + version: "^2.0.0" idf: "^5.0" diff --git a/examples/peripherals/usb/device/tusb_hid/main/tusb_hid_example_main.c b/examples/peripherals/usb/device/tusb_hid/main/tusb_hid_example_main.c index 022a46dfbe..deb8c001e3 100644 --- a/examples/peripherals/usb/device/tusb_hid/main/tusb_hid_example_main.c +++ b/examples/peripherals/usb/device/tusb_hid/main/tusb_hid_example_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -9,6 +9,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "tinyusb.h" +#include "tinyusb_default_config.h" #include "class/hid/hid_device.h" #include "driver/gpio.h" @@ -164,13 +165,12 @@ void app_main(void) ESP_ERROR_CHECK(gpio_config(&boot_button_config)); ESP_LOGI(TAG, "USB initialization"); - const tinyusb_config_t tusb_cfg = { - .device_descriptor = NULL, - .string_descriptor = hid_string_descriptor, - .string_descriptor_count = sizeof(hid_string_descriptor) / sizeof(hid_string_descriptor[0]), - .external_phy = false, - .configuration_descriptor = hid_configuration_descriptor, - }; + tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(); + + tusb_cfg.descriptor.device = NULL; + tusb_cfg.descriptor.full_speed_config = hid_configuration_descriptor; + tusb_cfg.descriptor.string = hid_string_descriptor; + tusb_cfg.descriptor.string_count = sizeof(hid_string_descriptor) / sizeof(hid_string_descriptor[0]); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_LOGI(TAG, "USB initialization DONE"); diff --git a/examples/peripherals/usb/device/tusb_midi/main/idf_component.yml b/examples/peripherals/usb/device/tusb_midi/main/idf_component.yml index b60893c81e..f15f3aa5cb 100644 --- a/examples/peripherals/usb/device/tusb_midi/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_midi/main/idf_component.yml @@ -1,4 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_tinyusb: "^1.1" + espressif/esp_tinyusb: + version: "^2.0.0" idf: "^5.0" diff --git a/examples/peripherals/usb/device/tusb_midi/main/tusb_midi_main.c b/examples/peripherals/usb/device/tusb_midi/main/tusb_midi_main.c index 85f4467096..ae6956d05a 100644 --- a/examples/peripherals/usb/device/tusb_midi/main/tusb_midi_main.c +++ b/examples/peripherals/usb/device/tusb_midi/main/tusb_midi_main.c @@ -3,7 +3,7 @@ * * SPDX-License-Identifier: MIT * - * SPDX-FileContributor: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileContributor: 2022-2025 Espressif Systems (Shanghai) CO LTD */ #include @@ -11,6 +11,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "tinyusb.h" +#include "tinyusb_default_config.h" #include "esp_timer.h" static const char *TAG = "example"; @@ -134,13 +135,12 @@ void app_main(void) { ESP_LOGI(TAG, "USB initialization"); - tinyusb_config_t const tusb_cfg = { - .device_descriptor = NULL, // If device_descriptor is NULL, tinyusb_driver_install() will use Kconfig - .string_descriptor = s_str_desc, - .string_descriptor_count = sizeof(s_str_desc) / sizeof(s_str_desc[0]), - .external_phy = false, - .configuration_descriptor = s_midi_cfg_desc, - }; + tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(); + + tusb_cfg.descriptor.string = s_str_desc; + tusb_cfg.descriptor.string_count = sizeof(s_str_desc) / sizeof(s_str_desc[0]); + tusb_cfg.descriptor.full_speed_config = s_midi_cfg_desc; + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_LOGI(TAG, "USB initialization DONE"); diff --git a/examples/peripherals/usb/device/tusb_msc/main/idf_component.yml b/examples/peripherals/usb/device/tusb_msc/main/idf_component.yml index 06b047d6a2..f15f3aa5cb 100644 --- a/examples/peripherals/usb/device/tusb_msc/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_msc/main/idf_component.yml @@ -1,4 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_tinyusb: "^1.2" + espressif/esp_tinyusb: + version: "^2.0.0" idf: "^5.0" diff --git a/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c b/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c index bd3055d7ec..dd308bd85b 100644 --- a/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c +++ b/examples/peripherals/usb/device/tusb_msc/main/tusb_msc_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -17,7 +17,8 @@ #include "esp_check.h" #include "driver/gpio.h" #include "tinyusb.h" -#include "tusb_msc_storage.h" +#include "tinyusb_default_config.h" +#include "tinyusb_msc.h" #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SDMMCCARD #include "diskio_impl.h" #include "diskio_sdmmc.h" @@ -25,6 +26,10 @@ static const char *TAG = "example_main"; +/* Storage global variables */ +tinyusb_msc_storage_handle_t storage_hdl = NULL; +tinyusb_msc_mount_point_t mp; + /* TinyUSB descriptors ********************************************************************* */ #define EPNUM_MSC 1 @@ -125,11 +130,11 @@ const esp_console_cmd_t cmds[] = { } }; -// mount the partition and show all the files in BASE_PATH +// Set mount point to the application and list files in BASE_PATH by filesystem API static void _mount(void) { ESP_LOGI(TAG, "Mount storage..."); - ESP_ERROR_CHECK(tinyusb_msc_storage_mount(BASE_PATH)); + ESP_ERROR_CHECK(tinyusb_msc_set_storage_mount_point(storage_hdl, TINYUSB_MSC_STORAGE_MOUNT_APP)); // List all the files in this directory @@ -138,15 +143,15 @@ static void _mount(void) DIR *dh = opendir(BASE_PATH); if (!dh) { if (errno == ENOENT) { - //If the directory is not found + // If the directory is not found ESP_LOGE(TAG, "Directory doesn't exist %s", BASE_PATH); } else { - //If the directory is not readable then throw error and exit + // If the directory is not readable then throw error and exit ESP_LOGE(TAG, "Unable to read directory %s", BASE_PATH); } return; } - //While the next entry is not readable we will print directory files + // While the next entry is not readable we will print directory files while ((d = readdir(dh)) != NULL) { printf("%s\n", d->d_name); } @@ -156,20 +161,22 @@ static void _mount(void) // unmount storage static int console_unmount(int argc, char **argv) { - if (tinyusb_msc_storage_in_use_by_usb_host()) { - ESP_LOGE(TAG, "storage is already exposed"); + ESP_ERROR_CHECK(tinyusb_msc_get_storage_mount_point(storage_hdl, &mp)); + if (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) { + ESP_LOGE(TAG, "Storage is already exposed"); return -1; } ESP_LOGI(TAG, "Unmount storage..."); - ESP_ERROR_CHECK(tinyusb_msc_storage_unmount()); + ESP_ERROR_CHECK(tinyusb_msc_set_storage_mount_point(storage_hdl, TINYUSB_MSC_STORAGE_MOUNT_USB)); return 0; } // read BASE_PATH/README.MD and print its contents static int console_read(int argc, char **argv) { - if (tinyusb_msc_storage_in_use_by_usb_host()) { - ESP_LOGE(TAG, "storage exposed over USB. Application can't read from storage."); + ESP_ERROR_CHECK(tinyusb_msc_get_storage_mount_point(storage_hdl, &mp)); + if (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) { + ESP_LOGE(TAG, "Storage exposed over USB. Application can't read from storage."); return -1; } ESP_LOGD(TAG, "read from storage:"); @@ -190,7 +197,8 @@ static int console_read(int argc, char **argv) // create file BASE_PATH/README.MD if it does not exist static int console_write(int argc, char **argv) { - if (tinyusb_msc_storage_in_use_by_usb_host()) { + ESP_ERROR_CHECK(tinyusb_msc_get_storage_mount_point(storage_hdl, &mp)); + if (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) { ESP_LOGE(TAG, "storage exposed over USB. Application can't write to storage."); return -1; } @@ -211,32 +219,73 @@ static int console_write(int argc, char **argv) // Show storage size and sector size static int console_size(int argc, char **argv) { - if (tinyusb_msc_storage_in_use_by_usb_host()) { + ESP_ERROR_CHECK(tinyusb_msc_get_storage_mount_point(storage_hdl, &mp)); + if (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) { ESP_LOGE(TAG, "storage exposed over USB. Application can't access storage"); return -1; } - uint32_t sec_count = tinyusb_msc_storage_get_sector_count(); - uint32_t sec_size = tinyusb_msc_storage_get_sector_size(); - printf("Storage Capacity %lluMB\n", ((uint64_t) sec_count) * sec_size / (1024 * 1024)); + + uint32_t sec_count; + uint32_t sec_size; + + ESP_ERROR_CHECK(tinyusb_msc_get_storage_sector_size(storage_hdl, &sec_size)); + ESP_ERROR_CHECK(tinyusb_msc_get_storage_capacity(storage_hdl, &sec_count)); + + // Calculate size in MB or KB + uint64_t total_bytes = (uint64_t)sec_size * sec_count; + if (total_bytes >= (1024 * 1024)) { + uint64_t total_mb = total_bytes / (1024 * 1024); + printf("Storage Capacity %lluMB\n", total_mb); + } else { + uint64_t total_kb = total_bytes / 1024; + printf("Storage Capacity %lluKB\n", total_kb); + } return 0; } // exit from application static int console_status(int argc, char **argv) { - printf("storage exposed over USB: %s\n", tinyusb_msc_storage_in_use_by_usb_host() ? "Yes" : "No"); + ESP_ERROR_CHECK(tinyusb_msc_get_storage_mount_point(storage_hdl, &mp)); + printf("storage exposed over USB: %s\n", (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) ? "Yes" : "No"); return 0; } // exit from application static int console_exit(int argc, char **argv) { - tinyusb_msc_storage_deinit(); + ESP_ERROR_CHECK(tinyusb_msc_delete_storage(storage_hdl)); + ESP_ERROR_CHECK(tinyusb_driver_uninstall()); printf("Application Exiting\n"); exit(0); return 0; } +/** + * @brief Storage mount changed callback + * + * @param handle Storage handle + * @param event Event information + * @param arg User argument, provided during callback registration + */ +static void storage_mount_changed_cb(tinyusb_msc_storage_handle_t handle, tinyusb_msc_event_t *event, void *arg) +{ + switch (event->id) { + case TINYUSB_MSC_EVENT_MOUNT_START: + // Verify that all the files are closed before unmounting + break; + case TINYUSB_MSC_EVENT_MOUNT_COMPLETE: + ESP_LOGI(TAG, "Storage mounted to application: %s", (event->mount_point == TINYUSB_MSC_STORAGE_MOUNT_APP) ? "Yes" : "No"); + break; + case TINYUSB_MSC_EVENT_MOUNT_FAILED: + case TINYUSB_MSC_EVENT_FORMAT_REQUIRED: + ESP_LOGE(TAG, "Storage mount failed or format required"); + break; + default: + break; + } +} + #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH static esp_err_t storage_init_spiflash(wl_handle_t *wl_handle) { @@ -334,36 +383,46 @@ void app_main(void) { ESP_LOGI(TAG, "Initializing storage..."); + tinyusb_msc_storage_config_t storage_cfg = { + .mount_point = TINYUSB_MSC_STORAGE_MOUNT_USB, // Initial mount point to USB + .fat_fs = { + .base_path = NULL, // Use default base path + .config.max_files = 5, // Maximum number of files that can be opened simultaneously + .format_flags = 0, // No special format flags + }, + }; + #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH static wl_handle_t wl_handle = WL_INVALID_HANDLE; ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle)); + // Set the storage medium to the wear leveling handle + storage_cfg.medium.wl_handle = wl_handle; - const tinyusb_msc_spiflash_config_t config_spi = { - .wl_handle = wl_handle - }; - ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi)); + ESP_ERROR_CHECK(tinyusb_msc_new_storage_spiflash(&storage_cfg, &storage_hdl)); #else // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH static sdmmc_card_t *card = NULL; ESP_ERROR_CHECK(storage_init_sdmmc(&card)); - - const tinyusb_msc_sdmmc_config_t config_sdmmc = { - .card = card - }; - ESP_ERROR_CHECK(tinyusb_msc_storage_init_sdmmc(&config_sdmmc)); + // Set the storage medium to the SD/MMC card + storage_cfg.medium.card = card; + ESP_ERROR_CHECK(tinyusb_msc_new_storage_sdmmc(&storage_cfg, &storage_hdl)); #endif // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH - //mounted in the app by default + // Configure the callback for mount changed events + ESP_ERROR_CHECK(tinyusb_msc_set_storage_callback(storage_mount_changed_cb, NULL)); + // Re-mount to the APP _mount(); ESP_LOGI(TAG, "USB MSC initialization"); - const tinyusb_config_t tusb_cfg = { - .device_descriptor = &descriptor_config, - .string_descriptor = string_desc_arr, - .string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]), - .external_phy = false, - .configuration_descriptor = desc_configuration, - }; + + tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(); + + tusb_cfg.descriptor.device = &descriptor_config; + tusb_cfg.descriptor.full_speed_config = desc_configuration; + tusb_cfg.descriptor.string = string_desc_arr; + tusb_cfg.descriptor.string_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]); + ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); + ESP_LOGI(TAG, "USB MSC initialization DONE"); esp_console_repl_t *repl = NULL; diff --git a/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml b/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml index ff67b5da1e..f15f3aa5cb 100644 --- a/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml +++ b/examples/peripherals/usb/device/tusb_serial_device/main/idf_component.yml @@ -1,4 +1,5 @@ ## IDF Component Manager Manifest File dependencies: - espressif/esp_tinyusb: "^1" + espressif/esp_tinyusb: + version: "^2.0.0" idf: "^5.0" diff --git a/examples/peripherals/usb/device/tusb_serial_device/main/tusb_serial_device_main.c b/examples/peripherals/usb/device/tusb_serial_device/main/tusb_serial_device_main.c index 0a2cc3a2c3..1b0c25f81e 100644 --- a/examples/peripherals/usb/device/tusb_serial_device/main/tusb_serial_device_main.c +++ b/examples/peripherals/usb/device/tusb_serial_device/main/tusb_serial_device_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -9,7 +9,8 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "tinyusb.h" -#include "tusb_cdc_acm.h" +#include "tinyusb_default_config.h" +#include "tinyusb_cdc_acm.h" #include "sdkconfig.h" static const char *TAG = "example"; @@ -44,26 +45,18 @@ void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) void app_main(void) { ESP_LOGI(TAG, "USB initialization"); - const tinyusb_config_t tusb_cfg = { - .device_descriptor = NULL, - .string_descriptor = NULL, - .external_phy = false, - .configuration_descriptor = NULL, - }; - + const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG(); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); tinyusb_config_cdcacm_t acm_cfg = { - .usb_dev = TINYUSB_USBDEV_0, .cdc_port = TINYUSB_CDC_ACM_0, - .rx_unread_buf_sz = 64, .callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback .callback_rx_wanted_char = NULL, .callback_line_state_changed = NULL, .callback_line_coding_changed = NULL }; - ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + ESP_ERROR_CHECK(tinyusb_cdcacm_init(&acm_cfg)); /* the second way to register a callback */ ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( TINYUSB_CDC_ACM_0, @@ -72,7 +65,7 @@ void app_main(void) #if (CONFIG_TINYUSB_CDC_COUNT > 1) acm_cfg.cdc_port = TINYUSB_CDC_ACM_1; - ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); + ESP_ERROR_CHECK(tinyusb_cdcacm_init(&acm_cfg)); ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( TINYUSB_CDC_ACM_1, CDC_EVENT_LINE_STATE_CHANGED,