forked from payne92/bare-metal-arm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.h
executable file
·207 lines (175 loc) · 6.23 KB
/
usb.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// USB standard request codes
#define mGET_STATUS 0
#define mCLR_FEATURE 1
#define mSET_FEATURE 3
#define mSET_ADDRESS 5
#define mGET_DESC 6
#define mSET_DESC 7
#define mGET_CONFIG 8
#define mSET_CONFIG 9
#define mGET_INTF 10
#define mSET_INTF 11
#define mSYNC_FRAME 12
// USB descriptor types
#define mDEVICE 1
#define mCONFIGURATION 2
#define mSTRING 3
#define mINTERFACE 4
#define mENDPOINT 5
#define mDEVICE_QUALIFIER 6
#define mOTHER_SPEED_CONFIGURATION 7
#define mINTERFACE_POWER 8
// USB setup packet structure (assumes compiler is little endian!)
typedef struct {
uint8_t bmRequestType;
uint8_t bRequest;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} USB_SETUP;
// Device descriptor
typedef struct {
uint8_t bLength;
uint8_t bDscType;
uint16_t bcdUSB;
uint8_t bDevCls;
uint8_t bDevSubCls;
uint8_t bDevProtocol;
uint8_t bMaxPktSize0;
uint16_t idVendor;
uint16_t idProduct;
uint16_t bcdDevice;
uint8_t iMFR;
uint8_t iProduct;
uint8_t iSerialNum;
uint8_t bNumCfg;
} __attribute__((packed)) USB_DEV_DSC;
// Configuration descriptor
typedef struct {
uint8_t bLength;
uint8_t bDscType;
uint16_t wTotalLength;
uint8_t bNumIntf;
uint8_t bCfgValue;
uint8_t iCfg;
uint8_t bmAttributes;
uint8_t bMaxPower;
} __attribute__((packed)) USB_CFG_DSC;
// Interface descriptor
typedef struct {
uint8_t bLength;
uint8_t bDscType;
uint8_t bIntfNum;
uint8_t bAltSetting;
uint8_t bNumEPs;
uint8_t bIntfCls;
uint8_t bIntfSubCls;
uint8_t bIntfProtocol;
uint8_t iIntf;
} __attribute__((packed)) USB_INTF_DSC;
// Endpoint descriptor
typedef struct {
uint8_t bLength;
uint8_t bDscType;
uint8_t bEPAdr;
uint8_t bmAttributes;
uint16_t wMaxPktSize;
uint8_t bInterval;
} __attribute__((packed)) USB_EP_DSC;
// --------------------------------------------------------------------------------------
#define CDC_COMM_INTF_ID 0x00
#define CDC_COMM_UEP UEP2
#define CDC_INT_BD_IN ep2Bi
#define CDC_INT_EP_SIZE 8
#define CDC_DATA_INTF_ID 0x01
#define CDC_DATA_UEP UEP3
#define CDC_BULK_BD_OUT ep3Bo
#define CDC_BULK_OUT_EP_SIZE 8
#define CDC_BULK_BD_IN ep3Bi
#define CDC_BULK_IN_EP_SIZE 8
/* Class-Specific Requests */
#define SEND_ENCAPSULATED_COMMAND 0x00
#define GET_ENCAPSULATED_RESPONSE 0x01
#define SET_COMM_FEATURE 0x02
#define GET_COMM_FEATURE 0x03
#define CLEAR_COMM_FEATURE 0x04
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
/* Notifications *
* Note: Notifications are polled over
* Communication Interface (Interrupt Endpoint)
*/
#define NETWORK_CONNECTION 0x00
#define RESPONSE_AVAILABLE 0x01
#define SERIAL_STATE 0x20
/* Device Class Code */
#define CDC_DEVICE 0x02
/* Communication Interface Class Code */
#define COMM_INTF 0x02
/* Communication Interface Class SubClass Codes */
#define ABSTRACT_CONTROL_MODEL 0x02
/* Communication Interface Class Control Protocol Codes */
#define V25TER 0x01 // Common AT commands ("Hayes(TM)")
/* Data Interface Class Codes */
#define DATA_INTF 0x0A
/* Data Interface Class Protocol Codes */
#define NO_PROTOCOL 0x00 // No class specific protocol required
/* Communication Feature Selector Codes */
#define ABSTRACT_STATE 0x01
#define COUNTRY_SETTING 0x02
/* Functional Descriptors */
/* Type Values for the bDscType Field */
#define CS_INTERFACE 0x24
#define CS_ENDPOINT 0x25
/* bDscSubType in Functional Descriptors */
#define DSC_FN_HEADER 0x00
#define DSC_FN_CALL_MGT 0x01
#define DSC_FN_ACM 0x02 // ACM - Abstract Control Management
#define DSC_FN_DLM 0x03 // DLM - Direct Line Managment
#define DSC_FN_TELEPHONE_RINGER 0x04
#define DSC_FN_RPT_CAPABILITIES 0x05
#define DSC_FN_UNION 0x06
#define DSC_FN_COUNTRY_SELECTION 0x07
#define DSC_FN_TEL_OP_MODES 0x08
#define DSC_FN_USB_TERMINAL 0x09
/* more.... see Table 25 in USB CDC Specification 1.1 */
/* CDC Bulk IN transfer states */
#define CDC_TX_READY 0
#define CDC_TX_BUSY 1
#define CDC_TX_BUSY_ZLP 2 // ZLP: Zero Length Packet
#define CDC_TX_COMPLETING 3
/* Line Coding Structure */
#define LINE_CODING_LENGTH 0x07
/* Functional Descriptor Structure - See CDC Specification 1.1 for details */
// CDC header functional descriptor
typedef struct _USB_CDC_HEADER_FN_DSC {
uint8_t bFNLength;
uint8_t bDscType;
uint8_t bDscSubType;
uint16_t bcdCDC;
} __attribute__((packed)) USB_CDC_HEADER_FN_DSC;
// CDC Abstract Control Management Functional Descriptor
typedef struct _USB_CDC_ACM_FN_DSC {
uint8_t bFNLength;
uint8_t bDscType;
uint8_t bDscSubType;
uint8_t bmCapabilities;
} __attribute__((packed)) USB_CDC_ACM_FN_DSC;
// CDC Union Functional Descriptor
typedef struct _USB_CDC_UNION_FN_DSC {
uint8_t bFNLength;
uint8_t bDscType;
uint8_t bDscSubType;
uint8_t bMasterIntf;
uint8_t bSaveIntf0;
} __attribute__((packed)) USB_CDC_UNION_FN_DSC;
// CDC Call Management Functional Descriptor
typedef struct _USB_CDC_CALL_MGT_FN_DSC {
uint8_t bFNLength;
uint8_t bDscType;
uint8_t bDscSubType;
uint8_t bmCapabilities;
uint8_t bDataInterface;
} __attribute__((packed)) USB_CDC_CALL_MGT_FN_DSC;