Skip to content

Commit

Permalink
feat(esp_tinyusb): Added fs_cfg_desc default, return hs/fs_cfg_desc b…
Browse files Browse the repository at this point in the history
…ased on host speed
  • Loading branch information
roma-jam committed Feb 7, 2024
1 parent 92f4bcc commit 55b1332
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
36 changes: 28 additions & 8 deletions device/esp_tinyusb/descriptors_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "descriptors_control.h"
#include "usb_descriptors.h"

#define USB_STRING_DESCRIPTOR_ARRAY_SIZE 8 // Max 8 string descriptors for a device. LANGID, Manufacturer, Product, Serial number + 4 user defined
#define MAX_DESC_BUF_SIZE 32 // Max length of string descriptor (can be extended, USB supports lengths up to 255 bytes)

static const char *TAG = "tusb_desc";
Expand All @@ -30,6 +29,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;
}

Expand All @@ -44,7 +44,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 //
}

/**
Expand All @@ -58,6 +68,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];

Expand Down Expand Up @@ -96,11 +107,10 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid)
esp_err_t tinyusb_set_descriptors(const tinyusb_config_t *config)
{
assert(config);
const char **string_descriptor;
// Create descriptor struct
s_desc_cfg = calloc(1, sizeof(tinyusb_descriptor_config_t));
assert(s_desc_cfg);
s_desc_cfg->str = calloc(1, USB_STRING_DESCRIPTOR_ARRAY_SIZE);
assert(s_desc_cfg->str);
// Parse configuration and save descriptors's pointer
// Select Device Descriptor
if (NULL == config->device_descriptor) {
Expand All @@ -118,15 +128,25 @@ 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
? config->hs_cfg_desc
: 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
Expand All @@ -143,17 +163,18 @@ esp_err_t tinyusb_set_descriptors(const tinyusb_config_t *config)

// Select String Descriptors and count them
if (config->string_descriptor) {
s_desc_cfg->str = config->string_descriptor;
string_descriptor = config->string_descriptor;
s_desc_cfg->str_count = (config->string_descriptor_count != 0)
? config->string_descriptor_count
: 8; // '8' is for backward compatibility with esp_tinyusb v1.0.0. Do NOT remove!
} else {
s_desc_cfg->str = descriptor_str_kconfig;
string_descriptor = descriptor_str_kconfig;
while (descriptor_str_kconfig[++s_desc_cfg->str_count] != NULL);
ESP_LOGW(TAG, "The device's string descriptor is not provided by user, using default.");
}

assert(s_desc_cfg->str_count <= USB_STRING_DESCRIPTOR_ARRAY_SIZE);
memcpy(s_desc_cfg->str, string_descriptor, s_desc_cfg->str_count * sizeof(string_descriptor[0]));

ESP_LOGI(TAG, "\n"
"┌─────────────────────────────────┐\n"
Expand Down Expand Up @@ -186,7 +207,6 @@ esp_err_t tinyusb_set_descriptors(const tinyusb_config_t *config)
s_desc_cfg->dev->idVendor, s_desc_cfg->dev->idProduct, s_desc_cfg->dev->bcdDevice,
s_desc_cfg->dev->iManufacturer, s_desc_cfg->dev->iProduct, s_desc_cfg->dev->iSerialNumber,
s_desc_cfg->dev->bNumConfigurations);

return ESP_OK;
}

Expand Down
9 changes: 7 additions & 2 deletions device/esp_tinyusb/include_private/descriptors_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
extern "C" {
#endif

#define USB_STRING_DESCRIPTOR_ARRAY_SIZE 8 // Max 8 string descriptors for a device. LANGID, Manufacturer, Product, Serial number + 4 user defined

/**
* @brief USB Device descriptor pointers for tinyusb configuration
*
*/
typedef struct {
const tusb_desc_device_t *dev; /*!< Pointer to device descriptor */
const uint8_t *cfg; /*!< Pointer to configuration descriptor */
const char **str; /*!< Pointer to array of UTF-8 strings */
const uint8_t *cfg; /*!< Pointer to HS configuration descriptor */
#if (TUD_OPT_HIGH_SPEED)
const uint8_t *fs_cfg; /*!< Pointer to FS configuration descriptor */
#endif // TUD_OPT_HIGH_SPEED
const char *str[USB_STRING_DESCRIPTOR_ARRAY_SIZE]; /*!< Pointer to array of UTF-8 strings */
int str_count; /*!< Number of descriptors in str */
} tinyusb_descriptor_config_t;

Expand Down
4 changes: 4 additions & 0 deletions device/esp_tinyusb/include_private/usb_descriptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions device/esp_tinyusb/usb_descriptors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 55b1332

Please sign in to comment.