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 | *******************************************************************************/ |
||
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 | *************************************************************************/ |
||
91 | |||
92 | //DOM-IGNORE-BEGIN |
||
93 | /******************************************************************** |
||
94 | Change History: |
||
95 | Rev Description |
||
96 | ---- ----------- |
||
97 | 2.6 Changed the inplementation of the interrupt clearing macro |
||
98 | to be more efficient. |
||
99 | |||
100 | 2.6a Added DisableNonZeroEndpoints() function |
||
101 | |||
102 | 2.7 Addition of ConvertToVirtualAddress() function for |
||
103 | compatibility with PIC32. |
||
104 | |||
105 | Added prototype for USBSleepOnSuspend() function. This |
||
106 | function shows how to put the PIC24F to sleep while the USB |
||
107 | module is in suspend and have the USB module wake up the device |
||
108 | on activity on the bus. |
||
109 | |||
110 | 2.7a No change |
||
111 | ********************************************************************/ |
||
112 | //DOM-IGNORE-END |
||
113 | |||
114 | #ifndef USB_HAL_PIC24F_H |
||
115 | #define USB_HAL_PIC24F_H |
||
116 | |||
117 | //#if defined(USB_SUPPORT_DEVICE) | defined(USB_SUPPORT_OTG) |
||
118 | #include "Compiler.h" |
||
119 | |||
120 | #define USBSetBDTAddress(addr) U1BDTP1 = (((unsigned int)addr)/256); |
||
121 | #define USBPowerModule() U1PWRCbits.USBPWR = 1; |
||
122 | #define USBPingPongBufferReset U1CONbits.PPBRST |
||
123 | |||
124 | #define USBTransactionCompleteIE U1IEbits.TRNIE |
||
125 | #define USBTransactionCompleteIF U1IRbits.TRNIF |
||
126 | #define USBTransactionCompleteIFReg U1IR |
||
127 | #define USBTransactionCompleteIFBitNum 3 |
||
128 | |||
129 | #define USBResetIE U1IEbits.URSTIE |
||
130 | #define USBResetIF U1IRbits.URSTIF |
||
131 | #define USBResetIFReg U1IR |
||
132 | #define USBResetIFBitNum 0 |
||
133 | |||
134 | #define USBIdleIE U1IEbits.IDLEIE |
||
135 | #define USBIdleIF U1IRbits.IDLEIF |
||
136 | #define USBIdleIFReg U1IR |
||
137 | #define USBIdleIFBitNum 4 |
||
138 | |||
139 | #define USBActivityIE U1OTGIEbits.ACTVIE |
||
140 | #define USBActivityIF U1OTGIRbits.ACTVIF |
||
141 | #define USBActivityIFReg U1OTGIR |
||
142 | #define USBActivityIFBitNum 4 |
||
143 | |||
144 | #define USBSOFIE U1IEbits.SOFIE |
||
145 | #define USBSOFIF U1IRbits.SOFIF |
||
146 | #define USBSOFIFReg U1IR |
||
147 | #define USBSOFIFBitNum 2 |
||
148 | |||
149 | #define USBStallIE U1IEbits.STALLIE |
||
150 | #define USBStallIF U1IRbits.STALLIF |
||
151 | #define USBStallIFReg U1IR |
||
152 | #define USBStallIFBitNum 7 |
||
153 | |||
154 | #define USBErrorIE U1IEbits.UERRIE |
||
155 | #define USBErrorIF U1IRbits.UERRIF |
||
156 | #define USBErrorIFReg U1IR |
||
157 | #define USBErrorIFBitNum 1 |
||
158 | |||
159 | #define USBSE0Event U1CONbits.SE0 |
||
160 | #define USBSuspendControl U1PWRCbits.USUSPEND |
||
161 | #define USBPacketDisable U1CONbits.PKTDIS |
||
162 | #define USBResumeControl U1CONbits.RESUME |
||
163 | |||
164 | #define USBT1MSECIE U1OTGIEbits.T1MSECIE |
||
165 | #define USBT1MSECIF U1OTGIRbits.T1MSECIF |
||
166 | #define USBT1MSECIFReg U1OTGIR |
||
167 | #define USBT1MSECIFBitNum 6 |
||
168 | |||
169 | #define USBIDIE U1OTGIEbits.IDIE |
||
170 | #define USBIDIF U1OTGIRbits.IDIF |
||
171 | #define USBIDIFReg U1OTGIR |
||
172 | #define USBIDIFBitNum 7 |
||
173 | |||
174 | /* Buffer Descriptor Status Register Initialization Parameters */ |
||
175 | |||
176 | //The _BSTALL definition is changed from 0x04 to 0x00 to |
||
177 | // fix a difference in the PIC18 and PIC24 definitions of this |
||
178 | // bit. This should be changed back once the definitions are |
||
179 | // synced. |
||
180 | #define _BSTALL 0x04 //Buffer Stall enable |
||
181 | #define _DTSEN 0x08 //Data Toggle Synch enable |
||
182 | #define _DAT0 0x00 //DATA0 packet expected next |
||
183 | #define _DAT1 0x40 //DATA1 packet expected next |
||
184 | #define _DTSMASK 0x40 //DTS Mask |
||
185 | #define _USIE 0x80 //SIE owns buffer |
||
186 | #define _UCPU 0x00 //CPU owns buffer |
||
187 | |||
188 | #define _STAT_MASK 0xFC |
||
189 | |||
190 | #define USTAT_EP0_PP_MASK ~0x04 |
||
191 | #define USTAT_EP_MASK 0xFC |
||
192 | #define USTAT_EP0_OUT 0x00 |
||
193 | #define USTAT_EP0_OUT_EVEN 0x00 |
||
194 | #define USTAT_EP0_OUT_ODD 0x04 |
||
195 | |||
196 | #define USTAT_EP0_IN 0x08 |
||
197 | #define USTAT_EP0_IN_EVEN 0x08 |
||
198 | #define USTAT_EP0_IN_ODD 0x0C |
||
199 | |||
200 | typedef union |
||
201 | { |
||
202 | WORD UEP[16]; |
||
203 | } _UEP; |
||
204 | |||
205 | #define UEP_STALL 0x0002 |
||
206 | |||
207 | typedef union _POINTER |
||
208 | { |
||
209 | struct |
||
210 | { |
||
211 | BYTE bLow; |
||
212 | BYTE bHigh; |
||
213 | //byte bUpper; |
||
214 | }; |
||
215 | WORD _word; // bLow & bHigh |
||
216 | |||
217 | //pFunc _pFunc; // Usage: ptr.pFunc(); Init: ptr.pFunc = &<Function>; |
||
218 | |||
219 | BYTE* bRam; // Ram byte pointer: 2 bytes pointer pointing |
||
220 | // to 1 byte of data |
||
221 | WORD* wRam; // Ram word poitner: 2 bytes poitner pointing |
||
222 | // to 2 bytes of data |
||
223 | |||
224 | ROM BYTE* bRom; // Size depends on compiler setting |
||
225 | ROM WORD* wRom; |
||
226 | //rom near byte* nbRom; // Near = 2 bytes pointer |
||
227 | //rom near word* nwRom; |
||
228 | //rom far byte* fbRom; // Far = 3 bytes pointer |
||
229 | //rom far word* fwRom; |
||
230 | } POINTER; |
||
231 | |||
232 | //******** Depricated: v2.2 - will be removed at some point of time *** |
||
233 | #define _LS 0x00 // Use Low-Speed USB Mode |
||
234 | #define _FS 0x00 // Use Full-Speed USB Mode |
||
235 | #define _TRINT 0x00 // Use internal transceiver |
||
236 | #define _TREXT 0x00 // Use external transceiver |
||
237 | #define _PUEN 0x00 // Use internal pull-up resistor |
||
238 | #define _OEMON 0x00 // Use SIE output indicator |
||
239 | //********************************************************************** |
||
240 | |||
241 | #define USB_PULLUP_ENABLE 0x00 |
||
242 | //#define USB_PULLUP_DISABLE 0x00 |
||
243 | |||
244 | #define USB_OTG_ENABLE 0x04 |
||
245 | #define USB_OTG_DPLUS_ENABLE 0x80 |
||
246 | |||
247 | #define USB_INTERNAL_TRANSCEIVER 0x00 |
||
248 | #define USB_EXTERNAL_TRANSCEIVER 0x01 |
||
249 | |||
250 | #define USB_FULL_SPEED 0x00 |
||
251 | //USB_LOW_SPEED not currently supported in PIC24F USB products |
||
252 | #define ConvertToPhysicalAddress(a) ((WORD)(a)) |
||
253 | #define ConvertToVirtualAddress(a) ((void *)(a)) |
||
254 | |||
255 | |||
256 | /**************************************************************** |
||
257 | Function: |
||
258 | void USBModuleDisable(void) |
||
259 | |||
260 | Description: |
||
261 | This macro is used to disable the USB module |
||
262 | |||
263 | Parameters: |
||
264 | None |
||
265 | |||
266 | Return Values: |
||
267 | None |
||
268 | |||
269 | Remarks: |
||
270 | None |
||
271 | |||
272 | ****************************************************************/ |
||
273 | #define USBModuleDisable() {\ |
||
274 | U1CON = 0;\ |
||
275 | U1IE = 0;\ |
||
276 | U1OTGIE = 0;\ |
||
277 | U1PWRCbits.USBPWR = 1;\ |
||
278 | USBDeviceState = DETACHED_STATE;\ |
||
279 | } |
||
280 | |||
281 | |||
282 | /******************************************************************** |
||
283 | * Function (macro): void USBClearInterruptFlag(register, BYTE if_flag_offset) |
||
284 | * |
||
285 | * PreCondition: None |
||
286 | * |
||
287 | * Input: |
||
288 | * register - the register mnemonic for the register holding the interrupt |
||
289 | * flag to be "kleared" |
||
290 | * BYTE if_flag_offset - the bit position offset (for the interrupt flag to |
||
291 | * "klear") from the "right of the register" |
||
292 | * |
||
293 | * Output: None |
||
294 | * |
||
295 | * Side Effects: None |
||
296 | * |
||
297 | * Overview: Klears the specified USB interrupt flag. |
||
298 | * |
||
299 | * Note: Individual USB interrupt flag bits are "Kleared" by writing |
||
300 | * '1' to the bit, in a word write operation. |
||
301 | *******************************************************************/ |
||
302 | #define USBClearInterruptFlag(reg_name, if_flag_offset) (reg_name = (1 << if_flag_offset)) |
||
303 | |||
304 | /******************************************************************** |
||
305 | Function: |
||
306 | void DisableNonZeroEndpoints(UINT8 last_ep_num) |
||
307 | |||
308 | Summary: |
||
309 | Clears the control registers for the specified non-zero endpoints |
||
310 | |||
311 | PreCondition: |
||
312 | None |
||
313 | |||
314 | Parameters: |
||
315 | UINT8 last_ep_num - the last endpoint number to clear. This |
||
316 | number should include all endpoints used in any configuration. |
||
317 | |||
318 | Return Values: |
||
319 | None |
||
320 | |||
321 | Remarks: |
||
322 | None |
||
323 | |||
324 | *******************************************************************/ |
||
325 | #define DisableNonZeroEndpoints(last_ep_num) memset((void*)&U1EP1,0x00,(last_ep_num * 2)); |
||
326 | |||
327 | |||
328 | /******************************************************************** |
||
329 | Function: |
||
330 | BOOL USBSleepOnSuspend(void) |
||
331 | |||
332 | Summary: |
||
333 | Places the PIC24F core into sleep and sets up the USB module |
||
334 | to wake up the device on USB activity. |
||
335 | |||
336 | PreCondition: |
||
337 | IPL (in the SR register) must be non-zero. |
||
338 | |||
339 | Parameters: |
||
340 | None |
||
341 | |||
342 | Return Values: |
||
343 | TRUE - if entered sleep successfully |
||
344 | FALSE - if there was an error entering sleep |
||
345 | |||
346 | Remarks: |
||
347 | Please note that before calling this function that it is the |
||
348 | responsibility of the application to place all of the other |
||
349 | peripherals or board features into a lower power state if |
||
350 | required. |
||
351 | |||
352 | *******************************************************************/ |
||
353 | BOOL USBSleepOnSuspend(void); |
||
354 | |||
355 | #define USBClearUSBInterrupt() IFS5bits.USB1IF = 0; |
||
356 | #if defined(USB_INTERRUPT) |
||
357 | #define USBMaskInterrupts() {IEC5bits.USB1IE = 0;} |
||
358 | #define USBUnmaskInterrupts() {IEC5bits.USB1IE = 1;} |
||
359 | #else |
||
360 | #define USBMaskInterrupts() |
||
361 | #define USBUnmaskInterrupts() |
||
362 | #endif |
||
363 | |||
364 | |||
365 | #if defined(USB_DISABLE_SOF_HANDLER) |
||
366 | #define USB_SOF_INTERRUPT 0x00 |
||
367 | #else |
||
368 | #define USB_SOF_INTERRUPT 0x04 |
||
369 | #endif |
||
370 | #if defined(USB_DISABLE_ERROR_HANDLER) |
||
371 | #define USB_ERROR_INTERRUPT 0x02 |
||
372 | #else |
||
373 | #define USB_ERROR_INTERRUPT 0x02 |
||
374 | #endif |
||
375 | |||
376 | //STALLIE, IDLEIE, TRNIE, and URSTIE are all enabled by default and are required |
||
377 | #if defined(USB_INTERRUPT) |
||
378 | #define USBEnableInterrupts() {IEC5bits.USB1IE=1;} |
||
379 | #else |
||
380 | #define USBEnableInterrupts() |
||
381 | #endif |
||
382 | |||
383 | #define USBDisableInterrupts() {IEC5bits.USB1IE=0;} |
||
384 | #define ENDPOINT_MASK 0b11110000 |
||
385 | |||
386 | #define EP_CTRL 0x0C // Cfg Control pipe for this ep |
||
387 | #define EP_OUT 0x18 // Cfg OUT only pipe for this ep |
||
388 | #define EP_IN 0x14 // Cfg IN only pipe for this ep |
||
389 | #define EP_OUT_IN 0x1C // Cfg both OUT & IN pipes for this ep |
||
390 | #define HSHK_EN 0x01 // Enable handshake packet |
||
391 | // Handshake should be disable for isoch |
||
392 | |||
393 | #define USB_HANDSHAKE_ENABLED 0x01 |
||
394 | #define USB_HANDSHAKE_DISABLED 0x00 |
||
395 | |||
396 | #define USB_OUT_ENABLED 0x08 |
||
397 | #define USB_OUT_DISABLED 0x00 |
||
398 | |||
399 | #define USB_IN_ENABLED 0x04 |
||
400 | #define USB_IN_DISABLED 0x00 |
||
401 | |||
402 | #define USB_ALLOW_SETUP 0x00 |
||
403 | #define USB_DISALLOW_SETUP 0x10 |
||
404 | |||
405 | #define USB_STALL_ENDPOINT 0x02 |
||
406 | |||
407 | #if (USB_PULLUP_OPTION == USB_PULLUP_ENABLE) || !defined(USB_PULLUP_OPTION) |
||
408 | #define PullUpConfiguration() U1OTGCONbits.OTGEN = 0; |
||
409 | #else |
||
410 | #define PullUpConfiguration() U1OTGCONbits.OTGEN = 1; U1OTGCON &= 0xFF0F; |
||
411 | #endif |
||
412 | |||
413 | #define SetConfigurationOptions() {\ |
||
414 | U1CNFG1 = USB_PING_PONG_MODE;\ |
||
415 | U1CNFG2 = USB_TRANSCEIVER_OPTION;\ |
||
416 | PullUpConfiguration();\ |
||
417 | U1EIE = 0x9F;\ |
||
418 | U1IE = 0x99 | USB_SOF_INTERRUPT | USB_ERROR_INTERRUPT;\ |
||
419 | } |
||
420 | |||
421 | // #if defined(USB_SPEED_OPTION) |
||
422 | // #if (USB_SPEED_OPTION == USB_LOW_SPEED) |
||
423 | // #error "Low speed operation in device mode is not currently supported in the PIC24F family devices." |
||
424 | // #endif |
||
425 | // #endif |
||
426 | |||
427 | #define USBClearInterruptRegister(reg) reg = 0xFF; |
||
428 | |||
429 | // Buffer Descriptor Status Register layout. |
||
430 | typedef union _BD_STAT |
||
431 | { |
||
432 | struct{ |
||
433 | unsigned :2; //Byte count |
||
434 | unsigned BSTALL :1; //Buffer Stall Enable |
||
435 | unsigned DTSEN :1; //Data Toggle Synch Enable |
||
436 | unsigned :2; //Reserved - write as 00 |
||
437 | unsigned DTS :1; //Data Toggle Synch Value |
||
438 | unsigned UOWN :1; //USB Ownership |
||
439 | }; |
||
440 | struct{ |
||
441 | unsigned :2; |
||
442 | unsigned PID0 :1; |
||
443 | unsigned PID1 :1; |
||
444 | unsigned PID2 :1; |
||
445 | unsigned PID3 :1; |
||
446 | }; |
||
447 | struct{ |
||
448 | unsigned :2; |
||
449 | unsigned PID :4; // Packet Identifier |
||
450 | }; |
||
451 | BYTE Val; |
||
452 | } BD_STAT; //Buffer Descriptor Status Register |
||
453 | |||
454 | // BDT Entry Layout |
||
455 | typedef union __BDT |
||
456 | { |
||
457 | union |
||
458 | { |
||
459 | struct |
||
460 | { |
||
461 | BYTE CNT __attribute__ ((packed)); |
||
462 | BD_STAT STAT __attribute__ ((packed)); |
||
463 | }; |
||
464 | struct |
||
465 | { |
||
466 | WORD count:10; //test |
||
467 | BYTE :6; |
||
468 | WORD ADR; //Buffer Address |
||
469 | }; |
||
470 | }; |
||
471 | DWORD Val; |
||
472 | WORD v[2]; |
||
473 | } BDT_ENTRY; |
||
474 | |||
475 | #if defined(USB_SUPPORT_DEVICE) | defined(USB_SUPPORT_OTG) |
||
476 | #if !defined(USBDEVICE_C) |
||
477 | //extern USB_VOLATILE USB_DEVICE_STATE USBDeviceState; |
||
478 | extern USB_VOLATILE BYTE USBActiveConfiguration; |
||
479 | extern USB_VOLATILE IN_PIPE inPipes[1]; |
||
480 | extern USB_VOLATILE OUT_PIPE outPipes[1]; |
||
481 | extern volatile BDT_ENTRY *pBDTEntryIn[USB_MAX_EP_NUMBER+1]; |
||
482 | #endif |
||
483 | #endif |
||
484 | |||
485 | #endif //USB_HAL_PIC24F_H |
Powered by WebSVN v2.8.3