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 6, 2024
1 parent df864be commit a7b8913
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
26 changes: 24 additions & 2 deletions device/esp_tinyusb/descriptors_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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

/**
Expand All @@ -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];

Expand Down Expand Up @@ -118,15 +130,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 Down
3 changes: 3 additions & 0 deletions device/esp_tinyusb/include_private/descriptors_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 a7b8913

Please sign in to comment.