@@ -168,6 +168,8 @@ static tinyusb_desc_webusb_url_t tinyusb_url_descriptor = {
168168/* 
169169 * Configuration Descriptor 
170170 * */ 
171+ 
172+ static  tinyusb_descriptor_cb_t  tinyusb_loaded_interfaces_callbacks [USB_INTERFACE_MAX ];
171173static  uint32_t  tinyusb_loaded_interfaces_mask  =  0 ;
172174static  uint8_t  tinyusb_loaded_interfaces_num  =  0 ;
173175static  uint16_t  tinyusb_config_descriptor_len  =  0 ;
@@ -308,47 +310,6 @@ __attribute__ ((weak)) int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_
308310 * Private API 
309311 * */ 
310312
311- __attribute__ ((weak )) uint16_t  tusb_cdc_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
312- __attribute__ ((weak )) uint16_t  tusb_dfu_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
313- __attribute__ ((weak )) uint16_t  tusb_hid_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
314- __attribute__ ((weak )) uint16_t  tusb_msc_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
315- __attribute__ ((weak )) uint16_t  tusb_midi_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
316- __attribute__ ((weak )) uint16_t  tusb_vendor_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
317- 
318- __attribute__ ((weak )) uint16_t  tusb_custom_get_descriptor_len () { return  0 ; }
319- __attribute__ ((weak )) uint16_t  tusb_custom_load_descriptor (uint8_t  *  dst , uint8_t  *  itf ) { return  0 ; }
320- 
321- static  uint16_t  tinyusb_load_descriptor (tinyusb_interface_t  interface , uint8_t  *  dst , uint8_t  *  itf )
322- {
323-  switch  (interface ) {
324-  case  USB_INTERFACE_CDC :
325-  return  tusb_cdc_load_descriptor (dst , itf );
326-  break ;
327-  case  USB_INTERFACE_DFU :
328-  return  tusb_dfu_load_descriptor (dst , itf );
329-  break ;
330-  case  USB_INTERFACE_HID :
331-  return  tusb_hid_load_descriptor (dst , itf );
332-  break ;
333-  case  USB_INTERFACE_VENDOR :
334-  return  tusb_vendor_load_descriptor (dst , itf );
335-  break ;
336-  case  USB_INTERFACE_MSC :
337-  return  tusb_msc_load_descriptor (dst , itf );
338-  break ;
339-  case  USB_INTERFACE_MIDI :
340-  return  tusb_midi_load_descriptor (dst , itf );
341-  break ;
342-  case  USB_INTERFACE_CUSTOM :
343-  return  tusb_custom_load_descriptor (dst , itf );
344-  break ;
345-  default :
346- 
347-  break ;
348-  }
349-  return  0 ;
350- }
351- 
352313static  bool  tinyusb_reserve_in_endpoint (uint8_t  endpoint ){
353314 if (endpoint  >  6  ||  (tinyusb_endpoints .in  &  BIT (endpoint )) !=  0 ){
354315 return  false;
@@ -379,6 +340,14 @@ static bool tinyusb_has_available_fifos(void){
379340 return  active_endpoints  <  max_endpoints ;
380341}
381342
343+ static  uint16_t  tinyusb_load_descriptor (tinyusb_interface_t  interface , uint8_t  *  dst , uint8_t  *  itf )
344+ {
345+  if (tinyusb_loaded_interfaces_callbacks [interface ]){
346+  return  tinyusb_loaded_interfaces_callbacks [interface ](dst , itf );
347+  }
348+  return  0 ;
349+ }
350+ 
382351static  bool  tinyusb_load_enabled_interfaces (){
383352 tinyusb_config_descriptor_len  +=  TUD_CONFIG_DESC_LEN ;
384353 tinyusb_config_descriptor  =  (uint8_t  * )malloc (tinyusb_config_descriptor_len );
@@ -411,7 +380,7 @@ static bool tinyusb_load_enabled_interfaces(){
411380 TUD_CONFIG_DESCRIPTOR (1 , tinyusb_loaded_interfaces_num , str_index , tinyusb_config_descriptor_len , USB_DEVICE_ATTRIBUTES , USB_DEVICE_POWER )
412381 };
413382 memcpy (tinyusb_config_descriptor , descriptor , TUD_CONFIG_DESC_LEN );
414-  if  ((tinyusb_loaded_interfaces_mask  &  ~ (BIT (USB_INTERFACE_CDC ) | BIT (USB_INTERFACE_DFU ))) ==  0 ) {
383+  if  ((tinyusb_loaded_interfaces_mask  ==   (BIT (USB_INTERFACE_CDC ) | BIT (USB_INTERFACE_DFU ))) ||  ( tinyusb_loaded_interfaces_mask   ==  BIT ( USB_INTERFACE_CDC )) ) {
415384 tinyusb_persist_set_enable (true);
416385 log_d ("USB Persist enabled" );
417386 }
@@ -493,6 +462,19 @@ static void usb_device_task(void *param) {
493462 * PUBLIC API 
494463 * */ 
495464
465+ esp_err_t  tinyusb_enable_interface (tinyusb_interface_t  interface , uint16_t  descriptor_len , tinyusb_descriptor_cb_t  cb )
466+ {
467+  if ((interface  >= USB_INTERFACE_MAX ) ||  (tinyusb_loaded_interfaces_mask  &  (1U  << interface ))){
468+  log_e ("Interface %u not enabled" , interface );
469+  return  ESP_FAIL ;
470+  }
471+  tinyusb_loaded_interfaces_mask  |= (1U  << interface );
472+  tinyusb_config_descriptor_len  +=  descriptor_len ;
473+  tinyusb_loaded_interfaces_callbacks [interface ] =  cb ;
474+  log_d ("Interface %u enabled" , interface );
475+  return  ESP_OK ;
476+ }
477+ 
496478esp_err_t  tinyusb_init (tinyusb_device_config_t  * config ) {
497479 static  bool  initialized  =  false;
498480 if (initialized ){
@@ -518,45 +500,6 @@ esp_err_t tinyusb_init(tinyusb_device_config_t *config) {
518500 return  err ;
519501}
520502
521- esp_err_t  tinyusb_enable_interface (tinyusb_interface_t  interface )
522- {
523-  uint16_t  descriptor_len  =  0 ;
524-  switch  (interface ) {
525-  case  USB_INTERFACE_CDC :
526-  descriptor_len  =  CFG_TUD_CDC  *  TUD_CDC_DESC_LEN ;
527-  break ;
528-  case  USB_INTERFACE_DFU :
529-  descriptor_len  =  CFG_TUD_DFU_RT  *  TUD_DFU_RT_DESC_LEN ;
530-  break ;
531-  case  USB_INTERFACE_HID :
532-  descriptor_len  =  CFG_TUD_HID  *  TUD_HID_INOUT_DESC_LEN ;
533-  break ;
534-  case  USB_INTERFACE_MSC :
535-  descriptor_len  =  CFG_TUD_MSC  *  TUD_MSC_DESC_LEN ;
536-  break ;
537-  case  USB_INTERFACE_MIDI :
538-  descriptor_len  =  CFG_TUD_MIDI  *  TUD_MIDI_DESC_LEN ;
539-  break ;
540-  case  USB_INTERFACE_VENDOR :
541-  descriptor_len  =  CFG_TUD_VENDOR  *  TUD_VENDOR_DESC_LEN ;
542-  break ;
543-  case  USB_INTERFACE_CUSTOM :
544-  descriptor_len  =  tusb_custom_get_descriptor_len ();
545-  break ;
546-  default :
547- 
548-  break ;
549-  }
550-  if  (descriptor_len ) {
551-  tinyusb_config_descriptor_len  +=  descriptor_len ;
552-  tinyusb_loaded_interfaces_mask  |= (1U  << interface );
553-  log_d ("Driver %u enabled" , interface );
554-  return  ESP_OK ;
555-  }
556-  log_e ("Driver %u not enabled" , interface );
557-  return  ESP_FAIL ;
558- }
559- 
560503uint8_t  tinyusb_add_string_descriptor (const  char  *  str ){
561504 if (str  ==  NULL  ||  tinyusb_string_descriptor_len  >= MAX_STRING_DESCRIPTORS ){
562505 return  0 ;
0 commit comments