Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tinyusb port to work with current master tinyusb (IDFGH-3381) #5355

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion .gitmodules
Expand Up @@ -81,7 +81,8 @@

[submodule "components/tinyusb/tinyusb"]
path = components/tinyusb/tinyusb
url = ../../espressif/tinyusb.git
url = https://github.com/hathach/tinyusb.git
branch = master

[submodule "examples/peripherals/secure_element/atecc608_ecdsa/components/esp-cryptoauthlib"]
path = examples/peripherals/secure_element/atecc608_ecdsa/components/esp-cryptoauthlib
Expand Down
4 changes: 3 additions & 1 deletion components/tinyusb/CMakeLists.txt
Expand Up @@ -23,7 +23,7 @@ if(CONFIG_USB_ENABLED)
"${COMPONENT_DIR}/port/common/src/descriptors_control.c"
"${COMPONENT_DIR}/port/common/src/usb_descriptors.c"
"${COMPONENT_DIR}/port/common/src/usbd.c"
"${COMPONENT_DIR}/port/esp32s2/src/device_controller_driver.c"
# "${COMPONENT_DIR}/port/esp32s2/src/device_controller_driver.c"
"${COMPONENT_DIR}/port/esp32s2/src/tinyusb.c"
# tusb:
"${COMPONENT_DIR}/tinyusb/src/common/tusb_fifo.c"
Expand All @@ -32,6 +32,8 @@ if(CONFIG_USB_ENABLED)
"${COMPONENT_DIR}/tinyusb/src/class/cdc/cdc_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/hid/hid_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/midi/midi_device.c"
"${COMPONENT_DIR}/tinyusb/src/class/vendor/vendor_device.c"
"${COMPONENT_DIR}/tinyusb/src/tusb.c"
"${COMPONENT_DIR}/tinyusb/src/portable/espressif/esp32s2/dcd_esp32s2.c"
)
endif()
42 changes: 42 additions & 0 deletions components/tinyusb/Kconfig
Expand Up @@ -9,6 +9,13 @@ menu "TinyUSB"
help
Adds support for TinyUSB

config USB_MAX_POWER_USAGE
int "Max power usage from USB port"
default 100
depends on USB_ENABLED
help
Maxx power used by device from USB port in mA.

config USB_CDC_ENABLED
bool "Enable USB Serial (CDC) TinyUSB driver"
default n
Expand Down Expand Up @@ -70,6 +77,27 @@ menu "TinyUSB"
Enable MIDI TinyUSB driver. It is recomended to use Menuconfig-driven descriptor (.descriptor = NULL
and .string_descriptor = NULL in the tinyusb_config_t structure).

config USB_MIDI_RX_BUFSIZE
int "MIDI FIFO size of RX"
default 64
depends on USB_MIDI_ENABLED
help
CDC FIFO size of RX

config USB_MIDI_TX_BUFSIZE
int "MIDI FIFO size of TX"
default 64
depends on USB_MIDI_ENABLED
help
CDC FIFO size of TX

config USB_VENDOR_ENABLED
bool "Vendor class TinyUSB driver"
default n
depends on USB_ENABLED
help
Enable USB vendor TinyUSB driver. Currently used by webusb driver.

config USB_CUSTOM_CLASS_ENABLED
bool "Enable a custom TinyUSB class"
default n
Expand Down Expand Up @@ -157,13 +185,27 @@ menu "TinyUSB"
help
Specify name of the MSC device

config USB_DESC_MIDI_STRING
string "MIDI Device String"
default "Espressif MIDI Device"
depends on USB_MIDI_ENABLED
help
Specify name of the MIDI device

config USB_DESC_HID_STRING
string "HID Device String"
default "Espressif HID Device"
depends on USB_HID_ENABLED
help
Specify name of the HID device

config USB_DESC_VENDOR_STRING
string "VENDOR Device String"
default "Espressif VENDOR Device"
depends on USB_VENDOR_ENABLED
help
Specify name of the VENDOR device

endmenu

endmenu
17 changes: 14 additions & 3 deletions components/tinyusb/port/common/include/descriptors_control.h
Expand Up @@ -25,6 +25,8 @@
* [MSB] HID | MSC | CDC [LSB]
*/
#define EPNUM_MSC 0x03
#define EPNUM_VENDOR 0x06
#define EPNUM_MIDI 0x05

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -52,15 +54,24 @@ enum {
ITF_NUM_HID,
# endif

ITF_NUM_TOTAL
# if CFG_TUD_MIDI
ITF_NUM_MIDI,
ITF_NUM_MIDI_STREAMING,
# endif

# if CFG_TUD_VENDOR
ITF_NUM_VENDOR,
# endif

ITF_NUM_TOTAL
};

enum {
CONFIG_TOTAL_LEN = TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN + CFG_TUD_MSC * TUD_MSC_DESC_LEN +
CFG_TUD_HID * TUD_HID_DESC_LEN
CFG_TUD_HID * TUD_HID_DESC_LEN + CFG_TUD_VENDOR * TUD_VENDOR_DESC_LEN + CFG_TUD_MIDI * TUD_MIDI_DESC_LEN
};

bool tusb_desc_set;
extern bool tusb_desc_set;
void tusb_set_descriptor(tusb_desc_device_t *desc, char **str_desc);
tusb_desc_device_t *tusb_get_active_desc(void);
char **tusb_get_active_str_desc(void);
Expand Down
11 changes: 6 additions & 5 deletions components/tinyusb/port/common/include/usb_descriptors.h
Expand Up @@ -20,11 +20,12 @@

#define USB_ESPRESSIF_VID 0x303A

#define USB_STRING_DESCRIPTOR_ARRAY_SIZE 7
#define USB_STRING_DESCRIPTOR_ARRAY_SIZE 9
typedef char *tusb_desc_strarray_device_t[USB_STRING_DESCRIPTOR_ARRAY_SIZE];

tusb_desc_device_t descriptor_tinyusb;
tusb_desc_strarray_device_t descriptor_str_tinyusb;
extern tusb_desc_device_t descriptor_tinyusb;
extern tusb_desc_strarray_device_t descriptor_str_tinyusb;

extern tusb_desc_device_t descriptor_kconfig;
extern tusb_desc_strarray_device_t descriptor_str_kconfig;

tusb_desc_device_t descriptor_kconfig;
tusb_desc_strarray_device_t descriptor_str_kconfig;
19 changes: 14 additions & 5 deletions components/tinyusb/port/common/src/descriptors_control.c
Expand Up @@ -18,7 +18,7 @@
static const char *TAG = "TUSB:descriptors_control";
static tusb_desc_device_t s_descriptor;
static char *s_str_descriptor[USB_STRING_DESCRIPTOR_ARRAY_SIZE];

bool tusb_desc_set;

#if CFG_TUD_HID //HID Report Descriptor
uint8_t const desc_hid_report[] = {
Expand All @@ -29,7 +29,7 @@ uint8_t const desc_hid_report[] = {

uint8_t const desc_configuration[] = {
// interface count, string index, total length, attribute, power in mA
TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, CONFIG_USB_MAX_POWER_USAGE),

# if CFG_TUD_CDC
// Interface number, string index, EP notification address and size, EP data address (out, in) and size.
Expand All @@ -41,7 +41,15 @@ uint8_t const desc_configuration[] = {
# endif
# if CFG_TUD_HID
// Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x84, 16, 10)
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x84, 16, 10),
# endif
# if CFG_TUD_VENDOR
// Interface number, string index, EP Out & IN address, EP size
TUD_VENDOR_DESCRIPTOR(ITF_NUM_VENDOR, 7, EPNUM_VENDOR, 0x80 | EPNUM_VENDOR, 64),
# endif
# if CFG_TUD_MIDI
// Interface number, string index, EP Out & EP In address, EP size
TUD_MIDI_DESCRIPTOR(ITF_NUM_MIDI, 8, EPNUM_MIDI, 0x80 | EPNUM_MIDI, (CFG_TUSB_RHPORT0_MODE & OPT_MODE_HIGH_SPEED) ? 512 : 64),
# endif
};

Expand Down Expand Up @@ -70,6 +78,7 @@ uint8_t const *tud_descriptor_device_cb(void)
uint8_t const *tud_descriptor_configuration_cb(uint8_t index)
{
(void)index; // for multiple configurations
ESP_LOG_BUFFER_HEX("", desc_configuration, 100);
return desc_configuration;
}

Expand All @@ -83,7 +92,7 @@ static uint16_t _desc_str[32];
* @param index
* @return uint16_t const*
*/
uint16_t const *tud_descriptor_string_cb(uint8_t index)
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
{
uint8_t chr_count;

Expand All @@ -108,7 +117,7 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index)
}

// first byte is len, second byte is string type
_desc_str[0] = TUD_DESC_STR_HEADER(chr_count);
_desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2*chr_count + 2);

return _desc_str;
}
Expand Down
18 changes: 15 additions & 3 deletions components/tinyusb/port/common/src/usb_descriptors.c
Expand Up @@ -35,7 +35,7 @@ tusb_desc_device_t descriptor_tinyusb = {
.bDeviceProtocol = 0x00,
#endif

.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,

.idVendor = 0xCafe,
.idProduct = USB_TUSB_PID,
Expand Down Expand Up @@ -77,7 +77,7 @@ tusb_desc_device_t descriptor_kconfig = {
.bDeviceProtocol = 0x00,
#endif

.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,

#if CONFIG_USB_DESC_USE_ESPRESSIF_VID
.idVendor = USB_ESPRESSIF_VID,
Expand Down Expand Up @@ -119,7 +119,19 @@ tusb_desc_strarray_device_t descriptor_str_kconfig = {
#endif

#if CONFIG_USB_HID_ENABLED
CONFIG_USB_DESC_HID_STRING // 6: HIDs
CONFIG_USB_DESC_HID_STRING, // 6: HIDs
#else
"",
#endif

#if CONFIG_USB_VENDOR_ENABLED
CONFIG_USB_DESC_VENDOR_STRING, // 7: Vendor
#else
"",
#endif

#if CONFIG_USB_MIDI_ENABLED
CONFIG_USB_DESC_MIDI_STRING, // 8: MIDI
#else
"",
#endif
Expand Down