From a7b89139d6760666ad300435ba5a0ce0cb559666 Mon Sep 17 00:00:00 2001 From: Roman Leonov Date: Tue, 6 Feb 2024 22:29:52 +0100 Subject: [PATCH] feat(esp_tinyusb): Added fs_cfg_desc default, return hs/fs_cfg_desc based on host speed --- device/esp_tinyusb/descriptors_control.c | 26 ++++++++++++++++-- .../include_private/descriptors_control.h | 3 +++ .../include_private/usb_descriptors.h | 4 +++ device/esp_tinyusb/usb_descriptors.c | 27 +++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/device/esp_tinyusb/descriptors_control.c b/device/esp_tinyusb/descriptors_control.c index a99fba58..0b00f838 100644 --- a/device/esp_tinyusb/descriptors_control.c +++ b/device/esp_tinyusb/descriptors_control.c @@ -30,6 +30,7 @@ static tinyusb_descriptor_config_t *s_desc_cfg; uint8_t const *tud_descriptor_device_cb(void) { assert(s_desc_cfg); + assert(s_desc_cfg->dev); return (uint8_t const *)s_desc_cfg->dev; } @@ -44,7 +45,17 @@ uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { (void)index; // Unused, this driver supports only 1 configuration assert(s_desc_cfg); + assert(s_desc_cfg->cfg); + +#if (TUD_OPT_HIGH_SPEED) + assert(s_desc_cfg->fs_cfg); + // Return configuration descriptor based on Host speed + return (tud_speed_get() == TUSB_SPEED_HIGH) + ? s_desc_cfg->cfg + : s_desc_cfg->fs_cfg; +#else return s_desc_cfg->cfg; +#endif // } /** @@ -58,6 +69,7 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { (void) langid; // Unused, this driver supports only one language in string descriptors assert(s_desc_cfg); + assert(s_desc_cfg->str); uint8_t chr_count; static uint16_t _desc_str[MAX_DESC_BUF_SIZE]; @@ -118,7 +130,7 @@ esp_err_t tinyusb_set_descriptors(const tinyusb_config_t *config) #if (CFG_TUD_HID > 0 || CFG_TUD_MIDI > 0 || CFG_TUD_CUSTOM_CLASS > 0 || CFG_TUD_ECM_RNDIS > 0 || CFG_TUD_DFU > 0 || CFG_TUD_DFU_RUNTIME > 0 || CFG_TUD_BTH > 0) ESP_RETURN_ON_FALSE(config->hs_cfg_desc, ESP_ERR_INVALID_ARG, TAG, "Configuration descriptor must be provided for this device"); #else - ESP_LOGW(TAG, "The device's configuration descriptor is not provided by user, using default."); + ESP_LOGW(TAG, "The device's HS configuration descriptor is not provided by user, using default."); #endif } s_desc_cfg->cfg = config->hs_cfg_desc @@ -126,7 +138,17 @@ esp_err_t tinyusb_set_descriptors(const tinyusb_config_t *config) : descriptor_cfg_kconfig; // Full Speed - // TODO: + if (NULL == config->fs_cfg_desc) { + // Default configuration descriptor is provided only for CDC, MSC and NCM classes +#if (CFG_TUD_HID > 0 || CFG_TUD_MIDI > 0 || CFG_TUD_CUSTOM_CLASS > 0 || CFG_TUD_ECM_RNDIS > 0 || CFG_TUD_DFU > 0 || CFG_TUD_DFU_RUNTIME > 0 || CFG_TUD_BTH > 0) + ESP_RETURN_ON_FALSE(config->fs_cfg_desc, ESP_ERR_INVALID_ARG, TAG, "Configuration descriptor must be provided for this device"); +#else + ESP_LOGW(TAG, "The device's FS configuration descriptor is not provided by user, using default."); +#endif + } + s_desc_cfg->fs_cfg = config->fs_cfg_desc + ? config->fs_cfg_desc + : descriptor_fs_cfg_kconfig; #else if (NULL == config->configuration_descriptor) { // Default configuration descriptor is provided only for CDC, MSC and NCM classes diff --git a/device/esp_tinyusb/include_private/descriptors_control.h b/device/esp_tinyusb/include_private/descriptors_control.h index b448199d..6512cce5 100644 --- a/device/esp_tinyusb/include_private/descriptors_control.h +++ b/device/esp_tinyusb/include_private/descriptors_control.h @@ -20,6 +20,9 @@ extern "C" { typedef struct { const tusb_desc_device_t *dev; /*!< Pointer to device descriptor */ const uint8_t *cfg; /*!< Pointer to configuration descriptor */ +#if (TUD_OPT_HIGH_SPEED) + const uint8_t *fs_cfg; /*!< Pointer to configuration descriptor */ +#endif // TUD_OPT_HIGH_SPEED const char **str; /*!< Pointer to array of UTF-8 strings */ int str_count; /*!< Number of descriptors in str */ } tinyusb_descriptor_config_t; diff --git a/device/esp_tinyusb/include_private/usb_descriptors.h b/device/esp_tinyusb/include_private/usb_descriptors.h index 98126bcd..2c73fce2 100644 --- a/device/esp_tinyusb/include_private/usb_descriptors.h +++ b/device/esp_tinyusb/include_private/usb_descriptors.h @@ -36,6 +36,10 @@ extern const char *descriptor_str_kconfig[]; */ extern const uint8_t descriptor_cfg_kconfig[]; +#if (TUD_OPT_HIGH_SPEED) +extern const uint8_t descriptor_fs_cfg_kconfig[]; +#endif // + uint8_t tusb_get_mac_string_id(void); #ifdef __cplusplus diff --git a/device/esp_tinyusb/usb_descriptors.c b/device/esp_tinyusb/usb_descriptors.c index 956cc0fb..3565e82f 100644 --- a/device/esp_tinyusb/usb_descriptors.c +++ b/device/esp_tinyusb/usb_descriptors.c @@ -191,6 +191,33 @@ uint8_t const descriptor_cfg_kconfig[] = { #endif }; +#if (TUD_OPT_HIGH_SPEED) +uint8_t const descriptor_fs_cfg_kconfig[] = { + // Configuration number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUSB_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), + +#if CFG_TUD_CDC + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, STRID_CDC_INTERFACE, 0x80 | EPNUM_0_CDC_NOTIF, 8, EPNUM_0_CDC, 0x80 | EPNUM_0_CDC, 64), +#endif + +#if CFG_TUD_CDC > 1 + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. + TUD_CDC_DESCRIPTOR(ITF_NUM_CDC1, STRID_CDC_INTERFACE, 0x80 | EPNUM_1_CDC_NOTIF, 8, EPNUM_1_CDC, 0x80 | EPNUM_1_CDC, 64), +#endif + +#if CFG_TUD_MSC + // Interface number, string index, EP Out & EP In address, EP size + TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, STRID_MSC_INTERFACE, EPNUM_MSC, 0x80 | EPNUM_MSC, 64), +#endif + +#if CFG_TUD_NCM + // Interface number, description string index, MAC address string index, EP notification address and size, EP data address (out, in), and size, max segment size. + TUD_CDC_NCM_DESCRIPTOR(ITF_NUM_NET, STRID_NET_INTERFACE, STRID_MAC, (0x80 | EPNUM_NET_NOTIF), 64, EPNUM_NET_DATA, (0x80 | EPNUM_NET_DATA), 64, CFG_TUD_NET_MTU), +#endif +}; +#endif // TUD_OPT_HIGH_SPEED + #if CFG_TUD_NCM uint8_t tusb_get_mac_string_id(void) {