Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | |||
3 | USB Hardware Abstraction Layer (HAL) (Header File) |
||
4 | |||
5 | Summary: |
||
6 | This file abstracts the hardware interface. The USB stack firmware can be |
||
7 | compiled to work on different USB microcontrollers, such as PIC18 and PIC24. |
||
8 | The USB related special function registers and bit names are generally very |
||
9 | similar between the device families, but small differences in naming exist. |
||
10 | |||
11 | Description: |
||
12 | This file abstracts the hardware interface. The USB stack firmware can be |
||
13 | compiled to work on different USB microcontrollers, such as PIC18 and PIC24. |
||
14 | The USB related special function registers and bit names are generally very |
||
15 | similar between the device families, but small differences in naming exist. |
||
16 | |||
17 | In order to make the same set of firmware work accross the device families, |
||
18 | when modifying SFR contents, a slightly abstracted name is used, which is |
||
19 | then "mapped" to the appropriate real name in the usb_hal_picxx.h header. |
||
20 | |||
21 | Make sure to include the correct version of the usb_hal_picxx.h file for |
||
22 | the microcontroller family which will be used. |
||
23 | |||
24 | This file is located in the "\<Install Directory\>\\Microchip\\Include\\USB" |
||
25 | directory. |
||
26 | |||
27 | When including this file in a new project, this file can either be |
||
28 | referenced from the directory in which it was installed or copied |
||
29 | directly into the user application folder. If the first method is |
||
30 | chosen to keep the file located in the folder in which it is installed |
||
31 | then include paths need to be added so that the library and the |
||
32 | application both know where to reference each others files. If the |
||
33 | application folder is located in the same folder as the Microchip |
||
34 | folder (like the current demo folders), then the following include |
||
35 | paths need to be added to the application's project: |
||
36 | |||
37 | . |
||
38 | |||
39 | ..\\..\\MicrochipInclude |
||
40 | |||
41 | If a different directory structure is used, modify the paths as |
||
42 | required. An example using absolute paths instead of relative paths |
||
43 | would be the following: |
||
44 | |||
45 | C:\\Microchip Solutions\\Microchip\\Include |
||
46 | |||
47 | C:\\Microchip Solutions\\My Demo Application |
||
48 | |||
49 | *******************************************************************************/ |
||
50 | //DOM-IGNORE-BEGIN |
||
51 | /****************************************************************************** |
||
52 | |||
53 | File Description: |
||
54 | |||
55 | This file defines the interface to the USB hardware abstraction layer. |
||
56 | |||
57 | * Filename: usb_hal_pic18.h |
||
58 | Dependencies: See INCLUDES section |
||
59 | Processor: Use this header file when using this firmware with PIC18 USB |
||
60 | microcontrollers |
||
61 | Hardware: |
||
62 | Complier: Microchip C18 (for PIC18) |
||
63 | Company: Microchip Technology, Inc. |
||
64 | |||
65 | Software License Agreement: |
||
66 | |||
67 | The software supplied herewith by Microchip Technology Incorporated |
||
68 | (the Company) for its PIC® Microcontroller is intended and |
||
69 | supplied to you, the Companys customer, for use solely and |
||
70 | exclusively on Microchip PIC Microcontroller products. The |
||
71 | software is owned by the Company and/or its supplier, and is |
||
72 | protected under applicable copyright laws. All rights are reserved. |
||
73 | Any use in violation of the foregoing restrictions may subject the |
||
74 | user to criminal sanctions under applicable laws, as well as to |
||
75 | civil liability for the breach of the terms and conditions of this |
||
76 | license. |
||
77 | |||
78 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
79 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
80 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
81 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
82 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
83 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
84 | |||
85 | *************************************************************************/ |
||
86 | |||
87 | //DOM-IGNORE-BEGIN |
||
88 | /******************************************************************** |
||
89 | Change History: |
||
90 | Rev Description |
||
91 | ---- ----------- |
||
92 | 2.6 Changed the inplementation of the interrupt clearing macro |
||
93 | to be more efficient. |
||
94 | |||
95 | 2.6a Added DisableNonZeroEndpoints() function |
||
96 | |||
97 | 2.7 Added ConvertToPhysicalAddress() and ConvertToVirtualAddress() |
||
98 | macros. Needed for compatiblity with PIC32. |
||
99 | |||
100 | Added USBDisableInterrupts() macro. Fixes issue in dual role |
||
101 | example where a device in polling mode can still have interrupts |
||
102 | enabled from the host mode causing an incorrect vectoring to the |
||
103 | host interrupt controller while in device mode. |
||
104 | |||
105 | 2.7a No change |
||
106 | ********************************************************************/ |
||
107 | //DOM-IGNORE-END |
||
108 | |||
109 | #ifndef USB_HAL_PIC18_H |
||
110 | #define USB_HAL_PIC18_H |
||
111 | |||
112 | /**************************************************************** |
||
113 | Function: |
||
114 | void USBPowerModule(void) |
||
115 | |||
116 | Description: |
||
117 | This macro is used to power up the USB module if required<br> |
||
118 | PIC18: defines as nothing<br> |
||
119 | PIC24: defines as U1PWRCbits.USBPWR = 1;<br> |
||
120 | |||
121 | Parameters: |
||
122 | None |
||
123 | |||
124 | Return Values: |
||
125 | None |
||
126 | |||
127 | Remarks: |
||
128 | None |
||
129 | |||
130 | ****************************************************************/ |
||
131 | #define USBPowerModule() |
||
132 | |||
133 | /**************************************************************** |
||
134 | Function: |
||
135 | void USBModuleDisable(void) |
||
136 | |||
137 | Description: |
||
138 | This macro is used to disable the USB module |
||
139 | |||
140 | Parameters: |
||
141 | None |
||
142 | |||
143 | Return Values: |
||
144 | None |
||
145 | |||
146 | Remarks: |
||
147 | None |
||
148 | |||
149 | ****************************************************************/ |
||
150 | #define USBModuleDisable() {\ |
||
151 | UCON = 0;\ |
||
152 | UIE = 0;\ |
||
153 | USBDeviceState = DETACHED_STATE;\ |
||
154 | } |
||
155 | |||
156 | /**************************************************************** |
||
157 | Function: |
||
158 | USBSetBDTAddress(addr) |
||
159 | |||
160 | Description: |
||
161 | This macro is used to power up the USB module if required |
||
162 | |||
163 | Parameters: |
||
164 | None |
||
165 | |||
166 | Return Values: |
||
167 | None |
||
168 | |||
169 | Remarks: |
||
170 | None |
||
171 | |||
172 | ****************************************************************/ |
||
173 | #define USBSetBDTAddress(addr) |
||
174 | |||
175 | /******************************************************************** |
||
176 | * Function (macro): void USBClearInterruptFlag(register, BYTE if_and_flag_mask) |
||
177 | * |
||
178 | * PreCondition: None |
||
179 | * |
||
180 | * Input: |
||
181 | * register - the register mnemonic for the register holding the interrupt |
||
182 | flag to be cleared |
||
183 | * BYTE if_and_flag_mask - an AND mask for the interrupt flag that will be |
||
184 | cleared |
||
185 | * |
||
186 | * Output: None |
||
187 | * |
||
188 | * Side Effects: None |
||
189 | * |
||
190 | * Overview: Clears the specified USB interrupt flag. |
||
191 | * |
||
192 | * Note: |
||
193 | *******************************************************************/ |
||
194 | #define USBClearInterruptFlag(reg_name, if_and_flag_mask) (reg_name &= if_and_flag_mask) |
||
195 | |||
196 | /******************************************************************** |
||
197 | Function: |
||
198 | void USBClearInterruptRegister(WORD reg) |
||
199 | |||
200 | Summary: |
||
201 | Clears the specified interrupt register |
||
202 | |||
203 | PreCondition: |
||
204 | None |
||
205 | |||
206 | Parameters: |
||
207 | WORD reg - the register name that needs to be cleared |
||
208 | |||
209 | Return Values: |
||
210 | None |
||
211 | |||
212 | Remarks: |
||
213 | None |
||
214 | |||
215 | *******************************************************************/ |
||
216 | #define USBClearInterruptRegister(reg) reg = 0; |
||
217 | |||
218 | /******************************************************************** |
||
219 | Function: |
||
220 | void DisableNonZeroEndpoints(UINT8 last_ep_num) |
||
221 | |||
222 | Summary: |
||
223 | Clears the control registers for the specified non-zero endpoints |
||
224 | |||
225 | PreCondition: |
||
226 | None |
||
227 | |||
228 | Parameters: |
||
229 | UINT8 last_ep_num - the last endpoint number to clear. This |
||
230 | number should include all endpoints used in any configuration. |
||
231 | |||
232 | Return Values: |
||
233 | None |
||
234 | |||
235 | Remarks: |
||
236 | None |
||
237 | |||
238 | *******************************************************************/ |
||
239 | #define DisableNonZeroEndpoints(last_ep_num) memset((void*)&U1EP1,0x00,(last_ep_num)); |
||
240 | |||
241 | #if defined(USB_DISABLE_SOF_HANDLER) |
||
242 | #define USB_SOF_INTERRUPT 0x00 |
||
243 | #else |
||
244 | #define USB_SOF_INTERRUPT 0x40 |
||
245 | #endif |
||
246 | |||
247 | #if defined(USB_DISABLE_ERROR_HANDLER) |
||
248 | #define USB_ERROR_INTERRUPT 0x02 |
||
249 | #else |
||
250 | #define USB_ERROR_INTERRUPT 0x02 |
||
251 | #endif |
||
252 | |||
253 | //STALLIE, IDLEIE, TRNIE, and URSTIE are all enabled by default and are required |
||
254 | #if defined(USB_INTERRUPT) |
||
255 | #define USBEnableInterrupts() {RCONbits.IPEN = 1;IPR2bits.USBIP = 1;PIE2bits.USBIE = 1;INTCONbits.GIEH = 1;} |
||
256 | #else |
||
257 | #define USBEnableInterrupts() |
||
258 | #endif |
||
259 | |||
260 | #define USBDisableInterrupts() {PIE2bits.USBIE = 0;} |
||
261 | #if defined(USB_INTERRUPT) |
||
262 | #define USBMaskInterrupts() {PIE2bits.USBIE = 0;} |
||
263 | #define USBUnmaskInterrupts() {PIE2bits.USBIE = 1;} |
||
264 | #else |
||
265 | #define USBMaskInterrupts() |
||
266 | #define USBUnmaskInterrupts() |
||
267 | #endif |
||
268 | |||
269 | #define ENDPOINT_MASK 0b01111000 |
||
270 | |||
271 | |||
272 | #define USBPingPongBufferReset UCONbits.PPBRST |
||
273 | |||
274 | |||
275 | #define USBTransactionCompleteIE UIEbits.TRNIE |
||
276 | #define USBTransactionCompleteIF UIRbits.TRNIF |
||
277 | #define USBTransactionCompleteIFReg UIR |
||
278 | #define USBTransactionCompleteIFBitNum 0xF7 //AND mask for clearing TRNIF bit position 4 |
||
279 | |||
280 | #define USBResetIE UIEbits.URSTIE |
||
281 | #define USBResetIF UIRbits.URSTIF |
||
282 | #define USBResetIFReg UIR |
||
283 | #define USBResetIFBitNum 0xFE //AND mask for clearing URSTIF bit position 0 |
||
284 | |||
285 | #define USBIdleIE UIEbits.IDLEIE |
||
286 | #define USBIdleIF UIRbits.IDLEIF |
||
287 | #define USBIdleIFReg UIR |
||
288 | #define USBIdleIFBitNum 0xEF //AND mask for clearing IDLEIF bit position 5 |
||
289 | |||
290 | #define USBActivityIE UIEbits.ACTVIE |
||
291 | #define USBActivityIF UIRbits.ACTVIF |
||
292 | #define USBActivityIFReg UIR |
||
293 | #define USBActivityIFBitNum 0xFB //AND mask for clearing ACTVIF bit position 2 |
||
294 | |||
295 | #define USBSOFIE UIEbits.SOFIE |
||
296 | #define USBSOFIF UIRbits.SOFIF |
||
297 | #define USBSOFIFReg UIR |
||
298 | #define USBSOFIFBitNum 0xBF //AND mask for clearing SOFIF bit position 6 |
||
299 | |||
300 | #define USBStallIE UIEbits.STALLIE |
||
301 | #define USBStallIF UIRbits.STALLIF |
||
302 | #define USBStallIFReg UIR |
||
303 | #define USBStallIFBitNum 0xDF //AND mask for clearing STALLIF bit position 5 |
||
304 | |||
305 | #define USBErrorIE UIEbits.UERRIE |
||
306 | #define USBErrorIF UIRbits.UERRIF |
||
307 | #define USBErrorIFReg UIR |
||
308 | #define USBErrorIFBitNum 0xFD //UERRIF bit position 1. Note: This bit is read only and is cleared by clearing the enabled UEIR flags |
||
309 | |||
310 | #define USBSE0Event UCONbits.SE0 |
||
311 | #define USBSuspendControl UCONbits.SUSPND |
||
312 | #define USBPacketDisable UCONbits.PKTDIS |
||
313 | #define USBResumeControl UCONbits.RESUME |
||
314 | |||
315 | #define U1ADDR UADDR |
||
316 | #define U1IE UIE |
||
317 | #define U1IR UIR |
||
318 | #define U1EIR UEIR |
||
319 | #define U1EIE UEIE |
||
320 | #define U1CON UCON |
||
321 | #define U1EP0 UEP0 |
||
322 | #define U1CONbits UCONbits |
||
323 | #define U1EP1 UEP1 |
||
324 | #define U1CNFG1 UCFG |
||
325 | #define U1STAT USTAT |
||
326 | #define U1EP0bits UEP0bits |
||
327 | |||
328 | /* Buffer Descriptor Status Register Initialization Parameters */ |
||
329 | #define _BSTALL 0x04 //Buffer Stall enable |
||
330 | #define _DTSEN 0x08 //Data Toggle Synch enable |
||
331 | #define _INCDIS 0x10 //Address increment disable |
||
332 | #define _KEN 0x20 //SIE keeps buff descriptors enable |
||
333 | #define _DAT0 0x00 //DATA0 packet expected next |
||
334 | #define _DAT1 0x40 //DATA1 packet expected next |
||
335 | #define _DTSMASK 0x40 //DTS Mask |
||
336 | #define _USIE 0x80 //SIE owns buffer |
||
337 | #define _UCPU 0x00 //CPU owns buffer |
||
338 | |||
339 | #define _STAT_MASK 0xFF |
||
340 | |||
341 | /* BDT entry structure definition */ |
||
342 | typedef union _BD_STAT |
||
343 | { |
||
344 | BYTE Val; |
||
345 | struct{ |
||
346 | //If the CPU owns the buffer then these are the values |
||
347 | unsigned BC8:1; //bit 8 of the byte count |
||
348 | unsigned BC9:1; //bit 9 of the byte count |
||
349 | unsigned BSTALL:1; //Buffer Stall Enable |
||
350 | unsigned DTSEN:1; //Data Toggle Synch Enable |
||
351 | unsigned INCDIS:1; //Address Increment Disable |
||
352 | unsigned KEN:1; //BD Keep Enable |
||
353 | unsigned DTS:1; //Data Toggle Synch Value |
||
354 | unsigned UOWN:1; //USB Ownership |
||
355 | }; |
||
356 | struct{ |
||
357 | //if the USB module owns the buffer then these are |
||
358 | // the values |
||
359 | unsigned BC8:1; //bit 8 of the byte count |
||
360 | unsigned BC9:1; //bit 9 of the byte count |
||
361 | unsigned PID0:1; //Packet Identifier |
||
362 | unsigned PID1:1; |
||
363 | unsigned PID2:1; |
||
364 | unsigned PID3:1; |
||
365 | unsigned :1; |
||
366 | unsigned UOWN:1; //USB Ownership |
||
367 | }; |
||
368 | struct{ |
||
369 | unsigned :2; |
||
370 | unsigned PID:4; //Packet Identifier |
||
371 | unsigned :2; |
||
372 | }; |
||
373 | } BD_STAT; //Buffer Descriptor Status Register |
||
374 | |||
375 | // BDT Entry Layout |
||
376 | typedef union __BDT |
||
377 | { |
||
378 | struct |
||
379 | { |
||
380 | BD_STAT STAT; |
||
381 | BYTE CNT; |
||
382 | BYTE ADRL; //Buffer Address Low |
||
383 | BYTE ADRH; //Buffer Address High |
||
384 | }; |
||
385 | struct |
||
386 | { |
||
387 | unsigned :8; |
||
388 | unsigned :8; |
||
389 | WORD ADR; //Buffer Address |
||
390 | }; |
||
391 | DWORD Val; |
||
392 | BYTE v[4]; |
||
393 | } BDT_ENTRY; |
||
394 | |||
395 | //Definitions for the BDT |
||
396 | #ifndef USB_PING_PONG_MODE |
||
397 | #error "No ping pong mode defined." |
||
398 | #endif |
||
399 | |||
400 | #if (USB_PING_PONG_MODE == USB_PING_PONG__NO_PING_PONG) |
||
401 | extern volatile BDT_ENTRY BDT[(USB_MAX_EP_NUMBER + 1) * 2]; |
||
402 | #elif (USB_PING_PONG_MODE == USB_PING_PONG__EP0_OUT_ONLY) |
||
403 | extern volatile BDT_ENTRY BDT[((USB_MAX_EP_NUMBER+1) * 2)+1]; |
||
404 | #elif (USB_PING_PONG_MODE == USB_PING_PONG__FULL_PING_PONG) |
||
405 | extern volatile BDT_ENTRY BDT[(USB_MAX_EP_NUMBER + 1) * 4]; |
||
406 | #elif (USB_PING_PONG_MODE == USB_PING_PONG__ALL_BUT_EP0) |
||
407 | extern volatile BDT_ENTRY BDT[((USB_MAX_EP_NUMBER + 1) * 4)-2]; |
||
408 | #else |
||
409 | #error "No ping pong mode defined." |
||
410 | #endif |
||
411 | |||
412 | #define USTAT_EP0_PP_MASK ~0x02 |
||
413 | #define USTAT_EP_MASK 0x7E |
||
414 | #define USTAT_EP0_OUT 0x00 |
||
415 | #define USTAT_EP0_OUT_EVEN 0x00 |
||
416 | #define USTAT_EP0_OUT_ODD 0x02 |
||
417 | |||
418 | #define USTAT_EP0_IN 0x04 |
||
419 | #define USTAT_EP0_IN_EVEN 0x04 |
||
420 | #define USTAT_EP0_IN_ODD 0x06 |
||
421 | |||
422 | typedef union |
||
423 | { |
||
424 | WORD UEP[16]; |
||
425 | } _UEP; |
||
426 | |||
427 | #define UEP_STALL 0x0001 |
||
428 | |||
429 | #define USB_BDT_ADDRESS 0x400 |
||
430 | |||
431 | typedef union _POINTER |
||
432 | { |
||
433 | struct |
||
434 | { |
||
435 | BYTE bLow; |
||
436 | BYTE bHigh; |
||
437 | //byte bUpper; |
||
438 | }; |
||
439 | WORD _word; // bLow & bHigh |
||
440 | |||
441 | //pFunc _pFunc; // Usage: ptr.pFunc(); Init: ptr.pFunc = &<Function>; |
||
442 | |||
443 | BYTE* bRam; // Ram byte pointer: 2 bytes pointer pointing |
||
444 | // to 1 byte of data |
||
445 | WORD* wRam; // Ram word poitner: 2 bytes poitner pointing |
||
446 | // to 2 bytes of data |
||
447 | |||
448 | ROM BYTE* bRom; // Size depends on compiler setting |
||
449 | ROM WORD* wRom; |
||
450 | //rom near byte* nbRom; // Near = 2 bytes pointer |
||
451 | //rom near word* nwRom; |
||
452 | //rom far byte* fbRom; // Far = 3 bytes pointer |
||
453 | //rom far word* fwRom; |
||
454 | } POINTER; |
||
455 | |||
456 | //******** Depricated: v2.2 - will be removed at some point of time *** |
||
457 | #define _LS 0x00 // Use Low-Speed USB Mode |
||
458 | #define _FS 0x04 // Use Full-Speed USB Mode |
||
459 | #define _TRINT 0x00 // Use internal transceiver |
||
460 | #define _TREXT 0x08 // Use external transceiver |
||
461 | #define _PUEN 0x10 // Use internal pull-up resistor |
||
462 | #define _OEMON 0x40 // Use SIE output indicator |
||
463 | //********************************************************************** |
||
464 | |||
465 | #define USB_PULLUP_ENABLE 0x10 |
||
466 | #define USB_PULLUP_DISABLED 0x00 |
||
467 | |||
468 | #define USB_INTERNAL_TRANSCEIVER 0x00 |
||
469 | #define USB_EXTERNAL_TRANSCEIVER 0x08 |
||
470 | |||
471 | #define USB_FULL_SPEED 0x04 |
||
472 | #define USB_LOW_SPEED 0x00 |
||
473 | |||
474 | #define ConvertToPhysicalAddress(a) ((WORD)(a)) |
||
475 | #define ConvertToVirtualAddress(a) ((void *)(a)) |
||
476 | #define USBClearUSBInterrupt() PIR2bits.USBIF = 0; |
||
477 | |||
478 | #if !defined(USBDEVICE_C) |
||
479 | //extern USB_VOLATILE USB_DEVICE_STATE USBDeviceState; |
||
480 | extern USB_VOLATILE BYTE USBActiveConfiguration; |
||
481 | extern USB_VOLATILE IN_PIPE inPipes[1]; |
||
482 | extern USB_VOLATILE OUT_PIPE outPipes[1]; |
||
483 | extern volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1]; |
||
484 | #endif |
||
485 | |||
486 | /* Endpoint configuration options for USBEnableEndpoint() function */ |
||
487 | #define EP_CTRL 0x06 // Cfg Control pipe for this ep |
||
488 | #define EP_OUT 0x0C // Cfg OUT only pipe for this ep |
||
489 | #define EP_IN 0x0A // Cfg IN only pipe for this ep |
||
490 | #define EP_OUT_IN 0x0E // Cfg both OUT & IN pipes for this ep |
||
491 | |||
492 | // Handshake should be disable for isoch |
||
493 | #define USB_HANDSHAKE_ENABLED 0x10 |
||
494 | #define USB_HANDSHAKE_DISABLED 0x00 |
||
495 | |||
496 | #define USB_OUT_ENABLED 0x04 |
||
497 | #define USB_OUT_DISABLED 0x00 |
||
498 | |||
499 | #define USB_IN_ENABLED 0x02 |
||
500 | #define USB_IN_DISABLED 0x00 |
||
501 | |||
502 | #define USB_ALLOW_SETUP 0x00 |
||
503 | #define USB_DISALLOW_SETUP 0x08 |
||
504 | |||
505 | #define USB_STALL_ENDPOINT 0x01 |
||
506 | |||
507 | #define SetConfigurationOptions() {\ |
||
508 | U1CNFG1 = USB_PULLUP_OPTION | USB_TRANSCEIVER_OPTION | USB_SPEED_OPTION | USB_PING_PONG_MODE;\ |
||
509 | U1EIE = 0x9F;\ |
||
510 | UIE = 0x39 | USB_SOF_INTERRUPT | USB_ERROR_INTERRUPT;\ |
||
511 | } |
||
512 | |||
513 | #endif //#ifndef USB_HAL_PIC18_H |
Powered by WebSVN v2.8.3