This module allows to create and handle USB audio class instances. For detailed information on the USB audio class, refer to these specification documents:
Defining audio class descriptors
A typical audio class has one control interface and one streaming interface. Before you can declare a class instance, you must first properly define the audio class descriptors:
- Format descriptor
- Input terminal descriptor
- Output terminal descriptor
- Feature unit descriptor
Example 1: Layout of headphone descriptors:
Example 2: Layout of microphone descriptors:
The difference between headphones and microphone descriptors is in the direction of the isochronous endpoint descriptor. Microphone has the IN direction defined (from the USB device to the host). For headphones, the isochronous endpoint direction is set to OUT (from the host to the USB device). Both of these classes have two audio streaming descriptors:
- Streaming interface, alternate setting 0, is selected by the host when no isochronous transfers are required.
-
Streaming interface, alternate setting 1, has the following three descriptors defined:
- Audio streaming format descriptor (I, II, or III)
- Audio streaming endpoint descriptor
- Standard endpoint descriptor
Example: Descriptors for an audio class (Headphones: 2 channels, Fs = 48KHz, Format 16bit PCM):
Defining audio classes
When all descriptors are correctly defined, you can define the audio class:
If you wish to skip an optional descriptor, use NULL:
Using SOF events to start transfers in the audio class
To work with isochronous endpoints, you must use a SOF event. This means that the SOF event must be activated when calling the app_usbd_init function:
When timing is critical, it is possible to prepare for processing the SOF event directly in the interrupt - before the event queue is processed:
When the SOF event is processed, transfer may be started in any direction inside this event. The following example code shows transferring of audio data from the headphones to the buffer:
Event handling in the audio class
Audio class defines 3 events:
- APP_USBD_AUDIO_USER_EVT_CLASS_REQ - Audio class request - for configuration.
- APP_USBD_AUDIO_USER_EVT_RX_DONE - Rx DMA transfer finished.
- APP_USBD_AUDIO_USER_EVT_TX_DONE - Tx DMA transfer finished.
You can pass an event handler function to the class instance definition. The following is an example of a correct audio class user event handler prototype:
In this example code, the APP_USBD_AUDIO_USER_EVT_RX_DONE event is used to start the transfer of the data received from headphone output to the microphone input:
Registering an audio class to the USBD library
After the whole class instance is defined, you must register the new class to the USBD library: