-
Notifications
You must be signed in to change notification settings - Fork 7
/
usb_device_local.h
424 lines (393 loc) · 17.2 KB
/
usb_device_local.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright 2015 Microchip Technology Inc. (www.microchip.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
To request to license the code under the MLA license (www.microchip.com/mla_license),
please contact [email protected]
*******************************************************************************/
//DOM-IGNORE-END
#include "usb_config.h"
/* Short Packet States - Used by Control Transfer Read - CTRL_TRF_TX */
#define SHORT_PKT_NOT_USED 0
#define SHORT_PKT_PENDING 1
#define SHORT_PKT_SENT 2
/* Control Transfer States */
#define WAIT_SETUP 0
#define CTRL_TRF_TX 1
#define CTRL_TRF_RX 2
typedef union
{
struct
{
unsigned char ping_pong_state :1;
unsigned char transfer_terminated :1;
} bits;
uint8_t Val;
} EP_STATUS;
#if (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0000 // Used in USB Device Mode only
#define USB_NEXT_EP0_IN_PING_PONG 0x0000 // Used in USB Device Mode only
#define USB_NEXT_PING_PONG 0x0000 // Used in USB Device Mode only
#define EP0_OUT_EVEN 0 // Used in USB Device Mode only
#define EP0_OUT_ODD 0 // Used in USB Device Mode only
#define EP0_IN_EVEN 1 // Used in USB Device Mode only
#define EP0_IN_ODD 1 // Used in USB Device Mode only
#define EP1_OUT_EVEN 2 // Used in USB Device Mode only
#define EP1_OUT_ODD 2 // Used in USB Device Mode only
#define EP1_IN_EVEN 3 // Used in USB Device Mode only
#define EP1_IN_ODD 3 // Used in USB Device Mode only
#define EP2_OUT_EVEN 4 // Used in USB Device Mode only
#define EP2_OUT_ODD 4 // Used in USB Device Mode only
#define EP2_IN_EVEN 5 // Used in USB Device Mode only
#define EP2_IN_ODD 5 // Used in USB Device Mode only
#define EP3_OUT_EVEN 6 // Used in USB Device Mode only
#define EP3_OUT_ODD 6 // Used in USB Device Mode only
#define EP3_IN_EVEN 7 // Used in USB Device Mode only
#define EP3_IN_ODD 7 // Used in USB Device Mode only
#define EP4_OUT_EVEN 8 // Used in USB Device Mode only
#define EP4_OUT_ODD 8 // Used in USB Device Mode only
#define EP4_IN_EVEN 9 // Used in USB Device Mode only
#define EP4_IN_ODD 9 // Used in USB Device Mode only
#define EP5_OUT_EVEN 10 // Used in USB Device Mode only
#define EP5_OUT_ODD 10 // Used in USB Device Mode only
#define EP5_IN_EVEN 11 // Used in USB Device Mode only
#define EP5_IN_ODD 11 // Used in USB Device Mode only
#define EP6_OUT_EVEN 12 // Used in USB Device Mode only
#define EP6_OUT_ODD 12 // Used in USB Device Mode only
#define EP6_IN_EVEN 13 // Used in USB Device Mode only
#define EP6_IN_ODD 13 // Used in USB Device Mode only
#define EP7_OUT_EVEN 14 // Used in USB Device Mode only
#define EP7_OUT_ODD 14 // Used in USB Device Mode only
#define EP7_IN_EVEN 15 // Used in USB Device Mode only
#define EP7_IN_ODD 15 // Used in USB Device Mode only
#define EP8_OUT_EVEN 16 // Used in USB Device Mode only
#define EP8_OUT_ODD 16 // Used in USB Device Mode only
#define EP8_IN_EVEN 17 // Used in USB Device Mode only
#define EP8_IN_ODD 17 // Used in USB Device Mode only
#define EP9_OUT_EVEN 18 // Used in USB Device Mode only
#define EP9_OUT_ODD 18 // Used in USB Device Mode only
#define EP9_IN_EVEN 19 // Used in USB Device Mode only
#define EP9_IN_ODD 19 // Used in USB Device Mode only
#define EP10_OUT_EVEN 20 // Used in USB Device Mode only
#define EP10_OUT_ODD 20 // Used in USB Device Mode only
#define EP10_IN_EVEN 21 // Used in USB Device Mode only
#define EP10_IN_ODD 21 // Used in USB Device Mode only
#define EP11_OUT_EVEN 22 // Used in USB Device Mode only
#define EP11_OUT_ODD 22 // Used in USB Device Mode only
#define EP11_IN_EVEN 23 // Used in USB Device Mode only
#define EP11_IN_ODD 23 // Used in USB Device Mode only
#define EP12_OUT_EVEN 24 // Used in USB Device Mode only
#define EP12_OUT_ODD 24 // Used in USB Device Mode only
#define EP12_IN_EVEN 25 // Used in USB Device Mode only
#define EP12_IN_ODD 25 // Used in USB Device Mode only
#define EP13_OUT_EVEN 26 // Used in USB Device Mode only
#define EP13_OUT_ODD 26 // Used in USB Device Mode only
#define EP13_IN_EVEN 27 // Used in USB Device Mode only
#define EP13_IN_ODD 27 // Used in USB Device Mode only
#define EP14_OUT_EVEN 28 // Used in USB Device Mode only
#define EP14_OUT_ODD 28 // Used in USB Device Mode only
#define EP14_IN_EVEN 29 // Used in USB Device Mode only
#define EP14_IN_ODD 29 // Used in USB Device Mode only
#define EP15_OUT_EVEN 30 // Used in USB Device Mode only
#define EP15_OUT_ODD 30 // Used in USB Device Mode only
#define EP15_IN_EVEN 31 // Used in USB Device Mode only
#define EP15_IN_ODD 31 // Used in USB Device Mode only
#define EP(ep,dir,pp) (2*ep+dir) // Used in USB Device Mode only
#define BD(ep,dir,pp) ((8 * ep) + (4 * dir)) // Used in USB Device Mode only
#elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0004
#define USB_NEXT_EP0_IN_PING_PONG 0x0000
#define USB_NEXT_PING_PONG 0x0000
#define EP0_OUT_EVEN 0
#define EP0_OUT_ODD 1
#define EP0_IN_EVEN 2
#define EP0_IN_ODD 2
#define EP1_OUT_EVEN 3
#define EP1_OUT_ODD 3
#define EP1_IN_EVEN 4
#define EP1_IN_ODD 4
#define EP2_OUT_EVEN 5
#define EP2_OUT_ODD 5
#define EP2_IN_EVEN 6
#define EP2_IN_ODD 6
#define EP3_OUT_EVEN 7
#define EP3_OUT_ODD 7
#define EP3_IN_EVEN 8
#define EP3_IN_ODD 8
#define EP4_OUT_EVEN 9
#define EP4_OUT_ODD 9
#define EP4_IN_EVEN 10
#define EP4_IN_ODD 10
#define EP5_OUT_EVEN 11
#define EP5_OUT_ODD 11
#define EP5_IN_EVEN 12
#define EP5_IN_ODD 12
#define EP6_OUT_EVEN 13
#define EP6_OUT_ODD 13
#define EP6_IN_EVEN 14
#define EP6_IN_ODD 14
#define EP7_OUT_EVEN 15
#define EP7_OUT_ODD 15
#define EP7_IN_EVEN 16
#define EP7_IN_ODD 16
#define EP8_OUT_EVEN 17
#define EP8_OUT_ODD 17
#define EP8_IN_EVEN 18
#define EP8_IN_ODD 18
#define EP9_OUT_EVEN 19
#define EP9_OUT_ODD 19
#define EP9_IN_EVEN 20
#define EP9_IN_ODD 20
#define EP10_OUT_EVEN 21
#define EP10_OUT_ODD 21
#define EP10_IN_EVEN 22
#define EP10_IN_ODD 22
#define EP11_OUT_EVEN 23
#define EP11_OUT_ODD 23
#define EP11_IN_EVEN 24
#define EP11_IN_ODD 24
#define EP12_OUT_EVEN 25
#define EP12_OUT_ODD 25
#define EP12_IN_EVEN 26
#define EP12_IN_ODD 26
#define EP13_OUT_EVEN 27
#define EP13_OUT_ODD 27
#define EP13_IN_EVEN 28
#define EP13_IN_ODD 28
#define EP14_OUT_EVEN 29
#define EP14_OUT_ODD 29
#define EP14_IN_EVEN 30
#define EP14_IN_ODD 30
#define EP15_OUT_EVEN 31
#define EP15_OUT_ODD 31
#define EP15_IN_EVEN 32
#define EP15_IN_ODD 32
#define EP(ep,dir,pp) (2u*ep+dir+(((ep==0)&&(dir==0))?pp:1))
#define BD(ep,dir,pp) (4u*((2u*ep)+dir+(((ep==0)&&(dir==0))?pp:1)))
#elif (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG)
#if defined (__18CXX) || defined(__C30__) || defined __XC16__ || defined(__XC8)
#if (defined (__dsPIC33E__) || defined (__PIC24E__))
#define USB_NEXT_EP0_OUT_PING_PONG 0x0008
#define USB_NEXT_EP0_IN_PING_PONG 0x0008
#define USB_NEXT_PING_PONG 0x0008
#else
#define USB_NEXT_EP0_OUT_PING_PONG 0x0004
#define USB_NEXT_EP0_IN_PING_PONG 0x0004
#define USB_NEXT_PING_PONG 0x0004
#endif
#elif defined(__C32__)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0008
#define USB_NEXT_EP0_IN_PING_PONG 0x0008
#define USB_NEXT_PING_PONG 0x0008
#else
#error "Not defined for this compiler"
#endif
#define EP0_OUT_EVEN 0
#define EP0_OUT_ODD 1
#define EP0_IN_EVEN 2
#define EP0_IN_ODD 3
#define EP1_OUT_EVEN 4
#define EP1_OUT_ODD 5
#define EP1_IN_EVEN 6
#define EP1_IN_ODD 7
#define EP2_OUT_EVEN 8
#define EP2_OUT_ODD 9
#define EP2_IN_EVEN 10
#define EP2_IN_ODD 11
#define EP3_OUT_EVEN 12
#define EP3_OUT_ODD 13
#define EP3_IN_EVEN 14
#define EP3_IN_ODD 15
#define EP4_OUT_EVEN 16
#define EP4_OUT_ODD 17
#define EP4_IN_EVEN 18
#define EP4_IN_ODD 19
#define EP5_OUT_EVEN 20
#define EP5_OUT_ODD 21
#define EP5_IN_EVEN 22
#define EP5_IN_ODD 23
#define EP6_OUT_EVEN 24
#define EP6_OUT_ODD 25
#define EP6_IN_EVEN 26
#define EP6_IN_ODD 27
#define EP7_OUT_EVEN 28
#define EP7_OUT_ODD 29
#define EP7_IN_EVEN 30
#define EP7_IN_ODD 31
#define EP8_OUT_EVEN 32
#define EP8_OUT_ODD 33
#define EP8_IN_EVEN 34
#define EP8_IN_ODD 35
#define EP9_OUT_EVEN 36
#define EP9_OUT_ODD 37
#define EP9_IN_EVEN 38
#define EP9_IN_ODD 39
#define EP10_OUT_EVEN 40
#define EP10_OUT_ODD 41
#define EP10_IN_EVEN 42
#define EP10_IN_ODD 43
#define EP11_OUT_EVEN 44
#define EP11_OUT_ODD 45
#define EP11_IN_EVEN 46
#define EP11_IN_ODD 47
#define EP12_OUT_EVEN 48
#define EP12_OUT_ODD 49
#define EP12_IN_EVEN 50
#define EP12_IN_ODD 51
#define EP13_OUT_EVEN 52
#define EP13_OUT_ODD 53
#define EP13_IN_EVEN 54
#define EP13_IN_ODD 55
#define EP14_OUT_EVEN 56
#define EP14_OUT_ODD 57
#define EP14_IN_EVEN 58
#define EP14_IN_ODD 59
#define EP15_OUT_EVEN 60
#define EP15_OUT_ODD 61
#define EP15_IN_EVEN 62
#define EP15_IN_ODD 63
#define EP(ep,dir,pp) (4*ep+2*dir+pp)
#if defined (__18CXX) || defined(__C30__) || defined __XC16__ || (__XC8)
#if (defined(__dsPIC33E__) || defined (__PIC24E__))
#define BD(ep,dir,pp) (8*(4*ep+2*dir+pp))
#else
#define BD(ep,dir,pp) (4*(4*ep+2*dir+pp))
#endif
#elif defined(__C32__)
#define BD(ep,dir,pp) (8*(4*ep+2*dir+pp))
#else
#error "Not defined for this compiler"
#endif
#elif (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0)
#define USB_NEXT_EP0_OUT_PING_PONG 0x0000
#define USB_NEXT_EP0_IN_PING_PONG 0x0000
#define USB_NEXT_PING_PONG 0x0004
#define EP0_OUT_EVEN 0
#define EP0_OUT_ODD 0
#define EP0_IN_EVEN 1
#define EP0_IN_ODD 1
#define EP1_OUT_EVEN 2
#define EP1_OUT_ODD 3
#define EP1_IN_EVEN 4
#define EP1_IN_ODD 5
#define EP2_OUT_EVEN 6
#define EP2_OUT_ODD 7
#define EP2_IN_EVEN 8
#define EP2_IN_ODD 9
#define EP3_OUT_EVEN 10
#define EP3_OUT_ODD 11
#define EP3_IN_EVEN 12
#define EP3_IN_ODD 13
#define EP4_OUT_EVEN 14
#define EP4_OUT_ODD 15
#define EP4_IN_EVEN 16
#define EP4_IN_ODD 17
#define EP5_OUT_EVEN 18
#define EP5_OUT_ODD 19
#define EP5_IN_EVEN 20
#define EP5_IN_ODD 21
#define EP6_OUT_EVEN 22
#define EP6_OUT_ODD 23
#define EP6_IN_EVEN 24
#define EP6_IN_ODD 25
#define EP7_OUT_EVEN 26
#define EP7_OUT_ODD 27
#define EP7_IN_EVEN 28
#define EP7_IN_ODD 29
#define EP8_OUT_EVEN 30
#define EP8_OUT_ODD 31
#define EP8_IN_EVEN 32
#define EP8_IN_ODD 33
#define EP9_OUT_EVEN 34
#define EP9_OUT_ODD 35
#define EP9_IN_EVEN 36
#define EP9_IN_ODD 37
#define EP10_OUT_EVEN 38
#define EP10_OUT_ODD 39
#define EP10_IN_EVEN 40
#define EP10_IN_ODD 41
#define EP11_OUT_EVEN 42
#define EP11_OUT_ODD 43
#define EP11_IN_EVEN 44
#define EP11_IN_ODD 45
#define EP12_OUT_EVEN 46
#define EP12_OUT_ODD 47
#define EP12_IN_EVEN 48
#define EP12_IN_ODD 49
#define EP13_OUT_EVEN 50
#define EP13_OUT_ODD 51
#define EP13_IN_EVEN 52
#define EP13_IN_ODD 53
#define EP14_OUT_EVEN 54
#define EP14_OUT_ODD 55
#define EP14_IN_EVEN 56
#define EP14_IN_ODD 57
#define EP15_OUT_EVEN 58
#define EP15_OUT_ODD 59
#define EP15_IN_EVEN 60
#define EP15_IN_ODD 61
#define EP(ep,dir,pp) (4*ep+2*dir+((ep==0)?0:(pp-2)))
#define BD(ep,dir,pp) (4*(4*ep+2*dir+((ep==0)?0:(pp-2))))
#else
#error "No ping pong mode defined."
#endif
/****** Event callback enabling/disabling macros ********************
This section of code is used to disable specific USB events that may not be
desired by the user. This can save code size and increase throughput and
decrease CPU utiliazation.
********************************************************************/
#if defined USB_DISABLE_SUSPEND_HANDLER
#define USB_SUSPEND_HANDLER(event,pointer,size)
#warning "Disabling the suspend handler is not recommended. Proper suspend handling is required to create a compliant USB device."
#else
#define USB_SUSPEND_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_WAKEUP_FROM_SUSPEND_HANDLER
#define USB_WAKEUP_FROM_SUSPEND_HANDLER(event,pointer,size)
#warning "Disabling the wake from suspend handler is not recommended. Proper suspend handling is required to create a compliant USB device."
#else
#define USB_WAKEUP_FROM_SUSPEND_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_SOF_HANDLER
#define USB_SOF_HANDLER(event,pointer,size)
#else
#define USB_SOF_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_TRANSFER_TERMINATED_HANDLER
#define USB_TRANSFER_TERMINATED_HANDLER(event,pointer,size)
#else
#define USB_TRANSFER_TERMINATED_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_ERROR_HANDLER
#define USB_ERROR_HANDLER(event,pointer,size)
#else
#define USB_ERROR_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_NONSTANDARD_EP0_REQUEST_HANDLER
#define USB_NONSTANDARD_EP0_REQUEST_HANDLER(event,pointer,size)
#else
#define USB_NONSTANDARD_EP0_REQUEST_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_SET_DESCRIPTOR_HANDLER
#define USB_SET_DESCRIPTOR_HANDLER(event,pointer,size)
#else
#define USB_SET_DESCRIPTOR_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_SET_CONFIGURATION_HANDLER
#define USB_SET_CONFIGURATION_HANDLER(event,pointer,size)
#else
#define USB_SET_CONFIGURATION_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif
#if defined USB_DISABLE_TRANSFER_COMPLETE_HANDLER
#define USB_TRANSFER_COMPLETE_HANDLER(event,pointer,size)
#else
#define USB_TRANSFER_COMPLETE_HANDLER(event,pointer,size) USER_USB_CALLBACK_EVENT_HANDLER(event,pointer,size)
#endif