USB音频类描述符总结

最近在学习USB音频描述符,下面是对音频类描述符的一些个人理解:
参考博客:https://blog.csdn.net/Z_HUALIN/article/details/80732036

先上代码:
const unsigned char CONFIGURATION_Descriptor[] = {
//CONFIGURATION(9 bytes)
CONFIGURATION_DESCRIPTOR_LENGTH, //Size of this descriptor in bytes.
CONFIGURATION_DESCRIPTOR_TYPE, //Descriptor type.
TOTAL_LENGTH(length), //Total length of byte returned for this configuration.
3+1, //Number of interfaces support by this configuration.
0x01, //Value to use as an argument to the SetConfiguration() request to select this configuration.
0, //Index of string descriptor describing this configuration.
0xA0, //Configuration characteristics.
MAX_POWER, //Maximum power consumption of the USB device form the bus in this specific configuration when the device is fully operation.

///
//INTERFACE_ASSOCIATION Descriptor (8 bytes)
0x08, //Size of this descriptor in bytes.
0x0B, //INTERFACE_ASSOCIATION Descriptor Type
0x00, //The first interface number associated with this function is 0
0x03, //The number of contiguous interfaces associated with this function is 3
0x01, //The function belongs to the Audio class
0x01, //The function belongs to the Audio Control
0x00, //The function
0x00, //The Function string descriptor index is 0

// Audio Control Interface (Interface 0)
// Standard AC(Audio Control) Interface Descriptor
0x09, // length of descriptor (9 bytes)
0x04, // descriptor type (INTERFACE)
0x00, // interface number (0)
0x00, // alternate setting (0)
0x00, // number of endpoints (0)
0x01, // interface class (1…defined by USB spec (Audio class)
0x01, // interface sub-class (1…Audio Control)
0x00, // interface protocol (0…unuse)
0x03, // interface string index (not supported)

// Class-specific AC Interface Descriptor
//CAC_Descriptor:
0x0A,    		// length of descriptor (10 bytes)
0x24,    		// descriptor type (CS INTERFACE)
0x01,    		// descriptor subtype (1..Header subtype)
0x00,0x01,      // Audio Device Class Specification Release Number in Binary-Coded Decimal.
0x48,0x00, 		// Total Length The combined length for this descriptor, including all Unit and Terminal
0x02,    		// The number of AudioStreaming and MIDIStreaming interfaces in the Audio Interface Collection to which this AudioControl interface belongs
0x01,    		// AudioStreaming Interface 1 belong to this AudioControl interface ()
0x02,   		// AudioStreaming Interface 2 belong to this AudioControl interface ()

// USB Microphone Input Terminal Descriptor
//Input Terminal Descriptor (ID1 ,MIC in)
0x0C,    		// length of descriptor (12 bytes)
0x24,    		// descriptor type (CS INTERFACE)
0x02,    		// descriptor subtype (INPUT_TERMINAL subtype)
0x01,    		// Terminal ID (ID of this Terminal=1)
0x01, 0x02,     // Terminal Type (Microphone In)
0x00,    		// Associated Terminal (unused)
0x02,    		// Number of logical ouput channels (2..Audio Control)
0x03, 0x00,     // Channel Config bitmap(Left and Right)
0x00,			// iChannel Names (unused)
0x00,			// iTerminal (unused)

// USB Microphone Feature Unit Descriptor
// Feature Unit   Descriptor(ID2, Feature Unit)
0x0A,    		// length of descriptor (7+(ch+1)*n)=7+(2+1)*1=10
0x24,    		// descriptor type (CS INTERFACE)
0x06,    		// descriptor subtype (FEATURE_UNIT)
0x02,    		// Terminal ID (ID of this Terminal=2)
0x01,    		// SourceID (From ID1)
0x01,    		// ControlSize (Two Byte)
0x01,    		// bmaControls[0] (Master channel mute)
0x00,    		// bmaControls[1] (unused)
0x00,    		// bmaControls[2] (unused)
0x00,    		// iFeature (unused)

// USB Microphone Output Terminal Descriptor
// Output Terminal   Descriptor   => (ID3 , USB Streaming Out)
0x09,    		// length of descriptor
0x24,    		// descriptor type (CS_INTERFACE)
0x03,    		// descriptor subtype (OUTPUT_TERMINAL)
0x03,   	 	// Terminal ID (ID of this Terminal=3)
0x01,0x01,      // Terminal Type (USB Streaming)
0x00,			// Associated Terminal (unused)
0x02,    		// SourceID (From ID2)
0x00,			// iTerminal (unused)

// Speaker Input Terminal Descriptor
// Input Terminal   Descriptor   => (ID4, USB Streaming IN)
0x0C,    		// length of descriptor
0x24,    		// descriptor type (CS_INTERFACE)
0x02,    		// descriptor subtype (INPUT_TERMINAL subtype)
0x04,    		// Terminal ID (ID of this Terminal=4)
0x01,0x01,      // Terminal Type (USB Streaming)
0x00,    		// Associated Terminal (unused)
0x02,    		// Number of logical ouput channels (2 channels)                  // STEREO
0x03, 0x00,     // Channel Config bitmap(D1=R, D0=L)            // STEREO
0x00,			// iChannel Names (unused)
0x00,			// iTerminal (unused)

// Speaker Feature Unit Descriptor
// Output Terminal  Descriptor   => (ID5 , Feature Unit )
0x0A,    		// length of descriptor (7+(ch+1)*n)=7+(2+1)*1=10        // STEREO
0x24,    		// descriptor type (CS_INTERFACE)
0x06,    		// descriptor subtype (FEATURE_UNIT)
0x05,    		// Terminal ID (ID of this Terminal=5)
0x04,    		// SourceID (From ID4)
0x01,    		// ControlSize (Two Byte)
0x01,   		// bmaControls[0] (Master channel mute) // STEREO
0x00,    		// bmaControls[1] (unused)       		// STEREO
0x00,    		// bmaControls[2] (unused)       		// STEREO
0x00,    		// iFeature  (unused)

// Speaker Output Terminal Descriptor
// Output Terminal   Descriptor   => (ID6 ,speaker )
0x09,    		// length of descriptor
0x24,    		// descriptor type (CS_INTERFACE)
0x03,    		// descriptor subtype (OUTPUT_TERMINAL)
0x06,    		// Terminal ID (ID of this Terminal=6)
0x01,0x03,      // TerminalType (Speaker)
0x00,			// AssocTerminal (unused)
0x05,    		// SourceID (From ID5)
0x00,			// iTerminal (unused)
//EndCAC:

// Audio Stream Interface IN(Interface 1)
// USB Microphone Standard AS Interface Descriptor (Alt. Set. 0)
0x09, // length of descriptor
0x04, // descriptor type (INTERFACE)
0x01, // index of this interface (1)
0x00, // alternate setting (0)
0x00, // number of endpoints (0)
0x01, // interface class (Audio class)
0x02, // interface sub-class (AUDIO_STREAMING)
0x00, // interface protocol (unused)
0x03, // interface string index (not supported)

// USB Microphone Standard AS Interface Descriptor (Alt. Set. 1)
0x09,    		// length of descriptor
0x04,    		// descriptor type (INTERFACE)
0x01,    		// index of this interface (1)
0x01,    		// alternate setting (1)
0x01,    		// number of endpoints (1)
0x01,    		// interface class (Audio class)
0x02,    		// interface sub-class (AUDIO_STREAMING)
0x00,			// interface protocol (unused)
0x00,			// interface string index (not supported)

// USB Microphone Class-spicific AS General Interface Descriptor
0x07,			// length of descriptor
0x24,    		// descriptor type (CS_INTERFACE)
0x01,    		// Descriptor SubType (GENERAL)
0x03,    		// bTerminalLink (ID3)
0x01,    		// Interface delay
0x01, 0x00,     // Format Tag (PCM Format)

// USB Microphone Type I Format Descriptor
0x0B,			// length of descriptor
0x24,   		// descriptor type (CS_INTERFACE)
0x02,    		// Descriptor SubType (FORMAT_TYPE)
0x01,    		// FormatType (FORMAT_TYPE_I)
0x02,    		// NrChannels (Two channels)
0x02,    		// SubFrame Size (two byte)
0x10,    		// Bit Resolution (16 bit)
0x01,    		// SampFreqType (one sample freq)
0x80,0xBB,0x00, // SamFreq (48000 Hz)

// INPUT use Endpoint 7
//Endpoint7_Descriptor:
9, 				// length of the descriptor
5, 				// DescriptorType (endpoint descriptor type)
0x87,           // EndPointAddress (EP7 IN)
0x0D,           // Endpoint's attribute (isochronous pipe)
192, 0,         // max packet size (192)
1,              // polling interval for this endpoint
0,				// Refresh (unused)
0,				// SynchAddress (unused)
//EndEndpoint7:

// USB Microphone Class-specific Isoc. Audio Data Endpoint Descriptor
0x07,    		// length of the descriptor
0x25,    		// DescriptorType (CS_ENDPOINT)
0x01,   		// Descriptor Subtype (GENERAL)
0x00,    		// bmAttributes : D0=Sampling frequency
0x00,			// bLockDelayUnits
0x00,0x00,		// LockDelay

// Audio Stream Interface OUT (Interface 2)
// USB Speaker Standard AS Interface Descriptor (Alt. Set. 0)
0x09, // length of the descriptor
0x04, // descriptor type (INTERFACE)
0x02, // index of this interface (2)
0x00, // alternate setting (0)
0x00, // number of endpoints (0)
0x01, // interface class (Audio class)
0x02, // interface sub-class (AUDIO_STREAMING)
0x00, // interface protocol (unused)
0x03, // interface string index (not supported)

// USB Speaker Standard AS Interface Descriptor (Alt. Set. 1)
0x09,    		// length of descriptor
0x04,    		// descriptor type (INTERFACE)
0x02,    		// index of this interface (2)
0x01,    		// alternate setting (1)
0x01,    		// number of endpoints (1)
0x01,    		// interface class (Audio class)
0x02,    		// interface sub-class (AUDIO_STREAMING)
0x00,			// interface protocol (unused)
0x00,			// interface string index (not supported)

// USB Speaker Class-spicific AS General Interface Descriptor
0x07,			// length of descriptor
0x24,    		// descriptor type (CS_INTERFACE)
0x01,    		// Descriptor SubType (GENERAL
0x04,    		// TerminalLink (ID4)
0x01,    		// Interface delay
0x01, 0x00,     // Format Tag (PCM Format)

// USB Speaker Type I Format Descriptor
0x0B,			// length of descriptor
0x24,    		// descriptor type (CS_INTERFACE)
0x02,    		// Descriptor SubType (FORMAT_TYPE)
0x01,    		// FormatType (FORMAT_TYPE_I)
0x02,    		// NrChannels (Two channels)                // STEREO
0x02,    		// TSubFrame Size (two byte)
0x10,    		// Bit Resolution (16 bit)
0x01,   		// SampFreqType (one sample freq)
0x80,0xBB,0x00, // SamFreq (48000 Hz)

// OUTPUT use Endpoint 8
//Endpoint8_Descriptor:
9, 				// length of the descriptor
5, 				// DescriptorType (endpoint descriptor type)
0x08,           // EndPointAddress (EP8 OUT)
0x0D,           // Endpoint's attribute (isochronous pipe)
192,0,          // max packet size (192) // STEREO
1,              // polling interval for this endpoint(1 ms)
0,				// Refresh (unused)
0,				// SynchAddress (unused)

//EndEndpoint8:
// USB Speaker Class-specific Isoc. Audio Data Endpoint Descriptor
0x07,    		// length of the descriptor
0x25,    		// DescriptorType (CS_ENDPOINT)
0x01,    		// Descriptor Subtype (GENERAL)
0x00,    		// bmAttributes : D0=Sampling frequency
0x00,			// bLockDelayUnits
0x00,0x00,		// LockDelay

}

1.设备描述符下包含一个配置描述符,配置描述符下包含了3组接口,IF0作为音频控制接口,IF1作为microphone接口,IF2作为speaker接口。

2.在IF0的定义中没有包含任何端点,所以与音频相关的控制信息将通过默认的控制端点0来进行信息的交付。也就是说端点0除了响应默认的控制事务(Setinterfance等等)以外还需要响应音频类相关控制事务(set cur,get max等等),音量调节就是通过控制端点0来完成的。

3.IF0中的Feature Unit定义了音量调节方式,mute和Volume,其中ID号为2的Unit控制microphone,ID 5控制speaker。具体关系常见其拓扑图。

4.IF0中两个IT(Input Terminal)定义了音频流逻辑通道数及其具体组成形式,在该示例中包含了2个逻辑通道,分别是LEFT&RIGHT,如果需要更多的通道可以通过修改相应描述符单元得到(同时注意修改相应的Streaming描述符中物理通道数,相应USB endpoint支持的包字节大小)。

  1. IF1和IF2中都定义了2个setting,其中setting 0都不包含传输endpoint,setting 1都包含一个同步传输endpoint。这里要说明的是,之所以都要定义一个不含有任何传输endpoint(除了默认端点0)的setting,是由usb audio设备特性决定的,该setting不可省略,该setting用于在设备没有被使用时作为usb设备的设置。如果没有利用该usb设备进行放音和录音则主机使用的接口是IF(1,0)(接口1的设置0)和IF(2,0)(接口2的设置0),如果开始放音则使用的接口是IF(1,0)和IF(2,1),如果即用于放音又用于录音则使用IF(1,1)和IF(2,1)。这里叙述的有些累赘,其实大家可以利用USB协议相关分析软件得到其具体交互过程。

6.在IF1和IF2中定义了音频流格式为PCM格式(更多关于格式的信息参考USB audio相关文档)。支持一个采样频点:32K,如果需要更多的采样频点的支持,可以通过修改相应字段得到(注意修改相应同步端点支持的包大小)。

7.IF1(microphone)定义了endpoint 1 作为其同步传输输入端点。端点支持的最大传输字节为0x84(132byte),这里的0x44是由3222=128=0x80(采样率通道数每个样点的字节数)得到的(0x80byte/ms),之所以会多出4字节是因为在之前的应用中如果支持44.1K时的一个考虑,当然这里可以将其定义为0x80。

8.在IF1和IF2的AS endpoint Descriptor中的字段attributes可以用来定义是否支持采样率相关事务处理。也就是说如果在同一个IF的同一setting中支持多个采样率,那么在进行同步传输前,主机可以发起一个告知设备即将使用的采样率的事务处理,而这里的attributes用来定义设备是否支持这类事务处理。

9.支持多个采样率不仅可以通过在第9点所叙述的方式来实现(同一setting下支持多个采样率),也可以通过增加新的setting来支持(每个setting支持一个单独的采样率)。两种方式各有优缺点,第一种方式比较简洁不过需要由主机告知设备即将使用的采样率。第二种方式会较大幅度的增加整个描述符长度,但是不需要主机告知设备使用的采样率。也就是说在第一种方式下主机先告诉即将使用的接口setting,然后再发起一个采样率的SET CUR事务处理。而第二种方式只需要告诉即将使用的接口就可以了(因为一个setting只支持一个采样率,不同采样率将采用不同setting)。

10.同步问题,因为usb传输都是主机发起的,而设备只是被动的响应来进行数据传输。对于同步传输,usb在每一个 frame中启动一次同步传输,该同步传输的启动是基于主机USB主控制器所使用的时钟来进行的,而设备得到数据是依据自己的时钟源,所以这里就存在两个时钟源差异的问题。我采用的方式是利用设备响应主机的SOF中断来调整自身时钟来与主机同步。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值