diff --git a/components/usb_device/README.md b/components/usb_device/README.md index 8d4af90fb2..1facc80744 100644 --- a/components/usb_device/README.md +++ b/components/usb_device/README.md @@ -61,7 +61,7 @@ for back-compatibility. - [Enabling X-Input (Xbox 360)](#enabling-x-input-xbox-360) - [Enabling mass storage (MSC)](#enabling-mass-storage-msc) - [Routing the console over CDC](#routing-the-console-over-cdc) - - [Endpoint budget (ESP32-S3 USB-OTG)](#endpoint-budget-esp32-s3-usb-otg) + - [Endpoint budget](#endpoint-budget) - [Example](#example) - [Notes](#notes) @@ -397,11 +397,29 @@ successful `initialize()`. The `ota` example uses this to carry its logs alongside the OTA vendor / WebUSB interface on one cable. -## Endpoint budget (ESP32-S3 USB-OTG) - -The ESP32-S3 / -S2 USB-OTG core is full-speed and, besides EP0, provides roughly -**5 usable data IN endpoints** and **5 usable data OUT endpoints**. Each function -consumes: +## Endpoint budget + +`initialize()` checks the endpoints against the USB-OTG core behind the port the +device will use (`Config::port`, or esp_tinyusb's default for the target), with +the limits TinyUSB's DWC2 driver enforces: + +| Core | Where | Data IN endpoints | Endpoint numbers | +|---|---|---|---| +| Full-speed | ESP32-S2, ESP32-S3, ESP32-P4 port 0 | 4 (5 IN, EP0 IN included) | 1 to 6 | +| High-speed | ESP32-P4 port 1 (its default) | 7 (8 IN, EP0 IN included) | 1 to 15 | + +TinyUSB also gives IN endpoint *n* the core's TX FIFO *n*, so a data IN endpoint +must be *numbered* within the data IN count as well (1 to 4 on the full-speed +core). Past the count, an IN endpoint fails to open when the host configures the +device, which then stays unconfigured +([hathach/tinyusb#4450](https://github.com/hathach/tinyusb/issues/4450)); past +the FIFOs, it opens but never transmits +([hathach/tinyusb#4007](https://github.com/hathach/tinyusb/issues/4007)). +`initialize()` numbers the IN endpoints contiguously from 1, and gives X-Input's +separate OUT endpoint the number after all of them, so a combination within the +count also fits the numbering. OUT endpoints share their function's number, so +only the highest number limits them. The FIFO RAM is not a limit for any +combination of these functions. Each function consumes: | Function | IN endpoints | OUT endpoints | |-------------------|---------------------------------------------|--------------------------------| @@ -411,11 +429,19 @@ consumes: | X-Input (Xbox 360)| 1 (interrupt-IN) | 1 (interrupt-OUT) | | MSC | 1 (bulk-IN) | 1 (bulk-OUT) | -This is why the device is **selectable** ("not all at once"). Combinations that -fit comfortably: CDC+Vendor (3 IN / 2 OUT, used by the example), CDC+Vendor+HID, -CDC+Vendor+MSC. Enabling CDC+Vendor+HID+MSC reaches 5 IN endpoints — at the hard -limit, not recommended. `initialize()` returns `std::errc::value_too_large` if the -IN or OUT budget is exceeded. +On the full-speed core this is why the device is **selectable** ("not all at +once"). Combinations that fit there: + +- CDC + Vendor: 3 IN / 2 OUT (used by the example) +- CDC + Vendor + HID, CDC + Vendor + MSC, CDC + Vendor + X-Input: 4 IN, the limit +- Vendor + HID + X-Input + MSC: 4 IN + +CDC + Vendor + HID + MSC needs 5 data IN endpoints: it does not fit the +full-speed core, and `initialize()` returns `std::errc::value_too_large` for it +there, logging which limit was exceeded. Every combination fits the high-speed +core (CDC + Vendor + HID + X-Input + MSC is 6 data IN). To offer files next to CDC + Vendor + HID, carry them over the +vendor interface (a file-transfer protocol on the vendor / WebUSB link) instead of +MSC. ## Example diff --git a/components/usb_device/src/usb_device.cpp b/components/usb_device/src/usb_device.cpp index 6dc52a7980..9dfd457ff4 100644 --- a/components/usb_device/src/usb_device.cpp +++ b/components/usb_device/src/usb_device.cpp @@ -14,6 +14,7 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "soc/soc_caps.h" // SOC_USB_OTG_PERIPH_NUM: which USB-OTG cores the target has #include "tinyusb.h" #include "tinyusb_default_config.h" #if CONFIG_IDF_TARGET_ESP32P4 @@ -202,12 +203,39 @@ ssize_t cdc_console_write(int, const void *data, size_t size) { constexpr TickType_t kUsbWriteTimeoutTicks = pdMS_TO_TICKS(250); constexpr TickType_t kUsbWriteDrainPollTicks = pdMS_TO_TICKS(1) > 0 ? pdMS_TO_TICKS(1) : 1; -// ESP32-S3 / -S2 USB-OTG (DWC2, full-speed) endpoint budget: besides the control -// endpoint EP0, there are ~5 usable data IN endpoints and ~5 usable data OUT -// endpoints. See the README endpoint-budget table for which class combinations -// fit. -constexpr uint8_t kMaxInEndpoints = 5; -constexpr uint8_t kMaxOutEndpoints = 5; +// Endpoint limits of the USB-OTG (DWC2) core behind a port, as TinyUSB's DWC2 +// driver enforces them (the _dwc2_controller table in its dwc2_esp32.h): +// - endpoint numbers 0 .. ep_count - 1, for IN and OUT alike; +// - ep_in_count IN endpoints, EP0 IN included, so ep_in_count - 1 data IN; and +// IN endpoint n uses TX FIFO n, so a data IN endpoint must also be numbered +// below ep_in_count. A data IN endpoint over the count fails to open at +// SET_CONFIGURATION and leaves the device unconfigured +// (hathach/tinyusb#4450); one numbered past the FIFOs opens but never +// transmits (hathach/tinyusb#4007). +// The FIFO RAM never binds for what initialize() builds (no double buffering): +// the full-speed worst case (EP0 + 4 x 64-byte IN, 64-byte OUT) needs ~142 of +// 256 words, the high-speed one (EP0, 3 x 512-byte bulk IN, CDC notification, +// HID, X-Input, 512-byte OUT) ~730 of 992. +struct OtgEndpointLimits { + uint8_t ep_count; ///< endpoint numbers 0 .. ep_count - 1 + uint8_t ep_in_count; ///< IN endpoints (TX FIFOs), EP0 IN included + const char *core; ///< for the error message +}; +constexpr OtgEndpointLimits kFullSpeedCore{7, 5, "full-speed"}; +constexpr OtgEndpointLimits kHighSpeedCore{16, 8, "high-speed"}; + +// The core behind TinyUSB port `port`: ESP32-S2 / -S3 / -H4 have the +// full-speed core only; the ESP32-P4 has the full-speed core on port 0 and +// the high-speed core on port 1; the ESP32-S31 has the high-speed core only. +constexpr OtgEndpointLimits otg_endpoint_limits([[maybe_unused]] int port) { +#if CONFIG_IDF_TARGET_ESP32S31 + return kHighSpeedCore; +#elif (SOC_USB_OTG_PERIPH_NUM > 1) + return port == static_cast(TINYUSB_PORT_HIGH_SPEED_0) ? kHighSpeedCore : kFullSpeedCore; +#else + return kFullSpeedCore; +#endif +} // esp_tinyusb's MSC storage backend supports two LUNs, and at most one medium of // each type (its SD card and wear-levelling media are singletons). constexpr size_t kMaxMscLuns = 2; @@ -1061,11 +1089,11 @@ bool UsbDevice::initialize(std::error_code &ec) { impl_->owned_strings.push_back(config_.xinput->interface_name); // Use SEPARATE endpoint numbers for IN and OUT. The retail controller shares // number 1, but the ESP32-S3 DWC2 corrupts the interrupt-IN stream (a leading - // 0x01 byte) when the same number is used for both directions. + // 0x01 byte) when the same number is used for both directions. The OUT + // number is taken after every other function's (below), so the IN numbers + // stay contiguous and within the TX FIFOs (see otg_endpoint_limits). const uint8_t in_ep = next_ep++; - const uint8_t out_ep = next_ep++; impl_->xinput_ep_in = static_cast(0x80 | in_ep); // interrupt IN - impl_->xinput_ep_out = out_ep; // interrupt OUT in_used++; out_used++; impl_->xinput_itf = xinput_itf; @@ -1085,10 +1113,27 @@ bool UsbDevice::initialize(std::error_code &ec) { out_used++; } - // --- Endpoint budget check --- - if (in_used > kMaxInEndpoints || out_used > kMaxOutEndpoints) { - logger_.error("Endpoint budget exceeded: IN={} (max {}), OUT={} (max {})", in_used, - kMaxInEndpoints, out_used, kMaxOutEndpoints); + // Every IN endpoint is numbered by now (contiguously from 1): the highest + // number any of them has. + const uint8_t max_in_number = static_cast(next_ep - 1); + + if (config_.xinput) { + impl_->xinput_ep_out = next_ep++; // interrupt OUT (see the X-Input block above) + } + + // --- Endpoint budget check, against the core of the port this device will use + // (Config::port, else esp_tinyusb's default for the target) --- + const tinyusb_config_t default_tusb_cfg = TINYUSB_DEFAULT_CONFIG(); + const int usb_port = config_.port >= 0 ? config_.port : static_cast(default_tusb_cfg.port); + const OtgEndpointLimits limits = otg_endpoint_limits(usb_port); + const uint8_t max_data_in = static_cast(limits.ep_in_count - 1); + const uint8_t max_number = static_cast(next_ep - 1); // IN or OUT + if (in_used > max_data_in || max_in_number > max_data_in || max_number >= limits.ep_count) { + logger_.error("Endpoint budget exceeded on the {} USB-OTG core (port {}): {} data IN " + "endpoints (max {}), highest IN number {} (max {}), highest endpoint " + "number {} (max {}), {} OUT endpoints", + limits.core, usb_port, in_used, max_data_in, max_in_number, max_data_in, + max_number, limits.ep_count - 1, out_used); ec = std::make_error_code(std::errc::value_too_large); return false; } diff --git a/doc/en/buses/usb_cdc.rst b/doc/en/buses/usb_cdc.rst index adfb66ea65..15b232bda6 100644 --- a/doc/en/buses/usb_cdc.rst +++ b/doc/en/buses/usb_cdc.rst @@ -266,12 +266,39 @@ console config: UART0 primary (``CONFIG_ESP_CONSOLE_UART_DEFAULT``) with USB-Serial-JTAG as the secondary console for early-boot logs. The ``ota`` example uses this. -Endpoint budget (ESP32-S3 USB-OTG) ----------------------------------- +Endpoint budget +--------------- + +``initialize()`` checks the endpoints against the USB-OTG core behind the port +the device will use (``Config::port``, or esp_tinyusb's default for the target), +with the limits TinyUSB's DWC2 driver enforces: + +.. list-table:: + :header-rows: 1 -The ESP32-S3 (and -S2) USB-OTG core is full-speed and, besides the control -endpoint EP0, provides roughly **5 usable data IN endpoints** and **5 usable data -OUT endpoints**. Each function consumes: + * - Core + - Where + - Data IN endpoints + - Endpoint numbers + * - Full-speed + - ESP32-S2, ESP32-S3, ESP32-P4 port 0 + - 4 (5 IN, EP0 IN included) + - 1 to 6 + * - High-speed + - ESP32-P4 port 1 (its default) + - 7 (8 IN, EP0 IN included) + - 1 to 15 + +TinyUSB also gives IN endpoint *n* the core's TX FIFO *n*, so a data IN endpoint +must be *numbered* within the data IN count as well (1 to 4 on the full-speed +core). Past the count, an IN endpoint fails to open when the host configures the +device, which then stays unconfigured (`hathach/tinyusb#4450 +`_); past the FIFOs, it opens +but never transmits (`hathach/tinyusb#4007 +`_). ``initialize()`` numbers +the IN endpoints contiguously from 1, with X-Input's separate OUT number after +them, so a combination within the count also fits the numbering. The FIFO RAM is +not a limit for any combination of these functions. Each function consumes: .. list-table:: :header-rows: 1 @@ -295,17 +322,21 @@ OUT endpoints**. Each function consumes: - 1 (bulk-IN) - 1 (bulk-OUT) -This is why the device is **selectable** ("not all at once"). Combinations that -fit comfortably: +On the full-speed core this is why the device is **selectable** ("not all at +once"). Combinations that fit there: - CDC + Vendor: 3 IN / 2 OUT (used by the example) -- CDC + Vendor + HID: 4 IN / 2-3 OUT -- CDC + Vendor + MSC: 4 IN / 3 OUT - -Enabling CDC + Vendor + HID + MSC together reaches 5 IN endpoints, which is at the -hard limit and is not recommended. ``espp::UsbDevice`` computes the totals as -functions are enabled and returns ``std::errc::value_too_large`` if the IN or OUT -budget is exceeded. +- CDC + Vendor + HID: 4 IN / 2-3 OUT, the limit +- CDC + Vendor + MSC: 4 IN / 3 OUT, the limit +- CDC + Vendor + X-Input: 4 IN / 3 OUT, the limit +- Vendor + HID + X-Input + MSC: 4 IN / 3-4 OUT + +CDC + Vendor + HID + MSC needs 5 data IN endpoints: it does not fit the +full-speed core, and ``espp::UsbDevice`` returns ``std::errc::value_too_large`` +for it there, logging which limit was exceeded. Every combination fits the +high-speed core. To offer files next to CDC + Vendor + HID on the full-speed +core, carry them over the vendor interface (a file-transfer protocol on the +vendor / WebUSB link) instead of MSC. Notes -----