cancel
Showing results for 
Search instead for 
Did you mean: 

USB composite device CDC+HID

SS.Sagar
Associate III
Posted on April 19, 2018 at 18:11

Hi,

i have working HID example with me. But i was trying to integrate CDC class in the existing HID class. I am using cubemx code. After initializing and flashing i got the error as 'Device Descriptor Request Failed' .

I am confused to initialize the USB driver for HID and CDC as well at the same time.  

Is it fine if i initialize the code for HID and CDC separately? 

Or i will have to initialize both code at the same time. 

Do i have to use different USB structure that is USBD_ClassTypeDef . ?

Here is my usb descriptor file.

/* USB HID device Configuration Descriptor */

__ALIGN_BEGIN static uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END =

{

0x09, /* bLength: Configuration Descriptor size */

USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */

USB_HID_CONFIG_DESC_SIZ,

/* wTotalLength: Bytes returned */

0x00,

0x03, /*bNumInterfaces: 1 interface*/

0x01, /*bConfigurationValue: Configuration value*/

0x00, /*iConfiguration: Index of string descriptor describing

the configuration*/

0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */

0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/

/************** Descriptor of Keyboard interface ****************/

/* 09 */

0x09, /*bLength: Interface Descriptor size*/

USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/

0x00, /*bInterfaceNumber: Number of Interface*/

0x00, /*bAlternateSetting: Alternate setting*/

0x01, /*bNumEndpoints*/

0x03, /*bInterfaceClass: HID*/

0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/

0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ // KEYBOARD

0, /*iInterface: Index of string descriptor*/

/******************** Descriptor of Keyboard HID ********************/

/* 18 */

0x09, /*bLength: HID Descriptor size*/

HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/

0x11, /*bcdHID: HID Class Spec release number*/

0x01,

0x00, /*bCountryCode: Hardware target country*/

0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/

0x22, /*bDescriptorType*/

HID_KEYBOARD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/

0x00,

/******************** Descriptor of endpoint ********************/

/* 27 */

0x07, /*bLength: Endpoint Descriptor size*/

USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/

HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/

0x03, /*bmAttributes: Interrupt endpoint*/

HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */

0x00,

HID_FS_BINTERVAL, /*bInterval: Polling Interval (10 ms)*/

/* 34 */

/******** /IAD should be positioned just before the CDC interfaces ******

IAD to associate the two CDC interfaces */

0x08, /* bLength */

0x0B, /* bDescriptorType */

0x01, /* bFirstInterface */

0x02, /* bInterfaceCount */

0x02, /* bFunctionClass */

0x00, /* bFunctionSubClass */

0x01, /* bFunctionProtocol */

0x00, /* iFunction (Index of string descriptor describing this function) */

/*Interface Descriptor */

0x09, /* bLength: Interface Descriptor size */

USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */

/* Interface descriptor type */

0x01, /* bInterfaceNumber: Number of Interface */

0x00, /* bAlternateSetting: Alternate setting */

0x01, /* bNumEndpoints: One endpoints used */

0x02, /* bInterfaceClass: Communication Interface Class */

0x02, /* bInterfaceSubClass: Abstract Control Model */

0x01, /* bInterfaceProtocol: Common AT commands */

0x00, /* iInterface: */

/*Header Functional Descriptor*/

0x05, /* bLength: Endpoint Descriptor size */

0x24, /* bDescriptorType: CS_INTERFACE */

0x00, /* bDescriptorSubtype: Header Func Desc */

0x10, /* bcdCDC: spec release number */

0x01,

/*Call Management Functional Descriptor*/

0x05, /* bFunctionLength */

0x24, /* bDescriptorType: CS_INTERFACE */

0x01, /* bDescriptorSubtype: Call Management Func Desc */

0x00, /* bmCapabilities: D0+D1 */

0x01, /* bDataInterface: 1 */

/*ACM Functional Descriptor*/

0x04, /* bFunctionLength */

0x24, /* bDescriptorType: CS_INTERFACE */

0x02, /* bDescriptorSubtype: Abstract Control Management desc */

0x02, /* bmCapabilities */

/*Union Functional Descriptor*/

0x05, /* bFunctionLength */

0x24, /* bDescriptorType: CS_INTERFACE */

0x06, /* bDescriptorSubtype: Union func desc */

0x00, /* bMasterInterface: Communication class interface */

0x01, /* bSlaveInterface0: Data Class Interface */

/*Endpoint 2 Descriptor*/

0x07, /* bLength: Endpoint Descriptor size */

USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */

CDC_CMD_EP, /* bEndpointAddress */

0x03, /* bmAttributes: Interrupt */

LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */

HIBYTE(CDC_CMD_PACKET_SIZE),

0x10, /* bInterval: */

/*---------------------------------------------------------------------------*/

/*Data class interface descriptor*/

0x09, /* bLength: Endpoint Descriptor size */

USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */

0x02, /* bInterfaceNumber: Number of Interface */

0x00, /* bAlternateSetting: Alternate setting */

0x02, /* bNumEndpoints: Two endpoints used */

0x0A, /* bInterfaceClass: CDC */

0x00, /* bInterfaceSubClass: */

0x00, /* bInterfaceProtocol: */

0x00, /* iInterface: */

/*Endpoint OUT Descriptor*/

0x07, /* bLength: Endpoint Descriptor size */

USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */

CDC_OUT_EP, /* bEndpointAddress */

0x02, /* bmAttributes: Bulk */

LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */

HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),

0x00, /* bInterval: ignore for Bulk transfer */

/*Endpoint IN Descriptor*/

0x07, /* bLength: Endpoint Descriptor size */

USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */

CDC_IN_EP, /* bEndpointAddress */

0x02, /* bmAttributes: Bulk */

LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */

HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE),

0x00 /* bInterval: ignore for Bulk transfer */

} ;

/* USB HID device Configuration Descriptor */

__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END =

{

/* 18 */

0x09, /*bLength: HID Descriptor size*/

HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/

0x11, /*bcdHID: HID Class Spec release number*/

0x01,

0x00, /*bCountryCode: Hardware target country*/

0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/

0x22, /*bDescriptorType*/

HID_KEYBOARD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/

0x00,

};

/* USB Standard Device Descriptor */

__ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =

{

USB_LEN_DEV_QUALIFIER_DESC,

USB_DESC_TYPE_DEVICE_QUALIFIER,

0x00,

0x02,

0x00,

0x00,

0x00,

0x40,

0x01,

0x00,

};

#cubemx-stm32f103c8t6 #2-usb-descriptors #usb-composite
15 REPLIES 15
SS.Sagar
Associate III
Posted on April 19, 2018 at 20:39

Is this descriptor part correct?

Posted on April 19, 2018 at 23:16

No, the descriptors don't look entirely correct. But what is more important, we don't get any support for composite devices in the ST USB library. People

http://www.cnblogs.com/shangdawei/archive/2012/10/24/2737053.html

 or use a 3rd party USB library. There are also other opensource projects that have USB driver with composite support for STM32, such as Nuttx.

-- pa

Posted on April 20, 2018 at 08:33

Why would you say that ? At least the library for the F0x2 and for the F30x have support for CDC+HID (with examples). I must admit that I did not try their composite examples, but they exist and the peripheral has enough endpoints to support it.

hobb.epy

‌ what part are you using ?

Posted on April 20, 2018 at 15:24

There does exist an

http://www.st.com/en/embedded-software/stsw-stm32121.html

which has composite examples, but they are way too specific to their application and are hard to adapt to different needs. And the USB device library of Cube in it's current form is completely incapable of supporting composite devices.

https://github.com/IntergatedCircuits/DebugDongleFW

implements a composite device with 2 HID and 1 CDC interface on an STM32F0 chip. The

https://github.com/IntergatedCircuits/USBDevice

supports any number of interfaces on a device, doesn't require manual definition of USB descriptors and can be ported to pretty much any STM32.
Posted on April 20, 2018 at 15:50

Hello Ben K,

Thank you for mentioning your library. I'm struggling to get a working HID on the F042, so I will definitively check it out. However what do you mean by 'doesn't require manual definition of USB descriptors' ?

Posted on April 20, 2018 at 16:51

It means that the application-configurable elements of the descriptors are set via the Config structure element of the device / interface handle, and the descriptors are assembled by the library internally, e.g. in

https://github.com/IntergatedCircuits/USBDevice/blob/master/Device/Src/usbd_desc.c

.
Posted on April 20, 2018 at 18:40

This is exactly the situation. You can find N more or less finished examples of composite and even a hub, and use that as starting point of your own N+1 more or less finished thing. 

The ST HAL/LL libraries are simple, approachable, very tempting to hack and roll your own. This is very good for learning and teaching. Unfortunately, not so good for making a real product. 

Regards,

Pavel

Posted on April 20, 2018 at 18:58

Kraal wrote:

hobb.epy

what part are you using ?

I didn't get your question?

I checked the links attached in the thread. i am confused at Number_of_interfaces in configuration descriptor.

If i am using HID+CDC and i have to IAD then number of interface should be 2 or 3?

i compared the descriptor with other descriptor as well and seems to be fine.

Posted on April 22, 2018 at 11:05

What microcontroller ?