ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
tinyusb_component.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3) || \
2 defined(USE_ESP32_VARIANT_ESP32S31) || defined(USE_ESP32_VARIANT_ESP32H4)
3#include "tinyusb_component.h"
5#include "esphome/core/log.h"
6#include "tinyusb_default_config.h"
7
8namespace esphome::tinyusb {
9
10static const char *const TAG = "tinyusb";
11
13 // Use the device's MAC address as its serial number if no serial number is defined
14 if (this->string_descriptor_[SERIAL_NUMBER] == nullptr) {
15 static char mac_addr_buf[13];
16 get_mac_address_into_buffer(mac_addr_buf);
17 this->string_descriptor_[SERIAL_NUMBER] = mac_addr_buf;
18 }
19
20 // Start from esp_tinyusb defaults to keep required task settings valid across esp_tinyusb updates.
21 this->tusb_cfg_ = TINYUSB_DEFAULT_CONFIG();
22 this->tusb_cfg_.port = TINYUSB_PORT_FULL_SPEED_0;
23 this->tusb_cfg_.phy.skip_setup = false;
24 this->tusb_cfg_.descriptor = {
25 .device = &this->usb_descriptor_,
26 .string = this->string_descriptor_,
27 .string_count = SIZE,
28 };
29
30 // Defense-in-depth: esp_tinyusb's tinyusb_descriptors_set() fails with
31 // ESP_ERR_INVALID_ARG when no configuration descriptor is provided and
32 // no class that has a built-in default (CDC/MSC/NCM) is compiled in. In
33 // that case the internal task exits without notifying us, and
34 // tinyusb_driver_install() blocks 5s on the notify-take -- long enough
35 // to trip the task watchdog. Bail early so the rest of the device can
36 // still boot.
37#if !(CFG_TUD_CDC > 0 || CFG_TUD_MSC > 0 || CFG_TUD_NCM > 0)
38 if (this->tusb_cfg_.descriptor.full_speed_config == nullptr) {
39 ESP_LOGE(TAG, "No USB class configured");
40 this->mark_failed();
41 return;
42 }
43#endif
44
45 esp_err_t result = tinyusb_driver_install(&this->tusb_cfg_);
46 if (result != ESP_OK) {
47 ESP_LOGE(TAG, "tinyusb_driver_install failed: %s", esp_err_to_name(result));
48 this->mark_failed();
49 }
50}
51
53 ESP_LOGCONFIG(TAG,
54 "TinyUSB:\n"
55 " Product ID: 0x%04X\n"
56 " Vendor ID: 0x%04X\n"
57 " Manufacturer: '%s'\n"
58 " Product: '%s'\n"
59 " Serial: '%s'\n",
60 this->usb_descriptor_.idProduct, this->usb_descriptor_.idVendor, this->string_descriptor_[MANUFACTURER],
61 this->string_descriptor_[PRODUCT], this->string_descriptor_[SERIAL_NUMBER]);
62}
63
64} // namespace esphome::tinyusb
65#endif // USE_ESP32_VARIANT_ESP32P4 || USE_ESP32_VARIANT_ESP32S2 || USE_ESP32_VARIANT_ESP32S3 ||
66 // USE_ESP32_VARIANT_ESP32S31 || USE_ESP32_VARIANT_ESP32H4
void mark_failed()
Mark this component as failed.
const char * string_descriptor_[SIZE]
tusb_desc_device_t usb_descriptor_
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Definition helpers.cpp:744