COSA
An Object-Oriented Platform for Arduino Programming
Core.h
Go to the documentation of this file.
1 
22 #ifndef COSA_USB_CORE_H
23 #define COSA_USB_CORE_H
24 
25 #include "Cosa/Types.h"
26 
27 // Standard requests
28 #define GET_STATUS 0
29 #define CLEAR_FEATURE 1
30 #define SET_FEATURE 3
31 #define SET_ADDRESS 5
32 #define GET_DESCRIPTOR 6
33 #define SET_DESCRIPTOR 7
34 #define GET_CONFIGURATION 8
35 #define SET_CONFIGURATION 9
36 #define GET_INTERFACE 10
37 #define SET_INTERFACE 11
38 
39 // bmRequestType
40 #define REQUEST_HOSTTODEVICE 0x00
41 #define REQUEST_DEVICETOHOST 0x80
42 #define REQUEST_DIRECTION 0x80
43 
44 #define REQUEST_STANDARD 0x00
45 #define REQUEST_CLASS 0x20
46 #define REQUEST_VENDOR 0x40
47 #define REQUEST_TYPE 0x60
48 
49 #define REQUEST_DEVICE 0x00
50 #define REQUEST_INTERFACE 0x01
51 #define REQUEST_ENDPOINT 0x02
52 #define REQUEST_OTHER 0x03
53 #define REQUEST_RECIPIENT 0x03
54 
55 #define REQUEST_DEVICETOHOST_CLASS_INTERFACE \
56  (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
57 
58 #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE \
59  (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
60 
61 // Class requests
62 #define CDC_SET_LINE_CODING 0x20
63 #define CDC_GET_LINE_CODING 0x21
64 #define CDC_SET_CONTROL_LINE_STATE 0x22
65 #define CDC_SEND_BREAK 0x23
66 
67 #define MSC_RESET 0xFF
68 #define MSC_GET_MAX_LUN 0xFE
69 
70 #define HID_GET_REPORT 0x01
71 #define HID_GET_IDLE 0x02
72 #define HID_GET_PROTOCOL 0x03
73 #define HID_SET_REPORT 0x09
74 #define HID_SET_IDLE 0x0A
75 #define HID_SET_PROTOCOL 0x0B
76 
77 // Descriptors
78 
79 #define USB_DEVICE_DESC_SIZE 18
80 #define USB_CONFIGUARTION_DESC_SIZE 9
81 #define USB_INTERFACE_DESC_SIZE 9
82 #define USB_ENDPOINT_DESC_SIZE 7
83 
84 #define USB_DEVICE_DESCRIPTOR_TYPE 1
85 #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
86 #define USB_STRING_DESCRIPTOR_TYPE 3
87 #define USB_INTERFACE_DESCRIPTOR_TYPE 4
88 #define USB_ENDPOINT_DESCRIPTOR_TYPE 5
89 
90 #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
91 #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
92 #define USB_DEVICE_CLASS_STORAGE 0x08
93 #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
94 
95 #define USB_CONFIG_POWERED_MASK 0x40
96 #define USB_CONFIG_BUS_POWERED 0x80
97 #define USB_CONFIG_SELF_POWERED 0xC0
98 #define USB_CONFIG_REMOTE_WAKEUP 0x20
99 
100 // bMaxPower in Configuration Descriptor
101 #define USB_CONFIG_POWER_MA(mA) ((mA)/2)
102 
103 // bEndpointAddress in Endpoint Descriptor
104 #define USB_ENDPOINT_DIRECTION_MASK 0x80
105 #define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
106 #define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
107 
108 #define USB_ENDPOINT_TYPE_MASK 0x03
109 #define USB_ENDPOINT_TYPE_CONTROL 0x00
110 #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
111 #define USB_ENDPOINT_TYPE_BULK 0x02
112 #define USB_ENDPOINT_TYPE_INTERRUPT 0x03
113 
114 #define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
115 
116 #define CDC_V1_10 0x0110
117 #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
118 
119 #define CDC_CALL_MANAGEMENT 0x01
120 #define CDC_ABSTRACT_CONTROL_MODEL 0x02
121 #define CDC_HEADER 0x00
122 #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
123 #define CDC_UNION 0x06
124 #define CDC_CS_INTERFACE 0x24
125 #define CDC_CS_ENDPOINT 0x25
126 #define CDC_DATA_INTERFACE_CLASS 0x0A
127 
128 #define MSC_SUBCLASS_SCSI 0x06
129 #define MSC_PROTOCOL_BULK_ONLY 0x50
130 
131 #define HID_HID_DESCRIPTOR_TYPE 0x21
132 #define HID_REPORT_DESCRIPTOR_TYPE 0x22
133 #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
134 
135 
136 // Device
137 typedef struct {
138  uint8_t len;
139  uint8_t dtype;
140  uint16_t usbVersion;
141  uint8_t deviceClass;
142  uint8_t deviceSubClass;
143  uint8_t deviceProtocol;
144  uint8_t packetSize0;
145  uint16_t idVendor;
146  uint16_t idProduct;
147  uint16_t deviceVersion;
148  uint8_t iManufacturer;
149  uint8_t iProduct;
150  uint8_t iSerialNumber;
153 
154 // Config
155 typedef struct {
156  uint8_t len;
157  uint8_t dtype;
158  uint16_t clen;
159  uint8_t numInterfaces;
160  uint8_t config;
161  uint8_t iconfig;
162  uint8_t attributes;
163  uint8_t maxPower;
165 
166 // Interface
167 typedef struct {
168  uint8_t len;
169  uint8_t dtype;
170  uint8_t number;
171  uint8_t alternate;
172  uint8_t numEndpoints;
173  uint8_t interfaceClass;
175  uint8_t protocol;
176  uint8_t iInterface;
178 
179 // Endpoint
180 typedef struct {
181  uint8_t len;
182  uint8_t dtype;
183  uint8_t addr;
184  uint8_t attr;
185  uint16_t packetSize;
186  uint8_t interval;
188 
189 // Interface Association Descriptor
190 typedef struct {
191  uint8_t len;
192  uint8_t dtype;
193  uint8_t firstInterface;
194  uint8_t interfaceCount;
195  uint8_t functionClass;
198  uint8_t iInterface;
199 } IADDescriptor;
200 
201 // CDC CS interface descriptor
202 typedef struct {
203  uint8_t len;
204  uint8_t dtype;
205  uint8_t subtype;
206  uint8_t d0;
207  uint8_t d1;
209 
210 typedef struct {
211  uint8_t len;
212  uint8_t dtype;
213  uint8_t subtype;
214  uint8_t d0;
216 
217 typedef struct {
218  uint8_t len;
219  uint8_t dtype;
220  uint8_t subtype;
221  uint8_t bmCapabilities;
222  uint8_t bDataInterface;
224 
225 typedef struct {
226  uint8_t len;
227  uint8_t dtype;
228  uint8_t subtype;
229  uint8_t bmCapabilities;
231 
232 typedef struct {
233  // IAD
235 
236  // Control
243 
244  // Data
248 } CDCDescriptor;
249 
250 typedef struct {
254 } MSCDescriptor;
255 
256 typedef struct {
257  uint8_t len; // 9
258  uint8_t dtype; // 0x21
259  uint8_t addr;
260  uint8_t versionL; // 0x101
261  uint8_t versionH; // 0x101
262  uint8_t country;
263  uint8_t desctype; // 0x22 report
264  uint8_t descLenL;
265  uint8_t descLenH;
267 
268 typedef struct {
272 } HIDDescriptor;
273 
274 #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
275  { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
276 
277 #define D_CONFIG(_totalLength,_interfaces) \
278  { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
279 
280 #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
281  { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
282 
283 #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
284  { 7, 5, _addr,_attr,_packetSize, _interval }
285 
286 #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
287  { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
288 
289 #define D_HIDREPORT(_descriptorLength) \
290  { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
291 
292 #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
293 #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
294 
295 #endif
InterfaceDescriptor dif
Definition: Core.h:245
CDCCSInterfaceDescriptor functionalDescriptor
Definition: Core.h:241
uint8_t len
Definition: Core.h:191
uint16_t usbVersion
Definition: Core.h:140
uint8_t iInterface
Definition: Core.h:198
uint8_t number
Definition: Core.h:170
uint8_t addr
Definition: Core.h:183
uint16_t packetSize
Definition: Core.h:185
EndpointDescriptor in
Definition: Core.h:271
uint8_t numInterfaces
Definition: Core.h:159
uint16_t deviceVersion
Definition: Core.h:147
EndpointDescriptor in
Definition: Core.h:246
uint8_t maxPower
Definition: Core.h:163
uint8_t bmCapabilities
Definition: Core.h:221
uint8_t deviceClass
Definition: Core.h:141
uint8_t attributes
Definition: Core.h:162
uint8_t len
Definition: Core.h:138
uint16_t clen
Definition: Core.h:158
EndpointDescriptor out
Definition: Core.h:253
uint8_t iconfig
Definition: Core.h:161
uint8_t interval
Definition: Core.h:186
uint8_t alternate
Definition: Core.h:171
uint8_t numEndpoints
Definition: Core.h:172
uint8_t packetSize0
Definition: Core.h:144
uint8_t interfaceClass
Definition: Core.h:173
CMFunctionalDescriptor callManagement
Definition: Core.h:239
uint8_t dtype
Definition: Core.h:192
IADDescriptor iad
Definition: Core.h:234
uint8_t versionL
Definition: Core.h:260
uint8_t iSerialNumber
Definition: Core.h:150
EndpointDescriptor out
Definition: Core.h:247
uint8_t iManufacturer
Definition: Core.h:148
uint8_t bNumConfigurations
Definition: Core.h:151
uint8_t bmCapabilities
Definition: Core.h:229
ACMFunctionalDescriptor controlManagement
Definition: Core.h:240
uint8_t bDataInterface
Definition: Core.h:222
EndpointDescriptor cifin
Definition: Core.h:242
HIDDescDescriptor desc
Definition: Core.h:270
uint8_t deviceProtocol
Definition: Core.h:143
uint8_t desctype
Definition: Core.h:263
uint8_t deviceSubClass
Definition: Core.h:142
uint8_t iProduct
Definition: Core.h:149
InterfaceDescriptor cif
Definition: Core.h:237
uint8_t config
Definition: Core.h:160
uint8_t addr
Definition: Core.h:259
EndpointDescriptor in
Definition: Core.h:252
CDCCSInterfaceDescriptor header
Definition: Core.h:238
uint8_t dtype
Definition: Core.h:169
uint8_t protocol
Definition: Core.h:175
uint8_t interfaceCount
Definition: Core.h:194
uint8_t functionProtocol
Definition: Core.h:197
uint8_t funtionSubClass
Definition: Core.h:196
uint8_t len
Definition: Core.h:181
uint8_t dtype
Definition: Core.h:139
uint16_t idProduct
Definition: Core.h:146
uint16_t idVendor
Definition: Core.h:145
uint8_t country
Definition: Core.h:262
uint8_t descLenH
Definition: Core.h:265
uint8_t descLenL
Definition: Core.h:264
InterfaceDescriptor hid
Definition: Core.h:269
uint8_t dtype
Definition: Core.h:258
uint8_t functionClass
Definition: Core.h:195
InterfaceDescriptor msc
Definition: Core.h:251
uint8_t firstInterface
Definition: Core.h:193
uint8_t len
Definition: Core.h:168
uint8_t dtype
Definition: Core.h:182
uint8_t iInterface
Definition: Core.h:176
uint8_t len
Definition: Core.h:257
uint8_t versionH
Definition: Core.h:261
uint8_t dtype
Definition: Core.h:157
uint8_t attr
Definition: Core.h:184
uint8_t interfaceSubClass
Definition: Core.h:174
uint8_t len
Definition: Core.h:156