Skip to content

Commit 341dc18

Browse files
committed
fix(usb): Add support for HighSpeed USB
This commit adds support for HighSpeed USB as present on ESP32-P4
1 parent 157b4c8 commit 341dc18

File tree

8 files changed

+29
-10
lines changed

8 files changed

+29
-10
lines changed

cores/esp32/USBCDC.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ USBCDC *devices[MAX_USB_CDC_DEVICES] = {NULL, NULL};
3131
static uint16_t load_cdc_descriptor(uint8_t *dst, uint8_t *itf) {
3232
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB CDC");
3333
uint8_t descriptor[TUD_CDC_DESC_LEN] = {// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
34-
TUD_CDC_DESCRIPTOR(*itf, str_index, 0x85, 64, 0x03, 0x84, 64)
34+
TUD_CDC_DESCRIPTOR(*itf, str_index, 0x85, CFG_TUD_ENDOINT_SIZE, 0x03, 0x84, CFG_TUD_ENDOINT_SIZE)
3535
};
3636
*itf += 2;
3737
memcpy(dst, descriptor, TUD_CDC_DESC_LEN);

cores/esp32/USBMSC.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" uint16_t tusb_msc_load_descriptor(uint8_t *dst, uint8_t *itf) {
2424
uint8_t ep_num = tinyusb_get_free_duplex_endpoint();
2525
TU_VERIFY(ep_num != 0);
2626
uint8_t descriptor[TUD_MSC_DESC_LEN] = {// Interface number, string index, EP Out & EP In address, EP size
27-
TUD_MSC_DESCRIPTOR(*itf, str_index, ep_num, (uint8_t)(0x80 | ep_num), 64)
27+
TUD_MSC_DESCRIPTOR(*itf, str_index, ep_num, (uint8_t)(0x80 | ep_num), CFG_TUD_ENDOINT_SIZE)
2828
};
2929
*itf += 1;
3030
memcpy(dst, descriptor, TUD_MSC_DESC_LEN);

cores/esp32/esp32-hal-tinyusb.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ esp_err_t init_usb_hal(bool external_phy) {
131131
.controller = USB_PHY_CTRL_OTG,
132132
.target = USB_PHY_TARGET_INT,
133133
.otg_mode = USB_OTG_MODE_DEVICE,
134+
#if CONFIG_IDF_TARGET_ESP32P4
135+
.otg_speed = USB_PHY_SPEED_HIGH,
136+
#else
134137
.otg_speed = USB_PHY_SPEED_FULL,
138+
#endif
135139
.ext_io_conf = NULL,
136140
.otg_io_conf = NULL,
137141
};
@@ -169,7 +173,11 @@ void deinit_usb_hal() {
169173

170174
esp_err_t tinyusb_driver_install(const tinyusb_config_t *config) {
171175
init_usb_hal(config->external_phy);
172-
if (!tusb_init()) {
176+
#if CONFIG_IDF_TARGET_ESP32P4
177+
if (!tud_init(1)) {
178+
#else
179+
if (!tud_init(0)) {
180+
#endif
173181
log_e("Can't initialize the TinyUSB stack.");
174182
return ESP_FAIL;
175183
}

cores/esp32/esp32-hal-tinyusb.h

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ extern "C" {
3131
#define USB_ESPRESSIF_VID 0x303A
3232
#define USB_STRING_DESCRIPTOR_ARRAY_SIZE 10
3333

34+
#ifndef CFG_TUD_ENDOINT_SIZE
35+
#if CONFIG_IDF_TARGET_ESP32P4
36+
#define CFG_TUD_ENDOINT_SIZE 512
37+
#else
38+
#define CFG_TUD_ENDOINT_SIZE 64
39+
#endif
40+
#endif
41+
3442
typedef struct {
3543
uint16_t vid;
3644
uint16_t pid;

libraries/USB/src/USBHID.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ extern "C" uint16_t tusb_hid_load_descriptor(uint8_t *dst, uint8_t *itf) {
206206
uint8_t descriptor[TUD_HID_INOUT_DESC_LEN] = {
207207
// HID Input & Output descriptor
208208
// Interface number, string index, protocol, report descriptor len, EP OUT & IN address, size & polling interval
209-
TUD_HID_INOUT_DESCRIPTOR(*itf, str_index, tinyusb_interface_protocol, tinyusb_hid_device_descriptor_len, ep_out, (uint8_t)(0x80 | ep_in), 64, 1)
209+
TUD_HID_INOUT_DESCRIPTOR(*itf, str_index, tinyusb_interface_protocol, tinyusb_hid_device_descriptor_len, ep_out, (uint8_t)(0x80 | ep_in), CFG_TUD_ENDOINT_SIZE, 1)
210210
};
211211
*itf += 1;
212212
memcpy(dst, descriptor, TUD_HID_INOUT_DESC_LEN);

libraries/USB/src/USBMIDI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" uint16_t tusb_midi_load_descriptor(uint8_t *dst, uint8_t *itf) {
2424
uint8_t ep_out = tinyusb_get_free_out_endpoint();
2525
TU_VERIFY(ep_out != 0);
2626
uint8_t descriptor[TUD_MIDI_DESC_LEN] = {
27-
TUD_MIDI_DESCRIPTOR(*itf, str_index, ep_out, (uint8_t)(0x80 | ep_in), 64),
27+
TUD_MIDI_DESCRIPTOR(*itf, str_index, ep_out, (uint8_t)(0x80 | ep_in), CFG_TUD_ENDOINT_SIZE),
2828
};
2929
*itf += 2;
3030
memcpy(dst, descriptor, TUD_MIDI_DESC_LEN);

libraries/USB/src/USBVendor.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ esp_err_t arduino_usb_event_handler_register_with(esp_event_base_t event_base, i
2424

2525
static USBVendor *_Vendor = NULL;
2626
static QueueHandle_t rx_queue = NULL;
27-
static uint8_t USB_VENDOR_ENDPOINT_SIZE = 64;
27+
static uint16_t USB_VENDOR_ENDPOINT_SIZE = CFG_TUD_ENDOINT_SIZE;
2828

2929
uint16_t tusb_vendor_load_descriptor(uint8_t *dst, uint8_t *itf) {
3030
uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB Vendor");
@@ -68,10 +68,13 @@ extern "C" bool tinyusb_vendor_control_request_cb(uint8_t rhport, uint8_t stage,
6868
return false;
6969
}
7070

71-
USBVendor::USBVendor(uint8_t endpoint_size) : itf(0), cb(NULL) {
71+
USBVendor::USBVendor(uint16_t endpoint_size) : itf(0), cb(NULL) {
7272
if (!_Vendor) {
7373
_Vendor = this;
74-
if (endpoint_size <= 64) {
74+
if (endpoint_size == 0) {
75+
endpoint_size = CFG_TUD_ENDOINT_SIZE;
76+
}
77+
if (endpoint_size <= CFG_TUD_ENDOINT_SIZE) {
7578
USB_VENDOR_ENDPOINT_SIZE = endpoint_size;
7679
}
7780
tinyusb_enable_interface(USB_INTERFACE_VENDOR, TUD_VENDOR_DESC_LEN, tusb_vendor_load_descriptor);
@@ -97,7 +100,7 @@ size_t USBVendor::setRxBufferSize(size_t rx_queue_len) {
97100
}
98101

99102
void USBVendor::begin() {
100-
setRxBufferSize(256); //default if not preset
103+
setRxBufferSize(512); //default if not preset
101104
}
102105

103106
void USBVendor::end() {

libraries/USB/src/USBVendor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class USBVendor : public Stream {
7474
arduino_usb_vendor_control_request_handler_t cb;
7575

7676
public:
77-
USBVendor(uint8_t endpoint_size = 64);
77+
USBVendor(uint16_t endpoint_size = 0);
7878
void begin(void);
7979
void end(void);
8080
size_t setRxBufferSize(size_t);

0 commit comments

Comments
 (0)