?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /******************************************************************************
2  
3 USB OTG Layer
4  
5  
6 *******************************************************************************/
7 //DOM-IGNORE-BEGIN
8 /******************************************************************************
9  
10 File Name: usb_otg.c
11 Dependencies: None
12 Processor: PIC24F/PIC32MX
13 Compiler: C30/C32
14 Company: Microchip Technology, Inc.
15  
16 Software License Agreement
17  
18 The software supplied herewith by Microchip Technology Incorporated
19 (the “Company”) for its PICmicro® Microcontroller is intended and
20 supplied to you, the Company’s customer, for use solely and
21 exclusively on Microchip PICmicro Microcontroller products. The
22 software is owned by the Company and/or its supplier, and is
23 protected under applicable copyright laws. All rights are reserved.
24 Any use in violation of the foregoing restrictions may subject the
25 user to criminal sanctions under applicable laws, as well as to
26 civil liability for the breach of the terms and conditions of this
27 license.
28  
29 THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
30 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
31 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
32 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
33 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
34 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
35  
36 Change History:
37 Rev Description
38 ---------- ----------------------------------------------------------
39 2.6 - 2.7a No change
40  
41 *******************************************************************************/
42  
43 #include "GenericTypeDefs.h"
44 #include "HardwareProfile.h"
45 #include "Compiler.h"
46 #include "usb_config.h"
47 #include "USB/usb.h"
48 //#include "USB/usb_device.h"
49 //#include "USB/usb_host.h"
50 #include "USB/usb_otg.h"
51 #include "USB/usb_host_generic.h"
52 #include "uart2.h"
53  
54 //#define DEBUG_MODE
55  
56  
57 // *****************************************************************************
58 // *****************************************************************************
59 // Section: HNP Event Flow
60 // *****************************************************************************
61 // *****************************************************************************
62 /*
63 // *****************************************************************************
64 (A becomes Device, B becomes Host)
65 A side(Host) B side(Device)
66 // *****************************************************************************
67 Suspend-SelectRole(ROLE_DEVICE) -------> Idle-SelectRole(ROLE_HOST)
68 Detach - OTG_EVENT_DISCONNECT <------- D+ Pullup Disabled
69 D+ Pullup Enabled -------> Attach - OTG_EVENT_CONNECT
70  
71 // *****************************************************************************
72 (A becomes Host, B becomes Device)
73 A side(Device) B side(Host)
74 // *****************************************************************************
75 Idle-SelectRole(ROLE_HOST) <------- Suspend-SelectRole(ROLE_DEVICE)
76 D+ Pullup Disabled -------> Detach - OTG_EVENT_DISCONNECT
77 USGOTGInitializeHostStack()
78 */
79  
80  
81 // *****************************************************************************
82 // Global Variables
83 // *****************************************************************************
84 //DEFAULT_ROLE needs to be configured as ROLE_HOST or ROLE_DEVICE if not using a Micro AB OTG cable, see usb_config.h for configuring
85 BYTE CurrentRole, DefaultRole;
86 WORD HNPTimeOut, SRPTimeOut;
87 BOOL HNPActive=0, SRPActive=0, SRPReady=0, HNPEnable=0, HNPSupport=0, HNPAltSupport=0, HNPTimeOutFlag=0, SRPTimeOutFlag=0;
88 BOOL RoleSwitch=0;
89 extern volatile WORD usbOverrideHostState;
90  
91 // *****************************************************************************
92 // Local Prototypes
93 // *****************************************************************************
94 void USBOTGActivateSrp();
95 void USBOTGDeactivateSrp();
96 void USBOTGActivateHnp();
97 void USBOTGDisableInterrupts();
98  
99 //DOM-IGNORE-BEGIN
100 /****************************************************************************
101 Function:
102 void USBOTGDisableInterrupts()
103  
104 Description:
105 This function disables the apropriate interrupts
106  
107 Precondition:
108 None
109  
110 Parameters:
111 None
112  
113 Return Values:
114 None
115  
116 Remarks:
117 None
118 ***************************************************************************/
119 //DOM-IGNORE-END
120 void USBOTGDisableInterrupts()
121 {
122 #if defined(__C30__)
123 //Disable All Interrupts
124 IEC5 = 0;
125 U1IR=0xFF;
126 U1IE=0;
127 U1OTGIE &= 0x8C;
128 U1OTGIR = 0x7D;
129 IFS5 = 0;
130 #elif defined( __PIC32MX__ )
131 // Disable All Interrupts
132 IEC1CLR = 0x02000000;
133 U1IR=0xFF;
134 U1IE=0;
135 U1OTGIE &= 0x8C;
136 U1OTGIR = 0x7D;
137 IFS1CLR = 0x02000000;
138 #endif
139 }
140  
141 //DOM-IGNORE-BEGIN
142 /****************************************************************************
143 Function:
144 void InitializeDeviceStack()
145  
146 Description:
147 This function initializes the device stack for use in an OTG application
148  
149 Precondition:
150 None
151  
152 Parameters:
153 None
154  
155 Return Values:
156 None
157  
158 Remarks:
159 None
160 ***************************************************************************/
161 //DOM-IGNORE-END
162 void USBOTGInitializeDeviceStack()
163 {
164 //Disable Interrupts
165 USBOTGDisableInterrupts();
166  
167 //Enable D+ Pullup
168 U1OTGCONbits.DPPULUP = 1;
169  
170 //Disable D+ Pulldown
171 U1OTGCONbits.DPPULDWN = 0;
172  
173 //Switch Role To Device
174 CurrentRole = ROLE_DEVICE;
175  
176 //Ensure HNP/SRP Fail Timer Check Is Disabled
177 U1OTGIEbits.T1MSECIE = 0;
178 HNPTimeOutFlag = 0;
179 SRPTimeOutFlag = 0;
180 SRPReady = 0;
181  
182 //Initialize Device Stack
183 USBDeviceInit();
184  
185 //Call Device Tasks
186 USBDeviceTasks();
187  
188 //Deactivate HNP
189 USBOTGDeactivateHnp();
190  
191 //Disable HNP
192 USBOTGDisableHnp();
193  
194 //Set Role Switch Flag
195 RoleSwitch = TRUE;
196 }
197  
198  
199 //DOM-IGNORE-BEGIN
200 /****************************************************************************
201 Function:
202 void InitializeHostStack()
203  
204 Description:
205 This function initializes the host stack for use in an OTG application
206  
207 Precondition:
208 None
209  
210 Parameters:
211 None
212  
213 Return Values:
214 None
215  
216 Remarks:
217 None
218 ***************************************************************************/
219 //DOM-IGNORE-END
220 void USBOTGInitializeHostStack()
221 {
222 //Enable D+ Pulldown
223 U1OTGCONbits.DPPULDWN = 1;
224  
225 //Disable D+ Pullup
226 U1OTGCONbits.DPPULUP = 0;
227  
228 //Switch Role To Host
229 CurrentRole = ROLE_HOST;
230  
231 //Ensure HNP/SRP Fail Timer Check Is Disabled
232 U1OTGIEbits.T1MSECIE = 0;
233 HNPTimeOutFlag = 0;
234 SRPTimeOutFlag = 0;
235 SRPReady = 0;
236  
237 //If A side Host Then
238 if (DefaultRole == ROLE_HOST)
239 {
240 //Substate Initialize
241 usbOverrideHostState = 0;
242 USBHostTasks();
243 }
244  
245 //If B side, Host Stack Needs To Be Manually Initialized Here To Perfom Reset Within TB_ACON_BSE0 (1ms Max)
246 else if (DefaultRole == ROLE_DEVICE)
247 {
248 //Substate Initialize
249 usbOverrideHostState = 0;
250 USBHostTasks();
251  
252 //Issue Power On
253 usbOverrideHostState = 0x0020;
254 USBHostTasks();
255  
256 //Issue Reset
257 usbOverrideHostState = 0x0110;
258 USBHostTasks();
259 }
260  
261 //Deactivate HNP
262 USBOTGDeactivateHnp();
263  
264 //Disable HNP
265 USBOTGDisableHnp();
266  
267 //Set Role Switch Flag
268 RoleSwitch = TRUE;
269 }
270  
271  
272 //DOM-IGNORE-BEGIN
273 /****************************************************************************
274 Function:
275 void USBOTGInitialize()
276  
277 Description:
278 This function initializes an OTG application and initializes a default role of Host or Device
279  
280 Precondition:
281 None
282  
283 Parameters:
284 None
285  
286 Return Values:
287 None
288  
289 Remarks:
290 #define USB_MICRO_AB_OTG_CABLE should be commented out in usb_config.h
291 if not using a micro AB OTG cable
292 ***************************************************************************/
293 //DOM-IGNORE-END
294 void USBOTGInitialize()
295 {
296 #ifdef USB_MICRO_AB_OTG_CABLE
297 //Power Up Module
298 U1PWRCbits.USBPWR = 1;
299  
300 //Ensure Fail Timer Check Is Disabled
301 U1OTGIEbits.T1MSECIE = 0;
302 HNPTimeOutFlag = 0;
303 SRPTimeOutFlag = 0;
304 SRPReady = 0;
305  
306 #if defined(__C30__)
307 //Configure VBUS I/O
308 PORTGbits.RG12 = 0;
309 LATGbits.LATG12 = 0;
310 TRISGbits.TRISG12 = 0;
311 #elif defined(__PIC32MX__)
312 VBUS_Off;
313 #endif
314  
315 //Initialize VB_SESS_END Interrupt
316 U1OTGIR = 0x04;
317 U1OTGIEbits.SESENDIE = 1;
318  
319 //Disable HNP
320 USBOTGDisableHnp();
321  
322 //Deactivate HNP
323 USBOTGDeactivateHnp();
324  
325 //Enable OTG Features
326 U1OTGCONbits.OTGEN = 1;
327  
328 //Enable D- Pulldown
329 U1OTGCONbits.DMPULDWN = 1;
330  
331 //Clear ID Flag
332 U1OTGIR = 0x80;
333  
334 //Delay Some Time For Module To Detect Cable
335 USBOTGDelayMs(40);
336  
337 //Clear ID Flag
338 U1OTGIR = 0x80;
339  
340 //Enable ID Interrupt
341 U1OTGIEbits.IDIE = 1;
342  
343 //If "A" side Plug Then
344 if (U1OTGSTATbits.ID == CABLE_A_SIDE)
345 {
346 #ifdef DEBUG_MODE
347 UART2PrintString( "\r\n***** USB OTG Initialize A Side**********\r\n" );
348 #endif
349  
350 //Configure Default Host Role
351 DefaultRole=ROLE_HOST;
352 CurrentRole=DefaultRole;
353  
354 //Set Role Switch Flag
355 RoleSwitch = TRUE;
356  
357 //Initialize Host Stack
358 USBOTGInitializeHostStack();
359  
360 //Start Session
361 USBOTGSession(START_SESSION);
362 }
363  
364 //Else, "B" side Plug
365 else
366 {
367 #ifdef DEBUG_MODE
368 UART2PrintString( "\r\n***** USB OTG Initialize B Side**********\r\n" );
369 #endif
370  
371 //Configure Default Device Role
372 DefaultRole=ROLE_DEVICE;
373 CurrentRole=DefaultRole;
374  
375 //Set Role Switch Flag
376 RoleSwitch = TRUE;
377  
378 //Initialize Device Stack
379 USBOTGInitializeDeviceStack();
380 }
381  
382 #else
383 //Power Up Module
384 U1PWRCbits.USBPWR = 1;
385  
386 //Ensure HNP Fail Timer Check Is Disabled
387 U1OTGIEbits.T1MSECIE = 0;
388 HNPTimeOutFlag = 0;
389 SRPTimeOutFlag = 0;
390 SRPReady = 0;
391  
392 #if defined(__C30__)
393 //Configure VBUS I/O
394 PORTGbits.RG12 = 0;
395 LATGbits.LATG12 = 0;
396 TRISGbits.TRISG12 = 0;
397 #elif defined(__PIC32MX__)
398 VBUS_Off;
399 #endif
400  
401 //Disable HNP
402 USBOTGDisableHnp();
403  
404 //Deactivate HNP
405 USBOTGDeactivateHnp();
406  
407 //Enable OTG Features
408 U1OTGCONbits.OTGEN = 1;
409  
410 //Enable D- Pulldown
411 U1OTGCONbits.DMPULDWN = 1;
412  
413 DefaultRole = DEFAULT_ROLE;
414 CurrentRole = DefaultRole;
415  
416 if (CurrentRole == ROLE_HOST)
417 {
418 #ifdef DEBUG_MODE
419 UART2PrintString( "\r\n***** USB OTG Initialize A Side**********\r\n" );
420 #endif
421  
422 //Set Role Switch Flag
423 RoleSwitch = TRUE;
424  
425 //Initialize Host Stack
426 USBOTGInitializeHostStack();
427  
428 //Start Session
429 USBOTGSession(START_SESSION);
430 }
431  
432 else if (CurrentRole == ROLE_DEVICE)
433 {
434 #ifdef DEBUG_MODE
435 UART2PrintString( "\r\n***** USB OTG Initialize B Side**********\r\n" );
436 #endif
437  
438 //Set Role Switch Flag
439 RoleSwitch = TRUE;
440  
441 //Initialize Device Stack
442 USBOTGInitializeDeviceStack();
443 }
444 #endif
445  
446 }
447  
448  
449 //DOM-IGNORE-BEGIN
450 /****************************************************************************
451 Function:
452 void USBOTGSelectRole(BOOL role)
453  
454 Description:
455 This function initiates a role switch. The parameter role that is passed to this function
456 is the desired role to switch to.
457  
458 Precondition:
459 None
460  
461 Parameters:
462 BOOL role - ROLE_DEVICE or ROLE_HOST
463  
464 Return Values:
465 None
466  
467 Remarks:
468 None
469 ***************************************************************************/
470 //DOM-IGNORE-END
471 void USBOTGSelectRole(BOOL role)
472 {
473 //If HNP Is Enabled Then
474 if (HNPEnable)
475 {
476 //If Currentrole=Host and role=Device Then
477 if(CurrentRole == ROLE_HOST && role == ROLE_DEVICE)
478 {
479 //If Session Is Valid Then
480 if (USBOTGGetSessionStatus())
481 {
482 #ifdef DEBUG_MODE
483 if (DefaultRole == ROLE_HOST)
484 {
485 UART2PrintString( "\r\n*****USB OTG A Event - HNP - A Host Suspend *****\r\n" );
486 }
487 else if (DefaultRole == ROLE_DEVICE)
488 {
489 UART2PrintString( "\r\n***** USB OTG B Event - HNP - B Host Suspend *****\r\n" );
490 }
491 #endif
492  
493 //Disable Interrupts
494 USBOTGDisableInterrupts();
495  
496 //If A side Host Then
497 if (DefaultRole == ROLE_HOST)
498 {
499 //Configure Timer For TA_AIDL_BDIS
500 HNPTimeOut = DELAY_TA_AIDL_BDIS;
501 HNPTimeOutFlag = 1;
502  
503 //Enable 1ms Timer Interrupt
504 U1OTGIR = 0x40;
505 U1OTGIEbits.T1MSECIE = 1;
506  
507 //Enable Resume Signaling Interrupt
508 U1IR = 0x20;
509 U1IEbits.RESUMEIE = 1;
510 }
511  
512 //Enable Detach Interrupt
513 U1IR = 0x01;
514 U1IEbits.DETACHIE = 1;
515  
516 //Enable USB Interrupt
517 #if defined(__C30__)
518 IFS5 &= 0xFFBF;
519 IEC5 |= 0x0040;
520 #elif defined(__PIC32MX__)
521 IFS1CLR = 0x02000000;
522 IPC11CLR = 0x0000FF00;
523 IPC11SET = 0x00001000;
524 IEC1SET = 0x02000000;
525 #endif
526  
527 //Activate Hnp (Used to capture an OTG_EVENT_DISCONNECT)
528 USBOTGActivateHnp();
529  
530 //Suspend Bus
531 U1CONbits.SOFEN = 0;
532  
533 //Put the state machine in suspend mode.
534 usbOverrideHostState = 0x410;
535 }
536  
537 //If Session Is Invalid Then
538 else
539 {
540 //Display Some Debug Messages
541 #ifdef DEBUG_MODE
542 if (DefaultRole == ROLE_HOST)
543 {
544 UART2PrintString( "\r\n*****USB OTG A Error - Session Is Invalid - Cannot Suspend *****\r\n" );
545 }
546 else if (DefaultRole == ROLE_DEVICE)
547 {
548 UART2PrintString( "\r\n***** USB OTG B Error - Session Is Invalid - Cannot Suspend *****\r\n" );
549 }
550 #endif
551 }
552 }
553  
554 //Else If Currentrole=Device and role=Host Then
555 else if (CurrentRole == ROLE_DEVICE && role == ROLE_HOST)
556 {
557 //If B Side Device Then
558 if (DefaultRole == ROLE_DEVICE)
559 {
560 //Delay 4 < time < 150ms IDLE time (TB_AIDL_BDIS)
561 USBOTGDelayMs(DELAY_TB_AIDL_BDIS);
562 }
563  
564 //If A side Device Then
565 else if (DefaultRole == ROLE_HOST)
566 {
567 //Delay 3 < time < 200ms IDLE time (TA_BIDL_ADIS)
568 USBOTGDelayMs(DELAY_TA_BIDL_ADIS);
569 }
570  
571 //Disable Interrupts
572 USBOTGDisableInterrupts();
573  
574 //If B side Device Then
575 if (DefaultRole == ROLE_DEVICE)
576 {
577 //Activate HNP (Used to capture an OTG_EVENT_CONNECT event)
578 USBOTGActivateHnp();
579  
580 //Configure Timer For TB_ASE0_BRST Check
581 HNPTimeOut = DELAY_TB_ASE0_BRST;
582 HNPTimeOutFlag = 1;
583  
584 //Enable 1ms Timer Interrupt
585 U1OTGIR = 0x40;
586 U1OTGIEbits.T1MSECIE = 1;
587  
588 //Disable Device Mode
589 U1CONbits.USBEN=0;
590  
591 //Enable Host Mode
592 U1CONbits.HOSTEN = 1;
593  
594 //Enable Attach Interrupt to capture OTG_EVENT_CONNECT from other side
595 U1IR = 0x40;
596 U1IEbits.ATTACHIE = 1;
597  
598 //Cause an OTG_EVENT_DISCONNECT on other side of cable by
599 //disabling D+ Pullup Resistor
600 U1OTGCONbits.DPPULUP = 0;
601 U1OTGCONbits.DPPULDWN = 1;
602  
603 //Wait For Line To Discharge To SE0 State
604 while(U1CONbits.SE0 == 0)
605 {}
606  
607 //Clear Attach Interrupt
608 U1IR = 0x40;
609  
610 //Enable USB Interrupt
611 #if defined(__C30__)
612 IFS5 &= 0xFFBF;
613 IEC5 |= 0x0040;
614 #elif defined(__PIC32MX__)
615 IFS1CLR = 0x02000000;
616 IPC11CLR = 0x0000FF00;
617 IPC11SET = 0x00001000;
618 IEC1SET = 0x02000000;
619 #endif
620 }
621  
622 //If A side Device Then
623 else if (DefaultRole == ROLE_HOST)
624 {
625 #ifdef DEBUG_MODE
626 UART2PrintString( "\r\n***** USB OTG A Event - Role Switch - Device -> Host *****\r\n" );
627 #endif
628  
629 //Initialize Host Stack
630 USBOTGInitializeHostStack();
631 }
632 }
633 }
634  
635 //Handles Case If B side Abruptly Ends Role As Host Without Sending A Set Feature
636 else if (CurrentRole == ROLE_DEVICE && DefaultRole == ROLE_HOST)
637 {
638 #ifdef DEBUG_MODE
639 UART2PrintString( "\r\n***** USB OTG A Event - Role Switch - Device -> Host *****\r\n" );
640 #endif
641  
642 //Disable Interrupts
643 USBOTGDisableInterrupts();
644  
645 //Delay 3 < time < 200ms IDLE time (TA_BIDL_ADIS)
646 USBOTGDelayMs(DELAY_TA_BIDL_ADIS);
647  
648 //Switch Back To Host
649 USBOTGInitializeHostStack();
650 }
651 }
652  
653  
654 //DOM-IGNORE-BEGIN
655 /****************************************************************************
656 Function:
657 BOOL USBOTGRequestSession()
658  
659 Description:
660 This function requests a Session from an A side Host using SRP. The function will return
661 TRUE if the request was successful or FALSE otherwise.
662  
663 Precondition:
664 None
665  
666 Parameters:
667 None
668  
669 Return Values:
670 TRUE or FALSE
671  
672 Remarks:
673 This function should only be called by a B side Device.
674 ***************************************************************************/
675 //DOM-IGNORE-END
676 BOOL USBOTGRequestSession()
677 {
678 //If B side Device Then
679 if (DefaultRole == ROLE_DEVICE)
680 {
681 //If Session Is Invalid and SRP Is Not In Progress Then
682 if (!USBOTGGetSessionStatus() && SRPReady == 0)
683 {
684 UART2PrintString( "\r\n***** USB OTG B Event - SRP - Trying To Request Session *****\r\n" );
685  
686 //Initial Conditions Check
687 //Discharge VBUS
688 USBOTGDischargeVBus();
689  
690 //Ensure single ended zero state for 2ms (TB_SE0_SRP)
691 if (U1CONbits.SE0 == 1)
692 {
693 //Delay TB_SE0_SRP
694 USBOTGDelayMs(DELAY_TB_SE0_SRP);
695  
696 //If Not Single ended zero and line has been stable Then
697 if (U1OTGSTATbits.LSTATE == 1 && U1CONbits.SE0 != 1)
698 {
699 //Display Error Message
700 UART2PrintString( "\r\n***** USB OTG B Error - SRP No SE0 - Cannot Request Session*****\r\n" );
701 return FALSE;
702 }
703 }
704 else
705 {
706 //Display Error Message
707 UART2PrintString( "\r\n***** USB OTG B Error - SRP No SE0 - Cannot Request Session*****\r\n" );
708 return FALSE;
709 }
710  
711 //D+ Pulsing
712 #ifdef DEBUG_MODE
713 UART2PrintString( "\r\n***** USB OTG B Event - SRP - D+ Pulsing*****\r\n" );
714 #endif
715  
716 //Ensure OTG Bit Is Set
717 U1OTGCONbits.OTGEN = 1;
718  
719 //Disable D+ Pulldown
720 U1OTGCONbits.DPPULDWN = 0;
721  
722 //Enable D+ Pullup
723 U1OTGCONbits.DPPULUP = 1;
724  
725 //5ms <= Delay < 10ms (TB_DATA_PLS)
726 USBOTGDelayMs(DELAY_TB_DATA_PLS);
727  
728 //Disable D+ Pullup
729 U1OTGCONbits.DPPULUP = 0;
730  
731 //VBUS Pulsing
732 #ifdef DEBUG_MODE
733 UART2PrintString( "\r\n***** USB OTG B Event - SRP - VBUS Pulsing*****\r\n" );
734 #endif
735 //Delay 10ms To Give A side Time To Get Ready For VBUS Pulsing
736 USBOTGDelayMs(10);
737  
738 //Enable VBUS
739 VBUS_On;
740  
741 //Delay 1ms
742 USBOTGDelayMs(1);
743  
744 //Disable VBUS
745 VBUS_Off;
746  
747 //Discharge VBUS
748 U1OTGCONbits.VBUSDIS = 1;
749  
750 //Delay some time to allow VBUS to settle
751 USBOTGDelayMs(DELAY_VBUS_SETTLE);
752  
753 //Disable Discharge Bit
754 U1OTGCONbits.VBUSDIS = 0;
755  
756 //Configure Timer For TB_SRP_FAIL Check
757 SRPTimeOut = (DELAY_TB_SRP_FAIL - DELAY_VBUS_SETTLE);
758 SRPTimeOutFlag = 1;
759  
760 //Enable 1ms Timer Interrupt
761 U1OTGIR = 0x40;
762 U1OTGIEbits.T1MSECIE = 1;
763  
764 //Indicate SRP Is Ready
765 SRPReady = 1;
766  
767 return TRUE;
768 }
769 else
770 {
771 //Session Is Still Active
772 UART2PrintString( "\r\n***** USB OTG B Error - Request Session Failed - Session Is Already Active *****\r\n" );
773  
774 return FALSE;
775 }
776 }
777  
778 //Should Never Get Here Unless This Function Is Called By A-side
779 return FALSE;
780 }
781  
782  
783 //DOM-IGNORE-BEGIN
784 /****************************************************************************
785 Function:
786 BOOL USBOTGGetSessionStatus()
787  
788 Description:
789 This function gets a session status. The function will return
790 TRUE if VBUS > Session Valid Voltage or FALSE if VBUS < Session Valid Voltage.
791  
792 Precondition:
793 None
794  
795 Parameters:
796 None
797  
798 Return Values:
799 TRUE or FALSE
800  
801 Remarks:
802  
803 ***************************************************************************/
804 //DOM-IGNORE-END
805 BOOL USBOTGGetSessionStatus()
806 {
807 //If VBUS < VA_SESS_VLD Then
808 if (U1OTGSTATbits.SESVD == 0)
809 {
810 //Session Invalid
811 return FALSE;
812 }
813 else
814 {
815 //Session Valid
816 return TRUE;
817 }
818 }
819  
820  
821 //DOM-IGNORE-BEGIN
822 /****************************************************************************
823 Function:
824 void USBOTGDischargeVBus()
825  
826 Description:
827 This function discharges VBUS.
828  
829 Precondition:
830 None
831  
832 Parameters:
833 None
834  
835 Return Values:
836 None
837  
838 Remarks:
839  
840 ***************************************************************************/
841 //DOM-IGNORE-END
842 void USBOTGDischargeVBus()
843 {
844 //Enable VBUS Discharge Enable Bit
845 U1OTGCONbits.VBUSDIS = 1;
846  
847 //Delay 50ms
848 //8k pulldown internal to device + ~60k on the board = ~7k, 6.2uF, time to discharge is ~50ms
849 USBOTGDelayMs(50);
850  
851 //Disable VBUS Discharge Bit
852 U1OTGCONbits.VBUSDIS = 0;
853 }
854  
855  
856 //DOM-IGNORE-BEGIN
857 /****************************************************************************
858 Function:
859 void USBOTGSession(BYTE Value)
860  
861 Description:
862 This function starts, ends, or toggles a VBUS session.
863  
864 Precondition:
865 This function assumes I/O controlling DC/DC converter has already been initialized
866  
867 Parameters:
868 Value - START_SESSION, END_SESSION, TOGGLE_SESSION
869  
870 Return Values:
871 TRUE - Session Started, FALSE - Session Not Started
872  
873 Remarks:
874 ***************************************************************************/
875 //DOM-IGNORE-END
876 BOOL USBOTGSession(BYTE Value)
877 {
878 //If START_SESSION or (TOGGLE_SESSION and VBUS Is Off)
879 if (Value == START_SESSION || (Value == TOGGLE_SESSION && VBUS_Status == 0))
880 {
881 //Enable VBUS
882 VBUS_On;
883  
884 //Wait 100ms for VBUS to rise (TA_WAIT_VRISE)
885 USBOTGDelayMs(DELAY_TA_WAIT_VRISE);
886  
887 //If PGOOD Is Still Low Then
888 if (PGOOD == 0)
889 {
890 //Disable VBUS
891 VBUS_Off;
892  
893 //Indicate Error Message
894 UART2PrintString( "\r\n*****USB OTG A Error - VBUS Rise Time - B Device Not Supported *****\r\n" );
895  
896 return FALSE;
897 }
898  
899 //If PGOOD is High Then
900 else
901 {
902 #ifdef OVERCURRENT_DETECTION
903 #if defined(__C30__)
904 //Enable Change Notification For Overcurrent Detection
905 CNEN6bits.CN80IE = 1;
906 IFS1bits.CNIF = 0;
907 IEC1bits.CNIE = 1;
908 IPC4bits.CNIP = 7;
909 #elif defined(__PIC32MX__)
910 BYTE temp;
911 //Enable Change Notification For Overcurrent Detection
912 CNCONSET = 0x00008000; // Enable Change Notice module
913 CNENSET= 0x00000004; // Enable CN2
914 PORTB = temp;
915 IPC6SET = 0x00140000; // Set priority level=5
916 IPC6SET = 0x00030000; // Set subpriority level=3
917 IFS1CLR = 0x00000001; // Clear the interrupt flag status bit
918 IEC1SET = 0x00000001; // Enable Change Notice interrupts
919 #endif
920 #endif
921  
922 #ifdef DEBUG_MODE
923 //Indicate Success
924 UART2PrintString( "\r\n***** USB OTG A Event - Session Started *****\r\n" );
925 #endif
926 }
927  
928 }
929  
930 //If END_SESSION or (TOGGLE_SESSION and VBUS Is On)
931 else if (Value == END_SESSION || (Value == TOGGLE_SESSION && VBUS_Status == 1))
932 {
933 #ifdef OVERCURRENT_DETECTION
934 #if defined(__C30__)
935 //Disable Change Notification For Overcurrent Detection
936 IEC1bits.CNIE = 0;
937 CNEN6bits.CN80IE = 0;
938 #elif defined(__PIC32MX__)
939 BYTE temp;
940 // Disable Change Notification
941 IEC1CLR = 0x00000001;
942 CNENCLR= 0x00000004;
943 PORTB = temp;
944 #endif
945 #endif
946  
947 //Disable VBUS
948 VBUS_Off;
949  
950 #ifdef DEBUG_MODE
951 if (DefaultRole == ROLE_HOST)
952 {
953 UART2PrintString( "\r\n***** USB OTG A Event - Session Ended *****\r\n" );
954 }
955 #endif
956 }
957  
958 return TRUE;
959 }
960  
961  
962 //DOM-IGNORE-BEGIN
963 /****************************************************************************
964 Function:
965 void USB_OTGEventHandler ( BYTE address, BYTE event, void *data, DWORD size )
966  
967 Description:
968 This function is the event handler used by both the Host and Device stacks for calling the OTG layer
969 when SRP and HNP events occur
970  
971 Precondition:
972 None
973  
974 Parameters:
975 BYTE event -
976 OTG_EVENT_SRP_DPLUS_HIGH
977 OTG_EVENT_SRP_DPLUS_LOW
978 OTG_EVENT_SRP_VBUS_HIGH
979 OTG_EVENT_SRP_VBUS_LOW
980 OTG_EVENT_DISCONNECT
981 OTG_EVENT_CONNECT
982 OTG_EVENT_HNP_ABORT
983 OTG_EVENT_HNP_FAILED
984  
985 Return Values:
986 None
987  
988 Remarks:
989 None
990 ***************************************************************************/
991 //DOM-IGNORE-END
992 void USB_OTGEventHandler ( BYTE address, BYTE event, void *data, DWORD size )
993 {
994 switch(event)
995 {
996 case OTG_EVENT_SRP_DPLUS_HIGH: //SRP D+ High Event (Caused by ATTACHIF)
997 //If A side Host Then
998 if (DefaultRole == ROLE_HOST)
999 {
1000 //If Session Is Invalid and VBUS is turned off (For Detecting D+ SRP Pulsing) then
1001 if (!USBOTGGetSessionStatus() && VBUS_Status == 0)
1002 {
1003 //If Full Speed Device That Is Performing the SRP Then
1004 if (U1OTGSTATbits.LSTATE == 1 && U1CONbits.JSTATE == 1)
1005 {
1006 #ifdef DEBUG_MODE
1007 UART2PrintString( "\r\n***** USB OTG A Event - SRP - D+ High *****\r\n" );
1008 #endif
1009  
1010 //Enable Session Valid Change Interrupt To Capture VBUS > VA_SESS_VLD
1011 U1OTGIR = 0x08;
1012 U1OTGIEbits.SESVDIE = 1;
1013  
1014 //Activate SRP
1015 USBOTGActivateSrp();
1016 }
1017 else
1018 {
1019 //Deactivate SRP
1020 USBOTGDeactivateSrp();
1021  
1022 //Indicate Error Message
1023 UART2PrintString( "\r\n***** USB OTG A Error - SRP - Low Speed Unsupported Device*****\r\n" );
1024 }
1025 }
1026 }
1027 break;
1028  
1029 case OTG_EVENT_SRP_DPLUS_LOW: //SRP D+ Low Event (Caused by DETACHIF)
1030 //If SRP Is Active Then
1031 if (SRPActive)
1032 {
1033 #ifdef DEBUG_MODE
1034 UART2PrintString( "\r\n***** USB OTG A Event - SRP - D+ Low *****\r\n" );
1035 #endif
1036 }
1037 break;
1038  
1039 case OTG_EVENT_SRP_VBUS_HIGH: //SRP VBUS > VA_SESS_VLD Event (Caused by SESVDIF)
1040 if (SRPActive)
1041 {
1042 #ifdef DEBUG_MODE
1043 UART2PrintString( "\r\n***** USB OTG A Event - SRP - VBUS > VA_SESS_VLD *****\r\n" );
1044 #endif
1045 }
1046 break;
1047  
1048 case OTG_EVENT_SRP_VBUS_LOW: //SRP VBUS < VA_SESS_VLD Event (Caused by SESVDIF)
1049 if (SRPActive)
1050 {
1051 #ifdef DEBUG_MODE
1052 UART2PrintString( "\r\n***** USB OTG A Event - SRP - VBUS < VA_SESS_VLD *****\r\n" );
1053 #endif
1054  
1055 //Disable Session Valid Change Interrupt
1056 U1OTGIEbits.SESVDIE = 0;
1057  
1058 //Deactivate SRP
1059 USBOTGDeactivateSrp();
1060  
1061 //Configure Timer For > TA_WAIT_BCON Check
1062 SRPTimeOut = DELAY_TA_WAIT_BCON;
1063 SRPTimeOutFlag = 1;
1064  
1065 //Enable 1ms Timer Interrupt
1066 U1OTGIR = 0x40;
1067 U1OTGIEbits.T1MSECIE = 1;
1068  
1069 //Set SRP Ready
1070 SRPReady = 1;
1071  
1072 //Start Session
1073 //If The B Side Connects within DELAY_TA_WAIT_BCON
1074 //this will cause a OTG_EVENT_SRP_CONNECT Event
1075 USBOTGSession(START_SESSION);
1076 }
1077 break;
1078  
1079 case OTG_EVENT_SRP_CONNECT: //SRP Connect Condition (Caused by ATTACHIF after Session Is Started)
1080 if (SRPReady)
1081 {
1082 //Clear SRP Ready
1083 SRPReady = 0;
1084  
1085 //Disable Time Out
1086 SRPTimeOutFlag = 0;
1087 }
1088 break;
1089  
1090 case OTG_EVENT_DISCONNECT: //Host Disconnect Condition (Caused by DETACHIF)
1091 //If Currentrole is Host And HNP Is Active Then
1092 if (CurrentRole == ROLE_HOST && HNPActive)
1093 {
1094  
1095 //Initialize Device Stack
1096 //Will Cause an OTG_EVENT_CONNECT Event on other side When D+ Pullup Is Enabled
1097 USBOTGInitializeDeviceStack();
1098  
1099 #ifdef DEBUG_MODE
1100 if (DefaultRole == ROLE_HOST)
1101 {
1102 UART2PrintString( "\r\n***** USB OTG A Event - B Device Disconnect *****\r\n" );
1103 UART2PrintString( "\r\n***** USB OTG A Event - Role Switch - Host -> Device *****\r\n" );
1104 }
1105 else if (DefaultRole == ROLE_DEVICE)
1106 {
1107 UART2PrintString( "\r\n***** USB OTG B Event - A Device Disconnect *****\r\n" );
1108 UART2PrintString( "\r\n***** USB OTG B Event - Role Switch - Host -> Device *****\r\n" );
1109 }
1110 #endif
1111 }
1112 break;
1113  
1114 case OTG_EVENT_CONNECT: //Device Connect Condition (Caused by ATTACHIF)
1115 //If Currentrole is Device And HNP Is Active Then
1116 if (CurrentRole == ROLE_DEVICE && HNPActive)
1117 {
1118 //Initialize Host Stack
1119 USBOTGInitializeHostStack();
1120  
1121 #ifdef DEBUG_MODE
1122 if (DefaultRole == ROLE_HOST)
1123 {
1124 UART2PrintString( "\r\n***** USB OTG A Event - Role Switch - Device -> Host *****\r\n" );
1125 UART2PrintString( "\r\n***** USB OTG A Event - B Device Connect *****\r\n" );
1126 }
1127 else if (DefaultRole == ROLE_DEVICE)
1128 {
1129 UART2PrintString( "\r\n***** USB OTG B Event - Role Switch - Device -> Host *****\r\n" );
1130 UART2PrintString( "\r\n***** USB OTG B Event - A Device Connect *****\r\n" );
1131 }
1132 #endif
1133 }
1134 break;
1135  
1136 case OTG_EVENT_HNP_ABORT: //HNP Abort Condition, Caused by Unsupported Device (CASE_HOLDING)
1137 //If HNP Is Enabled Then
1138 if (HNPEnable)
1139 {
1140 //If A side Host Then
1141 if (DefaultRole == ROLE_HOST)
1142 {
1143 //Switch To Device
1144 USBOTGSelectRole(ROLE_DEVICE);
1145  
1146 #ifdef DEBUG_MODE
1147 //Indicate Error Message
1148 UART2PrintString( "\r\n***** USB OTG A Error - HNP - Unsupported Device*****\r\n" );
1149 #endif
1150 }
1151  
1152 //If B side Host Then
1153 else if (DefaultRole == ROLE_DEVICE)
1154 {
1155 //Initialize Device Stack
1156 USBOTGInitializeDeviceStack();
1157  
1158 #ifdef DEBUG_MODE
1159 //Indicate Error Message
1160 UART2PrintString( "\r\n***** USB OTG B Error - HNP Abort - Unsupported Device*****\r\n" );
1161 #endif
1162 }
1163 }
1164 break;
1165  
1166 case OTG_EVENT_HNP_FAILED: //HNP Fail Condition, Caused by device not responding When HNP Is Enabled And A Suspend is Issued or Received(T1MSECIF)
1167 if (DefaultRole == ROLE_HOST)
1168 {
1169 //Initialize Host Stack
1170 USBOTGInitializeHostStack();
1171  
1172 //End Session
1173 USBOTGSession(END_SESSION);
1174  
1175 //Indicate Error
1176 UART2PrintString( "\r\n***** USB OTG A Error - HNP Failed - Device Not Responding - Session Ended *****\r\n" );
1177 }
1178 else if (DefaultRole == ROLE_DEVICE)
1179 {
1180 //Initialize Device Stack
1181 USBOTGInitializeDeviceStack();
1182  
1183 //Indicate Error Message
1184 UART2PrintString( "\r\n***** USB OTG B Error - HNP Failed - Device Not Responding *****\r\n" );
1185 }
1186 break;
1187  
1188 case OTG_EVENT_SRP_FAILED: //SRP Fail Condition, Caused by A or B side not Connecting on a SRP Transition (T1MSECIF)
1189 if (DefaultRole == ROLE_HOST)
1190 {
1191 //Clear SRP Ready
1192 SRPReady = 0;
1193  
1194 //End Session
1195 USBOTGSession(END_SESSION);
1196  
1197 //Indicate Error
1198 UART2PrintString( "\r\n***** USB OTG A Error - SRP Failed - Device Not Responding - Session Ended *****\r\n" );
1199 }
1200 else if (DefaultRole == ROLE_DEVICE)
1201 {
1202 //Clear SRP Ready
1203 SRPReady = 0;
1204  
1205 //SRP Failed
1206 UART2PrintString( "\r\n***** USB OTG B Error - SRP Failed - Device Not Responding *****\r\n" );
1207 }
1208 break;
1209  
1210 case OTG_EVENT_RESUME_SIGNALING:
1211 #ifdef DEBUG_MODE
1212 UART2PrintString("\r\n***** USB OTG A Event - Resume Signaling Detected *****\r\n");
1213 #endif
1214  
1215 //Disable Timers
1216 U1OTGIEbits.T1MSECIE = 0;
1217 HNPTimeOutFlag = 0;
1218 SRPTimeOutFlag = 0;
1219 SRPReady = 0;
1220  
1221 //Enable Host Resume Signaling
1222 U1CONbits.RESUME = 1;
1223  
1224 //Hold Resume for >= 20ms
1225 USBOTGDelayMs(40);
1226  
1227 //Disable Host Resume Signaling
1228 U1CONbits.RESUME = 0;
1229  
1230 //Send SOF
1231 U1CONbits.SOFEN = 1;
1232  
1233 //Resume Recovery
1234 USBOTGDelayMs(10);
1235  
1236 //Setup Normal Run State
1237 //usbOverrideHostState = 0x400;
1238 //Issue Reset
1239 usbOverrideHostState = 0x0110;
1240  
1241 //Deactivate Hnp
1242 USBOTGDeactivateHnp();
1243  
1244 //Disable Resume Interrupt
1245 U1IEbits.RESUMEIE = 0;
1246 break;
1247 }
1248 }
1249  
1250  
1251 // *****************************************************************************
1252 // *****************************************************************************
1253 // Section: Interrupt Handlers
1254 // *****************************************************************************
1255 // *****************************************************************************
1256  
1257 //DOM-IGNORE-BEGIN
1258 /****************************************************************************
1259 Function:
1260 void _CNInterrupt(void)
1261  
1262 Description:
1263 This is the interrupt service routine for the Change Notice interrupt. The
1264 following cases are serviced:
1265 * Overcurrent Detection (PGOOD Goes Low After VBUS Is Enabled)
1266  
1267 Precondition:
1268 MCP1253 Is Enabled and Activated => VBUS = 5V
1269  
1270 Parameters:
1271 None - None
1272  
1273 Returns:
1274 None
1275  
1276 Remarks:
1277 None
1278 ***************************************************************************/
1279 //DOM-IGNORE-END
1280 #ifdef OVERCURRENT_DETECTION
1281 #if defined(__C30__)
1282 void __attribute__((__interrupt__, auto_psv)) _CNInterrupt(void)
1283 #elif defined(__PIC32MX__)
1284 #pragma interrupt _CNInterrupt ipl5 vector 26
1285 void _CNInterrupt( void )
1286 #endif
1287 {
1288 BYTE temp;
1289  
1290 #ifdef DEBUG_MODE
1291 UART2PrintString( "\r\n***** PGOOD Change Notice Detected *****\r\n" );
1292 #endif
1293  
1294 //Debounce Interrupt To Ensure Wasn't Caused By Inrush
1295 USBOTGDelayMs(10);
1296  
1297 //If PGOOD Is Still Low
1298 if (PGOOD == 0)
1299 {
1300 //Disable VBUS
1301 VBUS_Off;
1302  
1303 //Indicate Error
1304 UART2PrintString( "\r\n***** USB OTG A Error - Overcurrent Condition Detected - Session Ended *****\r\n" );
1305  
1306 #if defined(__C30__)
1307 //Disable Change Notification
1308 IEC1bits.CNIE = 0;
1309 CNEN6bits.CN80IE = 0;
1310 #elif defined(__PIC32MX__)
1311 // Disable Change Notification
1312 IEC1CLR = 0x00000001;
1313 CNENCLR= 0x00000004;
1314 PORTB = temp;
1315 #endif
1316  
1317 //Initialize Host Stack
1318 USBOTGInitializeHostStack();
1319 }
1320  
1321 #if defined(__C30__)
1322 //Clear Interrupt Flag
1323 IFS1bits.CNIF = 0;
1324 #elif defined(__PIC32MX__)
1325 PORTB = temp;
1326 IFS1CLR = 0x00000001; // Clear the interrupt flag status bit
1327 #endif
1328 }
1329 #endif
1330  
1331  
1332  
1333 //DOM-IGNORE-BEGIN
1334 /****************************************************************************
1335 Function:
1336 BOOL USBOTGRoleSwitch()
1337  
1338 Description:
1339 This function returns whether a role switch occurred or not. This is used by the main application function
1340 to determine when to reinitialize the system (InitializeSystem())
1341  
1342 Precondition:
1343 None
1344  
1345 Parameters:
1346 None
1347  
1348 Return Values:
1349 BOOL - TRUE or FALSE
1350  
1351 Remarks:
1352 None
1353 ***************************************************************************/
1354 //DOM-IGNORE-END
1355 BOOL USBOTGRoleSwitch()
1356 {
1357 return RoleSwitch;
1358 }
1359  
1360  
1361 //DOM-IGNORE-BEGIN
1362 /****************************************************************************
1363 Function:
1364 void USBOTGClearRoleSwitch()
1365  
1366 Description:
1367 This function clears the RoleSwitch variable. After the main function detects the RoleSwitch
1368 and re-initializes the system, this function should be called to clear the RoleSwitch flag
1369  
1370 Precondition:
1371 None
1372  
1373 Parameters:
1374 None
1375  
1376 Return Values:
1377 None
1378  
1379 Remarks:
1380 None
1381 ***************************************************************************/
1382 //DOM-IGNORE-END
1383 void USBOTGClearRoleSwitch()
1384 {
1385 RoleSwitch = FALSE;
1386 }
1387  
1388 //DOM-IGNORE-BEGIN
1389 /****************************************************************************
1390 Function:
1391 BYTE USBOTGCurrentRoleIs()
1392  
1393 Description:
1394 This function returns whether the current role is ROLE_HOST or ROLE_DEVICE
1395  
1396 Precondition:
1397 None
1398  
1399 Parameters:
1400 None
1401  
1402 Return Values:
1403 BYTE - ROLE_HOST or ROLE_DEVICE
1404  
1405 Remarks:
1406 None
1407 ***************************************************************************/
1408 //DOM-IGNORE-END
1409 BYTE USBOTGCurrentRoleIs()
1410 {
1411 return CurrentRole;
1412 }
1413  
1414  
1415 //DOM-IGNORE-BEGIN
1416 /****************************************************************************
1417 Function:
1418 BYTE USBOTGDefaultRoleIs()
1419  
1420 Description:
1421 This function returns whether the default role is ROLE_HOST or ROLE_DEVICE
1422  
1423 Precondition:
1424 None
1425  
1426 Parameters:
1427 None
1428  
1429 Return Values:
1430 BYTE - ROLE_HOST or ROLE_DEVICE
1431  
1432 Remarks:
1433 None
1434 ***************************************************************************/
1435 //DOM-IGNORE-END
1436 BYTE USBOTGDefaultRoleIs()
1437 {
1438 return DefaultRole;
1439 }
1440  
1441  
1442 //DOM-IGNORE-BEGIN
1443 /****************************************************************************
1444 Function:
1445 void USBOTGEnableHnp()
1446  
1447 Description:
1448 This function enables HNP
1449  
1450 Precondition:
1451 None
1452  
1453 Parameters:
1454 None
1455  
1456 Return Values:
1457 None
1458  
1459 Remarks:
1460 None
1461 ***************************************************************************/
1462 //DOM-IGNORE-END
1463 void USBOTGEnableHnp()
1464 {
1465 HNPEnable = 1;
1466 }
1467  
1468  
1469 //DOM-IGNORE-BEGIN
1470 /****************************************************************************
1471 Function:
1472 void USBOTGDisableHnp()
1473  
1474 Description:
1475 This function disables HNP
1476  
1477 Precondition:
1478 None
1479  
1480 Parameters:
1481 None
1482  
1483 Return Values:
1484 None
1485  
1486 Remarks:
1487 None
1488 ***************************************************************************/
1489 //DOM-IGNORE-END
1490 void USBOTGDisableHnp()
1491 {
1492 HNPEnable = 0;
1493 }
1494  
1495  
1496 //DOM-IGNORE-BEGIN
1497 /****************************************************************************
1498 Function:
1499 void void USBOTGEnableAltHnp()
1500  
1501 Description:
1502 This function enables Alt HNP
1503  
1504 Precondition:
1505 None
1506  
1507 Parameters:
1508 None
1509  
1510 Return Values:
1511 None
1512  
1513 Remarks:
1514 None
1515 ***************************************************************************/
1516 //DOM-IGNORE-END
1517 void USBOTGEnableAltHnp()
1518 {
1519 HNPAltSupport = 1;
1520 }
1521  
1522  
1523 //DOM-IGNORE-BEGIN
1524 /****************************************************************************
1525 Function:
1526 void USBOTGDisableAltHnp()
1527  
1528 Description:
1529 This function disables Alt HNP
1530  
1531 Precondition:
1532 None
1533  
1534 Parameters:
1535 None
1536  
1537 Return Values:
1538 None
1539  
1540 Remarks:
1541 None
1542 ***************************************************************************/
1543 //DOM-IGNORE-END
1544 void USBOTGDisableAltHnp()
1545 {
1546 HNPAltSupport = 0;
1547 }
1548  
1549  
1550 //DOM-IGNORE-BEGIN
1551 /****************************************************************************
1552 Function:
1553 void USBOTGEnableSupportHnp()
1554  
1555 Description:
1556 This function enables HNP Support
1557  
1558 Precondition:
1559 None
1560  
1561 Parameters:
1562 None
1563  
1564 Return Values:
1565 None
1566  
1567 Remarks:
1568 None
1569 ***************************************************************************/
1570 //DOM-IGNORE-END
1571 void USBOTGEnableSupportHnp()
1572 {
1573 HNPSupport = 1;
1574 }
1575  
1576  
1577 //DOM-IGNORE-BEGIN
1578 /****************************************************************************
1579 Function:
1580 void USBOTGDisableSupportHnp()
1581  
1582 Description:
1583 This function disables HNP Support
1584  
1585 Precondition:
1586 None
1587  
1588 Parameters:
1589 None
1590  
1591 Return Values:
1592 None
1593  
1594 Remarks:
1595 None
1596 ***************************************************************************/
1597 //DOM-IGNORE-END
1598 void USBOTGDisableSupportHnp()
1599 {
1600 HNPSupport = 0;
1601 }
1602  
1603  
1604 //DOM-IGNORE-BEGIN
1605 /****************************************************************************
1606 Function:
1607 BOOL USBOTGHnpIsEnabled()
1608  
1609 Description:
1610 This function returns TRUE if HNP is enabled, FALSE otherwise
1611  
1612 Precondition:
1613 None
1614  
1615 Parameters:
1616 BOOL - TRUE or FALSE
1617  
1618 Return Values:
1619 None
1620  
1621 Remarks:
1622 None
1623 ***************************************************************************/
1624 //DOM-IGNORE-END
1625 BOOL USBOTGHnpIsEnabled()
1626 {
1627 return HNPEnable;
1628  
1629 }
1630  
1631  
1632 //DOM-IGNORE-BEGIN
1633 /****************************************************************************
1634 Function:
1635 BOOL USBOTGHnpIsActive()
1636  
1637 Description:
1638 This function returns TRUE if HNP is active, FALSE otherwise
1639  
1640 Precondition:
1641 None
1642  
1643 Parameters:
1644 BOOL - TRUE or FALSE
1645  
1646 Return Values:
1647 None
1648  
1649 Remarks:
1650 None
1651 ***************************************************************************/
1652 //DOM-IGNORE-END
1653 BOOL USBOTGHnpIsActive()
1654 {
1655 return HNPActive;
1656 }
1657  
1658  
1659 //DOM-IGNORE-BEGIN
1660 /****************************************************************************
1661 Function:
1662 BOOL USBOTGSrpIsActive()
1663  
1664 Description:
1665 This function returns TRUE if SRP is active, FALSE otherwise
1666  
1667 Precondition:
1668 None
1669  
1670 Parameters:
1671 BOOL - TRUE or FALSE
1672  
1673 Return Values:
1674 None
1675  
1676 Remarks:
1677 None
1678 ***************************************************************************/
1679 //DOM-IGNORE-END
1680 BOOL USBOTGSrpIsActive()
1681 {
1682 return SRPActive;
1683 }
1684  
1685  
1686 //DOM-IGNORE-BEGIN
1687 /****************************************************************************
1688 Function:
1689 void USBOTGActivateSrp()
1690  
1691 Description:
1692 This function activates SRP
1693  
1694 Precondition:
1695 None
1696  
1697 Parameters:
1698 None
1699  
1700 Return Values:
1701 None
1702  
1703 Remarks:
1704 None
1705 ***************************************************************************/
1706 //DOM-IGNORE-END
1707 void USBOTGActivateSrp()
1708 {
1709 SRPActive = 1;
1710 }
1711  
1712  
1713 //DOM-IGNORE-BEGIN
1714 /****************************************************************************
1715 Function:
1716 void USBOTGDeactivateSrp()
1717  
1718 Description:
1719 This function Deactivates SRP
1720  
1721 Precondition:
1722 None
1723  
1724 Parameters:
1725 None
1726  
1727 Return Values:
1728 None
1729  
1730 Remarks:
1731 None
1732 ***************************************************************************/
1733 //DOM-IGNORE-END
1734 void USBOTGDeactivateSrp()
1735 {
1736 SRPActive = 0;
1737 }
1738  
1739  
1740 //DOM-IGNORE-BEGIN
1741 /****************************************************************************
1742 Function:
1743 void USBOTGActivateHnp()
1744  
1745 Description:
1746 This function activates HNP
1747  
1748 Precondition:
1749 None
1750  
1751 Parameters:
1752 None
1753  
1754 Return Values:
1755 None
1756  
1757 Remarks:
1758 None
1759 ***************************************************************************/
1760 //DOM-IGNORE-END
1761 void USBOTGActivateHnp()
1762 {
1763 HNPActive = 1;
1764 }
1765  
1766  
1767 //DOM-IGNORE-BEGIN
1768 /****************************************************************************
1769 Function:
1770 void USBOTGDeactivateHnp()
1771  
1772 Description:
1773 This function deactivates HNP
1774  
1775 Precondition:
1776 None
1777  
1778 Parameters:
1779 None
1780  
1781 Return Values:
1782 None
1783  
1784 Remarks:
1785 None
1786 ***************************************************************************/
1787 //DOM-IGNORE-END
1788 void USBOTGDeactivateHnp()
1789 {
1790 HNPActive = 0;
1791 }
1792  
1793  
1794 //DOM-IGNORE-BEGIN
1795 /****************************************************************************
1796 Function:
1797 BOOL USBOTGGetHNPTimeOutFlag()
1798  
1799 Description:
1800 This function returns the HNPTimeOutFlag. This flag is used for timing the TB_ASE0_BRST USB OTG
1801 Timing parameter. This flag is checked in the 1ms Timer interrupt handler.
1802  
1803 Precondition:
1804 None
1805  
1806 Parameters:
1807 None
1808  
1809 Return Values:
1810 BOOL - TRUE or FALSE
1811  
1812 Remarks:
1813 None
1814 ***************************************************************************/
1815 //DOM-IGNORE-END
1816 BOOL USBOTGGetHNPTimeOutFlag()
1817 {
1818 return HNPTimeOutFlag;
1819 }
1820  
1821  
1822 //DOM-IGNORE-BEGIN
1823 /****************************************************************************
1824 Function:
1825 BOOL USBOTGIsHNPTimeOutExpired()
1826  
1827 Description:
1828 This function decrements HNPTimeOut and checks to see if HNPTimeOut has expired. This function
1829 returns TRUE if HNPTimeOut has expired, FALSE otherwise.
1830  
1831 Precondition:
1832 None
1833  
1834 Parameters:
1835 None
1836  
1837 Return Values:
1838 BOOL - TRUE - Time Expired
1839 FALSE - Time Not Expired
1840  
1841 Remarks:
1842 HNPTime should be > 0
1843 ***************************************************************************/
1844 //DOM-IGNORE-END
1845 BOOL USBOTGIsHNPTimeOutExpired()
1846 {
1847 //Decrement Count
1848 HNPTimeOut--;
1849  
1850 if (HNPTimeOut == 0)
1851 {
1852 //Clear Timer Flag
1853 HNPTimeOutFlag = 0;
1854  
1855 // Turn off the timer interrupt.
1856 U1OTGIEbits.T1MSECIE = 0;
1857  
1858 //Timer Expired
1859 return TRUE;
1860 }
1861 else
1862 {
1863 //Timer Not Expired
1864 return FALSE;
1865 }
1866 }
1867  
1868  
1869 //DOM-IGNORE-BEGIN
1870 /****************************************************************************
1871 Function:
1872 BOOL USBOTGIsSRPTimeOutExpired()
1873  
1874 Description:
1875 This function decrements SRPTimeOut and checks to see if SRPTimeOut has expired. This function
1876 returns TRUE if SRPTimeOut has expired, FALSE otherwise.
1877  
1878 Precondition:
1879 None
1880  
1881 Parameters:
1882 None
1883  
1884 Return Values:
1885 BOOL - TRUE - Time Expired
1886 FALSE - Time Not Expired
1887  
1888 Remarks:
1889 HNPTimeOut value should be > 0
1890 ***************************************************************************/
1891 //DOM-IGNORE-END
1892 BOOL USBOTGIsSRPTimeOutExpired()
1893 {
1894 //Decrement Count
1895 SRPTimeOut--;
1896  
1897 if (SRPTimeOut == 0)
1898 {
1899 //Clear Timer Flag
1900 SRPTimeOutFlag = 0;
1901  
1902 // Turn off the timer interrupt.
1903 U1OTGIEbits.T1MSECIE = 0;
1904  
1905 //Timer Expired
1906 return TRUE;
1907 }
1908 else
1909 {
1910 //Timer Not Expired
1911 return FALSE;
1912 }
1913 }
1914  
1915  
1916 //DOM-IGNORE-BEGIN
1917 /****************************************************************************
1918 Function:
1919 BOOL USBOTGGetSRPTimeOutFlag()
1920  
1921 Description:
1922 This function returns the SRPTimeOutFlag. This flag is used for timing the TA_WAIT_BCON USB OTG
1923 Timing parameter. This flag is checked in the 1ms Timer interrupt handler.
1924  
1925 Precondition:
1926 None
1927  
1928 Parameters:
1929 None
1930  
1931 Return Values:
1932 BOOL - TRUE or FALSE
1933  
1934 Remarks:
1935 None
1936 ***************************************************************************/
1937 //DOM-IGNORE-END
1938 BOOL USBOTGGetSRPTimeOutFlag()
1939 {
1940 return SRPTimeOutFlag;
1941 }
1942  
1943  
1944 //DOM-IGNORE-BEGIN
1945 /****************************************************************************
1946 Function:
1947 BOOL USBOTGSRPIsReady()
1948  
1949 Description:
1950 This function returns the value of SRPReady. This flag is set after the B-device finishes SRP
1951 and the A-device is ready for the B-device to connect
1952  
1953 Precondition:
1954 None
1955  
1956 Parameters:
1957 None
1958  
1959 Return Values:
1960 BOOL - TRUE or FALSE
1961  
1962 Remarks:
1963 None
1964 ***************************************************************************/
1965 //DOM-IGNORE-END
1966 BOOL USBOTGSRPIsReady()
1967 {
1968 return SRPReady;
1969 }
1970  
1971  
1972 //DOM-IGNORE-BEGIN
1973 /****************************************************************************
1974 Function:
1975 void USBOTGClearSRPTimeOutFlag()
1976  
1977 Description:
1978 This function clears the SRPTimeOutFlag. This flag is checked in the 1ms
1979 Timer interrupt handler.
1980  
1981 Precondition:
1982 None
1983  
1984 Parameters:
1985 None
1986  
1987 Return Values:
1988 None
1989  
1990 Remarks:
1991 None
1992 ***************************************************************************/
1993 //DOM-IGNORE-END
1994 void USBOTGClearSRPTimeOutFlag()
1995 {
1996 SRPTimeOutFlag = 0;
1997 }
1998  
1999  
2000 //DOM-IGNORE-BEGIN
2001 /****************************************************************************
2002 Function:
2003 void USBOTGClearSRPReady()
2004  
2005 Description:
2006 This function clears SRPReady.
2007  
2008 Precondition:
2009 None
2010  
2011 Parameters:
2012 None
2013  
2014 Return Values:
2015 None
2016  
2017 Remarks:
2018 None
2019 ***************************************************************************/
2020 //DOM-IGNORE-END
2021 void USBOTGClearSRPReady()
2022 {
2023 SRPReady = 0;
2024 }
2025  
2026  
2027 //DOM-IGNORE-BEGIN
2028 /****************************************************************************
2029 Function:
2030 void USBOTGDelayMs(WORD time)
2031  
2032 Description:
2033 This function will delay a given amount of time in milliseconds determined by the time parameter
2034 passed to this function. The function uses the hardware based 1 millisecond timer.
2035  
2036 Precondition:
2037 USB Module Must Be Enabled Prior To Calling This Function (U1PWRCbits.USBPWR = 1)
2038  
2039 Parameters:
2040 WORD time - The time to delay in milliseconds
2041  
2042 Return Values:
2043 BOOL - TRUE - Time Not Expired
2044 FALSE - Time Expired
2045  
2046 Remarks:
2047 Assumes USB Interrupt Is Disabled
2048 ***************************************************************************/
2049 //DOM-IGNORE-END
2050 void USBOTGDelayMs(WORD time)
2051 {
2052 WORD i;
2053  
2054 U1OTGIR = 0x40;
2055 for (i=0; i < time; i++)
2056 {
2057 while(U1OTGIRbits.T1MSECIF == 0)
2058 {}
2059 U1OTGIR = 0x40;
2060 }
2061 }
2062  
2063  
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3