usb作为目前最为通用的接口,为提高产品用户体验,减小产品的设计复杂度,立下了悍马功劳,但是也因其通用的特性,其相对于其他接口,协议更为复杂,同时也在不断发生演进,充实目前的协议规范,带宽有了很大的提升,同时尺寸也在不断缩小。
最近因为使用usb 进行音频数据的采集,对usb 协议做了一次系统性了解,同时以usb audio class 协议作为范本,进一步了解usb 协议规范。
USB 连接拓扑结构
usb 拓扑结构可以简单描述为主从模式,进一步扩展,可通过usb Hub扩展为树状结构。
Host<==>Slave
USB 速度
我们选择USB,其中很重要一个评价指标即是其速度是否能达到我们的应用需求。目前在Audio应用中,主要还是以USB2.0为主,这边以USB 2.0为例:
- High Speed - 480Mbits/s
High speed data is clocked at 480.00Mb/s with a data signalling tolerance of ± 500ppm.
-
Full Speed - 12Mbits/s
Full speed data is clocked at 12.000Mb/s with a data signalling tolerance of ±0.25% or 2,500ppm. -
Low Speed - 1.5Mbits/s
Low speed data is clocked at 1.50Mb/s with a data signalling tolerance of ±1.5% or 15,000ppm.
从电路上区分:
- USB 1.0 Low Speed: 1.5Mbps : D- 接一個上拉電阻
- USB 1.1 Full Speed : 12Mbps : D+ 接一個上拉電阻
- USB 2.0 High Speed: 480Mbps : D+ 接一個上拉電阻
Slave 所支持的速度模式,Host 端可通过硬件电路的上拉电压进行识别,具体如图所示:
High speed 和Full speed 设备如何区分呢?
- A device that is USB 2.0 High Speed capable first connects as an Full Speed device (D+ pulled high), but upon receiving a USB RESET (both D+ and D− driven LOW by host for 10 to 20 ms) it pulls the D− line high, known as chirp K. This indicates to the host that the device is high bandwidth.
- If the host/hub is also HS capable, it chirps (returns alternating J and K states on D− and D+ lines) letting the device know that the hub operates at high bandwidth. The device has to receive at least three sets of KJ chirps before it changes to high bandwidth terminations and begins high bandwidth signaling.
- Because USB 3.0 uses wiring separate and additional to that used by USB 2.0 and USB 1.x, such bandwidth negotiation is not required.
通过以上描述可以看出,device端声明High speed设备是通过发送一个K
信号到Host端,如果Host支持High speed 设备,则会回应J K
信号,通过这种方式达成模式的同步。
USB packet 类型
Token packet
三种类型的token,其中in和out指示数据方向,setup指示配置的开始
- In:slave->host
- out: host->slave
- setup:control transfer begin
Data packet
- Data 0
- Data 1
Handshake Packet
- ACK
- NAK
- STALL
SOF (start of frame)packet
An packet exchange example:
USB 描述符
以下是USB 描述符的层级结构,如图所示:
描述符的作用在于Slave向Host 声明设备的属性和接口以及相应参数,在linux 端通过lsusb
command 我们可以看到一个usb 设备的完整描述符结构。
usb设备在pc 上也是挂载在pci 总线上,因此也服从pci设备的地址标示方式。
查找pc 端所有usb 设备
>>lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 095: ID 0483:3748 STMicroelectronics ST-LINK/V2
Bus 001 Device 005: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 001 Device 096: ID 0483:5734 STMicroelectronics
Bus 001 Device 003: ID 413c:301a Dell Computer Corp.
Bus 001 Device 002: ID 413c:2107 Dell Computer Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
指定目标设备,查看描述符信息
>> lsusb -s 096 -v
Bus 001 Device 096: ID 0483:5734 STMicroelectronics
Couldn't open device, some information will be missing
Device Descriptor:
bLength 18
bDescriptorType 1
bcdUSB 2.00
bDeviceClass 0 (Defined at Interface level)
bDeviceSubClass 0
bDeviceProtocol 0
bMaxPacketSize0 64
idVendor 0x0483 STMicroelectronics
idProduct 0x5734
bcdDevice 2.00
iManufacturer 1
iProduct 2
iSerial 3
bNumConfigurations 1
Configuration Descriptor:
bLength 9
bDescriptorType 2
wTotalLength 129
bNumInterfaces 3
bConfigurationValue 1
iConfiguration 0
bmAttributes 0xa0
(Bus Powered)
Remote Wakeup
MaxPower 200mA
Interface Descriptor:
bLength 9
bDescriptorType 4
bInterfaceNumber 0
bAlternateSetting 0
bNumEndpoints 0
bInterfaceClass 1 Audio
bInterfaceSubClass 1 Control Device
bInterfaceProtocol 0
iInterface 0
AudioControl Interface Descriptor:
bLength 9
bDescriptorType 36
bDescriptorSubtype 1 (HEADER)
bcdADC 1.00
wTotalLength 41
bInCollection 1 //total 1 audio stream interface
baInterfaceNr( 0) 1 //audio stream id 1 belongs to audio control interface
AudioControl Interface Descriptor: