abcminiuser / lufa

LUFA - the Lightweight USB Framework for AVRs.
http://www.lufa-lib.org
1.04k stars 325 forks source link

How does a configuration descriptor get associated with the main device descriptor? #33

Closed trusktr closed 10 years ago

trusktr commented 10 years ago

I see that USB_Descriptor_Device_t has a field called NumberOfConfigurations, and I see the USB_Descriptor_Configuration_t stuct, but I'm not sure how I specify that a USB_Descriptor_Configuration_t belongs to a USB_Descriptor_Device_t.

Does the compiler or the running program somehow automatically associate NumberOfConfigurations USB_Descriptor_Configuration_ts with a USB_Descriptor_Device_t? If so, do they get associated in the order defined?

Maybe I just don't know enough about USB yet, but I was expecting USB_Descriptor_Device_t to contain USB_Descriptor_Configuration_ts (or an array of pointers to them).

abcminiuser commented 10 years ago

This is defined by the USB specification; the host will first request the single Device Descriptor from the attached USB device, and read its contents. From the NumberOfConfigurations field, it can determine how many distinct configurations the device supports, and thus the total number of Configuration Descriptors it contains.

To retrieve the Configuration Descriptor(s), the host will issue a GetDescriptor control request to the device, with the lower 8-bits of the wValue request parameter indicating which descriptor it is interested in receiving. A host will typically retrieve all Configuration Descriptor(s) in sequence, then issue a SetConfiguration control request to choose the one it wishes to use.

It is your responsibility to map the wValue index to each of the Configuration Descriptor(s) in your device via the CALLBACK_USB_GetDescriptor() function -- look at the way to the String descriptors are mapped as an example.

Note that it is extremely uncommon for USB devices to have more than one configuration - typically devices only have one configuration which exposes multiple features (such as a combined keyboard and mouse).

trusktr commented 10 years ago

That was so helpful. Thank you Dean!