diff --git a/pkg/tinyusb/Kconfig b/pkg/tinyusb/Kconfig index b5cd92bc4945..b1c0fcdb2f58 100644 --- a/pkg/tinyusb/Kconfig +++ b/pkg/tinyusb/Kconfig @@ -208,7 +208,7 @@ config TUSBD_FS_EP_SIZE default 64 config TUSBD_HS_EP_SIZE - int "Device endpoint Size in High-Speed mode [byte]" + int "Device endpoint size in High-Speed mode [byte]" default 512 config TUSBD_USE_CUSTOM_DESC diff --git a/pkg/tinyusb/Kconfig.cdc b/pkg/tinyusb/Kconfig.cdc index 1b019c8b5511..8472aa34ddd1 100644 --- a/pkg/tinyusb/Kconfig.cdc +++ b/pkg/tinyusb/Kconfig.cdc @@ -19,6 +19,14 @@ config TUSBD_CDC_NOTIF_EP_SIZE int "CDC notification endpoint size [byte]" default 8 +config TUSBD_CDC_FS_EP_SIZE + int "CDC Full-Speed endpoint size [byte]" + default 64 + +config TUSBD_CDC_HS_EP_SIZE + int "CDC High-Speed endpoint size [byte]" + default 512 + config TUSBD_CDC_0_STRING string "CDC0 descriptor string" depends on TUSBD_CDC_NUMOF > 0 diff --git a/pkg/tinyusb/Kconfig.msc b/pkg/tinyusb/Kconfig.msc index abe6857d8054..d5f852210392 100644 --- a/pkg/tinyusb/Kconfig.msc +++ b/pkg/tinyusb/Kconfig.msc @@ -14,8 +14,12 @@ config TUSBD_MSC_NUMOF int default 1 -config TUSBD_MSC_EP_SIZE - int "MSC endpoint size [byte]" +config TUSBD_MSC_FS_EP_SIZE + int "MSC Full-Speed endpoint size [byte]" + default 64 + +config TUSBD_MSC_HS_EP_SIZE + int "MSC High-Speed endpoint size [byte]" default 512 config TUSBD_MSC_STRING diff --git a/pkg/tinyusb/contrib/include/tinyusb_descriptors.h b/pkg/tinyusb/contrib/include/tinyusb_descriptors.h index cae43630ae04..b163200fc49c 100644 --- a/pkg/tinyusb/contrib/include/tinyusb_descriptors.h +++ b/pkg/tinyusb/contrib/include/tinyusb_descriptors.h @@ -118,22 +118,19 @@ enum { #endif /* !defined(HAVE_TUSBD_STR_IDX_TYPE) */ #if !defined(TUSBD_DESC_TOTAL_LEN) -#define TUSBD_DESC_CDC_0_LEN ((CONFIG_TUSBD_CDC_NUMOF > 0) ? TUD_CDC_DESC_LEN : 0) -#define TUSBD_DESC_CDC_1_LEN ((CONFIG_TUSBD_CDC_NUMOF > 1) ? TUD_CDC_DESC_LEN : 0) -#define TUSBD_DESC_HID_0_LEN ((CONFIG_TUSBD_HID_NUMOF > 0) ? TUD_HID_INOUT_DESC_LEN : 0) -#define TUSBD_DESC_HID_1_LEN ((CONFIG_TUSBD_HID_NUMOF > 1) ? TUD_HID_INOUT_DESC_LEN : 0) -#define TUSBD_DESC_MSC_LEN ((CONFIG_TUSBD_MSC_NUMOF > 0) ? TUD_MSC_DESC_LEN : 0) -#define TUSBD_DESC_VENDOR_LEN ((CONFIG_TUSBD_VENDOR_NUMOF > 0) ? TUD_VENDOR_DESC_LEN : 0) - #define TUSBD_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \ - TUSBD_DESC_CDC_0_LEN + \ - TUSBD_DESC_CDC_1_LEN + \ - TUSBD_DESC_HID_0_LEN + \ - TUSBD_DESC_HID_1_LEN + \ - TUSBD_DESC_MSC_LEN + \ - TUSBD_DESC_VENDOR_LEN) + (CONFIG_TUSBD_CDC_NUMOF * TUD_CDC_DESC_LEN) + \ + (CONFIG_TUSBD_HID_NUMOF * TUD_HID_INOUT_DESC_LEN) + \ + (CONFIG_TUSBD_MSC_NUMOF * TUD_MSC_DESC_LEN) + \ + (CONFIG_TUSBD_VENDOR_NUMOF * TUD_VENDOR_DESC_LEN)) #endif /* !defined(TUSBD_DESC_TOTAL_LEN) */ +#define TUSBD_DESC_ALT_TOTAL_LEN (TUD_CONFIG_DESC_LEN + \ + (CONFIG_TUSBD_CDC_NUMOF * TUD_CDC_DESC_LEN) + \ + (CONFIG_TUSBD_HID_NUMOF * TUD_HID_INOUT_DESC_LEN) + \ + (CONFIG_TUSBD_MSC_NUMOF * TUD_MSC_DESC_LEN) + \ + (CONFIG_TUSBD_VENDOR_NUMOF * TUD_VENDOR_DESC_LEN)) + #endif /* !defined(CONFIG_TUSBD_USE_CUSTOM_DESC) */ #ifdef __cplusplus diff --git a/pkg/tinyusb/contrib/include/tusb_config.h b/pkg/tinyusb/contrib/include/tusb_config.h index f090b43113a8..df5b44dd5ce4 100644 --- a/pkg/tinyusb/contrib/include/tusb_config.h +++ b/pkg/tinyusb/contrib/include/tusb_config.h @@ -186,28 +186,68 @@ #endif #endif +#ifndef CONFIG_TUSBD_CDC_NOTIF_EP_SIZE +#define CONFIG_TUSBD_CDC_NOTIF_EP_SIZE 8 +#endif + #ifndef CONFIG_TUSBD_EP0_SIZE -#define CONFIG_TUSBD_EP0_SIZE 64 +#define CONFIG_TUSBD_EP0_SIZE 64 +#endif + +#ifndef CONFIG_TUSBD_FS_EP_SIZE +#define CONFIG_TUSBD_FS_EP_SIZE 64 +#endif + +#ifndef CONFIG_TUSBD_HS_EP_SIZE +#define CONFIG_TUSBD_HS_EP_SIZE 512 +#endif + +#ifndef CONFIG_TUSBD_CDC_FS_EP_SIZE +#define CONFIG_TUSBD_CDC_FS_EP_SIZE CONFIG_TUSBD_FS_EP_SIZE #endif -#ifndef CONFIG_TUSBD_MSC_EP_SIZE -#define CONFIG_TUSBD_MSC_EP_SIZE 512 +#ifndef CONFIG_TUSBD_CDC_HS_EP_SIZE +#define CONFIG_TUSBD_CDC_HS_EP_SIZE CONFIG_TUSBD_HS_EP_SIZE #endif #ifndef CONFIG_TUSBD_HID_EP_SIZE -#define CONFIG_TUSBD_HID_EP_SIZE 64 +#define CONFIG_TUSBD_HID_EP_SIZE CONFIG_TUSBD_FS_EP_SIZE +#endif + +#ifndef CONFIG_TUSBD_HID_0_POLL_INTERVALL +#define CONFIG_TUSBD_HID_0_POLL_INTERVALL 10 +#endif + +#ifndef CONFIG_TUSBD_HID_1_POLL_INTERVALL +#define CONFIG_TUSBD_HID_1_POLL_INTERVALL 10 +#endif + +#ifndef CONFIG_TUSBD_MSC_FS_EP_SIZE +#define CONFIG_TUSBD_MSC_FS_EP_SIZE CONFIG_TUSBD_FS_EP_SIZE +#endif + +#ifndef CONFIG_TUSBD_MSC_HS_EP_SIZE +#define CONFIG_TUSBD_MSC_HS_EP_SIZE CONFIG_TUSBD_HS_EP_SIZE +#endif + +#ifndef CONFIG_TUSBD_VENDOR_FS_EP_SIZE +#define CONFIG_TUSBD_VENDOR_FS_EP_SIZE CONFIG_TUSBD_FS_EP_SIZE +#endif + +#ifndef CONFIG_TUSBD_VENDOR_HS_EP_SIZE +#define CONFIG_TUSBD_VENDOR_HS_EP_SIZE CONFIG_TUSBD_HS_EP_SIZE #endif #ifndef CONFIG_TUSBH_ENUM_SIZE -#define CONFIG_TUSBH_ENUM_SIZE 256 +#define CONFIG_TUSBH_ENUM_SIZE 256 #endif #ifndef CONFIG_TUSBH_HID_EPIN_SIZE -#define CONFIG_TUSBH_HID_EPIN_SIZE 64 +#define CONFIG_TUSBH_HID_EPIN_SIZE CONFIG_TUSBD_HID_EP_SIZE #endif #ifndef CONFIG_TUSBH_HID_EPOUT_SIZE -#define CONFIG_TUSBH_HID_EPOUT_SIZE 64 +#define CONFIG_TUSBH_HID_EPOUT_SIZE CONFIG_TUSBD_HID_EP_SIZE #endif /** @@ -223,9 +263,7 @@ #define CFG_TUSB_OS OPT_OS_CUSTOM /** Debug log level */ -#ifndef CFG_TUSB_DEBUG #define CFG_TUSB_DEBUG 0 -#endif /** * @brief DMA memory section and alignment @@ -235,13 +273,8 @@ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) */ -#ifndef CFG_TUSB_MEM_SECTION #define CFG_TUSB_MEM_SECTION -#endif - -#ifndef CFG_TUSB_MEM_ALIGN #define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) -#endif /** @} */ @@ -251,61 +284,20 @@ */ #define CFG_TUD_ENABLED MODULE_TINYUSB_DEVICE -#ifndef CFG_TUD_ENDPOINT0_SIZE #define CFG_TUD_ENDPOINT0_SIZE CONFIG_TUSBD_EP0_SIZE -#endif -#ifndef CFG_TUD_AUDIO #define CFG_TUD_AUDIO CONFIG_TUSBD_AUDIO_NUMOF -#endif - -#ifndef CFG_TUD_BTH #define CFG_TUD_BTH CONFIG_TUSBD_BTH_NUMOF -#endif - -#ifndef CFG_TUD_CDC #define CFG_TUD_CDC CONFIG_TUSBD_CDC_NUMOF -#endif - -#ifndef CFG_TUD_DFU #define CFG_TUD_DFU CONFIG_TUSBD_DFU_NUMOF -#endif - -#ifndef CFG_TUD_DFU_RUNTIME #define CFG_TUD_DFU_RUNTIME CONFIG_TUSBD_DFU_RT_NUMOF -#endif - -#ifndef CFG_TUD_HID #define CFG_TUD_HID CONFIG_TUSBD_HID_NUMOF -#endif - -#ifndef CFG_TUD_MIDI #define CFG_TUD_MIDI CONFIG_TUSBD_MIDI_NUMOF -#endif - -#ifndef CFG_TUD_MSC #define CFG_TUD_MSC CONFIG_TUSBD_MSC_NUMOF -#endif - -#ifndef CFG_TUD_ECM_RNDIS -#define CFG_TUD_ECM_RNDIS CONFIG_TUSBD_ECM_NUMOF -#endif - -#ifndef CFG_TUD_NCM -#define CFG_TUD_NCM CONFIG_TUSBD_NCM_NUMOF -#endif - -#ifndef CFG_TUD_USBTMC +#define CFG_TUD_NCM CONFIG_TUSBD_NET_CDC_NCM #define CFG_TUD_USBTMC CONFIG_TUSBD_USBTMC_NUMOF -#endif - -#ifndef CFG_TUD_VENDOR #define CFG_TUD_VENDOR CONFIG_TUSBD_VENDOR_NUMOF -#endif - -#ifndef CFG_TUD_VIDEO #define CFG_TUD_VIDEO CONFIG_TUSBD_VIDEO_NUMOF -#endif /** @} */ @@ -315,38 +307,18 @@ */ #define CFG_TUH_ENABLED MODULE_TINYUSB_HOST -#ifndef CFG_TUH_MAX_SPEED #define CFG_TUH_MAX_SPEED OPT_MODE_DEFAULT_SPEED -#endif -#ifndef CFG_TUH_ENUMERATION_BUFSIZE #define CFG_TUH_ENUMERATION_BUFSIZE CONFIG_TUSBH_ENUM_SIZE -#endif /** Hub typically has 4 ports */ -#ifndef CFG_TUH_DEVICE_MAX #define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) -#endif -#ifndef CFG_TUH_CDC #define CFG_TUH_CDC CONFIG_TUSBH_CDC_NUMOF -#endif - -#ifndef CFG_TUH_HID #define CFG_TUH_HID CONFIG_TUSBH_HID_NUMOF -#endif - -#ifndef CFG_TUH_HUB #define CFG_TUH_HUB CONFIG_TUSBH_HUB_NUMOF -#endif - -#ifndef CFG_TUH_MSC #define CFG_TUH_MSC CONFIG_TUSBH_MSC_NUMOF -#endif - -#ifndef CFG_TUD_VENDOR #define CFG_TUH_VENDOR CONFIG_TUSBH_VENDOR_NUMOF -#endif /** @} */ @@ -354,15 +326,10 @@ * @name Typical required CDC device class configurations * @{ */ -/** CDC RX FIFO size */ -#ifndef CFG_TUD_CDC_RX_BUFSIZE +#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? CONFIG_TUSBD_CDC_HS_EP_SIZE \ + : CONFIG_TUSBD_CDC_FS_EP_SIZE) #define CFG_TUD_CDC_RX_BUFSIZE CFG_TUD_CDC_EP_BUFSIZE -#endif - -/** CDC RX FIFO size */ -#ifndef CFG_TUD_CDC_TX_BUFSIZE #define CFG_TUD_CDC_TX_BUFSIZE CFG_TUD_CDC_EP_BUFSIZE -#endif /** @} */ @@ -370,53 +337,39 @@ * @name Typical required DFU device class configurations * @{ */ -#ifndef CFG_TUD_DFU_XFER_BUFSIZE -#define CFG_TUD_DFU_XFER_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) -#endif +#define CFG_TUD_DFU_XFER_BUFSIZE (TUD_OPT_HIGH_SPEED ? CONFIG_TUSBD_HS_EP_SIZE \ + : CONFIG_TUSBD_FS_EP_SIZE) /** @} */ /** * @name Typical required HID device class configurations * @{ */ -#ifndef CFG_TUD_HID_EP_BUFSIZE #define CFG_TUD_HID_EP_BUFSIZE CONFIG_TUSBD_HID_EP_SIZE -#endif /** @} */ /** * @name Typical required MIDI device class configurations * @{ */ -#ifndef CFG_TUD_MIDI_RX_BUFSIZE #define CFG_TUD_MIDI_RX_BUFSIZE CFG_TUD_MIDI_EP_BUFSIZE -#endif - -#ifndef CFG_TUD_MIDI_TX_BUFSIZE #define CFG_TUD_MIDI_TX_BUFSIZE CFG_TUD_MIDI_EP_BUFSIZE -#endif /** @} */ /** * @name Typical required MSC device class configurations * @{ */ -#ifndef CFG_TUD_MSC_EP_BUFSIZE -#define CFG_TUD_MSC_EP_BUFSIZE CONFIG_TUSBD_MSC_EP_SIZE -#endif +#define CFG_TUD_MSC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? CONFIG_TUSBD_MSC_HS_EP_SIZE \ + : CONFIG_TUSBD_MSC_FS_EP_SIZE) /** @} */ /** * @name Typical required HID host class configurations * @{ */ -#ifndef CFG_TUH_HID_EPIN_BUFSIZE #define CFG_TUH_HID_EPIN_BUFSIZE CONFIG_TUSBH_HID_EPIN_SIZE -#endif - -#ifndef CFG_TUH_HID_EPOUT_BUFSIZE #define CFG_TUH_HID_EPOUT_BUFSIZE CONFIG_TUSBH_HID_EPOUT_SIZE -#endif /** @} */ #ifdef __cplusplus diff --git a/pkg/tinyusb/contrib/tinyusb_descriptors.c b/pkg/tinyusb/contrib/tinyusb_descriptors.c index d35c5471da09..41eb72d3c678 100644 --- a/pkg/tinyusb/contrib/tinyusb_descriptors.c +++ b/pkg/tinyusb/contrib/tinyusb_descriptors.c @@ -30,6 +30,9 @@ #include "tinyusb_descriptors.h" +#define ENABLE_DEBUG 0 +#include "debug.h" + /* don't compile this part if CONFIG_TUSBD_USE_CUSTOM_DESC is set */ #if !defined(CONFIG_TUSBD_USE_CUSTOM_DESC) @@ -49,6 +52,15 @@ of device class interfaces. Custom descriptors have to be implemented. #endif +#define _TUD_CONFIG_DESC_NUMOF 1 + +enum { + _TUD_CONFIG_DESC_ID = 0, +#if _TUD_CONFIG_DESC_NUMOF == 2 + _TUD_CONFIG_DESC_ALT_ID = 1, +#endif +}; + /* * --------------------------------------------------------------------+ * Device Descriptors @@ -81,7 +93,7 @@ tusb_desc_device_t const tusb_desc_device = { .iProduct = TUSBD_STR_IDX_PRODUCT, .iSerialNumber = TUSBD_STR_IDX_SERIAL, - .bNumConfigurations = 0x01 + .bNumConfigurations = _TUD_CONFIG_DESC_NUMOF }; /* @@ -184,26 +196,6 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, *--------------------------------------------------------------------+ */ -#ifndef CONFIG_TUSBD_FS_EP_SIZE -#define CONFIG_TUSBD_FS_EP_SIZE 64 -#endif - -#ifndef CONFIG_TUSBD_HS_EP_SIZE -#define CONFIG_TUSBD_HS_EP_SIZE 512 -#endif - -#ifndef CONFIG_TUSBD_CDC_NOTIF_EP_SIZE -#define CONFIG_TUSBD_CDC_NOTIF_EP_SIZE 8 -#endif - -#ifndef CONFIG_TUSBD_HID_0_POLL_INTERVALL -#define CONFIG_TUSBD_HID_0_POLL_INTERVALL 10 -#endif - -#ifndef CONFIG_TUSBD_HID_1_POLL_INTERVALL -#define CONFIG_TUSBD_HID_1_POLL_INTERVALL 10 -#endif - #if CONFIG_USB_SELF_POWERED && CONFIG_USB_REM_WAKEUP #define DESC_DEV_ATTR (USB_CONF_ATTR_SELF_POWERED || USB_CONF_ATTR_REM_WAKEUP) #elif CONFIG_USB_SELF_POWERED @@ -214,62 +206,96 @@ void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, #define DESC_DEV_ATTR (0) #endif +#define _tusb_speed_fs 0 +#define _tusb_speed_hs 1 + +#define _TUD_CONFIG_DESC(id, len) \ + /* Config number, interface count, string index, total length, attribute, + * power in mA */ \ + TUD_CONFIG_DESCRIPTOR(id + 1, TUSBD_ITF_NUMOF, 0, len, \ + DESC_DEV_ATTR, CONFIG_USB_MAX_POWER) + +#define _TUD_CDC_DESC(speed, n) \ + /* Interface number, string index, EP notification address and size, + * EP Data Out & In, EP size. */ \ + TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_##n, TUSBD_STR_IDX_CDC_##n, \ + TUSBD_EP_CDC_##n##_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE, \ + TUSBD_EP_CDC_##n##_OUT, TUSBD_EP_CDC_##n##_IN, \ + speed ? CONFIG_TUSBD_CDC_HS_EP_SIZE \ + : CONFIG_TUSBD_CDC_FS_EP_SIZE) + +#define _TUD_HID_INOUT_DESC(speed, n) \ + /* Interface number, string index, protocol, report descriptor len, + * EP Out & EP In address, EP size, polling interval */ \ + TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_##n, TUSBD_STR_IDX_HID_##n, \ + HID_ITF_PROTOCOL_NONE, \ + sizeof(tusb_desc_hid_##n##_report), \ + TUSBD_EP_HID_##n##_OUT, TUSBD_EP_HID_##n##_IN, \ + CONFIG_TUSBD_HID_EP_SIZE, \ + CONFIG_TUSBD_HID_##n##_POLL_INTERVALL) + +#define _TUD_MSC_DESC(speed) \ + /* Interface number, string index, EP Out & In address, EP size */ \ + TUD_MSC_DESCRIPTOR(TUSBD_ITF_MSC, TUSBD_STR_IDX_MSC, \ + TUSBD_EP_MSC_OUT, TUSBD_EP_MSC_IN, \ + speed ? CONFIG_TUSBD_MSC_HS_EP_SIZE \ + : CONFIG_TUSBD_MSC_FS_EP_SIZE) + +#define _TUD_VENDOR_DESC(speed) \ + /* Interface number, string index, EP Out & In address, EP size */ \ + TUD_VENDOR_DESCRIPTOR(TUSBD_ITF_VENDOR, TUSBD_STR_IDX_VENDOR, \ + TUSBD_EP_VENDOR_OUT, TUSBD_EP_VENDOR_IN, \ + speed ? CONFIG_TUSBD_VENDOR_HS_EP_SIZE \ + : CONFIG_TUSBD_VENDOR_FS_EP_SIZE) + /* FS configuration */ __attribute__((weak)) uint8_t const tusb_desc_fs_config[] = { - /* Config number, interface count, string index, total length, attribute, - * power in mA */ - TUD_CONFIG_DESCRIPTOR(1, TUSBD_ITF_NUMOF, 0, TUSBD_DESC_TOTAL_LEN, - DESC_DEV_ATTR, CONFIG_USB_MAX_POWER), + _TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ID, TUSBD_DESC_TOTAL_LEN), #if CONFIG_TUSBD_CDC_NUMOF > 0 - /* Interface number, string index, EP notification address and size, - * EP Data Out & In, EP size. */ - TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_0, TUSBD_STR_IDX_CDC_0, - TUSBD_EP_CDC_0_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE, - TUSBD_EP_CDC_0_OUT, TUSBD_EP_CDC_0_IN, - CONFIG_TUSBD_FS_EP_SIZE), + _TUD_CDC_DESC(_tusb_speed_fs, 0), #endif #if CONFIG_TUSBD_CDC_NUMOF > 1 - /* Interface number, string index, EP notification address and size, - * EP Data Out & In, EP size. */ - TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_1, TUSBD_STR_IDX_CDC_1, - TUSBD_EP_CDC_1_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE, - TUSBD_EP_CDC_1_OUT, TUSBD_EP_CDC_1_IN, - CONFIG_TUSBD_FS_EP_SIZE), + _TUD_CDC_DESC(_tusb_speed_fs, 1), #endif #if CONFIG_TUSBD_HID_NUMOF > 0 - /* Interface number, string index, protocol, report descriptor len, - * EP Out & EP In address, EP size, polling interval */ - TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_0, TUSBD_STR_IDX_HID_0, - HID_TUSBD_ITF_PROTOCOL_NONE, - sizeof(tusb_desc_hid_0_report), - TUSBD_EP_HID_0_OUT, TUSBD_EP_HID_0_IN, - CONFIG_TUSBD_HID_EP_SIZE, - CONFIG_TUSBD_HID_0_POLL_INTERVALL), + _TUD_HID_INOUT_DESC(_tusb_speed_fs, 0), #endif #if CONFIG_TUSBD_HID_NUMOF > 1 - /* Interface number, string index, protocol, report descriptor len, - * EP Out & EP In address, EP size, polling interval */ - TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_1, TUSBD_STR_IDX_HID_1, - HID_TUSBD_ITF_PROTOCOL_NONE, - sizeof(tusb_desc_hid_1_report), - TUSBD_EP_HID_1_OUT, TUSBD_EP_HID_1_IN, - CONFIG_TUSBD_HID_EP_SIZE, - CONFIG_TUSBD_HID_1_POLL_INTERVALL), + _TUD_HID_INOUT_DESC(_tusb_speed_fs, 1), +#endif +#if CONFIG_TUSBD_MSC_NUMOF + _TUD_MSC_DESC(_tusb_speed_fs), +#endif +#if CONFIG_TUSBD_VENDOR_NUMOF + _TUD_VENDOR_DESC(_tusb_speed_fs), +#endif +}; + +#if _TUD_CONFIG_DESC_NUMOF == 2 +__attribute__((weak)) +uint8_t const tusb_desc_fs_config_alt[] = { + _TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ALT_ID, TUSBD_DESC_ALT_TOTAL_LEN), +#if CONFIG_TUSBD_CDC_NUMOF > 0 + _TUD_CDC_DESC(_tusb_speed_fs, 0), +#endif +#if CONFIG_TUSBD_CDC_NUMOF > 1 + _TUD_CDC_DESC(_tusb_speed_fs, 1), +#endif +#if CONFIG_TUSBD_HID_NUMOF > 0 + _TUD_HID_INOUT_DESC(_tusb_speed_fs, 0), +#endif +#if CONFIG_TUSBD_HID_NUMOF > 1 + _TUD_HID_INOUT_DESC(_tusb_speed_fs, 1), #endif #if CONFIG_TUSBD_MSC_NUMOF - /* Interface number, string index, EP Out & In address, EP size */ - TUD_MSC_DESCRIPTOR(TUSBD_ITF_MSC, TUSBD_STR_IDX_MSC, - TUSBD_EP_MSC_OUT, TUSBD_EP_MSC_IN, - CONFIG_TUSBD_FS_EP_SIZE), + _TUD_MSC_DESC(_tusb_speed_fs), #endif #if CONFIG_TUSBD_VENDOR_NUMOF - /* Interface number, string index, EP Out & In address, EP size */ - TUD_VENDOR_DESCRIPTOR(TUSBD_ITF_VENDOR, TUSBD_STR_IDX_VENDOR, - TUSBD_EP_VENDOR_OUT, TUSBD_EP_VENDOR_IN, - CONFIG_TUSBD_FS_EP_SIZE), + _TUD_VENDOR_DESC(_tusb_speed_fs), #endif }; +#endif /* _TUD_CONFIG_DESC_NUMOF == 2 */ #if TUD_OPT_HIGH_SPEED /* Per USB specs: high speed capable device must report device_qualifier @@ -278,60 +304,52 @@ uint8_t const tusb_desc_fs_config[] = { /* HS configuration */ __attribute__((weak)) uint8_t const tusb_desc_hs_config[] = { - /* Config number, interface count, string index, total length, attribute, - * power in mA */ - TUD_CONFIG_DESCRIPTOR(1, TUSBD_ITF_NUMOF, 0, TUSBD_DESC_TOTAL_LEN, - DESC_DEV_ATTR, CONFIG_USB_MAX_POWER), + _TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ID, TUSBD_DESC_TOTAL_LEN), #if CONFIG_TUSBD_CDC_NUMOF > 0 - /* Interface number, string index, EP notification address and size, - * EP Data Out & In, EP size. */ - TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_0, TUSBD_STR_IDX_CDC_0, - TUSBD_EP_CDC_0_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE, - TUSBD_EP_CDC_0_OUT, TUSBD_EP_CDC_0_IN, - CONFIG_TUSBD_HS_EP_SIZE), + _TUD_CDC_DESC(_tusb_speed_hs, 0), #endif #if CONFIG_TUSBD_CDC_NUMOF > 1 - /* Interface number, string index, EP notification address and size, - * EP Data Out & In, EP size. */ - TUD_CDC_DESCRIPTOR(TUSBD_ITF_CDC_1, TUSBD_STR_IDX_CDC_1, - TUSBD_EP_CDC_1_NOTIF, CONFIG_TUSBD_CDC_NOTIF_EP_SIZE, - TUSBD_EP_CDC_1_OUT, TUSBD_EP_CDC_1_IN, - CONFIG_TUSBD_HS_EP_SIZE), + _TUD_CDC_DESC(_tusb_speed_hs, 1), #endif #if CONFIG_TUSBD_HID_NUMOF > 0 - /* Interface number, string index, protocol, report descriptor len, - * EP Out & EP In address, EP size, polling interval */ - TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_0, TUSBD_STR_IDX_HID_0, - HID_TUSBD_ITF_PROTOCOL_NONE, - sizeof(tusb_desc_hid_0_report), - TUSBD_EP_HID_0_OUT, TUSBD_EP_HID_0_IN, - CONFIG_TUSBD_HID_EP_SIZE, - CONFIG_TUSBD_HID_0_POLL_INTERVALL), + _TUD_HID_INOUT_DESC(_tusb_speed_hs, 0), #endif #if CONFIG_TUSBD_HID_NUMOF > 1 - /* Interface number, string index, protocol, report descriptor len, - * EP Out & EP In address, EP size, polling interval */ - TUD_HID_INOUT_DESCRIPTOR(TUSBD_ITF_HID_1, TUSBD_STR_IDX_HID_1, - HID_TUSBD_ITF_PROTOCOL_NONE, - sizeof(tusb_desc_hid_1_report), - TUSBD_EP_HID_1_OUT, TUSBD_EP_HID_1_IN, - CONFIG_TUSBD_HID_EP_SIZE, - CONFIG_TUSBD_HID_1_POLL_INTERVALL), + _TUD_HID_INOUT_DESC(_tusb_speed_hs, 1), #endif #if CONFIG_TUSBD_MSC_NUMOF - /* Interface number, string index, EP Out & In address, EP size */ - TUD_MSC_DESCRIPTOR(TUSBD_ITF_MSC, TUSBD_STR_IDX_MSC, - TUSBD_EP_MSC_OUT, TUSBD_EP_MSC_IN, - CONFIG_TUSBD_HS_EP_SIZE), + _TUD_MSC_DESC(_tusb_speed_hs), #endif #if CONFIG_TUSBD_VENDOR_NUMOF - /* Interface number, string index, EP Out & In address, EP size */ - TUD_VENDOR_DESCRIPTOR(TUSBD_ITF_VENDOR, TUSBD_STR_IDX_VENDOR, - TUSBD_EP_VENDOR_OUT, TUSBD_EP_VENDOR_IN, - CONFIG_TUSBD_HS_EP_SIZE), + _TUD_VENDOR_DESC(_tusb_speed_hs), #endif }; +#if _TUD_CONFIG_DESC_NUMOF == 2 +__attribute__((weak)) +uint8_t const tusb_desc_hs_config_alt[] = { + _TUD_CONFIG_DESC(_TUD_CONFIG_DESC_ALT_ID, TUSBD_DESC_ALT_TOTAL_LEN), +#if CONFIG_TUSBD_CDC_NUMOF > 0 + _TUD_CDC_DESC(_tusb_speed_hs, 0), +#endif +#if CONFIG_TUSBD_CDC_NUMOF > 1 + _TUD_CDC_DESC(_tusb_speed_hs, 1), +#endif +#if CONFIG_TUSBD_HID_NUMOF > 0 + _TUD_HID_INOUT_DESC(_tusb_speed_hs, 0), +#endif +#if CONFIG_TUSBD_HID_NUMOF > 1 + _TUD_HID_INOUT_DESC(_tusb_speed_hs, 1), +#endif +#if CONFIG_TUSBD_MSC_NUMOF + _TUD_MSC_DESC(_tusb_speed_hs), +#endif +#if CONFIG_TUSBD_VENDOR_NUMOF + _TUD_VENDOR_DESC(_tusb_speed_hs), +#endif +}; +#endif /* _TUD_CONFIG_DESC_NUMOF == 2 */ + /* other speed configuration */ uint8_t tusb_desc_other_speed_config[TUSBD_DESC_TOTAL_LEN]; @@ -357,7 +375,7 @@ tusb_desc_device_qualifier_t const tusb_desc_device_qualifier = { #endif .bMaxPacketSize0 = CONFIG_TUSBD_EP0_SIZE, - .bNumConfigurations = 0x01, + .bNumConfigurations = _TUD_CONFIG_DESC_NUMOF, .bReserved = 0x00 }; @@ -383,18 +401,37 @@ uint8_t const *tud_descriptor_device_qualifier_cb(void) __attribute__((weak)) uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) { - (void)index; /* for multiple configurations */ + DEBUG("[tinyusb] %s: %u\n", __func__, index); + + assert(index < _TUD_CONFIG_DESC_NUMOF); - /* If the link speed is HS, return the FS config, and vice versa. - * Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */ - memcpy(tusb_desc_other_speed_config, - (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config - : tusb_desc_hs_config, - TUSBD_DESC_TOTAL_LEN); + if (index == _TUD_CONFIG_DESC_ID) { + /* If the link speed is HS, return the FS config, and vice versa. + * Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */ + memcpy(tusb_desc_other_speed_config, + (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config + : tusb_desc_hs_config, + TUSBD_DESC_TOTAL_LEN); - tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; + tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; - return tusb_desc_other_speed_config; + return tusb_desc_other_speed_config; + } +#if _TUD_CONFIG_DESC_NUMOF == 2 + else if (index == _TUD_CONFIG_DESC_ALT_ID) { + /* If the link speed is HS, return the FS config, and vice versa. + * Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG */ + memcpy(tusb_desc_other_speed_config, + (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_fs_config_alt + : tusb_desc_hs_config_alt, + TUSBD_DESC_ALT_TOTAL_LEN); + + tusb_desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG; + + return tusb_desc_other_speed_config; + } +#endif + return NULL; } #endif /* TUD_OPT_HIGH_SPEED */ @@ -407,15 +444,33 @@ uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) __attribute__((weak)) uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { - (void)index; /* for multiple configurations */ + DEBUG("[tinyusb] %s: %u\n", __func__, index); + + assert(index < _TUD_CONFIG_DESC_NUMOF); + if (index == _TUD_CONFIG_DESC_ID) { #if TUD_OPT_HIGH_SPEED - /* Although we are HS, host may be FS. */ - return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config - : tusb_desc_fs_config; -#else - return (uint8_t const *)tusb_desc_fs_config; -#endif + /* Although we are HS, host may be FS. */ + return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config + : tusb_desc_fs_config; +#else /* TUD_OPT_HIGH_SPEED */ + return (uint8_t const *)tusb_desc_fs_config; +#endif /* TUD_OPT_HIGH_SPEED */ + } + +#if _TUD_CONFIG_DESC_NUMOF == 2 + else if (index == _TUD_CONFIG_DESC_ALT_ID) { +#if TUD_OPT_HIGH_SPEED + /* Although we are HS, host may be FS. */ + return (tud_speed_get() == TUSB_SPEED_HIGH) ? tusb_desc_hs_config_alt + : tusb_desc_fs_config_alt; +#else /* TUD_OPT_HIGH_SPEED */ + return (uint8_t const *)tusb_desc_fs_config_alt; +#endif /* TUD_OPT_HIGH_SPEED */ + } +#endif /* _TUD_CONFIG_DESC_NUMOF == 2 */ + + return NULL; } /* @@ -456,28 +511,28 @@ char const* tusb_string_desc_array[] = { }, CONFIG_USB_MANUF_STR, /* 1: Manufacturer */ CONFIG_USB_PRODUCT_STR, /* 2: Product */ -#if CONFIG_USB_SERIAL_STR +#if CONFIG_USB_CUSTOM_SERIAL_STR CONFIG_USB_SERIAL_STR, /* 3: Serial number as configured */ #else NULL, /* 3: Serial number generated during runtime */ #endif #if CONFIG_TUSBD_CDC_NUMOF > 0 - CONFIG_TUSBD_CDC_0_STRING, /* CDC Interface 0 */ + CONFIG_TUSBD_CDC_0_STRING, /* CDC Interface 0 */ #endif #if CONFIG_TUSBD_CDC_NUMOF > 1 - CONFIG_TUSBD_CDC_1_STRING, /* CDC Interface 1 */ + CONFIG_TUSBD_CDC_1_STRING, /* CDC Interface 1 */ #endif #if CONFIG_TUSBD_HID_NUMOF > 0 - CONFIG_TUSBD_HID_0_STRING, /* HID Interface 0 */ + CONFIG_TUSBD_HID_0_STRING, /* HID Interface 0 */ #endif #if CONFIG_TUSBD_HID_NUMOF > 1 - CONFIG_TUSBD_HID_1_STRING, /* HID Interface 1 */ + CONFIG_TUSBD_HID_1_STRING, /* HID Interface 1 */ #endif #if CONFIG_TUSBD_MSC_NUMOF - CONFIG_TUSBD_MSC_STRING, /* MSC Interface */ + CONFIG_TUSBD_MSC_STRING, /* MSC Interface */ #endif #if CONFIG_TUSBD_VENDOR_NUMOF - CONFIG_TUSBD_VENDOR_STRING, /* Vendor Interface */ + CONFIG_TUSBD_VENDOR_STRING, /* Vendor Interface */ #endif }; @@ -506,27 +561,26 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) const char* str; -#ifdef CONFIG_USB_SERIAL_STR - /* if serial string is configured, each member in tusb_string_desc_array can be used */ - str = tusb_string_desc_array[index]; -#else - /* otherwise, a generated string has to be used in case of serial string index */ - static char _serial_str[(CONFIG_USB_SERIAL_BYTE_LENGTH << 1) + 1]; - static_assert(CONFIG_USB_SERIAL_BYTE_LENGTH <= UINT8_MAX/4, - "USB serial byte length must be at most 63 due to protocol " - "limitations"); - if ((index == TUSBD_STR_IDX_SERIAL) && (strlen(_serial_str) == 0)) { - /* generate the serial string if it is not yet generated */ - uint8_t luid_buf[CONFIG_USB_SERIAL_BYTE_LENGTH]; - luid_get(luid_buf, sizeof(luid_buf)); - fmt_bytes_hex(_serial_str, luid_buf, sizeof(luid_buf)); - _serial_str[(CONFIG_USB_SERIAL_BYTE_LENGTH << 1)] = 0; + if ((index == TUSBD_STR_IDX_SERIAL) && + (tusb_string_desc_array[index] == NULL)) { + /* otherwise, a generated string has to be used in case of serial string index */ + static char _serial_str[(CONFIG_USB_SERIAL_BYTE_LENGTH << 1) + 1] = { }; + static_assert(CONFIG_USB_SERIAL_BYTE_LENGTH <= UINT8_MAX/4, + "USB serial byte length must be at most 63 due to protocol " + "limitations"); + if ((index == TUSBD_STR_IDX_SERIAL) && (strlen(_serial_str) == 0)) { + /* generate the serial string if it is not yet generated */ + uint8_t luid_buf[CONFIG_USB_SERIAL_BYTE_LENGTH]; + luid_get(luid_buf, sizeof(luid_buf)); + fmt_bytes_hex(_serial_str, luid_buf, sizeof(luid_buf)); + _serial_str[(CONFIG_USB_SERIAL_BYTE_LENGTH << 1)] = 0; + } str = _serial_str; } else { str = tusb_string_desc_array[index]; } -#endif + /* cap at max char */ chr_count = (uint8_t)strlen(str); if (chr_count > 31) { @@ -540,7 +594,7 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) } /* first byte is length (including header), second byte is string type */ - _desc_str[0] = (uint16_t)((TUSB_DESC_STRING << 8 ) | (2*chr_count + 2)); + _desc_str[0] = (uint16_t)((TUSB_DESC_STRING << 8 ) | (2 * chr_count + 2)); return _desc_str; }