Skip to content

Commit 6e77d5b

Browse files
committed
rework esp32 configuration descriptor builder to manage our own descriptor
1 parent 7dfc5ab commit 6e77d5b

File tree

2 files changed

+49
-40
lines changed

2 files changed

+49
-40
lines changed

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,14 @@
3434
// USB Information can be defined in variant file e.g pins_arduino.h
3535
#include "Arduino.h"
3636

37-
// - USB_VID, USB_PID, USB_MANUFACTURER, USB_PRODUCT are defined on most
38-
// core that has built-in support for TinyUSB. Otherwise
39-
// - BOARD_VENDORID, BOARD_PRODUCTID, BOARD_MANUFACTURER, BOARD_NAME are use
40-
// if defined, mostly on mbed core
37+
/* VID, PID, Manufacturer and Product name:
38+
* - For most ports: USB_VID, USB_PID, USB_MANUFACTURER, USB_PRODUCT are
39+
* defined.
40+
* - For ESP32: Default USB_MANUFACTURER is Espressif (instead of Adafruit),
41+
* ARDUINO_BOARD as USB_PRODUCT
42+
* - For mbed core: BOARD_VENDORID, BOARD_PRODUCTID, BOARD_MANUFACTURER,
43+
* BOARD_NAME are defined
44+
*/
4145

4246
#ifndef USB_VID
4347
#ifdef BOARD_VENDORID
@@ -56,16 +60,19 @@
5660
#endif
5761

5862
#ifndef USB_MANUFACTURER
59-
6063
#ifdef BOARD_MANUFACTURER
6164
#define USB_MANUFACTURER BOARD_MANUFACTURER
65+
#elif defined(ARDUINO_ARCH_ESP32)
66+
#define USB_MANUFACTURER "Espressif Systems"
6267
#else
6368
#define USB_MANUFACTURER "Adafruit"
6469
#endif
6570
#endif
6671

6772
#ifndef USB_PRODUCT
68-
#ifdef BOARD_NAME
73+
#if defined(ARDUINO_BOARD)
74+
#define USB_PRODUCT ARDUINO_BOARD
75+
#elif defined(BOARD_NAME)
6976
#define USB_PRODUCT BOARD_NAME
7077
#else
7178
#define USB_PRODUCT "Unknown"
@@ -84,7 +91,12 @@ enum { STRID_LANGUAGE = 0, STRID_MANUFACTURER, STRID_PRODUCT, STRID_SERIAL };
8491

8592
Adafruit_USBD_Device TinyUSBDevice;
8693

87-
Adafruit_USBD_Device::Adafruit_USBD_Device(void) {}
94+
Adafruit_USBD_Device::Adafruit_USBD_Device(void) {
95+
#if defined(ARDUINO_ARCH_ESP32) && ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
96+
// auto begin for ESP32 USB OTG Mode with CDC on boot
97+
begin(0);
98+
#endif
99+
}
88100

89101
void Adafruit_USBD_Device::setConfigurationBuffer(uint8_t *buf,
90102
uint32_t buflen) {
@@ -152,36 +164,6 @@ bool Adafruit_USBD_Device::detach(void) { return tud_disconnect(); }
152164

153165
bool Adafruit_USBD_Device::attach(void) { return tud_connect(); }
154166

155-
// EPS32 use built-in core descriptor builder.
156-
// Therefore most of descriptors are stubs only
157-
#ifdef ARDUINO_ARCH_ESP32
158-
159-
void Adafruit_USBD_Device::clearConfiguration(void) {}
160-
161-
bool Adafruit_USBD_Device::addInterface(Adafruit_USBD_Interface &itf) {
162-
(void)itf;
163-
return true;
164-
}
165-
166-
bool Adafruit_USBD_Device::begin(uint8_t rhport) {
167-
(void)rhport;
168-
return true;
169-
}
170-
171-
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t *serial_utf16) {
172-
(void)serial_utf16;
173-
return 0;
174-
}
175-
176-
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
177-
uint16_t langid) {
178-
(void)index;
179-
(void)langid;
180-
return NULL;
181-
}
182-
183-
#else
184-
185167
void Adafruit_USBD_Device::clearConfiguration(void) {
186168
tusb_desc_device_t const desc_dev = {.bLength = sizeof(tusb_desc_device_t),
187169
.bDescriptorType = TUSB_DESC_DEVICE,
@@ -261,10 +243,28 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
261243
_desc_device.bDeviceSubClass = MISC_SUBCLASS_COMMON;
262244
_desc_device.bDeviceProtocol = MISC_PROTOCOL_IAD;
263245

246+
#if defined(ARDUINO_ARCH_ESP32)
247+
#if ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
248+
// follow USBCDC cdc descriptor
249+
uint8_t itfnum = allocInterface(2);
250+
uint8_t strid = addStringDescriptor("TinyUSB Serial");
251+
uint8_t const desc_cdc[TUD_CDC_DESC_LEN] = {
252+
TUD_CDC_DESCRIPTOR(itfnum, strid, 0x85, 64, 0x03, 0x84, 64)};
253+
254+
memcpy(_desc_cfg + _desc_cfg_len, desc_cdc, sizeof(desc_cdc));
255+
_desc_cfg_len += sizeof(desc_cdc);
256+
257+
// Update configuration descriptor
258+
tusb_desc_configuration_t *config = (tusb_desc_configuration_t *)_desc_cfg;
259+
config->wTotalLength = _desc_cfg_len;
260+
config->bNumInterfaces = _itf_count;
261+
#endif
262+
#else
264263
SerialTinyUSB.begin(115200);
265264

266265
// Init device hardware and call tusb_init()
267266
TinyUSB_Port_InitDevice(rhport);
267+
#endif
268268

269269
return true;
270270
}
@@ -520,6 +520,4 @@ void tud_dfu_runtime_reboot_to_dfu_cb(void) {
520520
}
521521
#endif
522522

523-
#endif // ESP32
524-
525523
#endif // CFG_TUD_ENABLED

src/arduino/Adafruit_USBD_Device.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,18 @@ class Adafruit_USBD_Device {
8888
}
8989

9090
uint8_t allocEndpoint(uint8_t in) {
91-
return in ? (0x80 | _epin_count++) : _epout_count++;
91+
uint8_t ret = in ? (0x80 | _epin_count++) : _epout_count++;
92+
#if defined(ARDUINO_ARCH_ESP32) && ARDUINO_USB_CDC_ON_BOOT && !ARDUINO_USB_MODE
93+
// ESP32 reserves 0x03, 0x84, 0x85 for CDC Serial
94+
if (ret == 0x03) {
95+
ret = _epout_count++;
96+
} else if (ret == 0x84 || ret == 0x85) {
97+
// Note: ESP32 does not have this much of EP IN
98+
_epin_count = 6;
99+
ret = 0x86;
100+
}
101+
#endif
102+
return ret;
92103
}
93104

94105
//------------- String descriptor -------------//

0 commit comments

Comments
 (0)