Fri, 17 Nov 2017 10:13:31 +0100
proper configuration, homing and planner optimization
2 | 1 | |
2 | // Copyright (c) 2010, Peter Barrett | |
3 | /* | |
4 | ** Permission to use, copy, modify, and/or distribute this software for | |
5 | ** any purpose with or without fee is hereby granted, provided that the | |
6 | ** above copyright notice and this permission notice appear in all copies. | |
7 | ** | |
8 | ** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL | |
9 | ** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED | |
10 | ** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR | |
11 | ** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES | |
12 | ** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
13 | ** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
14 | ** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
15 | ** SOFTWARE. | |
16 | */ | |
17 | ||
18 | #ifndef __USBCORE_H__ | |
19 | #define __USBCORE_H__ | |
20 | ||
21 | // Standard requests | |
22 | #define GET_STATUS 0 | |
23 | #define CLEAR_FEATURE 1 | |
24 | #define SET_FEATURE 3 | |
25 | #define SET_ADDRESS 5 | |
26 | #define GET_DESCRIPTOR 6 | |
27 | #define SET_DESCRIPTOR 7 | |
28 | #define GET_CONFIGURATION 8 | |
29 | #define SET_CONFIGURATION 9 | |
30 | #define GET_INTERFACE 10 | |
31 | #define SET_INTERFACE 11 | |
32 | ||
33 | ||
34 | // bmRequestType | |
35 | #define REQUEST_HOSTTODEVICE 0x00 | |
36 | #define REQUEST_DEVICETOHOST 0x80 | |
37 | #define REQUEST_DIRECTION 0x80 | |
38 | ||
39 | #define REQUEST_STANDARD 0x00 | |
40 | #define REQUEST_CLASS 0x20 | |
41 | #define REQUEST_VENDOR 0x40 | |
42 | #define REQUEST_TYPE 0x60 | |
43 | ||
44 | #define REQUEST_DEVICE 0x00 | |
45 | #define REQUEST_INTERFACE 0x01 | |
46 | #define REQUEST_ENDPOINT 0x02 | |
47 | #define REQUEST_OTHER 0x03 | |
48 | #define REQUEST_RECIPIENT 0x03 | |
49 | ||
50 | #define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE) | |
51 | #define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE) | |
52 | ||
53 | // Class requests | |
54 | ||
55 | #define CDC_SET_LINE_CODING 0x20 | |
56 | #define CDC_GET_LINE_CODING 0x21 | |
57 | #define CDC_SET_CONTROL_LINE_STATE 0x22 | |
58 | ||
59 | #define MSC_RESET 0xFF | |
60 | #define MSC_GET_MAX_LUN 0xFE | |
61 | ||
62 | #define HID_GET_REPORT 0x01 | |
63 | #define HID_GET_IDLE 0x02 | |
64 | #define HID_GET_PROTOCOL 0x03 | |
65 | #define HID_SET_REPORT 0x09 | |
66 | #define HID_SET_IDLE 0x0A | |
67 | #define HID_SET_PROTOCOL 0x0B | |
68 | ||
69 | // Descriptors | |
70 | ||
71 | #define USB_DEVICE_DESC_SIZE 18 | |
72 | #define USB_CONFIGUARTION_DESC_SIZE 9 | |
73 | #define USB_INTERFACE_DESC_SIZE 9 | |
74 | #define USB_ENDPOINT_DESC_SIZE 7 | |
75 | ||
76 | #define USB_DEVICE_DESCRIPTOR_TYPE 1 | |
77 | #define USB_CONFIGURATION_DESCRIPTOR_TYPE 2 | |
78 | #define USB_STRING_DESCRIPTOR_TYPE 3 | |
79 | #define USB_INTERFACE_DESCRIPTOR_TYPE 4 | |
80 | #define USB_ENDPOINT_DESCRIPTOR_TYPE 5 | |
81 | ||
82 | #define USB_DEVICE_CLASS_COMMUNICATIONS 0x02 | |
83 | #define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03 | |
84 | #define USB_DEVICE_CLASS_STORAGE 0x08 | |
85 | #define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF | |
86 | ||
87 | #define USB_CONFIG_POWERED_MASK 0x40 | |
88 | #define USB_CONFIG_BUS_POWERED 0x80 | |
89 | #define USB_CONFIG_SELF_POWERED 0xC0 | |
90 | #define USB_CONFIG_REMOTE_WAKEUP 0x20 | |
91 | ||
92 | // bMaxPower in Configuration Descriptor | |
93 | #define USB_CONFIG_POWER_MA(mA) ((mA)/2) | |
94 | ||
95 | // bEndpointAddress in Endpoint Descriptor | |
96 | #define USB_ENDPOINT_DIRECTION_MASK 0x80 | |
97 | #define USB_ENDPOINT_OUT(addr) ((addr) | 0x00) | |
98 | #define USB_ENDPOINT_IN(addr) ((addr) | 0x80) | |
99 | ||
100 | #define USB_ENDPOINT_TYPE_MASK 0x03 | |
101 | #define USB_ENDPOINT_TYPE_CONTROL 0x00 | |
102 | #define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01 | |
103 | #define USB_ENDPOINT_TYPE_BULK 0x02 | |
104 | #define USB_ENDPOINT_TYPE_INTERRUPT 0x03 | |
105 | ||
106 | #define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF) | |
107 | ||
108 | #define CDC_V1_10 0x0110 | |
109 | #define CDC_COMMUNICATION_INTERFACE_CLASS 0x02 | |
110 | ||
111 | #define CDC_CALL_MANAGEMENT 0x01 | |
112 | #define CDC_ABSTRACT_CONTROL_MODEL 0x02 | |
113 | #define CDC_HEADER 0x00 | |
114 | #define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02 | |
115 | #define CDC_UNION 0x06 | |
116 | #define CDC_CS_INTERFACE 0x24 | |
117 | #define CDC_CS_ENDPOINT 0x25 | |
118 | #define CDC_DATA_INTERFACE_CLASS 0x0A | |
119 | ||
120 | #define MSC_SUBCLASS_SCSI 0x06 | |
121 | #define MSC_PROTOCOL_BULK_ONLY 0x50 | |
122 | ||
123 | #define HID_HID_DESCRIPTOR_TYPE 0x21 | |
124 | #define HID_REPORT_DESCRIPTOR_TYPE 0x22 | |
125 | #define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23 | |
126 | ||
127 | ||
128 | // Device | |
129 | typedef struct { | |
130 | u8 len; // 18 | |
131 | u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE | |
132 | u16 usbVersion; // 0x200 | |
133 | u8 deviceClass; | |
134 | u8 deviceSubClass; | |
135 | u8 deviceProtocol; | |
136 | u8 packetSize0; // Packet 0 | |
137 | u16 idVendor; | |
138 | u16 idProduct; | |
139 | u16 deviceVersion; // 0x100 | |
140 | u8 iManufacturer; | |
141 | u8 iProduct; | |
142 | u8 iSerialNumber; | |
143 | u8 bNumConfigurations; | |
144 | } DeviceDescriptor; | |
145 | ||
146 | // Config | |
147 | typedef struct { | |
148 | u8 len; // 9 | |
149 | u8 dtype; // 2 | |
150 | u16 clen; // total length | |
151 | u8 numInterfaces; | |
152 | u8 config; | |
153 | u8 iconfig; | |
154 | u8 attributes; | |
155 | u8 maxPower; | |
156 | } ConfigDescriptor; | |
157 | ||
158 | // String | |
159 | ||
160 | // Interface | |
161 | typedef struct | |
162 | { | |
163 | u8 len; // 9 | |
164 | u8 dtype; // 4 | |
165 | u8 number; | |
166 | u8 alternate; | |
167 | u8 numEndpoints; | |
168 | u8 interfaceClass; | |
169 | u8 interfaceSubClass; | |
170 | u8 protocol; | |
171 | u8 iInterface; | |
172 | } InterfaceDescriptor; | |
173 | ||
174 | // Endpoint | |
175 | typedef struct | |
176 | { | |
177 | u8 len; // 7 | |
178 | u8 dtype; // 5 | |
179 | u8 addr; | |
180 | u8 attr; | |
181 | u16 packetSize; | |
182 | u8 interval; | |
183 | } EndpointDescriptor; | |
184 | ||
185 | // Interface Association Descriptor | |
186 | // Used to bind 2 interfaces together in CDC compostite device | |
187 | typedef struct | |
188 | { | |
189 | u8 len; // 8 | |
190 | u8 dtype; // 11 | |
191 | u8 firstInterface; | |
192 | u8 interfaceCount; | |
193 | u8 functionClass; | |
194 | u8 funtionSubClass; | |
195 | u8 functionProtocol; | |
196 | u8 iInterface; | |
197 | } IADDescriptor; | |
198 | ||
199 | // CDC CS interface descriptor | |
200 | typedef struct | |
201 | { | |
202 | u8 len; // 5 | |
203 | u8 dtype; // 0x24 | |
204 | u8 subtype; | |
205 | u8 d0; | |
206 | u8 d1; | |
207 | } CDCCSInterfaceDescriptor; | |
208 | ||
209 | typedef struct | |
210 | { | |
211 | u8 len; // 4 | |
212 | u8 dtype; // 0x24 | |
213 | u8 subtype; | |
214 | u8 d0; | |
215 | } CDCCSInterfaceDescriptor4; | |
216 | ||
217 | typedef struct | |
218 | { | |
219 | u8 len; | |
220 | u8 dtype; // 0x24 | |
221 | u8 subtype; // 1 | |
222 | u8 bmCapabilities; | |
223 | u8 bDataInterface; | |
224 | } CMFunctionalDescriptor; | |
225 | ||
226 | typedef struct | |
227 | { | |
228 | u8 len; | |
229 | u8 dtype; // 0x24 | |
230 | u8 subtype; // 1 | |
231 | u8 bmCapabilities; | |
232 | } ACMFunctionalDescriptor; | |
233 | ||
234 | typedef struct | |
235 | { | |
236 | // IAD | |
237 | IADDescriptor iad; // Only needed on compound device | |
238 | ||
239 | // Control | |
240 | InterfaceDescriptor cif; // | |
241 | CDCCSInterfaceDescriptor header; | |
242 | CMFunctionalDescriptor callManagement; // Call Management | |
243 | ACMFunctionalDescriptor controlManagement; // ACM | |
244 | CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION | |
245 | EndpointDescriptor cifin; | |
246 | ||
247 | // Data | |
248 | InterfaceDescriptor dif; | |
249 | EndpointDescriptor in; | |
250 | EndpointDescriptor out; | |
251 | } CDCDescriptor; | |
252 | ||
253 | typedef struct | |
254 | { | |
255 | InterfaceDescriptor msc; | |
256 | EndpointDescriptor in; | |
257 | EndpointDescriptor out; | |
258 | } MSCDescriptor; | |
259 | ||
260 | typedef struct | |
261 | { | |
262 | u8 len; // 9 | |
263 | u8 dtype; // 0x21 | |
264 | u8 addr; | |
265 | u8 versionL; // 0x101 | |
266 | u8 versionH; // 0x101 | |
267 | u8 country; | |
268 | u8 desctype; // 0x22 report | |
269 | u8 descLenL; | |
270 | u8 descLenH; | |
271 | } HIDDescDescriptor; | |
272 | ||
273 | typedef struct | |
274 | { | |
275 | InterfaceDescriptor hid; | |
276 | HIDDescDescriptor desc; | |
277 | EndpointDescriptor in; | |
278 | } HIDDescriptor; | |
279 | ||
280 | ||
281 | #define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \ | |
282 | { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs } | |
283 | ||
284 | #define D_CONFIG(_totalLength,_interfaces) \ | |
285 | { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) } | |
286 | ||
287 | #define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \ | |
288 | { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 } | |
289 | ||
290 | #define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \ | |
291 | { 7, 5, _addr,_attr,_packetSize, _interval } | |
292 | ||
293 | #define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \ | |
294 | { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 } | |
295 | ||
296 | #define D_HIDREPORT(_descriptorLength) \ | |
297 | { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 } | |
298 | ||
299 | #define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 } | |
300 | #define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 } | |
301 | ||
302 | ||
303 | #endif |