@@ -31,8 +31,6 @@ extern "C" {
3131#include  " r_usb_basic_api.h" 
3232#include  " r_usb_pcdc_api.h" 
3333
34- #define  USBD_ITF_CDC  (0 ) //  needs 2 interfaces
35- 
3634#ifndef  USBD_CDC_EP_CMD
3735#define  USBD_CDC_EP_CMD   (0x81 )
3836#endif 
@@ -121,45 +119,73 @@ const uint8_t *tud_descriptor_configuration_cb(uint8_t index) {
121119void  __SetupUSBDescriptor () {
122120    if  (!usbd_desc_cfg) {
123121
124-         uint8_t  interface_count = (__USBInstallSerial ? 3  : 0 ) + (__USBGetHIDReport ? 1  : 0 ) + (__USBInstallMSD ? 1  : 0 );
122+         //  This tracks the next (0 based) interface number to assign, incremented after each
123+         //  interface descriptor is created. After all interface descriptors have been created,
124+         //  it represents the (1 based) number of interface descriptors that have been defined.
125+         uint8_t  interface_count = 0 ;
125126
126-         uint8_t  cdc_desc[TUD_CDC_DESC_LEN + TUD_DFU_RT_DESC_LEN] = {
127+         /* 
128+          * -----    CDC 
129+          */  
130+         bool  install_CDC = __USBInstallSerial;
131+         uint8_t  cdc_desc[TUD_CDC_DESC_LEN] = {
127132            //  Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
128-             TUD_CDC_DESCRIPTOR (USBD_ITF_CDC, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE),
129-             TUD_DFU_RT_DESCRIPTOR (USBD_ITF_CDC+2 , USBD_STR_DFU_RT, 0x0d , 1000 , 4096 ),
133+             TUD_CDC_DESCRIPTOR (interface_count, USBD_STR_CDC, USBD_CDC_EP_CMD, USBD_CDC_CMD_MAX_SIZE, USBD_CDC_EP_OUT, USBD_CDC_EP_IN, USBD_CDC_IN_OUT_MAX_SIZE)
130134        };
135+         interface_count += (install_CDC ? 2  : 0 );
131136
132137        /* 
133-          * -----    HID 
134-          */   
138+          * -----    DFU 
139+          */  
140+         bool  install_DFU = false ;
141+ #if  CFG_TUD_DFU_RUNTIME
142+         install_DFU = __USBInstallSerial;
143+         uint8_t  dfu_desc[TUD_DFU_RT_DESC_LEN] = {
144+             //  Interface number, string index, attribute, timeout, xfer size
145+             TUD_DFU_RT_DESCRIPTOR (interface_count, USBD_STR_DFU_RT, 0x0d , 1000 , 4096 )
146+         };
147+         interface_count += (install_DFU ? 1  : 0 );
148+ #else 
149+         uint8_t  dfu_desc[0 ] = {};
150+ #endif 
135151
152+         /* 
153+          * -----    HID 
154+          */  
155+         bool  install_HID = false ;
136156        size_t  hid_report_len = 0 ;
137157        if  (__USBGetHIDReport) {
158+             install_HID = true ;
138159            __USBGetHIDReport (&hid_report_len);
139160        }
140-         uint8_t  hid_itf = __USBInstallSerial ? 3  : 0 ;
141161        uint8_t  hid_desc[TUD_HID_DESC_LEN] = {
142162            //  Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
143-             TUD_HID_DESCRIPTOR (hid_itf , 0 , HID_ITF_PROTOCOL_NONE, hid_report_len, USBD_HID_EP, CFG_TUD_HID_EP_BUFSIZE, 10 )
163+             TUD_HID_DESCRIPTOR (interface_count , 0 , HID_ITF_PROTOCOL_NONE, hid_report_len, USBD_HID_EP, CFG_TUD_HID_EP_BUFSIZE, 10 )
144164        };
165+         interface_count += (install_HID ? 1 : 0 );
145166
146167        /* 
147168         * -----    MASS STORAGE DEVICE 
148-          */    
149- 
169+          */  
170+          bool  install_MSD =  false ; 
150171#if  CFG_TUD_MSC
151-         uint8_t  msd_itf = (__USBInstallSerial ?  3  :  0 ) + (__USBGetHIDReport ?  1  :  0 ) ;
172+         install_MSD = __USBInstallMSD ;
152173        uint8_t  msd_desc[TUD_MSC_DESC_LEN] = {
153174            //  Interface number, string index, EP Out & EP In address, EP size
154-             TUD_MSC_DESCRIPTOR (msd_itf , 0 , USBD_MSD_EP_OUT, USBD_MSD_EP_IN, USBD_MSD_IN_OUT_SIZE)   
175+             TUD_MSC_DESCRIPTOR (interface_count , 0 , USBD_MSD_EP_OUT, USBD_MSD_EP_IN, USBD_MSD_IN_OUT_SIZE)   
155176        };
177+         interface_count += (install_MSD ? 1  : 0 );
156178#else 
157179        uint8_t  msd_desc[0 ] = {};
158180#endif 
159181
160- 
161-         int  usbd_desc_len = TUD_CONFIG_DESC_LEN + (__USBInstallSerial ? sizeof (cdc_desc) : 0 ) + (__USBGetHIDReport ? sizeof (hid_desc) : 0 ) + (__USBInstallMSD ? sizeof (msd_desc) : 0 );
162- 
182+         //  Create configuration descriptor
183+         int  usbd_desc_len =
184+             TUD_CONFIG_DESC_LEN
185+             + (install_CDC ? sizeof (cdc_desc) : 0 )
186+             + (install_DFU ? sizeof (dfu_desc) : 0 )
187+             + (install_HID ? sizeof (hid_desc) : 0 )
188+             + (install_MSD ? sizeof (msd_desc) : 0 );
163189        uint8_t  tud_cfg_desc[TUD_CONFIG_DESC_LEN] = {
164190            //  Config number, interface count, string index, total length, attribute, power in mA
165191            TUD_CONFIG_DESCRIPTOR (1 , interface_count, USBD_STR_0, usbd_desc_len, TUSB_DESC_CONFIG_ATT_SELF_POWERED, 500 )
@@ -172,15 +198,19 @@ void __SetupUSBDescriptor() {
172198            uint8_t  *ptr = usbd_desc_cfg;
173199            memcpy (ptr, tud_cfg_desc, sizeof (tud_cfg_desc));
174200            ptr += sizeof (tud_cfg_desc);
175-             if  (__USBInstallSerial ) {
201+             if  (install_CDC ) {
176202                memcpy (ptr, cdc_desc, sizeof (cdc_desc));
177203                ptr += sizeof (cdc_desc);
178204            }
179-             if  (__USBGetHIDReport) {
205+             if  (install_DFU) {
206+                 memcpy (ptr, dfu_desc, sizeof (dfu_desc));
207+                 ptr += sizeof (dfu_desc);
208+             }
209+             if  (install_HID) {
180210                memcpy (ptr, hid_desc, sizeof (hid_desc));
181211                ptr += sizeof (hid_desc);
182212            }
183-             if  (__USBInstallMSD ) {
213+             if  (install_MSD ) {
184214                memcpy (ptr, msd_desc, sizeof (msd_desc));
185215                ptr += sizeof (msd_desc);
186216            }
0 commit comments