forked from gen-xu/LodestoneV2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusb_otg.h
More file actions
910 lines (660 loc) · 22.4 KB
/
usb_otg.h
File metadata and controls
910 lines (660 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/*******************************************************************************
USB OTG (Header File)
Description:
This file provides the interface for a USB OTG
application.
This header file must be included after the application-specific
usb_config.h file, as usb_config.h configures parts of this file.
Summary:
This file provides the interface for a USB OTG
application.
*******************************************************************************/
//DOM-IGNORE-BEGIN
/******************************************************************************
* FileName: usb_otg.h
* Dependencies: None
* Processor: PIC24/dsPIC30/dsPIC33/PIC32MX
* Compiler: C30 v2.01/C32 v0.00.18
* Company: Microchip Technology, Inc.
Software License Agreement
The software supplied herewith by Microchip Technology Incorporated
(the “Company”) for its PICmicro® Microcontroller is intended and
supplied to you, the Company’s customer, for use solely and
exclusively on Microchip PICmicro Microcontroller products. The
software is owned by the Company and/or its supplier, and is
protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the
user to criminal sanctions under applicable laws, as well as to
civil liability for the breach of the terms and conditions of this
license.
THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
Author Date Comments
--------------------------------------------------------------------------------
MR 9-04-2008 First release
*******************************************************************************/
#ifndef __USBOTG_H__
#define __USBOTG_H__
//DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: OTG Firmware Version
// *****************************************************************************
// *****************************************************************************
#define USB_OTG_FW_MAJOR_VER 1 // Firmware version, major release number.
#define USB_OTG_FW_MINOR_VER 0 // Firmware version, minor release number.
#define USB_OTG_FW_DOT_VER 0 // Firmware version, dot release number.
// *****************************************************************************
// *****************************************************************************
// Section: USB Constants
// *****************************************************************************
// *****************************************************************************
//OTG Events
#define OTG_EVENT_DISCONNECT 0
#define OTG_EVENT_CONNECT 1
#define OTG_EVENT_NONE 2
#define OTG_EVENT_SRP_DPLUS_HIGH 3
#define OTG_EVENT_SRP_DPLUS_LOW 4
#define OTG_EVENT_SRP_VBUS_HIGH 5
#define OTG_EVENT_SRP_VBUS_LOW 6
#define OTG_EVENT_SRP_CONNECT 7
#define OTG_EVENT_HNP_ABORT 8
#define OTG_EVENT_HNP_FAILED 9
#define OTG_EVENT_SRP_FAILED 10
#define OTG_EVENT_RESUME_SIGNALING 11
//Role Defines
#define ROLE_DEVICE 0
#define ROLE_HOST 1
//Cable Defines
#define CABLE_A_SIDE 0
#define CABLE_B_SIDE 1
//Session Defines
#define START_SESSION 0
#define END_SESSION 1
#define TOGGLE_SESSION 2
//USB OTG Timing Parameter Defines
#define DELAY_TB_AIDL_BDIS 10 //100
#define DELAY_TA_BIDL_ADIS 10//150
#define DELAY_TB_ASE0_BRST 100
#define DELAY_TB_SE0_SRP 2
#define DELAY_TB_DATA_PLS 6
#define DELAY_TB_SRP_FAIL 5100
#define DELAY_TA_WAIT_VRISE 100
#define DELAY_TA_WAIT_BCON 1100
#define DELAY_TA_BDIS_ACON 1
#define DELAY_VBUS_SETTLE 500
#define DELAY_TA_AIDL_BDIS 255
// *****************************************************************************
// *****************************************************************************
// Section: HNP Event Flow
// *****************************************************************************
// *****************************************************************************
/*
// *****************************************************************************
(A becomes Device, B becomes Host)
A side(Host) B side(Device)
// *****************************************************************************
Suspend-SelectRole(ROLE_DEVICE) -------> Idle-SelectRole(ROLE_HOST)
Detach - OTG_EVENT_DISCONNECT <------- D+ Pullup Disabled
D+ Pullup Enabled -------> Attach - OTG_EVENT_CONNECT
// *****************************************************************************
(A becomes Host, B becomes Device)
A side(Device) B side(Host)
// *****************************************************************************
Idle-SelectRole(ROLE_HOST) <------- Suspend-SelectRole(ROLE_DEVICE)
D+ Pullup Disabled -------> Detach - OTG_EVENT_DISCONNECT
USGOTGInitializeHostStack()
*/
// *****************************************************************************
// *****************************************************************************
// Section: USB OTG Function Prototypes
// *****************************************************************************
// *****************************************************************************
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void InitializeHostStack()
Description:
This function initializes the host stack for use in an OTG application
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGInitializeHostStack();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void InitializeDeviceStack()
Description:
This function initializes the device stack for use in an OTG application
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGInitializeDeviceStack();
//DOM-IGNORE-END
/****************************************************************************
Function:
BOOL USBOTGRoleSwitch()
Description:
This function returns whether a role switch occurred or not. This is used by the main application function
to determine when to reinitialize the system (InitializeSystem())
Precondition:
None
Parameters:
None
Return Values:
BOOL - TRUE or FALSE
Remarks:
None
***************************************************************************/
BOOL USBOTGRoleSwitch();
/****************************************************************************
Function:
void USBOTGClearRoleSwitch()
Description:
This function clears the RoleSwitch variable. After the main function detects the RoleSwitch
and re-initializes the system, this function should be called to clear the RoleSwitch flag
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGClearRoleSwitch();
/****************************************************************************
Function:
void USBOTGInitialize()
Description:
This function initializes an OTG application and initializes a default role of Host or Device
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
#define USB_MICRO_AB_OTG_CABLE should be commented out in usb_config.h
if not using a micro AB OTG cable
***************************************************************************/
void USBOTGInitialize();
/****************************************************************************
Function:
void USBOTGSelectRole(BOOL role)
Description:
This function initiates a role switch via the Host Negotiation Protocol (HNP).
The parameter role that is passed to this function is the desired role to switch to.
Precondition:
None
Parameters:
BOOL role - ROLE_DEVICE or ROLE_HOST
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGSelectRole(BOOL role);
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGHnpIsEnabled()
Description:
This function returns TRUE if HNP is enabled, FALSE otherwise
Precondition:
None
Parameters:
BOOL - TRUE or FALSE
Return Values:
None
Remarks:
None
***************************************************************************/
BOOL USBOTGHnpIsEnabled();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGHnpIsActive()
Description:
This function returns TRUE if HNP is active, FALSE otherwise.
Precondition:
None
Parameters:
BOOL - TRUE or FALSE
Return Values:
None
Remarks:
HNP will become active on the host when it suspends the bus and HNP was enabled
by an acknowledegement of the SET_FEATURE(b_hnp_enable) by the peripheral.
HNP will become active on the peripheral side when it receives a bus idle condition
and HNP was enabled by a SET_FEATURE(b_hnp_enable) from the host
***************************************************************************/
BOOL USBOTGHnpIsActive();
//DOM-IGNORE-END
/****************************************************************************
Function:
void USBOTGSession(BYTE Value)
Description:
This function starts, ends, or toggles a VBUS session.
Precondition:
This function assumes I/O controlling DC/DC converter has already been initialized
Parameters:
Value - START_SESSION, END_SESSION, TOGGLE_SESSION
Return Values:
TRUE - Session Started, FALSE - Session Not Started
Remarks:
This function should only be called by an A-side Host
***************************************************************************/
BOOL USBOTGSession(BYTE Value);
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGEnableHnp()
Description:
This function enables HNP
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGEnableHnp();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGDisableHnp()
Description:
This function disables HNP
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGDisableHnp();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void void USBOTGEnableAltHnp()
Description:
This function enables Alt HNP
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGEnableAltHnp();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGDisableAltHnp()
Description:
This function disables Alt HNP
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGDisableAltHnp();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGEnableSupportHnp()
Description:
This function enables HNP Support
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGEnableSupportHnp();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGDisableSupportHnp()
Description:
This function disables HNP Support
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGDisableSupportHnp();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGSrpIsActive()
Description:
This function returns TRUE if SRP is active, FALSE otherwise
Precondition:
None
Parameters:
BOOL - TRUE or FALSE
Return Values:
None
Remarks:
None
***************************************************************************/
BOOL USBOTGSrpIsActive();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGDeactivateHnp()
Description:
This function deactivates HNP
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGDeactivateHnp();
//DOM-IGNORE-END
/****************************************************************************
Function:
BYTE USBOTGCurrentRoleIs()
Description:
This function returns whether the current role is ROLE_HOST or ROLE_DEVICE
Precondition:
None
Parameters:
None
Return Values:
BYTE - ROLE_HOST or ROLE_DEVICE
Remarks:
None
***************************************************************************/
BYTE USBOTGCurrentRoleIs();
/****************************************************************************
Function:
BYTE USBOTGDefaultRoleIs()
Description:
This function returns whether the default role is ROLE_HOST or ROLE_DEVICE
Precondition:
None
Parameters:
None
Return Values:
BYTE - ROLE_HOST or ROLE_DEVICE
Remarks:
If using a Micro AB USB OTG Cable, the A-side plug of the cable when plugged in
will assign a default role of ROLE_HOST. The B-side plug of the cable when plugged in
will assign a default role of ROLE_DEVICE.
If using a Standard USB Cable, ROLE_HOST or ROLE_DEVICE needs to be manually configured in
usb_config.h.
Both of these items can be easily configured using the USB Config Tool which will automatically
generate the apropriate information for your application
***************************************************************************/
BYTE USBOTGDefaultRoleIs();
/****************************************************************************
Function:
void USBOTGRequestSession()
Description:
This function requests a Session from an A side Host using the Session Request Protocol (SRP).
The function will return TRUE if the request was successful or FALSE otherwise.
Precondition:
None
Parameters:
None
Return Values:
TRUE or FALSE
Remarks:
This function should only be called by a B side Device.
***************************************************************************/
BOOL USBOTGRequestSession();
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGGetSessionStatus()
Description:
This function gets a session status. The function will return
TRUE if VBUS > Session Valid Voltage or FALSE if VBUS < Session Valid Voltage.
Precondition:
None
Parameters:
None
Return Values:
TRUE or FALSE
Remarks:
***************************************************************************/
BOOL USBOTGGetSessionStatus();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGDischargeVBus()
Description:
This function discharges VBUS.
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
***************************************************************************/
void USBOTGDischargeVBus();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USB_OTGEventHandler ( BYTE address, BYTE event, void *data, DWORD size )
Description:
This function is the event handler used by both the Host and Device stacks for calling the OTG layer
when SRP and HNP events occur
Precondition:
None
Parameters:
BYTE event -
OTG_EVENT_SRP_DPLUS_HIGH
OTG_EVENT_SRP_DPLUS_LOW
OTG_EVENT_SRP_VBUS_HIGH
OTG_EVENT_SRP_VBUS_LOW
OTG_EVENT_DISCONNECT
OTG_EVENT_CONNECT
OTG_EVENT_HNP_ABORT
OTG_EVENT_HNP_FAILED
Return Values:
None
Remarks:
None
***************************************************************************/
void USB_OTGEventHandler ( BYTE address, BYTE event, void *data, DWORD size );
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGDelayMs(WORD time)
Description:
This function will delay a given amount of time in milliseconds determined by the time parameter
passed to this function. The function uses the hardware based 1 millisecond timer.
Precondition:
USB Module Must Be Enabled Prior To Calling This Function (U1PWRCbits.USBPWR = 1)
Parameters:
WORD time - The time to delay in milliseconds
Return Values:
BOOL - TRUE - Time Not Expired
FALSE - Time Expired
Remarks:
Assumes USB Interrupt Is Disabled
***************************************************************************/
void USBOTGDelayMs(WORD time);
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGIsHNPTimeOutExpired()
Description:
This function decrements HNPTimeOut and checks to see if HNPTimeOut has expired. This function
returns TRUE if HNPTimeOut has expired, FALSE otherwise.
Precondition:
None
Parameters:
None
Return Values:
BOOL - TRUE - Time Expired
FALSE - Time Not Expired
Remarks:
HNPTimeOut value should be > 0
***************************************************************************/
BOOL USBOTGIsHNPTimeOutExpired();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGGetHNPTimeOutFlag()
Description:
This function returns the HNPTimeOutFlag. This flag is used for timing the TB_ASE0_BRST USB OTG
Timing parameter. This flag is checked in the 1ms Timer interrupt handler.
Precondition:
None
Parameters:
None
Return Values:
BOOL - TRUE or FALSE
Remarks:
None
***************************************************************************/
BOOL USBOTGGetHNPTimeOutFlag();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGIsSRPTimeOutExpired()
Description:
This function decrements SRPTimeOut and checks to see if SRPTimeOut has expired. This function
returns TRUE if SRPTimeOut has expired, FALSE otherwise.
Precondition:
None
Parameters:
None
Return Values:
BOOL - TRUE - Time Expired
FALSE - Time Not Expired
Remarks:
HNPTimeOut value should be > 0
***************************************************************************/
BOOL USBOTGIsSRPTimeOutExpired();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGGetSRPTimeOutFlag()
Description:
This function returns the SRPTimeOutFlag. This flag is used for timing the TA_WAIT_BCON USB OTG
Timing parameter. This flag is checked in the 1ms Timer interrupt handler.
Precondition:
None
Parameters:
None
Return Values:
BOOL - TRUE or FALSE
Remarks:
None
***************************************************************************/
BOOL USBOTGGetSRPTimeOutFlag();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGClearSRPTimeOutFlag()
Description:
This function clears the SRPTimeOutFlag. This flag is checked in the 1ms
Timer interrupt handler.
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGClearSRPTimeOutFlag();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
BOOL USBOTGSRPIsReady()
Description:
This function returns the value of SRPReady. This flag is set after the B-device finishes SRP
and the A-device is ready for the B-device to connect
Precondition:
None
Parameters:
None
Return Values:
BOOL - TRUE or FALSE
Remarks:
None
***************************************************************************/
BOOL USBOTGSRPIsReady();
//DOM-IGNORE-END
//DOM-IGNORE-BEGIN
/****************************************************************************
Function:
void USBOTGClearSRPReady()
Description:
This function clears SRPReady.
Precondition:
None
Parameters:
None
Return Values:
None
Remarks:
None
***************************************************************************/
void USBOTGClearSRPReady();
//DOM-IGNORE-END
#endif