-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathusb_hal_local.h
More file actions
564 lines (436 loc) · 17.1 KB
/
usb_hal_local.h
File metadata and controls
564 lines (436 loc) · 17.1 KB
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// 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 mla_licensing@microchip.com
*******************************************************************************/
//DOM-IGNORE-END
#ifndef _USB_HAL_LOCAL_H_
#define _USB_HAL_LOCAL_H_
#include "usb.h"
#if defined (__18CXX) || defined(__XC8)
#include "usb_pic18_local.h"
#elif defined (__C30__) || defined __XC16__
#if defined (__dsPIC33EP512MU810__)
#include <p33Exxxx.h>
#endif
#elif defined (__PIC32MX__) || defined (__PIC32MM__)
#include <p32xxxx.h>
#else
#error "Error! Unsupported processor"
#endif
// Misc Definitions:
#ifndef USB_DEVICE_ATTACH_DEBOUNCE_TIME
#define USB_DEVICE_ATTACH_DEBOUNCE_TIME 100 // 100 ms default time
#endif
#ifndef LOCAL_INLINE
#define LOCAL_INLINE static inline
#endif
#define OUT 0 //
#define IN 1 //
/* Endpoint control value and mask bits */
#define EP_HSHK 0x01 // Enable handshake
#define EP_STALL 0x02 // Stall endpoint
#define EP_EP_TX_EN 0x04 // Enable transmitting from endpoint
#define EP_EP_RX_EN 0x08 // Enable receiving from endpoint
#define EP_EP_CTL_DIS 0x10 // Disable control transfers to/from endpoint
#define EP_RETRY_DIS 0x40 // (Host Only) No retry of NAK'd transactions
#define EP_HOST_WOHUB 0x80 // (Host Only) Allow connection to low-speed hub
// Status mask
#if defined(__18CXX) || defined(__XC8)
#define UIR_SOF_TOK (1<<6)
#define UIR_TOK_DNE (1<<3)
#define UIR_USB_RST (1<<0)
#define UIR_UERR (1<<1)
#define UIR_UIDLE (1<<4)
#define UIR_STALL (1<<5)
#define STATUS_MASK (UIR_USB_RST|UIR_UERR|UIR_SOF_TOK|UIR_TOK_DNE| \
UIR_UIDLE|UIR_STALL)
#elif defined(__C30__) || defined __XC16__
// Status Bits
#define UIR_USB_RST 0x0001
#define UIR_UERR 0x0002
#define UIR_SOF_TOK 0x0004
#define UIR_TOK_DNE 0x0008
#define UIR_UIDLE 0x0010
#define UIR_RESUME 0x0020
#define UIR_ATTACH 0x0040
#define UIR_STALL 0x0080
// Error Status Bits
#define UEIR_PID_ERR 0x0001
#define UEIR_CRC5 0x0002
#define UEIR_HOST_EOF 0x0002
#define UEIR_CRC16 0x0004
#define UEIR_DFN8 0x0008
#define UEIR_BTO_ERR 0x0010
#define UEIR_DMA_ERR 0x0020
#define UEIR_BTS_ERR 0x0080
/* #define STATUS_MASK (UIR_USB_RST|UIR_UERR|UIR_SOF_TOK|UIR_TOK_DNE| \
UIR_UIDLE|UIR_RESUME|UIR_ATTACH|UIR_STALL) */
#define STATUS_MASK (UIR_USB_RST|UIR_UERR|UIR_TOK_DNE|UIR_STALL)
#else
// Status Bits
#define UIR_USB_RST 0x00000001
#define UIR_UERR 0x00000002
#define UIR_SOF_TOK 0x00000004
#define UIR_TOK_DNE 0x00000008
#define UIR_UIDLE 0x00000010
#define UIR_RESUME 0x00000020
#define UIR_ATTACH 0x00000040
#define UIR_STALL 0x00000080
// Error Status Bits
#define UEIR_PID_ERR 0x00000001
#define UEIR_CRC5 0x00000002
#define UEIR_HOST_EOF 0x00000002
#define UEIR_CRC16 0x00000004
#define UEIR_DFN8 0x00000008
#define UEIR_BTO_ERR 0x00000010
#define UEIR_DMA_ERR 0x00000020
#define UEIR_BTS_ERR 0x00000080
/*#define STATUS_MASK (UIR_USB_RST|UIR_UERR|UIR_SOF_TOK|UIR_TOK_DNE| \
UIR_UIDLE|UIR_RESUME|UIR_ATTACH|UIR_STALL) */
#define STATUS_MASK (UIR_USB_RST|UIR_UERR|UIR_TOK_DNE|UIR_STALL)
#endif
// Assumes HAL error flags are equal to USB OTG UEIR bits
// (see USBHALGetLastError and ErrorHandler).
#if( (USBHAL_PID_ERR != UEIR_PID_ERR ) || \
(USBHAL_CRC5 != UEIR_CRC5 ) || \
(USBHAL_HOST_EOF != UEIR_HOST_EOF) || \
(USBHAL_CRC16 != UEIR_CRC16 ) || \
(USBHAL_DFN8 != UEIR_DFN8 ) || \
(USBHAL_BTO_ERR != UEIR_BTO_ERR ) || \
(USBHAL_DMA_ERR != UEIR_DMA_ERR ) || \
(USBHAL_BTS_ERR != UEIR_BTS_ERR ) )
#error "USBHAL ErrorHandler must translate error flags"
#endif
// Assumes HAL flags shifted left 8 match core control flags (see USBHALSetEpConfiguration).
#if( ((USB_HAL_HANDSHAKE >> 8) != EP_HSHK) || \
((USB_HAL_TRANSMIT >> 8) != EP_EP_TX_EN) || \
((USB_HAL_RECEIVE >> 8) != EP_EP_RX_EN) || \
((USB_HAL_NO_RETRY >> 8) != EP_RETRY_DIS) || \
((USB_HAL_ALLOW_HUB >> 8) != EP_HOST_WOHUB) )
#error "USBHALSetEpConfiguration must translate control flags"
#endif
#define ERROR_MASK (UEIR_PID_ERR|UEIR_CRC5|UEIR_HOST_EOF|UEIR_CRC16| \
UEIR_DFN8|UEIR_BTO_ERR|UEIR_DMA_ERR|UEIR_BTS_ERR)
#define CTRL_MASK (EP_HSHK|EP_EP_TX_EN|EP_EP_RX_EN|EP_RETRY_DIS|EP_HOST_WOHUB)
#define RESISTOR_CTRL_MASK (UOTGCTRL_DM_LOW | UOTGCTRL_DP_LOW | \
UOTGCTRL_DM_HIGH | UOTGCTRL_DP_HIGH)
#define DATA_PTR_SIZE uint32_t
// USBHALConfigureDescriptor flags
#define USBHAL_DESC_BSTALL 0x04 // Stall the endpoint.
#define USBHAL_DESC_DTS 0x08 // Require Data Toggle Synchronization
#define USBHAL_DESC_NINC 0x10 // No Incrementing of DMA address
#define USBHAL_DESC_KEEP 0x20 // HW keeps buffer & descriptor
#define USBHAL_DESC_DATA1 0x40 // Indicates data packet 1
#define USBHAL_DESC_DATA0 0x00 // Indicates data packet 0
#define USBHAL_DESC_UOWN 0x80 // USB HW owns buffer & descriptor
/* Buffer Descriptor Table (BDT) definition
*************************************************************************
* These data structures define the buffer descriptor table used by the
* USB OTG Core to manage endpoint DMA.
*/
/*
* This is union describes the bitmap of
* the Setup & Status entry in the BDT.
*/
typedef union _BDT_SETUP
{
struct // Status Entry
{
#if defined(__18CXX) || defined(__XC8)
unsigned short BC_MSB: 2;
#else
unsigned short spare: 2;
#endif
unsigned short TOK_PID: 4; // Packit Identifier
unsigned short DAT01: 1; // Data-toggle bit
unsigned short UOWN: 1; // Descriptor owner: 0=SW, 1=HW
#if !defined(__18CXX) && !defined(__XC8)
unsigned short resvd: 8;
#endif
};
struct // Setup Entry
{
#if defined(__18CXX) || defined(__XC8)
unsigned short BC_MSB: 2;
#else
unsigned short spare: 2;
#endif
unsigned short BSTALL: 1; // Stalls EP if this descriptor needed
unsigned short DTS: 1; // Require data-toggle sync
unsigned short NINC: 1; // No Increment of DMA address
unsigned short KEEP: 1; // HW Keeps this buffer & descriptor
unsigned short DAT01: 1; // Data-toggle number (0 or 1)
unsigned short UOWN: 1; // Descriptor owner: 0=SW, 1=HW
#if !defined(__18CXX) && !defined(__XC8)
unsigned short resvd: 8;
#endif
};
#if !defined(__18CXX) && !defined(__XC8)
uint16_t Val;
#else
uint8_t Val;
#endif
} BDT_SETUP;
/*
* This union describes the byte-count
* entry in the BDT.
*/
typedef union _uint8_tCOUNT
{
struct // Byte-count bitmap
{
unsigned short BC0: 1;
unsigned short BC1: 1;
unsigned short BC2: 1;
unsigned short BC3: 1;
unsigned short BC4: 1;
unsigned short BC5: 1;
unsigned short BC6: 1;
unsigned short BC7: 1;
#if !defined(__18CXX) && !defined(__XC8)
unsigned short BC8: 1;
unsigned short BC9: 1;
unsigned short resvd: 6;
#endif
};
#if defined(__18CXX) || defined(__XC8)
//MCHP - here BC is only an unsigned char. This is not large enough for larger transfers
struct // Byte-count field
{
unsigned char BC; // Number of bytes in data buffer (really only 10 bits)
};
#else
struct // Byte-count field
{
unsigned short BC: 10; // Number of bytes in data buffer
unsigned short resvd: 6;
};
#endif
} uint8_tCOUNT;
/*
* Buffer Descriptor
*
* This union describes a single buffer descriptor. Each descriptor
* manages a single buffer. Each endpoint has 4 such descriptors, 2
* for receive and 2 for trasmit. Having two descriptors (odd and even,
* or ping and pong) per direction allows the HW to operate on one
* while the SW operates on the other.
*/
typedef union _BUFFER_DESCRIPTOR
{
uint32_t dword[2]; // Double-word access
uint16_t word[4]; // Word Access
uint8_t byte[8]; // Byte Access
struct
{
BDT_SETUP setup; // Setup & status entry
uint8_tCOUNT byte_cnt; // Byte count entry
unsigned int addr; // Physical address of data buffer
};
} BUF_DESC, *pBUF_DESC;
/* USB_HAL_PIPE
*************************************************************************
* A pipe is a virtual connection between two endpoints, one in the host
* and one the device. Data flows through a pipe in a single direction
* from one endpoint to the other. This structure defines the data that
* the USB HAL must track to manage a pipe.
*/
typedef union
{
uint8_t bitmap;
struct
{
uint8_t zero_pkt: 1; // Ensure transfer ends w/short or zero packet
uint8_t data_toggle: 1; // Data toggle: 0=DATA0/1=DATA1
uint8_t ping_pong: 1; // Current ping pong: 0=even/1=odd
uint8_t send_0_pkt: 1; // Flag indicating when to send a zero-sized packet
uint8_t reserved: 4; // Reserved
}field;
}PIPE_FLAGS;
typedef struct _USB_HAL_PIPE_DATA
{
uint8_t *buffer; // Pointer to the buffer
unsigned int max_pkt_size; // Max packet size of EP
unsigned int size; // Total number of bytes to transfer
unsigned int remaining; // Number of bytes remaining to transfer
unsigned int count; // Actual number of bytes transferred.
PIPE_FLAGS flags;
} USB_HAL_PIPE, *PUSB_HAL_PIPE;
/* USB_ROLE
*************************************************************************
* This enumeration identifies if the USB controller is currently acting
* as a USB device or as a USB host.
*/
typedef enum
{
DEVICE = 0, // USB controller is acting as a USB device
HOST = 1 // USB controller is acting as a USB host
} USB_ROLE;
/******************************************************************************
Function:
OTGCORE_IdentifyPacket
Summary:
This provides the endpoint number, direction and ping-pong ID and other
information identifying the packet that was just completed.
PreCondition:
Assumes that an UIR_TOK_DNE interrupt has occured.
Parameters:
None
Return Values:
flags Bitmapped flags identifying transfer (see below):
1 1 1 1 1 1
5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
| | \_____/ \_/ \_____/ | | \_/
| | | | | | | +-- reserved1
| | | | | | +----- ping_pong
| | | | | +------- direction
| | | | +------------ ep_num
| | | +------------------ reserved2
| | +------------------------ pid
| +----------------------------- data_toggle
+------------------------------- reserved3
size Size of data actually transferred (in bytes).
TRUE if successful, FALSE any of the parameters were NULL.
Remarks:
None
******************************************************************************/
typedef union
{
uint16_t bitmap;
uint8_t byte[2];
struct
{
// Byte 0
uint16_t reserved1: 2; // Reserved, ignore
uint16_t ping_pong: 1; // Buffer ping pong: 0=even/1=odd
uint16_t direction: 1; // Transfer direction: 0=Receive, 1=Transmit
uint16_t ep_num: 4; // Endpoint number
// Byte 1
uint16_t reserved2: 2; // Reserved, ignore
uint16_t pid: 4; // Packet ID (See USBCh9.h, "Packet IDs")
uint16_t data_toggle: 1; // Data toggle: 0=DATA0 packet, 1=DATA1 packet
uint16_t reserved3: 1; // Reserved, ignore
}field;
} TRANSFER_ID_FLAGS;
/* USB Register Macros
*************************************************************************
* These macros translate core registers across the different controller
* families.
*/
#if defined(__18CXX) || defined(__XC8)
#if defined(__18F4550) || defined(__18F2455) || defined(__18F2550) || defined(__18F4455)
#error "Please define any necessary register translation macros."
#endif
#define EnableUsbModule()
#define SetPingPongMode(m) U1CNFG1 |= (m)
#error "Please define any necessary register translation macros."
#elif defined(__C30__) || defined __XC16__
#define EnableUsbModule() U1PWRCbits.USBPWR = 1
#define SetPingPongMode(m)
// Do we need any others??? #error "Please define any necessary register translation macros.
#else
#define EnableUsbModule() U1PWRCbits.USBPWR = 1
#define SetPingPongMode(m)
#endif
// Misc bits
#ifndef UPWRC_USB_OP_EN
#define UPWRC_USB_OP_EN 0x01 // USB Operation Enable
#endif
#ifndef UPWRC_SUSPEND
#define UPWRC_SUSPEND 0x02 // Suspend bus activity
#endif
/* USB_HAL_DATA
*************************************************************************
* This structure contains the data that is required to manage an
* instance of the USB HAL.
*/
/*#if defined( USB_SUPPORT_DEVICE )
typedef struct _USB_HAL_DATA
{
// Data transfer "pipe" array
USB_HAL_PIPE pipe[USB_DEV_HIGHEST_EP_NUMBER+1][2]; // Rx=0, Tx=1
USB_ROLE current_role; // Acting as host or device?
unsigned long last_error; // Last error state detected
} USB_HAL_DATA, *PUSB_HAL_DATA;
// Macro to get a pointer to the desired pipe data.
#define FindPipe(e,d) &gHALData.pipe[(e)][(d)]
#endif // defined( USB_SUPPORT_DEVICE )*/
/******************************************************************************
Function:
USBHALClearStatus
Summary:
This routine clears the OTG Core's current status
PreCondition:
None
Parameters:
status Bitmap of status bits to clear (caller sets bits it
wishes to be cleared).
Return Values:
None
Remarks:
The status indicated have been cleared.
******************************************************************************/
#define USBHALClearStatus(s) (U1IR = (s))
/******************************************************************************
Function:
USBHALGetStatus
Summary:
This routine reads the OTG Core's current status.
PreCondition:
None
Parameters:
None
Return Values:
The contents of the USB OTG Core's interrupt status register.
Remarks:
None
******************************************************************************/
#define USBHALGetStatus() U1IR
/******************************************************************************
Function:
USBHALGetErrors
Summary:
This routine reads the current error status.
PreCondition:
None
Parameters:
None
Return Values:
The contents of the USB error-interrupt status register.
Remarks:
None
******************************************************************************/
#define USBHALGetErrors() U1EIR
/******************************************************************************
Function:
USBHALClearErrors
Summary:
This clears given error status.
PreCondition:
None
Paramters:
errors - Bitmap of the error status bits (caller sets the bets
it wishes to clear).
Return Values:
None
Side effects:
The errors indicated have been cleared.
Remarks:
None
******************************************************************************/
#define USBHALClearErrors(e) (U1EIR = (e))
#endif // _USB_HAL_LOCAL_H_
/*************************************************************************
* EOF usb_hal.h
*/