天天看點

電子工程師名片——USB裝置枚舉過程

一、USB枚舉過程

使用的工具是CATC(USB協定分析儀),整個成功枚舉過程見位址:​​javascript:void(0)​​    c8051f320_USB枚舉過程.usb

1、GetDescripor

U主機一次GetDescriptor是在USB控制傳輸中完成,控制傳輸分為Setup Stage、Data Stage和Status Stage。

1)Setup Stage

電子工程師名片——USB裝置枚舉過程

SOF幀是USB總線的同步信号,每隔1ms主機會向裝置發出一個SOF幀。

在枚舉之前,裝置還沒有配置設定位址是以用的是預設位址0和端點0,Setup Stage的資料包必須為DATA0,與之對應的Status Stage的資料包必須為DATA1

DATA0   80      (bmRequestType) Device to Host,Standard,Recipient is device   

GetDescriptor

(wValue)   51單片機為大端模式,01為Descriptor Type,00為Descriptor Index

(wIndex)   

(wLength) 51單片機為大端模式,Descriptor Length,64 Bytes        

2)Data Stage

電子工程師名片——USB裝置枚舉過程

因為在Setup Stage主機要求裝置傳回64 Bytes的Descriptor Data以便主機了解預設管道的最大資訊包尺寸,是以裝置在DATA1中傳回64 Bytes。

這64 Bytes中有Device_Descriptor 18 Bytes (12 01 00 02 00 00 0040 12 34 56 78 00 00 00 00 00 01)  最大資訊包尺寸為64 Bytes

Configuration_Descriptor

Interface_Descriptor

EndpointIN1_Descriptor

EndpointOUT1_Descriptor

EndpointIN2_Descriptor

EndpointOUT2_Descriptor

3)Status Stage

電子工程師名片——USB裝置枚舉過程

Data Stage結束之後,由于在Data Stage用的是IN令牌,是以在Status Stage用與IN方向相反的OUT來訓示狀态階段。

并且Satus Stage的資料包是一個0長資料包

USB2.0規範沒有要求這裡有複位動作,複位是一個謹慎的動作,在複位結束後可保證裝置處在一個已知狀态。

2、SetAddress

第一次主機GetDescriptor之後接着主機要對裝置配置設定位址

1)Setup Stage

電子工程師名片——USB裝置枚舉過程

DATA0   00      (bmRequestType)Hostto Device,Standard,Recipient is device   

SetAddress

(wValue)     大端模式,Device Address 0x02

(wIndex)   

(wLength)

2)Status Stage

電子工程師名片——USB裝置枚舉過程

SetAddress沒有資料階段,是以在Setup Stage之後立刻就是Status Stage

3、GetDescriptor——>wValue字段中Descriptor Type為0x01表示device descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

4、GetDescriptor——>wValue字段中Descriptor Type為0x02表示configuration descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

5、GetDescriptor——>wValue字段中Descriptor Type為0x02表示configuration descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

6、GetDescriptor——>wValue字段中Descriptor Type為0x01表示device descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

7、GetDescriptor——>wValue字段中Descriptor Type為0x02表示configuration descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

8、GetDescriptor——>wValue字段中Descriptor Type為0x02表示configuration descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

9、SetConfiguration

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

10、GetMaxLun

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

11、開始識别USB裝置具體某一類

電子工程師名片——USB裝置枚舉過程

二、USB枚舉失敗

USB枚舉失敗過程:​​javascript:void(0)​​     USB枚舉失敗會複位.usb

在控制傳輸的時候如果裝置回複給主機的描述符錯誤的話,主機會重新複位裝置,連續3次都出錯的話,主機停止枚舉裝置。

1、GetDescriptor——>wValue字段中Descriptor Type為0x01表示device descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

2、SetAddress

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

3、GetDescriptor——>wValue字段中Descriptor Type為0x01表示device descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

4、GetDescriptor——>wValue字段中Descriptor Type為0x02表示configuration descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程

5、GetDescriptor——>wValue字段中Descriptor Type為0x02表示configuration descriptor

電子工程師名片——USB裝置枚舉過程
電子工程師名片——USB裝置枚舉過程