add CFG_TUD_DESC_AUTO for auto descritpor (device, config)

This commit is contained in:
hathach 2018-07-01 15:11:58 +07:00
parent cfd5e1e6d1
commit ff219f1f01
9 changed files with 64 additions and 270 deletions

View File

@ -202,7 +202,7 @@ app_descriptor_configuration_t const desc_configuration =
#define ENDIAN_BE16_FROM( high, low) ENDIAN_BE16(high << 8 | low)
// array of pointer to string descriptors
uint16_t const * const string_descriptor_arr [] =
uint16_t const * const string_desc_arr [] =
{
[0] = (uint16_t []) { // supported language
ENDIAN_BE16_FROM( STRING_LEN_UNICODE(1), TUSB_DESC_STRING ),
@ -247,9 +247,9 @@ uint16_t const * const string_descriptor_arr [] =
/*------------- Variable used by tud_set_descriptors -------------*/
tud_desc_init_t usb_desc_init =
tud_desc_set_t usb_desc_init =
{
.device = (uint8_t const * ) &desc_device,
.configuration = (uint8_t const * ) &desc_configuration,
.string_arr = (uint8_t const **) string_descriptor_arr,
.string_arr = (uint8_t const **) string_desc_arr,
};

View File

@ -100,6 +100,6 @@ typedef struct ATTR_PACKED
extern tud_desc_init_t usb_desc_init;
extern tud_desc_set_t usb_desc_init;
#endif

View File

@ -65,7 +65,6 @@ int main(void)
print_greeting();
tusb_init();
tud_set_descriptors(&usb_desc_init);
while (1)
{

View File

@ -50,22 +50,25 @@
#define CFG_TUSB_RHPORT0_MODE OPT_MODE_DEVICE
#define CFG_TUSB_DEBUG 2
#define CFG_TUSB_OS OPT_OS_NONE // be passed from IDE/command line for easy project switching
//#define CFG_TUD_TASK_PRIO 0 // be passed from IDE/command line for easy project switching
//--------------------------------------------------------------------+
// DEVICE CONFIGURATION
//--------------------------------------------------------------------+
#define CFG_TUD_ENDOINT0_SIZE 64
//#define CFG_TUD_TASK_PRIO 0 // be passed from IDE/command line for easy project switching
#define CFG_TUD_DESC_AUTO 1
// #define CFG_TUD_DESC_VID 0xCAFE
// #define CFG_TUD_DESC_PID 0x0001
//------------- CLASS -------------//
#define CFG_TUD_HID_KEYBOARD 0
#define CFG_TUD_HID_MOUSE 0
#define CFG_TUD_HID_GENERIC 0 // not supported yet
#define CFG_TUD_MSC 1
#define CFG_TUD_CDC 1
#define CFG_TUD_MSC 1
#define CFG_TUD_HID_KEYBOARD 0 // TODO need update
#define CFG_TUD_HID_MOUSE 0 // TODO need update
#define CFG_TUD_HID_GENERIC 0 // TODO need update
/*------------------------------------------------------------------*/
/* CLASS DRIVER

View File

@ -38,202 +38,6 @@
#include "tusb_descriptors.h"
//--------------------------------------------------------------------+
// USB DEVICE DESCRIPTOR
//--------------------------------------------------------------------+
tusb_desc_device_t const desc_device =
{
.bLength = sizeof(tusb_desc_device_t),
.bDescriptorType = TUSB_DESC_DEVICE,
.bcdUSB = 0x0200,
// Use Interface Association Descriptor (IAD) for CDC
// As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1)
.bDeviceClass = TUSB_CLASS_MISC,
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
.bDeviceProtocol = MISC_PROTOCOL_IAD,
.bMaxPacketSize0 = CFG_TUD_ENDOINT0_SIZE,
.idVendor = CFG_VENDORID,
.idProduct = CFG_PRODUCTID,
.bcdDevice = 0x0100,
.iManufacturer = 0x01,
.iProduct = 0x02,
.iSerialNumber = 0x03,
.bNumConfigurations = 0x01
};
//--------------------------------------------------------------------+
// USB COFNIGURATION DESCRIPTOR
//--------------------------------------------------------------------+
app_descriptor_configuration_t const desc_configuration =
{
.configuration =
{
.bLength = sizeof(tusb_desc_configuration_t),
.bDescriptorType = TUSB_DESC_CONFIGURATION,
.wTotalLength = sizeof(app_descriptor_configuration_t),
.bNumInterfaces = ITF_TOTAL,
.bConfigurationValue = 1,
.iConfiguration = 0x00,
.bmAttributes = TUSB_DESC_CONFIG_ATT_BUS_POWER,
.bMaxPower = TUSB_DESC_CONFIG_POWER_MA(500)
},
// IAD points to CDC Interfaces
.cdc =
{
.iad =
{
.bLength = sizeof(tusb_desc_interface_assoc_t),
.bDescriptorType = TUSB_DESC_INTERFACE_ASSOCIATION,
.bFirstInterface = ITF_NUM_CDC,
.bInterfaceCount = 2,
.bFunctionClass = TUSB_CLASS_CDC,
.bFunctionSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL,
.bFunctionProtocol = CDC_COMM_PROTOCOL_ATCOMMAND,
.iFunction = 0
},
//------------- CDC Communication Interface -------------//
.comm_itf =
{
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_NUM_CDC,
.bAlternateSetting = 0,
.bNumEndpoints = 1,
.bInterfaceClass = TUSB_CLASS_CDC,
.bInterfaceSubClass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL,
.bInterfaceProtocol = CDC_COMM_PROTOCOL_ATCOMMAND,
.iInterface = 0x00
},
.header =
{
.bLength = sizeof(cdc_desc_func_header_t),
.bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
.bDescriptorSubType = CDC_FUNC_DESC_HEADER,
.bcdCDC = 0x0120
},
.call =
{
.bLength = sizeof(cdc_desc_func_call_management_t),
.bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
.bDescriptorSubType = CDC_FUNC_DESC_CALL_MANAGEMENT,
.bmCapabilities = { 0 },
.bDataInterface = ITF_NUM_CDC+1,
},
.acm =
{
.bLength = sizeof(cdc_desc_func_acm_t),
.bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
.bDescriptorSubType = CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT,
.bmCapabilities = { // 0x02
.support_line_request = 1,
}
},
.union_func =
{
.bLength = sizeof(cdc_desc_func_union_t), // plus number of
.bDescriptorType = TUSB_DESC_CLASS_SPECIFIC,
.bDescriptorSubType = CDC_FUNC_DESC_UNION,
.bControlInterface = ITF_NUM_CDC,
.bSubordinateInterface = ITF_NUM_CDC+1,
},
.ep_notif =
{
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = CDC_EDPT_NOTIF,
.bmAttributes = { .xfer = TUSB_XFER_INTERRUPT },
.wMaxPacketSize = { .size = CDC_EDPT_NOTIF_SIZE },
.bInterval = 0x10
},
//------------- CDC Data Interface -------------//
.data_itf =
{
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_NUM_CDC+1,
.bAlternateSetting = 0x00,
.bNumEndpoints = 2,
.bInterfaceClass = TUSB_CLASS_CDC_DATA,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = 0x00
},
.ep_out =
{
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = CDC_EDPT_OUT,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = CDC_EDPT_SIZE },
.bInterval = 0
},
.ep_in =
{
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = CDC_EDPT_IN,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = CDC_EDPT_SIZE },
.bInterval = 0
},
},
.msc =
{
.interface =
{
.bLength = sizeof(tusb_desc_interface_t),
.bDescriptorType = TUSB_DESC_INTERFACE,
.bInterfaceNumber = ITF_NUM_MSC,
.bAlternateSetting = 0x00,
.bNumEndpoints = 2,
.bInterfaceClass = TUSB_CLASS_MSC,
.bInterfaceSubClass = MSC_SUBCLASS_SCSI,
.bInterfaceProtocol = MSC_PROTOCOL_BOT,
.iInterface = 0x07
},
.ep_out =
{
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = MSC_EDPT_OUT,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = MSC_EDPT_SIZE},
.bInterval = 1
},
.ep_in =
{
.bLength = sizeof(tusb_desc_endpoint_t),
.bDescriptorType = TUSB_DESC_ENDPOINT,
.bEndpointAddress = MSC_EDPT_IN,
.bmAttributes = { .xfer = TUSB_XFER_BULK },
.wMaxPacketSize = { .size = MSC_EDPT_SIZE},
.bInterval = 1
}
}
};
//--------------------------------------------------------------------+
// STRING DESCRIPTORS
//--------------------------------------------------------------------+
@ -241,7 +45,7 @@ app_descriptor_configuration_t const desc_configuration =
#define ENDIAN_BE16_FROM( high, low) ENDIAN_BE16(high << 8 | low)
// array of pointer to string descriptors
uint16_t const * const string_descriptor_arr [] =
uint16_t const * const string_desc_arr [] =
{
[0] = (uint16_t []) { // supported language
ENDIAN_BE16_FROM( STRING_LEN_UNICODE(1), TUSB_DESC_STRING ),
@ -284,11 +88,12 @@ uint16_t const * const string_descriptor_arr [] =
}
};
/*------------- Variable used by tud_set_descriptors -------------*/
tud_desc_init_t usb_desc_init =
// tud_desc_set is required by tinyusb stack
// since CFG_TUD_DESC_AUTO is enabled, we only need to set string_arr
tud_desc_set_t tud_desc_set =
{
.device = (uint8_t const * ) &desc_device,
.configuration = (uint8_t const * ) &desc_configuration,
.string_arr = (uint8_t const **) string_descriptor_arr,
.device = NULL,
.config = NULL,
.string_arr = (uint8_t const **) string_desc_arr,
.hid_report = NULL
};

View File

@ -115,6 +115,6 @@ typedef struct ATTR_PACKED
extern tud_desc_init_t usb_desc_init;
extern tud_desc_set_t usb_desc_init;
#endif

View File

@ -177,6 +177,6 @@ typedef struct ATTR_PACKED
extern tud_desc_init_t usb_desc_init;
extern tud_desc_set_t usb_desc_init;
#endif

View File

@ -72,24 +72,17 @@ typedef struct {
void (* close ) (uint8_t);
} usbd_class_driver_t;
enum {
USBD_INTERFACE_NUM_MAX = 16 // USB specs specify up to 16 endpoints per device
};
typedef struct {
volatile uint8_t state;
uint8_t config_num;
uint8_t interface2class[USBD_INTERFACE_NUM_MAX]; // determine interface number belongs to which class
uint8_t itf2class[16]; // determine interface number belongs to which class
}usbd_device_info_t;
//--------------------------------------------------------------------+
// Class & Device Driver
//--------------------------------------------------------------------+
CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN uint8_t usbd_enum_buffer[CFG_TUD_ENUM_BUFFER_SIZE];
tud_desc_init_t _usbd_descs[CONTROLLER_DEVICE_NUMBER];
usbd_device_info_t usbd_devices[CONTROLLER_DEVICE_NUMBER];
static usbd_class_driver_t const usbd_class_drivers[] =
@ -145,14 +138,6 @@ static usbd_class_driver_t const usbd_class_drivers[] =
enum { USBD_CLASS_DRIVER_COUNT = sizeof(usbd_class_drivers) / sizeof(usbd_class_driver_t) };
//tusb_desc_device_qualifier_t _device_qual =
//{
// .bLength = sizeof(tusb_desc_device_qualifier_t),
// .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
// .bcdUSB = 0x0200,
// .bDeviceClass =
//};
//--------------------------------------------------------------------+
// DCD Event
@ -216,12 +201,6 @@ bool tud_n_mounted(uint8_t rhport)
return usbd_devices[rhport].state == TUSB_DEVICE_STATE_CONFIGURED;
}
bool tud_n_set_descriptors(uint8_t rhport, tud_desc_init_t const* desc_cfg)
{
_usbd_descs[rhport] = *desc_cfg;
return true;
}
//--------------------------------------------------------------------+
// IMPLEMENTATION
//--------------------------------------------------------------------+
@ -248,7 +227,6 @@ tusb_error_t usbd_init (void)
osal_task_create(&_usbd_task_def);
//------------- Core init -------------//
arrclr_( _usbd_descs );
//------------- class init -------------//
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
@ -378,7 +356,7 @@ static tusb_error_t proc_control_request_st(uint8_t rhport, tusb_control_request
else if ( TUSB_REQ_RCPT_INTERFACE == p_request->bmRequestType_bit.recipient)
{
static uint8_t drid;
uint8_t const class_code = usbd_devices[rhport].interface2class[ u16_low_u8(p_request->wIndex) ];
uint8_t const class_code = usbd_devices[rhport].itf2class[ u16_low_u8(p_request->wIndex) ];
for (drid = 0; drid < USBD_CLASS_DRIVER_COUNT; drid++)
{
@ -427,14 +405,19 @@ static tusb_error_t proc_set_config_req(uint8_t rhport, uint8_t config_number)
usbd_devices[rhport].config_num = config_number;
//------------- parse configuration & open drivers -------------//
uint8_t const * p_desc_config = _usbd_descs[rhport].configuration;
TU_ASSERT(p_desc_config != NULL, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
#if CFG_TUD_DESC_AUTO
extern uint8_t const * const _desc_auto_config;
uint8_t const * desc_cfg = _desc_auto_config;
#else
uint8_t const * desc_cfg = tud_desc_set.config;
TU_ASSERT(desc_cfg != NULL, TUSB_ERROR_DESCRIPTOR_CORRUPTED);
#endif
uint8_t const * p_desc = p_desc_config + sizeof(tusb_desc_configuration_t);
uint8_t const * p_desc = desc_cfg + sizeof(tusb_desc_configuration_t);
uint16_t const config_len = ((tusb_desc_configuration_t*)p_desc_config)->wTotalLength;
uint16_t const cfg_len = ((tusb_desc_configuration_t*)desc_cfg)->wTotalLength;
while( p_desc < p_desc_config + config_len )
while( p_desc < desc_cfg + cfg_len )
{
if ( TUSB_DESC_INTERFACE_ASSOCIATION == p_desc[DESCRIPTOR_OFFSET_TYPE])
{
@ -455,8 +438,8 @@ static tusb_error_t proc_set_config_req(uint8_t rhport, uint8_t config_number)
TU_ASSERT( drid < USBD_CLASS_DRIVER_COUNT, TUSB_ERROR_NOT_SUPPORTED_YET );
// Check duplicate interface number TODO support alternate setting
TU_ASSERT( 0 == usbd_devices[rhport].interface2class[p_desc_itf->bInterfaceNumber], TUSB_ERROR_FAILED);
usbd_devices[rhport].interface2class[p_desc_itf->bInterfaceNumber] = class_code;
TU_ASSERT( 0 == usbd_devices[rhport].itf2class[p_desc_itf->bInterfaceNumber], TUSB_ERROR_FAILED);
usbd_devices[rhport].itf2class[p_desc_itf->bInterfaceNumber] = class_code;
uint16_t length=0;
TU_ASSERT_ERR( usbd_class_drivers[drid].open( rhport, p_desc_itf, &length ) );
@ -480,26 +463,33 @@ static uint16_t get_descriptor(uint8_t rhport, tusb_control_request_t const * co
uint8_t const * desc_data = NULL ;
uint16_t len = 0;
//------------- Descriptor Check -------------//
tud_desc_init_t const* descs = &_usbd_descs[rhport];
tud_desc_set_t descs = tud_desc_set;
#if CFG_TUD_DESC_AUTO
extern tusb_desc_device_t const _desc_auto_device;
extern uint8_t const * const _desc_auto_config;
descs.device = (uint8_t const*) &_desc_auto_device;
descs.config = _desc_auto_config;
#endif
switch(desc_type)
{
case TUSB_DESC_DEVICE:
desc_data = descs->device;
desc_data = descs.device;
len = sizeof(tusb_desc_device_t);
break;
case TUSB_DESC_CONFIGURATION:
desc_data = descs->configuration;
len = ((tusb_desc_configuration_t*)descs->configuration)->wTotalLength;
desc_data = descs.config;
len = ((tusb_desc_configuration_t const*) desc_data)->wTotalLength;
break;
case TUSB_DESC_STRING:
// windows sometimes ask for string at index 238 !!!
if ( !(desc_index < 100) ) return 0;
desc_data = descs->string_arr[desc_index];
desc_data = descs.string_arr[desc_index];
VERIFY( desc_data != NULL, 0 );
len = desc_data[0]; // first byte of descriptor is its size
@ -538,7 +528,7 @@ void dcd_bus_event(uint8_t rhport, usbd_bus_event_type_t bus_event)
switch(bus_event)
{
case USBD_BUS_EVENT_RESET :
memclr_(&usbd_devices[rhport], sizeof(usbd_device_info_t));
varclr_(&usbd_devices[rhport]);
osal_queue_flush(_usbd_q);
osal_semaphore_reset_isr(_usbd_ctrl_sem);
for (uint8_t i = 0; i < USBD_CLASS_DRIVER_COUNT; i++)
@ -561,8 +551,8 @@ void dcd_bus_event(uint8_t rhport, usbd_bus_event_type_t bus_event)
break;
case USBD_BUS_EVENT_UNPLUGGED:
// invoke callback
tud_umount_cb(rhport);
varclr_(&usbd_devices[rhport]);
tud_umount_cb(rhport); // invoke callback
break;
case USBD_BUS_EVENT_SUSPENDED:

View File

@ -59,20 +59,22 @@
/// \brief Descriptor pointer collector to all the needed.
typedef struct {
uint8_t const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
uint8_t const * configuration; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
uint8_t const** string_arr; ///< a array of pointers to string descriptors
uint8_t const * device; ///< pointer to device descriptor \ref tusb_desc_device_t
uint8_t const * config; ///< pointer to the whole configuration descriptor, starting by \ref tusb_desc_configuration_t
uint8_t const** string_arr; ///< a array of pointers to string descriptors
uint8_t const * hid_report; ///< pointer to HID report descriptor only needed if CFG_TUD_HID_* is enabled
}tud_desc_set_t;
// Must be defined by application
extern tud_desc_set_t tud_desc_set;
uint8_t const * p_hid_keyboard_report; ///< pointer to HID report descriptor of Keyboard interface. Only needed if CFG_TUD_HID_KEYBOARD is enabled
uint8_t const * p_hid_mouse_report; ///< pointer to HID report descriptor of Mouse interface. Only needed if CFG_TUD_HID_MOUSE is enabled
}tud_desc_init_t;
//--------------------------------------------------------------------+
// APPLICATION API (Multiple Root Ports)
// Should be used only with MCU that support more than 1 ports
//--------------------------------------------------------------------+
bool tud_n_mounted(uint8_t rhport);
bool tud_n_set_descriptors(uint8_t rhport, tud_desc_init_t const* desc_cfg);
//--------------------------------------------------------------------+
// APPLICATION API (Single Port)
@ -83,11 +85,6 @@ static inline bool tud_mounted(void)
return tud_n_mounted(0);
}
static inline bool tud_set_descriptors(tud_desc_init_t const* desc_cfg)
{
return tud_n_set_descriptors(0, desc_cfg);
}
//--------------------------------------------------------------------+
// APPLICATION CALLBACK
//--------------------------------------------------------------------+