Merge branch 'feature/usb_device_examples_update_v2.0.0' into 'master'

feature(usb_device): Examples update to esp_tinyusb v2.0.0

Closes IDF-13618

See merge request espressif/esp-idf!41610
This commit is contained in:
Tomas Rezucha
2025-09-15 09:28:49 +02:00
18 changed files with 169 additions and 168 deletions

View File

@@ -1,7 +1,7 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: espressif/esp_tinyusb:
version: "^1.3.0" version: "^2.0.0"
rules: rules:
- if: "idf_version >=4.4" - if: "idf_version >=4.4"
- if: "target in [esp32s2, esp32s3]" - if: "target in [esp32s2, esp32s3]"

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@@ -12,6 +12,7 @@
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_event.h" #include "esp_event.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tinyusb_default_config.h"
#include "tinyusb_net.h" #include "tinyusb_net.h"
#include "wired_iface.h" #include "wired_iface.h"
#include "dhcpserver/dhcpserver_options.h" #include "dhcpserver/dhcpserver_options.h"
@@ -44,9 +45,7 @@ void mac_spoof(mac_spoof_direction_t direction, uint8_t *buffer, uint16_t len, u
esp_err_t wired_bridge_init(wired_rx_cb_t rx_cb, wired_free_cb_t free_cb) esp_err_t wired_bridge_init(wired_rx_cb_t rx_cb, wired_free_cb_t free_cb)
{ {
const tinyusb_config_t tusb_cfg = { const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.external_phy = false,
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_net_config_t net_config = { tinyusb_net_config_t net_config = {
@@ -56,7 +55,7 @@ esp_err_t wired_bridge_init(wired_rx_cb_t rx_cb, wired_free_cb_t free_cb)
esp_read_mac(net_config.mac_addr, ESP_MAC_WIFI_STA); esp_read_mac(net_config.mac_addr, ESP_MAC_WIFI_STA);
esp_err_t ret = tinyusb_net_init(TINYUSB_USBDEV_0, &net_config); esp_err_t ret = tinyusb_net_init(&net_config);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "USB net init but not connect wifi"); ESP_LOGE(TAG, "USB net init but not connect wifi");
return ret; return ret;
@@ -113,9 +112,7 @@ static esp_err_t netif_recv_callback(void *buffer, uint16_t len, void *ctx)
*/ */
esp_err_t wired_netif_init(void) esp_err_t wired_netif_init(void)
{ {
const tinyusb_config_t tusb_cfg = { const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.external_phy = false,
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
const tinyusb_net_config_t net_config = { const tinyusb_net_config_t net_config = {
@@ -125,7 +122,7 @@ esp_err_t wired_netif_init(void)
.on_recv_callback = netif_recv_callback, .on_recv_callback = netif_recv_callback,
}; };
esp_err_t ret = tinyusb_net_init(TINYUSB_USBDEV_0, &net_config); esp_err_t ret = tinyusb_net_init(&net_config);
if (ret != ESP_OK) { if (ret != ESP_OK) {
ESP_LOGE(TAG, "Cannot initialize USB Net device"); ESP_LOGE(TAG, "Cannot initialize USB Net device");
return ret; return ret;

View File

@@ -1,11 +1,4 @@
# ESP32S3 has USB-OTG, let's prefer virtual Ethernet (USB-NCM device) # ESP32S3 has USB-OTG, let's prefer virtual Ethernet (USB-NCM device)
CONFIG_EXAMPLE_WIRED_INTERFACE_IS_USB=y CONFIG_EXAMPLE_WIRED_INTERFACE_IS_USB=y
CONFIG_EXAMPLE_WIRED_INTERFACE_IS_ETHERNET=n CONFIG_EXAMPLE_WIRED_INTERFACE_IS_ETHERNET=n
# TinyUSB needs to be initialized and run from one core
# that's why we pin the task to CPU0 and init tusb in the task
# on dual core devices (ESP32S3)
CONFIG_TINYUSB_TASK_AFFINITY_CPU0=y
CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK=y
CONFIG_TINYUSB_NET_MODE_NCM=y CONFIG_TINYUSB_NET_MODE_NCM=y

View File

@@ -1,3 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: "^1.2" espressif/esp_tinyusb:
version: "^2.0.0"

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@@ -10,8 +10,9 @@
#include "esp_partition.h" #include "esp_partition.h"
#include "esp_check.h" #include "esp_check.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tusb_msc_storage.h" #include "tinyusb_default_config.h"
#include "tusb_cdc_acm.h" #include "tinyusb_msc.h"
#include "tinyusb_cdc_acm.h"
#define BASE_PATH "/usb" // base path to mount the partition #define BASE_PATH "/usb" // base path to mount the partition
@@ -144,42 +145,36 @@ void app_main(void)
ESP_LOGI(TAG, "Initializing storage..."); ESP_LOGI(TAG, "Initializing storage...");
static wl_handle_t wl_handle = WL_INVALID_HANDLE; static wl_handle_t wl_handle = WL_INVALID_HANDLE;
ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle)); ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle));
const tinyusb_msc_spiflash_config_t config_spi = { tinyusb_msc_storage_handle_t storage_hdl = NULL;
.wl_handle = wl_handle
const tinyusb_msc_storage_config_t storage_cfg = {
.mount_point = TINYUSB_MSC_STORAGE_MOUNT_APP, // Initial mount point to APP
.medium.wl_handle = wl_handle,
.fat_fs = {
.base_path = BASE_PATH, // User specific base path
},
}; };
ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi));
ESP_ERROR_CHECK(tinyusb_msc_storage_mount(BASE_PATH)); ESP_ERROR_CHECK(tinyusb_msc_new_storage_spiflash(&storage_cfg, &storage_hdl));
file_operations(); file_operations();
ESP_LOGI(TAG, "USB Composite initialization"); ESP_LOGI(TAG, "USB Composite initialization");
const tinyusb_config_t tusb_cfg = { const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.device_descriptor = NULL,
.string_descriptor = NULL,
.string_descriptor_count = 0,
.external_phy = false,
#if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = NULL,
.hs_configuration_descriptor = NULL,
.qualifier_descriptor = NULL,
#else
.configuration_descriptor = NULL,
#endif // TUD_OPT_HIGH_SPEED
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_config_cdcacm_t acm_cfg = { tinyusb_config_cdcacm_t acm_cfg = {
.usb_dev = TINYUSB_USBDEV_0,
.cdc_port = TINYUSB_CDC_ACM_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 = &tinyusb_cdc_rx_callback, // the first way to register a callback
.callback_rx_wanted_char = NULL, .callback_rx_wanted_char = NULL,
.callback_line_state_changed = NULL, .callback_line_state_changed = NULL,
.callback_line_coding_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 */ /* the second way to register a callback */
ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback(
TINYUSB_CDC_ACM_0, TINYUSB_CDC_ACM_0,

View File

@@ -1,3 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: "^1" espressif/esp_tinyusb:
version: "^2.0.0"

View File

@@ -1,13 +1,12 @@
/* /*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
// DESCRIPTION: // DESCRIPTION:
// This example contains minimal code to make ESP32-S2 based device // This example contains minimal code to make a USB device, recognizable by USB-host as
// recognizable by USB-host devices as a USB Serial Device printing output from // a USB Serial Device printing output from the application.
// the application.
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -16,8 +15,9 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tusb_cdc_acm.h" #include "tinyusb_default_config.h"
#include "tusb_console.h" #include "tinyusb_cdc_acm.h"
#include "tinyusb_console.h"
#include "sdkconfig.h" #include "sdkconfig.h"
static const char *TAG = "example"; static const char *TAG = "example";
@@ -27,23 +27,11 @@ void app_main(void)
/* Setting TinyUSB up */ /* Setting TinyUSB up */
ESP_LOGI(TAG, "USB initialization"); ESP_LOGI(TAG, "USB initialization");
const tinyusb_config_t tusb_cfg = { const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.device_descriptor = NULL,
.string_descriptor = NULL,
.external_phy = false, // In the most cases you need to use a `false` value
#if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = NULL,
.hs_configuration_descriptor = NULL,
.qualifier_descriptor = NULL,
#else
.configuration_descriptor = NULL,
#endif // TUD_OPT_HIGH_SPEED
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_config_cdcacm_t acm_cfg = { 0 }; // the configuration uses default values 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"); ESP_LOGI(TAG, "USB initialization DONE");
while (1) { while (1) {
@@ -54,13 +42,13 @@ void app_main(void)
fprintf(stderr, "example: print -> stderr\n"); fprintf(stderr, "example: print -> stderr\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); 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"); ESP_LOGI(TAG, "log -> USB");
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
fprintf(stdout, "example: print -> stdout\n"); fprintf(stdout, "example: print -> stdout\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); vTaskDelay(1000 / portTICK_PERIOD_MS);
fprintf(stderr, "example: print -> stderr\n"); fprintf(stderr, "example: print -> stderr\n");
vTaskDelay(1000 / portTICK_PERIOD_MS); 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
} }
} }

View File

@@ -1,3 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: "^1.1" espressif/esp_tinyusb:
version: "^2.0.0"

View File

@@ -9,6 +9,7 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tinyusb_default_config.h"
#include "class/hid/hid_device.h" #include "class/hid/hid_device.h"
#include "driver/gpio.h" #include "driver/gpio.h"
@@ -164,19 +165,15 @@ void app_main(void)
ESP_ERROR_CHECK(gpio_config(&boot_button_config)); ESP_ERROR_CHECK(gpio_config(&boot_button_config));
ESP_LOGI(TAG, "USB initialization"); ESP_LOGI(TAG, "USB initialization");
const tinyusb_config_t tusb_cfg = { tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.device_descriptor = NULL,
.string_descriptor = hid_string_descriptor, tusb_cfg.descriptor.device = NULL;
.string_descriptor_count = sizeof(hid_string_descriptor) / sizeof(hid_string_descriptor[0]), tusb_cfg.descriptor.full_speed_config = hid_configuration_descriptor;
.external_phy = false, tusb_cfg.descriptor.string = hid_string_descriptor;
tusb_cfg.descriptor.string_count = sizeof(hid_string_descriptor) / sizeof(hid_string_descriptor[0]);
#if (TUD_OPT_HIGH_SPEED) #if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = hid_configuration_descriptor, // HID configuration descriptor for full-speed and high-speed are the same tusb_cfg.descriptor.high_speed_config = hid_configuration_descriptor;
.hs_configuration_descriptor = hid_configuration_descriptor,
.qualifier_descriptor = NULL,
#else
.configuration_descriptor = hid_configuration_descriptor,
#endif // TUD_OPT_HIGH_SPEED #endif // TUD_OPT_HIGH_SPEED
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_LOGI(TAG, "USB initialization DONE"); ESP_LOGI(TAG, "USB initialization DONE");

View File

@@ -1,3 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: "^1.1" espressif/esp_tinyusb:
version: "^2.0.0"

View File

@@ -3,7 +3,7 @@
* *
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
* *
* SPDX-FileContributor: 2022-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileContributor: 2022-2025 Espressif Systems (Shanghai) CO LTD
*/ */
#include <stdlib.h> #include <stdlib.h>
@@ -11,6 +11,7 @@
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tinyusb_default_config.h"
#include "esp_timer.h" #include "esp_timer.h"
static const char *TAG = "example"; static const char *TAG = "example";
@@ -149,19 +150,16 @@ void app_main(void)
{ {
ESP_LOGI(TAG, "USB initialization"); ESP_LOGI(TAG, "USB initialization");
tinyusb_config_t const tusb_cfg = { tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.device_descriptor = NULL, // If device_descriptor is NULL, tinyusb_driver_install() will use Kconfig
.string_descriptor = s_str_desc, tusb_cfg.descriptor.string = s_str_desc;
.string_descriptor_count = sizeof(s_str_desc) / sizeof(s_str_desc[0]), tusb_cfg.descriptor.string_count = sizeof(s_str_desc) / sizeof(s_str_desc[0]);
.external_phy = false, tusb_cfg.descriptor.full_speed_config = s_midi_cfg_desc;
#if (TUD_OPT_HIGH_SPEED) #if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = s_midi_cfg_desc, // HID configuration descriptor for full-speed and high-speed are the same tusb_cfg.descriptor.high_speed_config = s_midi_hs_cfg_desc;
.hs_configuration_descriptor = s_midi_hs_cfg_desc, tusb_cfg.descriptor.qualifier = NULL;
.qualifier_descriptor = NULL,
#else
.configuration_descriptor = s_midi_cfg_desc,
#endif // TUD_OPT_HIGH_SPEED #endif // TUD_OPT_HIGH_SPEED
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_LOGI(TAG, "USB initialization DONE"); ESP_LOGI(TAG, "USB initialization DONE");

View File

@@ -1,3 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: "^1.4.2" espressif/esp_tinyusb:
version: "^2.0.0"

View File

@@ -20,7 +20,8 @@
#include "esp_partition.h" #include "esp_partition.h"
#include "driver/gpio.h" #include "driver/gpio.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tusb_msc_storage.h" #include "tinyusb_default_config.h"
#include "tinyusb_msc.h"
#ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SDMMC #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SDMMC
#include "sdmmc_cmd.h" #include "sdmmc_cmd.h"
#include "diskio_impl.h" #include "diskio_impl.h"
@@ -44,6 +45,10 @@
static const char *TAG = "example_main"; static const char *TAG = "example_main";
static esp_console_repl_t *repl = NULL; static esp_console_repl_t *repl = NULL;
/* Storage global variables */
tinyusb_msc_storage_handle_t storage_hdl = NULL;
tinyusb_msc_mount_point_t mp;
static SemaphoreHandle_t _wait_console_smp = NULL; static SemaphoreHandle_t _wait_console_smp = NULL;
/* TinyUSB descriptors /* TinyUSB descriptors
@@ -168,11 +173,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) static void _mount(void)
{ {
ESP_LOGI(TAG, "Mount storage..."); 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 // List all the files in this directory
ESP_LOGI(TAG, "\nls command output:"); ESP_LOGI(TAG, "\nls command output:");
@@ -180,15 +185,15 @@ static void _mount(void)
DIR *dh = opendir(BASE_PATH); DIR *dh = opendir(BASE_PATH);
if (!dh) { if (!dh) {
if (errno == ENOENT) { 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); ESP_LOGE(TAG, "Directory doesn't exist %s", BASE_PATH);
} else { } 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); ESP_LOGE(TAG, "Unable to read directory %s", BASE_PATH);
} }
return; 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) { while ((d = readdir(dh)) != NULL) {
printf("%s\n", d->d_name); printf("%s\n", d->d_name);
} }
@@ -198,20 +203,22 @@ static void _mount(void)
// unmount storage // unmount storage
static int console_unmount(int argc, char **argv) static int console_unmount(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));
ESP_LOGE(TAG, "storage is already exposed"); if (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) {
ESP_LOGE(TAG, "Storage is already exposed");
return -1; return -1;
} }
ESP_LOGI(TAG, "Unmount storage..."); 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; return 0;
} }
// read BASE_PATH/README.MD and print its contents // read BASE_PATH/README.MD and print its contents
static int console_read(int argc, char **argv) static int console_read(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));
ESP_LOGE(TAG, "storage exposed over USB. Application can't read from storage."); if (mp == TINYUSB_MSC_STORAGE_MOUNT_USB) {
ESP_LOGE(TAG, "Storage exposed over USB. Application can't read from storage.");
return -1; return -1;
} }
ESP_LOGD(TAG, "read from storage:"); ESP_LOGD(TAG, "read from storage:");
@@ -232,7 +239,8 @@ static int console_read(int argc, char **argv)
// create file BASE_PATH/README.MD if it does not exist // create file BASE_PATH/README.MD if it does not exist
static int console_write(int argc, char **argv) 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."); ESP_LOGE(TAG, "storage exposed over USB. Application can't write to storage.");
return -1; return -1;
} }
@@ -253,29 +261,43 @@ static int console_write(int argc, char **argv)
// Show storage size and sector size // Show storage size and sector size
static int console_size(int argc, char **argv) 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"); ESP_LOGE(TAG, "storage exposed over USB. Application can't access storage");
return -1; return -1;
} }
uint32_t sec_count = tinyusb_msc_storage_get_sector_count();
uint32_t sec_size = tinyusb_msc_storage_get_sector_size(); uint32_t sec_count;
printf("Storage Capacity %lluMB\n", ((uint64_t) sec_count) * sec_size / (1024 * 1024)); 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; return 0;
} }
// Show storage status // Show storage status
static int console_status(int argc, char **argv) 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; return 0;
} }
// Exit from application // Exit from application
static int console_exit(int argc, char **argv) static int console_exit(int argc, char **argv)
{ {
tinyusb_msc_unregister_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED); ESP_ERROR_CHECK(tinyusb_msc_delete_storage(storage_hdl));
tinyusb_msc_storage_deinit(); ESP_ERROR_CHECK(tinyusb_driver_uninstall());
tinyusb_driver_uninstall();
xSemaphoreGive(_wait_console_smp); xSemaphoreGive(_wait_console_smp);
@@ -284,10 +306,29 @@ static int console_exit(int argc, char **argv)
return 0; return 0;
} }
// callback that is delivered when storage is mounted/unmounted by application. /**
static void storage_mount_changed_cb(tinyusb_msc_event_t *event) * @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)
{ {
ESP_LOGI(TAG, "Storage mounted to application: %s", event->mount_changed_data.is_mounted ? "Yes" : "No"); 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 #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH
@@ -414,48 +455,50 @@ void app_main(void)
return; return;
} }
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 #ifdef CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH
static wl_handle_t wl_handle = WL_INVALID_HANDLE; static wl_handle_t wl_handle = WL_INVALID_HANDLE;
ESP_ERROR_CHECK(storage_init_spiflash(&wl_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 = { ESP_ERROR_CHECK(tinyusb_msc_new_storage_spiflash(&storage_cfg, &storage_hdl));
.wl_handle = wl_handle,
.callback_mount_changed = storage_mount_changed_cb, /* First way to register the callback. This is while initializing the storage. */
.mount_config.max_files = 5,
};
ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi));
ESP_ERROR_CHECK(tinyusb_msc_register_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED, storage_mount_changed_cb)); /* Other way to register the callback i.e. registering using separate API. If the callback had been already registered, it will be overwritten. */
#else // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH #else // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH
static sdmmc_card_t *card = NULL; static sdmmc_card_t *card = NULL;
ESP_ERROR_CHECK(storage_init_sdmmc(&card)); ESP_ERROR_CHECK(storage_init_sdmmc(&card));
// Set the storage medium to the SD/MMC card
const tinyusb_msc_sdmmc_config_t config_sdmmc = { storage_cfg.medium.card = card;
.card = card, ESP_ERROR_CHECK(tinyusb_msc_new_storage_sdmmc(&storage_cfg, &storage_hdl));
.callback_mount_changed = storage_mount_changed_cb, /* First way to register the callback. This is while initializing the storage. */
.mount_config.max_files = 5,
};
ESP_ERROR_CHECK(tinyusb_msc_storage_init_sdmmc(&config_sdmmc));
ESP_ERROR_CHECK(tinyusb_msc_register_callback(TINYUSB_MSC_EVENT_MOUNT_CHANGED, storage_mount_changed_cb)); /* Other way to register the callback i.e. registering using separate API. If the callback had been already registered, it will be overwritten. */
#endif // CONFIG_EXAMPLE_STORAGE_MEDIA_SPIFLASH #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(); _mount();
ESP_LOGI(TAG, "USB MSC initialization"); ESP_LOGI(TAG, "USB MSC initialization");
const tinyusb_config_t tusb_cfg = {
.device_descriptor = &descriptor_config, tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.string_descriptor = string_desc_arr,
.string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]), tusb_cfg.descriptor.device = &descriptor_config;
.external_phy = false, tusb_cfg.descriptor.full_speed_config = msc_fs_configuration_desc;
tusb_cfg.descriptor.string = string_desc_arr;
tusb_cfg.descriptor.string_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]);
#if (TUD_OPT_HIGH_SPEED) #if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = msc_fs_configuration_desc, tusb_cfg.descriptor.high_speed_config = msc_hs_configuration_desc;
.hs_configuration_descriptor = msc_hs_configuration_desc, tusb_cfg.descriptor.qualifier = &device_qualifier;
.qualifier_descriptor = &device_qualifier,
#else
.configuration_descriptor = msc_fs_configuration_desc,
#endif // TUD_OPT_HIGH_SPEED #endif // TUD_OPT_HIGH_SPEED
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
ESP_LOGI(TAG, "USB MSC initialization DONE"); ESP_LOGI(TAG, "USB MSC initialization DONE");
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT(); esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();

View File

@@ -1,4 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: espressif/esp_tinyusb:
version: "^1.3.0" version: "^2.0.0"

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@@ -22,6 +22,7 @@
#include "esp_private/wifi.h" #include "esp_private/wifi.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tinyusb_default_config.h"
#include "tinyusb_net.h" #include "tinyusb_net.h"
static const char *TAG = "USB_NCM"; static const char *TAG = "USB_NCM";
@@ -100,9 +101,7 @@ void app_main(void)
ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "USB NCM device initialization"); ESP_LOGI(TAG, "USB NCM device initialization");
const tinyusb_config_t tusb_cfg = { const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.external_phy = false,
};
ESP_GOTO_ON_ERROR(tinyusb_driver_install(&tusb_cfg), err, TAG, "Failed to install TinyUSB driver"); ESP_GOTO_ON_ERROR(tinyusb_driver_install(&tusb_cfg), err, TAG, "Failed to install TinyUSB driver");
tinyusb_net_config_t net_config = { tinyusb_net_config_t net_config = {
@@ -113,7 +112,7 @@ void app_main(void)
esp_read_mac(net_config.mac_addr, ESP_MAC_WIFI_STA); esp_read_mac(net_config.mac_addr, ESP_MAC_WIFI_STA);
uint8_t *mac = net_config.mac_addr; uint8_t *mac = net_config.mac_addr;
ESP_LOGI(TAG, "Network interface HW address: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); ESP_LOGI(TAG, "Network interface HW address: %02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
ESP_GOTO_ON_ERROR(tinyusb_net_init(TINYUSB_USBDEV_0, &net_config), err, TAG, "Failed to initialize TinyUSB NCM device class"); ESP_GOTO_ON_ERROR(tinyusb_net_init(&net_config), err, TAG, "Failed to initialize TinyUSB NCM device class");
ESP_LOGI(TAG, "WiFi initialization"); ESP_LOGI(TAG, "WiFi initialization");
ESP_GOTO_ON_ERROR(start_wifi(&s_is_wifi_connected), err, TAG, "Failed to init and start WiFi"); ESP_GOTO_ON_ERROR(start_wifi(&s_is_wifi_connected), err, TAG, "Failed to init and start WiFi");

View File

@@ -1,3 +1 @@
CONFIG_TINYUSB_TASK_AFFINITY_CPU0=y
CONFIG_TINYUSB_INIT_IN_DEFAULT_TASK=y
CONFIG_TINYUSB_NET_MODE_NCM=y CONFIG_TINYUSB_NET_MODE_NCM=y

View File

@@ -1,3 +1,4 @@
## IDF Component Manager Manifest File ## IDF Component Manager Manifest File
dependencies: dependencies:
espressif/esp_tinyusb: "^1" espressif/esp_tinyusb:
version: "^2.0.0"

View File

@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Unlicense OR CC0-1.0 * SPDX-License-Identifier: Unlicense OR CC0-1.0
*/ */
@@ -10,7 +10,8 @@
#include "freertos/task.h" #include "freertos/task.h"
#include "freertos/queue.h" #include "freertos/queue.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tusb_cdc_acm.h" #include "tinyusb_default_config.h"
#include "tinyusb_cdc_acm.h"
#include "sdkconfig.h" #include "sdkconfig.h"
static const char *TAG = "example"; static const char *TAG = "example";
@@ -78,32 +79,18 @@ void app_main(void)
app_message_t msg; app_message_t msg;
ESP_LOGI(TAG, "USB initialization"); ESP_LOGI(TAG, "USB initialization");
const tinyusb_config_t tusb_cfg = { const tinyusb_config_t tusb_cfg = TINYUSB_DEFAULT_CONFIG();
.device_descriptor = NULL,
.string_descriptor = NULL,
.external_phy = false,
#if (TUD_OPT_HIGH_SPEED)
.fs_configuration_descriptor = NULL,
.hs_configuration_descriptor = NULL,
.qualifier_descriptor = NULL,
#else
.configuration_descriptor = NULL,
#endif // TUD_OPT_HIGH_SPEED
};
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_config_cdcacm_t acm_cfg = { tinyusb_config_cdcacm_t acm_cfg = {
.usb_dev = TINYUSB_USBDEV_0,
.cdc_port = TINYUSB_CDC_ACM_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 = &tinyusb_cdc_rx_callback, // the first way to register a callback
.callback_rx_wanted_char = NULL, .callback_rx_wanted_char = NULL,
.callback_line_state_changed = NULL, .callback_line_state_changed = NULL,
.callback_line_coding_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 */ /* the second way to register a callback */
ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback(
TINYUSB_CDC_ACM_0, TINYUSB_CDC_ACM_0,
@@ -112,7 +99,7 @@ void app_main(void)
#if (CONFIG_TINYUSB_CDC_COUNT > 1) #if (CONFIG_TINYUSB_CDC_COUNT > 1)
acm_cfg.cdc_port = TINYUSB_CDC_ACM_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( ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback(
TINYUSB_CDC_ACM_1, TINYUSB_CDC_ACM_1,
CDC_EVENT_LINE_STATE_CHANGED, CDC_EVENT_LINE_STATE_CHANGED,