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 | ..\\..\\Microchip\\Include |
||
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 | *******************************************************************************/ |
||
51 | //DOM-IGNORE-BEGIN |
||
52 | /****************************************************************************** |
||
53 | |||
54 | File Description: |
||
55 | |||
56 | This file defines the interface to the USB hardware abstraction layer. |
||
57 | |||
58 | Filename: usb_hal.h |
||
59 | Dependancies: none |
||
60 | Processor: PIC18, PIC24, or PIC32 USB Microcontrollers |
||
61 | Hardware: The code is natively intended to be used on the following |
||
62 | hardware platforms: PICDEM FS USB Demo Board, |
||
63 | PIC18F87J50 FS USB Plug-In Module, or |
||
64 | Explorer 16 + PIC24 USB PIM. The firmware may be |
||
65 | modified for use on other USB platforms by editing the |
||
66 | HardwareProfile.h file. |
||
67 | Compiler: Microchip C18 (for PIC18) or C30 (for PIC24) |
||
68 | Company: Microchip Technology, Inc. |
||
69 | |||
70 | Software License Agreement: |
||
71 | |||
72 | The software supplied herewith by Microchip Technology Incorporated |
||
73 | (the Company) for its PICmicro® Microcontroller is intended and |
||
74 | supplied to you, the Companys customer, for use solely and |
||
75 | exclusively on Microchip PICmicro Microcontroller products. The |
||
76 | software is owned by the Company and/or its supplier, and is |
||
77 | protected under applicable copyright laws. All rights are reserved. |
||
78 | Any use in violation of the foregoing restrictions may subject the |
||
79 | user to criminal sanctions under applicable laws, as well as to |
||
80 | civil liability for the breach of the terms and conditions of this |
||
81 | license. |
||
82 | |||
83 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
84 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
85 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
86 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
87 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
88 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
89 | |||
90 | Change History: |
||
91 | Rev Description |
||
92 | ---- ----------- |
||
93 | 2.6 Changed the inplementation of the interrupt clearing macro |
||
94 | to be more efficient. |
||
95 | |||
96 | 2.6a Added DisableNonZeroEndpoints() function |
||
97 | |||
98 | 2.7 Changed the definition of USB_PULLUP_ENABLE and USB_PULLUP_DISABLE |
||
99 | to be more useful. |
||
100 | |||
101 | Added CovertToVirtualAddress() function for compatibility with PIC32. |
||
102 | |||
103 | Added USBDisableInterrupts() function that disables the USB |
||
104 | interrupts on the initialization function. This is required if |
||
105 | running a dual role application where the host uses interrupts |
||
106 | and the device mode runs polling mode. In this case the host |
||
107 | doesn't disable the interrupts so the first interrupt caused by |
||
108 | the USB device will incorrectly vector to the host interrupt |
||
109 | interrupt handler. |
||
110 | |||
111 | Modified the SetConfigurationOptions() function to explicitly |
||
112 | reconfigure the pull-up/pull-down settings for the D+/D- pins |
||
113 | in case the host leaves the pull-downs enabled. |
||
114 | |||
115 | 2.7a Fixed USBSetBDTAddress() macro, so that it correctly loads the entire |
||
116 | U1BDTPx register set, enabling the BDT to be anywhere in RAM. Previous |
||
117 | implementation wouldn't work on a large RAM device if the linker |
||
118 | decided to place the BDT[] array at an address > 64kB |
||
119 | |||
120 | *************************************************************************/ |
||
121 | |||
122 | #if !defined(USB_HAL_PIC32_H) |
||
123 | #include "Compiler.h" |
||
124 | |||
125 | #define USB_HAL_PIC32_H |
||
126 | |||
127 | #define USBSetBDTAddress(addr) {U1BDTP3 = (((DWORD)KVA_TO_PA(addr)) >> 24); U1BDTP2 = (((DWORD)KVA_TO_PA(addr)) >> 16); U1BDTP1 = (((DWORD)KVA_TO_PA(addr)) >> 8);} |
||
128 | #define USBPowerModule() U1PWRCbits.USBPWR = 1; |
||
129 | #define USBPingPongBufferReset U1CONbits.PPBRST |
||
130 | |||
131 | #define USBTransactionCompleteIE U1IEbits.TRNIE |
||
132 | #define USBTransactionCompleteIF U1IRbits.TRNIF |
||
133 | #define USBTransactionCompleteIFReg U1IR |
||
134 | #define USBTransactionCompleteIFBitNum 3 |
||
135 | |||
136 | #define USBResetIE U1IEbits.URSTIE |
||
137 | #define USBResetIF U1IRbits.URSTIF |
||
138 | #define USBResetIFReg U1IR |
||
139 | #define USBResetIFBitNum 0 |
||
140 | |||
141 | #define USBIdleIE U1IEbits.IDLEIE |
||
142 | #define USBIdleIF U1IRbits.IDLEIF |
||
143 | #define USBIdleIFReg U1IR |
||
144 | #define USBIdleIFBitNum 4 |
||
145 | |||
146 | #define USBActivityIE U1OTGIEbits.ACTVIE |
||
147 | #define USBActivityIF U1OTGIRbits.ACTVIF |
||
148 | #define USBActivityIFReg U1OTGIR |
||
149 | #define USBActivityIFBitNum 4 |
||
150 | |||
151 | #define USBSOFIE U1IEbits.SOFIE |
||
152 | #define USBSOFIF U1IRbits.SOFIF |
||
153 | #define USBSOFIFReg U1IR |
||
154 | #define USBSOFIFBitNum 2 |
||
155 | |||
156 | #define USBStallIE U1IEbits.STALLIE |
||
157 | #define USBStallIF U1IRbits.STALLIF |
||
158 | #define USBStallIFReg U1IR |
||
159 | #define USBStallIFBitNum 7 |
||
160 | |||
161 | #define USBErrorIE U1IEbits.UERRIE |
||
162 | #define USBErrorIF U1IRbits.UERRIF |
||
163 | #define USBErrorIFReg U1IR |
||
164 | #define USBErrorIFBitNum 1 |
||
165 | |||
166 | #define USBSE0Event 0// U1IRbits.URSTIF// U1CONbits.SE0 |
||
167 | #define USBSuspendControl U1PWRCbits.USUSPEND |
||
168 | #define USBPacketDisable U1CONbits.PKTDIS |
||
169 | #define USBResumeControl U1CONbits.RESUME |
||
170 | |||
171 | #define USBT1MSECIE U1OTGIEbits.T1MSECIE |
||
172 | #define USBT1MSECIF U1OTGIRbits.T1MSECIF |
||
173 | #define USBT1MSECIFReg U1OTGIR |
||
174 | #define USBT1MSECIFBitNum 6 |
||
175 | |||
176 | #define USBIDIE U1OTGIEbits.IDIE |
||
177 | #define USBIDIF U1OTGIRbits.IDIF |
||
178 | #define USBIDIFReg U1OTGIR |
||
179 | #define USBIDIFBitNum 7 |
||
180 | |||
181 | #define USB_OTG_ENABLE 0x04 |
||
182 | #define USB_OTG_DPLUS_ENABLE 0x80 |
||
183 | |||
184 | /* Buffer Descriptor Status Register Initialization Parameters */ |
||
185 | |||
186 | //The _BSTALL definition is changed from 0x04 to 0x00 to |
||
187 | // fix a difference in the PIC18 and PIC24 definitions of this |
||
188 | // bit. This should be changed back once the definitions are |
||
189 | // synced. |
||
190 | #define _BSTALL 0x04 //Buffer Stall enable |
||
191 | #define _DTSEN 0x08 //Data Toggle Synch enable |
||
192 | #define _DAT0 0x00 //DATA0 packet expected next |
||
193 | #define _DAT1 0x40 //DATA1 packet expected next |
||
194 | #define _DTSMASK 0x40 //DTS Mask |
||
195 | #define _USIE 0x80 //SIE owns buffer |
||
196 | #define _UCPU 0x00 //CPU owns buffer |
||
197 | |||
198 | #define _STAT_MASK 0xFC |
||
199 | |||
200 | |||
201 | |||
202 | #define USTAT_EP0_PP_MASK ~0x04 |
||
203 | #define USTAT_EP_MASK 0xFC |
||
204 | #define USTAT_EP0_OUT 0x00 |
||
205 | #define USTAT_EP0_OUT_EVEN 0x00 |
||
206 | #define USTAT_EP0_OUT_ODD 0x04 |
||
207 | |||
208 | #define USTAT_EP0_IN 0x08 |
||
209 | #define USTAT_EP0_IN_EVEN 0x08 |
||
210 | #define USTAT_EP0_IN_ODD 0x0C |
||
211 | #define ENDPOINT_MASK 0xF0 |
||
212 | |||
213 | typedef union |
||
214 | { |
||
215 | WORD UEP[16]; |
||
216 | } _UEP; |
||
217 | |||
218 | #define UEP_STALL 0x0002 |
||
219 | |||
220 | typedef union _POINTER |
||
221 | { |
||
222 | struct |
||
223 | { |
||
224 | BYTE bLow; |
||
225 | BYTE bHigh; |
||
226 | //byte bUpper; |
||
227 | }; |
||
228 | WORD _word; // bLow & bHigh |
||
229 | |||
230 | //pFunc _pFunc; // Usage: ptr.pFunc(); Init: ptr.pFunc = &<Function>; |
||
231 | |||
232 | BYTE* bRam; // Ram byte pointer: 2 bytes pointer pointing |
||
233 | // to 1 byte of data |
||
234 | WORD* wRam; // Ram word poitner: 2 bytes poitner pointing |
||
235 | // to 2 bytes of data |
||
236 | |||
237 | ROM BYTE* bRom; // Size depends on compiler setting |
||
238 | ROM WORD* wRom; |
||
239 | //rom near byte* nbRom; // Near = 2 bytes pointer |
||
240 | //rom near word* nwRom; |
||
241 | //rom far byte* fbRom; // Far = 3 bytes pointer |
||
242 | //rom far word* fwRom; |
||
243 | } POINTER; |
||
244 | |||
245 | //******** Depricated: v2.2 - will be removed at some point of time *** |
||
246 | #define _LS 0x00 // Use Low-Speed USB Mode |
||
247 | #define _FS 0x00 // Use Full-Speed USB Mode |
||
248 | #define _TRINT 0x00 // Use internal transceiver |
||
249 | #define _TREXT 0x00 // Use external transceiver |
||
250 | #define _PUEN 0x00 // Use internal pull-up resistor |
||
251 | #define _OEMON 0x00 // Use SIE output indicator |
||
252 | //********************************************************************** |
||
253 | |||
254 | #define USB_PULLUP_ENABLE 0x00 |
||
255 | #define USB_PULLUP_DISABLE 0x04 |
||
256 | |||
257 | #define USB_INTERNAL_TRANSCEIVER 0x00 |
||
258 | #define USB_EXTERNAL_TRANSCEIVER 0x01 |
||
259 | |||
260 | #define USB_FULL_SPEED 0x00 |
||
261 | //USB_LOW_SPEED not currently supported in PIC24F USB products |
||
262 | |||
263 | #if(USB_PING_PONG_MODE != USB_PING_PONG__FULL_PING_PONG) |
||
264 | #error "Unsupported ping pong mode for this device" |
||
265 | #endif |
||
266 | |||
267 | #define ConvertToPhysicalAddress(a) ((DWORD)KVA_TO_PA(a)) |
||
268 | #define ConvertToVirtualAddress(a) PA_TO_KVA1(a) |
||
269 | |||
270 | /**************************************************************** |
||
271 | Function: |
||
272 | void USBModuleDisable(void) |
||
273 | |||
274 | Description: |
||
275 | This macro is used to disable the USB module |
||
276 | |||
277 | Parameters: |
||
278 | None |
||
279 | |||
280 | Return Values: |
||
281 | None |
||
282 | |||
283 | Remarks: |
||
284 | None |
||
285 | |||
286 | ****************************************************************/ |
||
287 | #define USBModuleDisable() {\ |
||
288 | U1CON = 0;\ |
||
289 | U1IE = 0;\ |
||
290 | U1OTGIE = 0;\ |
||
291 | U1PWRCbits.USBPWR = 1;\ |
||
292 | USBDeviceState = DETACHED_STATE;\ |
||
293 | } |
||
294 | |||
295 | /******************************************************************** |
||
296 | * Function (macro): void USBClearInterruptFlag(register, BYTE if_flag_offset) |
||
297 | * |
||
298 | * PreCondition: None |
||
299 | * |
||
300 | * Input: |
||
301 | * register - the register mnemonic for the register holding the interrupt |
||
302 | * flag to be "kleared" |
||
303 | * BYTE if_flag_offset - the bit position offset (for the interrupt flag to |
||
304 | * "klear") from the "right of the register" |
||
305 | * |
||
306 | * Output: None |
||
307 | * |
||
308 | * Side Effects: None |
||
309 | * |
||
310 | * Overview: Klears the specified USB interrupt flag. |
||
311 | * |
||
312 | * Note: Individual USB interrupt flag bits are "Kleared" by writing |
||
313 | * '1' to the bit |
||
314 | *******************************************************************/ |
||
315 | #define USBClearInterruptFlag(reg_name, if_flag_offset) (reg_name = (1 << if_flag_offset)) |
||
316 | |||
317 | /******************************************************************** |
||
318 | Function: |
||
319 | void DisableNonZeroEndpoints(UINT8 last_ep_num) |
||
320 | |||
321 | Summary: |
||
322 | Clears the control registers for the specified non-zero endpoints |
||
323 | |||
324 | PreCondition: |
||
325 | None |
||
326 | |||
327 | Parameters: |
||
328 | UINT8 last_ep_num - the last endpoint number to clear. This |
||
329 | number should include all endpoints used in any configuration. |
||
330 | |||
331 | Return Values: |
||
332 | None |
||
333 | |||
334 | Remarks: |
||
335 | None |
||
336 | |||
337 | *******************************************************************/ |
||
338 | #define DisableNonZeroEndpoints(last_ep_num) {\ |
||
339 | UINT8 i;\ |
||
340 | UINT32 *p = (UINT32*)&U1EP1;\ |
||
341 | for(i=0;i<last_ep_num;i++)\ |
||
342 | {\ |
||
343 | *p = 0;\ |
||
344 | p += 4;\ |
||
345 | }\ |
||
346 | } |
||
347 | |||
348 | #define USBClearUSBInterrupt() IFS1bits.USBIF = 0; |
||
349 | #if defined(USB_DISABLE_SOF_HANDLER) |
||
350 | #define USB_SOF_INTERRUPT 0x00 |
||
351 | #else |
||
352 | #define USB_SOF_INTERRUPT 0x04 |
||
353 | #endif |
||
354 | #if defined(USB_DISABLE_ERROR_HANDLER) |
||
355 | #define USB_ERROR_INTERRUPT 0x02 |
||
356 | #else |
||
357 | #define USB_ERROR_INTERRUPT 0x02 |
||
358 | #endif |
||
359 | |||
360 | //STALLIE, IDLEIE, TRNIE, and URSTIE are all enabled by default and are required |
||
361 | #if defined(USB_INTERRUPT) |
||
362 | #define USBEnableInterrupts() {IEC1bits.USBIE = 1;IPC11CLR=0x0000FF00;IPC11SET=0x00001000;INTEnableSystemMultiVectoredInt(); INTEnableInterrupts();} |
||
363 | #else |
||
364 | #define USBEnableInterrupts() |
||
365 | #endif |
||
366 | |||
367 | #define USBDisableInterrupts() {IEC1bits.USBIE = 0;} |
||
368 | |||
369 | #if defined(USB_INTERRUPT) |
||
370 | #define USBMaskInterrupts() {IEC1bits.USBIE = 0;} |
||
371 | #define USBUnmaskInterrupts() {IEC1bits.USBIE = 1;} |
||
372 | #else |
||
373 | #define USBMaskInterrupts() |
||
374 | #define USBUnmaskInterrupts() |
||
375 | #endif |
||
376 | |||
377 | #define EP_CTRL 0x0C // Cfg Control pipe for this ep |
||
378 | #define EP_OUT 0x18 // Cfg OUT only pipe for this ep |
||
379 | #define EP_IN 0x14 // Cfg IN only pipe for this ep |
||
380 | #define EP_OUT_IN 0x1C // Cfg both OUT & IN pipes for this ep |
||
381 | #define HSHK_EN 0x01 // Enable handshake packet |
||
382 | // Handshake should be disable for isoch |
||
383 | |||
384 | #define USB_HANDSHAKE_ENABLED 0x01 |
||
385 | #define USB_HANDSHAKE_DISABLED 0x00 |
||
386 | |||
387 | #define USB_OUT_ENABLED 0x08 |
||
388 | #define USB_OUT_DISABLED 0x00 |
||
389 | |||
390 | #define USB_IN_ENABLED 0x04 |
||
391 | #define USB_IN_DISABLED 0x00 |
||
392 | |||
393 | #define USB_ALLOW_SETUP 0x00 |
||
394 | #define USB_DISALLOW_SETUP 0x10 |
||
395 | |||
396 | #define USB_STALL_ENDPOINT 0x02 |
||
397 | |||
398 | #define SetConfigurationOptions() {U1CNFG1 = 0;U1EIE = 0x9F;U1IE = 0x99 | USB_SOF_INTERRUPT | USB_ERROR_INTERRUPT; U1OTGCON &= 0x000F; U1OTGCON |= USB_PULLUP_OPTION;} |
||
399 | |||
400 | #define USBClearInterruptRegister(reg) reg = 0xFF; |
||
401 | |||
402 | // Buffer Descriptor Status Register layout. |
||
403 | typedef union __attribute__ ((packed)) _BD_STAT |
||
404 | { |
||
405 | struct __attribute__ ((packed)){ |
||
406 | unsigned :2; |
||
407 | unsigned BSTALL :1; //Buffer Stall Enable |
||
408 | unsigned DTSEN :1; //Data Toggle Synch Enable |
||
409 | unsigned :2; //Reserved - write as 00 |
||
410 | unsigned DTS :1; //Data Toggle Synch Value |
||
411 | unsigned UOWN :1; //USB Ownership |
||
412 | }; |
||
413 | struct __attribute__ ((packed)){ |
||
414 | unsigned :2; |
||
415 | unsigned PID0 :1; |
||
416 | unsigned PID1 :1; |
||
417 | unsigned PID2 :1; |
||
418 | unsigned PID3 :1; |
||
419 | |||
420 | }; |
||
421 | struct __attribute__ ((packed)){ |
||
422 | unsigned :2; |
||
423 | unsigned PID :4; //Packet Identifier |
||
424 | }; |
||
425 | WORD Val; |
||
426 | } BD_STAT; |
||
427 | |||
428 | // BDT Entry Layout |
||
429 | typedef union __attribute__ ((packed))__BDT |
||
430 | { |
||
431 | struct __attribute__ ((packed)) |
||
432 | { |
||
433 | BD_STAT STAT; |
||
434 | WORD CNT:10; |
||
435 | DWORD ADR; //Buffer Address |
||
436 | }; |
||
437 | struct __attribute__ ((packed)) |
||
438 | { |
||
439 | DWORD res :16; |
||
440 | DWORD count:10; |
||
441 | }; |
||
442 | DWORD w[2]; |
||
443 | WORD v[4]; |
||
444 | QWORD Val; |
||
445 | } BDT_ENTRY; |
||
446 | |||
447 | #if defined(USB_SUPPORT_DEVICE) | defined(USB_SUPPORT_OTG) |
||
448 | #if !defined(USBDEVICE_C) |
||
449 | //extern USB_VOLATILE USB_DEVICE_STATE USBDeviceState; |
||
450 | extern USB_VOLATILE BYTE USBActiveConfiguration; |
||
451 | extern USB_VOLATILE IN_PIPE inPipes[1]; |
||
452 | extern USB_VOLATILE OUT_PIPE outPipes[1]; |
||
453 | extern volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1]; |
||
454 | #endif |
||
455 | #endif |
||
456 | |||
457 | #endif //USB_HAL_PIC32_H |
Powered by WebSVN v2.8.3