-
Notifications
You must be signed in to change notification settings - Fork 7
/
drv_usbdp_device.c
3391 lines (2793 loc) · 136 KB
/
drv_usbdp_device.c
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
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
USB Device Driver Implementation of device mode operation routines
Company:
Microchip Technology Inc.
File Name:
drv_usbdp_device.c
Summary:
USB Device Driver Dynamic Implementation of device mode operation routines
Description:
The USB device driver provides a simple interface to manage the USB
modules on Microchip microcontrollers. This file Implements the
interface routines for the USB driver when operating in device mode.
While building the driver from source, ALWAYS use this file in the build if
device mode operation is required.
******************************************************************************/
//DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
******************************************************************************/
//DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Include Files
// *****************************************************************************
// *****************************************************************************
#include "driver/usb/usbdp/src/drv_usbdp_local.h"
#include "driver/usb/usbdp/drv_usbdp.h"
// *****************************************************************************
// *****************************************************************************
// Section: Global Data
// *****************************************************************************
// *****************************************************************************
/* USB device driver instance object */
DRV_USBDP_OBJ gDrvUSBDPObj[DRV_USBDP_INSTANCES_NUMBER];
/* Array of endpoint objects. Two objects for control endpoint, one for each
* direction */
DRV_USBDP_ENDPOINT_OBJ gDrvUSBControlEndpoints[DRV_USBDP_INSTANCES_NUMBER] [2];
/* Array of endpoint objects. One object per non-control endpoint as one
* endpoint can handle one direction at a time */
DRV_USBDP_ENDPOINT_OBJ gDrvUSBNonControlEndpoints[DRV_USBDP_INSTANCES_NUMBER] [DRV_USBDP_ENDPOINTS_NUMBER - 1];
/* Array of endpoint types to map the endpoint type as per bit values of the
* UDP_CSR register */
const uint8_t gDrvUSBDeviceEndpointTypeMap[2][4] =
{
{(uint8_t)UDP_CSR_EPTYPE_CTRL_Val, (uint8_t)UDP_CSR_EPTYPE_ISO_OUT_Val, (uint8_t)UDP_CSR_EPTYPE_BULK_OUT_Val, (uint8_t)UDP_CSR_EPTYPE_INT_OUT_Val},
{(uint8_t)UDP_CSR_EPTYPE_CTRL_Val, (uint8_t)UDP_CSR_EPTYPE_ISO_IN_Val, (uint8_t)UDP_CSR_EPTYPE_BULK_IN_Val, (uint8_t)UDP_CSR_EPTYPE_INT_IN_Val}
};
/*******************************************************************************
* This structure is a pointer to a set of USB Driver Device mode functions.
* This set is exported to the device layer when the device layer must use the
* USB Controller.
******************************************************************************/
DRV_USB_DEVICE_INTERFACE gDrvUSBDPInterface =
{
.open = DRV_USBDP_Open,
.close = DRV_USBDP_Close,
.eventHandlerSet = DRV_USBDP_ClientEventCallBackSet,
.deviceAddressSet = DRV_USBDP_AddressSet,
.deviceCurrentSpeedGet = DRV_USBDP_CurrentSpeedGet,
.deviceSOFNumberGet = DRV_USBDP_SOFNumberGet,
.deviceAttach = DRV_USBDP_Attach,
.deviceDetach = DRV_USBDP_Detach,
.deviceEndpointEnable = DRV_USBDP_EndpointEnable,
.deviceEndpointDisable = DRV_USBDP_EndpointDisable,
.deviceEndpointStall = DRV_USBDP_EndpointStall,
.deviceEndpointStallClear = DRV_USBDP_EndpointStallClear,
.deviceEndpointIsEnabled = DRV_USBDP_EndpointIsEnabled,
.deviceEndpointIsStalled = DRV_USBDP_EndpointIsStalled,
.deviceIRPSubmit = DRV_USBDP_IRPSubmit,
.deviceIRPCancel = DRV_USBDP_IRPCancel,
.deviceIRPCancelAll = DRV_USBDP_IRPCancelAll,
.deviceRemoteWakeupStop = DRV_USBDP_RemoteWakeupStop,
.deviceRemoteWakeupStart = DRV_USBDP_RemoteWakeupStart,
.deviceTestModeEnter = NULL
};
// *****************************************************************************
/* Function:
SYS_MODULE_OBJ DRV_USBDP_Initialize
(
const SYS_MODULE_INDEX drvIndex,
const SYS_MODULE_INIT * const init
)
Summary:
Dynamic implementation of DRV_USBDP_Initialize system interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Initialize system interface
function. Function performs the following task:
- Initializes the necessary USB module as per the instance init data
- Updates internal data structure for the particular USB instance
- Returns the USB instance value as a handle to the system
Remarks:
See drv_usbdp.h for usage information.
*/
SYS_MODULE_OBJ DRV_USBDP_Initialize
(
const SYS_MODULE_INDEX drvIndex,
const SYS_MODULE_INIT * const init
)
{
DRV_USBDP_OBJ * drvObj = (DRV_USBDP_OBJ *)NULL; /* USB driver object pointer */
DRV_USBDP_INIT * usbInit = (DRV_USBDP_INIT *)NULL; /* USB driver initialization pointer */
SYS_MODULE_OBJ retVal = SYS_MODULE_OBJ_INVALID; /* Return value */
uint8_t index; /* Loop counter */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if(drvIndex >= DRV_USBDP_INSTANCES_NUMBER)
{
/* Driver module index out of range */
SYS_DEBUG(SYS_ERROR_INFO,"\r\nUSBDP Driver: Driver module index out of range in DRV_USBDP_Initialize().");
}
else if(gDrvUSBDPObj[drvIndex].inUse == true)
{
/* Driver already initialized */
SYS_DEBUG(SYS_ERROR_INFO, "\r\nUSBDP Driver: Driver already initialized in DRV_USBDP_Initialize().");
}
else
{
/* Assign the init data and driver object to the local pointer */
usbInit = (DRV_USBDP_INIT *) init;
drvObj = &gDrvUSBDPObj[drvIndex];
/* Create the global mutex and proceed if successful. */
if(OSAL_RESULT_TRUE == OSAL_MUTEX_Create((OSAL_MUTEX_HANDLE_TYPE *)&drvObj->mutexID))
{
/* Mutex create successful, go ahead with initialization */
/* Populate the driver instance object with required data */
drvObj->inUse = true;
drvObj->status = SYS_STATUS_BUSY;
drvObj->usbID = usbInit->usbID;
drvObj->operationSpeed = usbInit->operationSpeed;
drvObj->isOpened = false;
drvObj->pEventCallBack = NULL;
drvObj->sessionInvalidEventSent = false;
drvObj->interruptSource = usbInit->interruptSource;
drvObj->isInInterruptContext = false;
/* Set the starting VBUS level. */
drvObj->vbusLevel = DRV_USB_VBUS_LEVEL_INVALID;
drvObj->vbusComparator = usbInit->vbusComparator;
/* Assign the endpoint object pointer array with the endpoint
* objects. Control endpoint is bidirectional endpoint, so only one
* object is needed. Non control endpoints are unidirectional
* endpoints, so multidimensional array with one object per endpoint
* direction is used. By this method, the endpoint objects can later
* be accessed by using the endpoint number as index irrespective of
* endpoint number. */
drvObj->deviceEndpointObj[0] = &gDrvUSBControlEndpoints[drvIndex][0];
for(index = 1; index < DRV_USBDP_ENDPOINTS_NUMBER ; index++)
{
drvObj->deviceEndpointObj[index] = &gDrvUSBNonControlEndpoints[drvIndex][index - 1];
}
/* USB Peripheral is configured in Device mode */
MATRIX_REGS->CCFG_USBMR |= CCFG_USBMR_USBMODE_Msk;
/* The USB alarm enables a fast restart signal to the PMC. */
PMC_REGS->PMC_FSMR |= PMC_FSMR_USBAL_Msk;
/* Disable the function endpoints de-address the device. */
drvObj->usbID->UDP_FADDR &= ~UDP_FADDR_Msk;
/* Disable all USB Peripheral Interrupts. We are not touching the
* USB System Interrupt as the NVIC is not yet initialized. */
drvObj->usbID->UDP_IDR = UDP_IDR_Msk;
/* Initialize device specific flags */
drvObj->isAttached = false;
drvObj->status = SYS_STATUS_READY;
retVal = drvIndex;
}
else
{
/* Mutex create failed. Send a message and exit. */
SYS_DEBUG(SYS_ERROR_INFO, "\r\nUSBDP Driver: Mutex create failed in DRV_USBDP_Initialize().");
}
}
return (retVal);
} /* end of DRV_USBDP_Initialize() */
// *****************************************************************************
/* Function:
void DRV_USBDP_Deinitialize( const SYS_MODULE_INDEX drvIndex )
Summary:
Dynamic implementation of DRV_USBDP_Deinitialize system interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Deinitialize system
interface function.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_Deinitialize
(
const SYS_MODULE_INDEX drvIndex
)
{
DRV_USBDP_OBJ * hDriver = (DRV_USBDP_OBJ *)NULL; /* Local Driver Object Handler */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if(drvIndex == SYS_MODULE_OBJ_INVALID)
{
/* Invalid driver module index */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO,"\r\nUSBDP Driver: Invalid driver module index in DRV_USBDP_Deinitialize()");
}
else if(drvIndex >= DRV_USBDP_INSTANCES_NUMBER)
{
/* Driver module index out of range */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO,"\r\nUSBDP Driver: Driver module index out of range in DRV_USBDP_Deinitialize()");
}
else if(gDrvUSBDPObj[drvIndex].inUse == false)
{
/* Driver not in use */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO,"\r\nUSBDP Driver: Driver not in use in DRV_USBDP_Deinitialize()");
}
else
{
/* Assign the driver object to the local pointer */
hDriver = &gDrvUSBDPObj[drvIndex];
/* Release the USB instance object */
hDriver->inUse = false;
/* De-initialize the status*/
hDriver->status = SYS_STATUS_UNINITIALIZED;
/* Reset the open flag */
hDriver->isOpened = false;
/* Delete the mutex */
OSAL_MUTEX_Delete((OSAL_MUTEX_HANDLE_TYPE *)&hDriver->mutexID);
/* Reset the USB driver event callback to NULL */
hDriver->pEventCallBack = NULL;
/* Disable and clear all Interrupts */
SYS_INT_SourceDisable(hDriver->interruptSource);
UDP_REGS->UDP_IDR = UDP_IDR_Msk;
UDP_REGS->UDP_ICR = UDP_ICR_Msk;
hDriver->usbID->UDP_FADDR &= ~UDP_FADDR_FEN_Msk;
}
} /* end of DRV_USBDP_Deinitialize() */
// *****************************************************************************
/* Function:
DRV_HANDLE DRV_USBDP_Open
(
const SYS_MODULE_INDEX drvIndex,
const DRV_IO_INTENT ioIntent
)
Summary:
Dynamic implementation of DRV_USBDP_Open system interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Open system interface
function.
Remarks:
See drv_usbdp.h for usage information.
*/
DRV_HANDLE DRV_USBDP_Open
(
const SYS_MODULE_INDEX drvIndex,
const DRV_IO_INTENT ioIntent
)
{
DRV_USBDP_OBJ * drvObj; /* Local Driver Object Handler */
DRV_HANDLE retVal = DRV_HANDLE_INVALID; /* Return Value */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if(drvIndex >= DRV_USBDP_INSTANCES_NUMBER)
{
/* Driver module index out of range */
SYS_DEBUG(SYS_ERROR_DEBUG, "\r\nUSBDP Driver: Driver module index out of range in DRV_USBDP_Open().");
}
else
{
/* Assign the driver object to the local pointer */
drvObj = &gDrvUSBDPObj[drvIndex];
if(drvObj->status != SYS_STATUS_READY)
{
/* The driver status not ready */
SYS_DEBUG(SYS_ERROR_INFO, "\r\nUSBDP Driver: Driver status not ready in DRV_USBDP_Open().");
}
else if(ioIntent != (DRV_IO_INTENT_EXCLUSIVE | DRV_IO_INTENT_NONBLOCKING | DRV_IO_INTENT_READWRITE))
{
/* Unsupported IO Intent */
SYS_DEBUG(SYS_ERROR_DEBUG, "\r\nUSBDP Driver: Unsupported IO Intent in DRV_USBDP_Open().");
}
if(drvObj->isOpened == true)
{
/* Driver can be opened only once. Multiple calls not allowed */
SYS_DEBUG(SYS_ERROR_DEBUG, "\r\nUSBDP Driver: Driver can be opened only once. Multiple calls to DRV_USBDP_Open().");
}
else
{
/* Open the Driver Object */
drvObj->isOpened = true;
/* Clear prior value */
drvObj->pEventCallBack = NULL;
/* Handle is the pointer to the client object */
retVal = ((DRV_HANDLE) drvObj);
}
}
/* Return handle */
return (retVal);
}/* end of DRV_USBDP_Open() */
// *****************************************************************************
/* Function:
SYS_STATUS DRV_USBDP_Status( const SYS_MODULE_OBJ object )
Summary:
Dynamic implementation of DRV_USBDP_Status system interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Status system interface
function.
Remarks:
See drv_usbdp.h for usage information.
*/
SYS_STATUS DRV_USBDP_Status
(
SYS_MODULE_OBJ object
)
{
SYS_STATUS retVal = SYS_STATUS_UNINITIALIZED; /* Return Value */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if(object == SYS_MODULE_OBJ_INVALID)
{
/* System Module Object Invalid */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: System Module Object Invalid in DRV_USBDP_Status().");
retVal = SYS_STATUS_ERROR;
}
else
{
retVal = (SYS_STATUS) gDrvUSBDPObj[object].status;
}
/* Return the status of the driver object */
return (retVal);
}/* end of DRV_USBDP_Status() */
// *****************************************************************************
/* Function:
void DRV_USBDP_Close( DRV_HANDLE client )
Summary:
Dynamic implementation of DRV_USBDP_Close client interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Close client interface
function.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_Close
(
DRV_HANDLE handle
)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_Close().");
}
else
{
/* Assign the driver handle to the local pointer */
hDriver = (DRV_USBDP_OBJ *)handle;
if(false == hDriver->isOpened)
{
/* Driver handle not opened */
SYS_DEBUG(SYS_ERROR_INFO, "\r\nUSBDP Driver: Driver handle not opened in DRV_USBDP_Close().");
}
else
{
/* Release the client handle */
hDriver->isOpened = false;
hDriver->pEventCallBack = NULL;
}
}
}/* end of DRV_USBDP_Close() */
// *****************************************************************************
/* Function:
void DRV_USBDP_Tasks(SYS_MODULE_OBJ object)
Summary:
Dynamic implementation of DRV_USBDP_Tasks system interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Tasks system interface
function.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_Tasks
(
SYS_MODULE_OBJ object
)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
DRV_USB_VBUS_LEVEL vbusLevel = DRV_USB_VBUS_LEVEL_INVALID; /* USB VBUS Level data */
/* Assign the driver object to the local pointer */
hDriver = &gDrvUSBDPObj[object];
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if(hDriver->status <= SYS_STATUS_UNINITIALIZED)
{
/* Driver is not initialized */
SYS_DEBUG(SYS_ERROR_INFO, "\r\nUSBDP Driver: Driver is not initialized in DRV_USBDP_Tasks().");
}
else if ((DRV_USB_EVENT_CALLBACK)NULL != hDriver->pEventCallBack)
{
/* We have a valid client call back function. If we have a VBUS
* comparator function, then check is VBUS level is changed. If there is
* no VBUS comparator function, then return with VBUS Valid. This is in
* scenario where the device cannot afford a GPIO for VBUS state and
* hence it is always valid */
if( hDriver->vbusComparator != NULL)
{
vbusLevel = hDriver->vbusComparator();
}
else
{
vbusLevel = DRV_USB_VBUS_LEVEL_VALID;
}
if(hDriver->vbusLevel != vbusLevel)
{
/* This means there was a change in the level */
if(vbusLevel == DRV_USB_VBUS_LEVEL_VALID)
{
/* We have a valid VBUS level */
hDriver->pEventCallBack(hDriver->hClientArg, DRV_USBDP_EVENT_DEVICE_SESSION_VALID, NULL);
/* We should be ready for send session invalid event
* to the application when they happen.*/
hDriver->sessionInvalidEventSent = false;
}
else
{
/* Any thing other than valid is considered invalid.
* This event may occur multiple times, but we send
* it only once. */
if(!hDriver->sessionInvalidEventSent)
{
hDriver->pEventCallBack(hDriver->hClientArg, DRV_USBDP_EVENT_DEVICE_SESSION_INVALID, NULL);
hDriver->sessionInvalidEventSent = true;
}
}
hDriver->vbusLevel = vbusLevel;
}
}
}/* end of DRV_USBDP_Tasks() */
// *****************************************************************************
/* Function:
void DRV_USBDP_AddressSet(DRV_HANDLE handle, uint8_t address)
Summary:
Dynamic implementation of DRV_USBDP_AddressSet client interface
function.
Description:
This is the dynamic implementation of DRV_USBDP_AddressSet
client interface initialization function for USB device. Function checks
the input handle validity and on success updates the Device General Control
Register with the address value and enables the address.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_AddressSet
(
DRV_HANDLE handle,
uint8_t address
)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
udp_registers_t * usbID; /* USB instance pointer */
uint32_t regValue; /* USB shadow register */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_AddressSet().");
}
else
{
/* Assign the driver handler to the local pointer */
hDriver = (DRV_USBDP_OBJ *) handle;
usbID = hDriver->usbID;
/* Disable the device address */
usbID->UDP_GLB_STAT &= ~UDP_GLB_STAT_FADDEN_Msk;
/* Clear the Function Enable bit to disable the endpoints */
usbID->UDP_FADDR &= ~UDP_FADDR_FEN_Msk;
/* Copy new address to FADDR */
regValue = usbID->UDP_FADDR;
regValue &= ~UDP_FADDR_FADD_Msk;
regValue |= UDP_FADDR_FADD(address);
usbID->UDP_FADDR = regValue;
/* Enable the new address */
usbID->UDP_FADDR |= UDP_FADDR_FEN_Msk;
}
}/* end of DRV_USBDP_AddressSet() */
// *****************************************************************************
/* Function:
USB_SPEED DRV_USBDP_CurrentSpeedGet(DRV_HANDLE handle)
Summary:
Dynamic implementation of DRV_USBDP_CurrentSpeedGet client
interface function.
Description:
This is the dynamic implementation of DRV_USBDP_CurrentSpeedGet
client interface initialization function for USB device.
Function checks the input handle validity and on success returns value to
indicate HIGH/FULL speed operation.
Remarks:
See drv_usbdp.h for usage information.
*/
USB_SPEED DRV_USBDP_CurrentSpeedGet
(
DRV_HANDLE handle
)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
USB_SPEED retVal = USB_SPEED_ERROR; /* Return value */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_CurrentSpeedGet().");
}
else
{
hDriver = (DRV_USBDP_OBJ *) handle;
/* The current speed in contained in the
* device speed member of the driver object */
retVal = hDriver->deviceSpeed;
}
return (retVal);
}/* end of DRV_USBDP_CurrentSpeedGet() */
// *****************************************************************************
/* Function:
void DRV_USBDP_RemoteWakeupStart(DRV_HANDLE handle)
Summary:
Dynamic implementation of DRV_USBDP_RemoteWakeupStart client
interface function.
Description:
This is dynamic implementation of DRV_USBDP_RemoteWakeupStart
client interface function for USB device.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_RemoteWakeupStart
(
DRV_HANDLE handle
)
{
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_RemoteWakeupStart().");
}
else
{
/* Not yet supported. Do nothing. */
}
}/* end of DRV_USBDP_RemoteWakeupStart() */
// *****************************************************************************
/* Function:
void DRV_USBDP_RemoteWakeupStop(DRV_HANDLE handle)
Summary:
Dynamic implementation of DRV_USBDP_RemoteWakeupStop client
interface function.
Description:
This is dynamic implementation of DRV_USBDP_RemoteWakeupStop
client interface function for USB device.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_RemoteWakeupStop(DRV_HANDLE handle)
{
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_RemoteWakeupStop().");
}
else
{
/* Not yet supported. Do nothing. */
}
}/* end of DRV_USBDP_RemoteWakeupStop() */
// *****************************************************************************
/* Function:
void DRV_USBDP_Attach(DRV_HANDLE handle)
Summary:
Dynamic implementation of DRV_USBDP_Attach client
interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Attach
client interface function for USB device.
This function checks if the handle passed is valid and if so, performs the
device attach operation. EOR, SUSP, WAKEUP, & SOF interrupts are enabled and
EOR, WAKEUP, & SOF interrupts are cleared.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_Attach
(
DRV_HANDLE handle
)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
udp_registers_t * usbID; /* USB instance pointer */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_Attach().");
}
else
{
/* Assign the driver handler to the local pointer */
hDriver = (DRV_USBDP_OBJ *) handle;
usbID = hDriver->usbID;
/* Update the driver flag indicating attach */
hDriver->isAttached = true;
/* Enable the USB device by clearing the TXVDIS bit.
* And Set the PUON bit to enable the Pull-Up. */
usbID->UDP_TXVC &= ~UDP_TXVC_TXVDIS_Msk;
usbID->UDP_TXVC |= UDP_TXVC_PUON_Msk;
/* Enables all interrupts except RESUME. RESUMEIF will be enabled only
* on getting SUSPEND */
usbID->UDP_IER = ( UDP_IER_RXSUSP_Msk |
UDP_IER_WAKEUP_Msk |
UDP_IER_RXRSM_Msk |
UDP_IER_SOFINT_Msk);
/* Enable the USB interrupt */
SYS_INT_SourceEnable(hDriver->interruptSource);
}
}/* end of DRV_USBDP_Attach() */
// *****************************************************************************
/* Function:
void DRV_USBDP_Detach(DRV_HANDLE handle)
Summary:
Dynamic implementation of DRV_USBDP_Detach client
interface function.
Description:
This is the dynamic implementation of DRV_USBDP_Detach
client interface function for USB device.
This function checks if the passed handle is valid and if so, performs a
device detach operation.
Remarks:
See drv_usbdp.h for usage information.
*/
void DRV_USBDP_Detach
(
DRV_HANDLE handle
)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
udp_registers_t * usbID; /* USB instance pointer */
uint32_t regValue; /* USB shadow register */
bool interruptWasEnabled = false; /* USB interrupt status holder */
USB_ERROR retVal = USB_ERROR_NONE; /* Return value */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_Detach().");
}
else if(false == ((DRV_USBDP_OBJ *)handle)->inUse)
{
/* Driver Object not in use */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Driver Object not in use in DRV_USBDP_Detach().");
}
else
{
/* Assign the driver handler to the local pointer */
hDriver = (DRV_USBDP_OBJ *) handle;
usbID = hDriver->usbID;
/* Check if we are in interrupt context. This is to ensure that this function
* is interrupt and thread safe. If we are not in interrupt, we disable
* the interrupt and grab the mutex. If we are already in interrupt
* context, there is no need to disable interrupt or grab mutex */
if(hDriver->isInInterruptContext == false)
{
if(OSAL_MUTEX_Lock((OSAL_MUTEX_HANDLE_TYPE *)&hDriver->mutexID, OSAL_WAIT_FOREVER) == OSAL_RESULT_TRUE)
{
interruptWasEnabled = SYS_INT_SourceDisable(hDriver->interruptSource);
}
else
{
/* There was an error in getting the mutex */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Mutex lock failed in DRV_USBDP_Detach()");
retVal = USB_ERROR_OSAL_FUNCTION;
}
}
if(retVal == USB_ERROR_NONE)
{
/* Update the driver flag indicating detach */
hDriver->isAttached = false;
/* Disable the endpoints first */
DRV_USBDP_EndpointDisable((DRV_HANDLE)hDriver, DRV_USB_DEVICE_ENDPOINT_ALL);
/* Clear and disable all the interrupts */
usbID->UDP_IDR = UDP_IDR_Msk;
usbID->UDP_ICR = UDP_ICR_Msk;
/* Reset the endpoints */
usbID->UDP_RST_EP = UDP_RST_EP_Msk;
usbID->UDP_RST_EP &= ~UDP_RST_EP_Msk;
/* Disable the function address enable */
usbID->UDP_GLB_STAT &= ~UDP_GLB_STAT_FADDEN_Msk;
usbID->UDP_FADDR &= ~UDP_FADDR_FEN_Msk;
/* Disable the USB device by setting the TXVDIS bit.
* And Clear the PUON bit to disable the Pull-Up. */
regValue = usbID->UDP_TXVC;
regValue &= ~UDP_TXVC_Msk;
regValue |= UDP_TXVC_TXVDIS_Msk;
usbID->UDP_TXVC = regValue;
if(hDriver->isInInterruptContext == false)
{
/* We were not in interrupt context. Restore the interrupt
* status and unlock the Mutex */
SYS_INT_SourceRestore(hDriver->interruptSource, interruptWasEnabled);
OSAL_MUTEX_Unlock((OSAL_MUTEX_HANDLE_TYPE *)&hDriver->mutexID);
}
}
}
}/* end of DRV_USBDP_Detach() */
// *****************************************************************************
/* Function:
uint16_t DRV_USBDP_SOFNumberGet(DRV_HANDLE client)
Summary:
Dynamic implementation of DRV_USBDP_SOFNumberGet client
interface function.
Description:
This is the dynamic implementation of DRV_USBDP_SOFNumberGet
client interface function for USB device.
Function checks the validity of the input arguments and on success returns
the Frame count value.
Remarks:
See drv_usbdp.h for usage information.
*/
uint16_t DRV_USBDP_SOFNumberGet(DRV_HANDLE handle)
{
DRV_USBDP_OBJ * hDriver; /* USB driver object pointer */
udp_registers_t * usbID; /* USB instance pointer */
uint16_t retVal = 0; /* Return value */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if((DRV_HANDLE_INVALID == handle) || ((DRV_HANDLE)NULL == handle))
{
/* Invalid Driver Handle */
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "\r\nUSBDP Driver: Invalid Driver Handle in DRV_USBDP_SOFNumberGet().");
}
else
{
/* Assign the driver handler to the local pointer */
hDriver = (DRV_USBDP_OBJ *) handle;
usbID = hDriver->usbID;
/* Get the Frame count */
retVal = usbID->UDP_FRM_NUM & UDP_FRM_NUM_FRM_NUM_Msk;
}
return retVal;
}/* end of DRV_USBDP_SOFNumberGet() */
// *****************************************************************************
/* Function:
void _DRV_USBDP_IRPQueueFlush
(
DRV_USBDP_ENDPOINT_OBJ * endpointObj,
USB_DEVICE_IRP_STATUS status
)
Summary:
Dynamic implementation of _DRV_USBDP_IRPQueueFlush function.
Description:
This is the dynamic implementation of _DRV_USBDP_IRPQueueFlush
function for USB device.
Function scans for all the IRPs on the endpoint queue and cancels them all.
Remarks:
This is a local function and should not be called directly by the
application.
*/
void _DRV_USBDP_IRPQueueFlush
(
DRV_USBDP_ENDPOINT_OBJ * endpointObj,
USB_DEVICE_IRP_STATUS status
)
{
USB_DEVICE_IRP_LOCAL * iterator = NULL; /* IRP pointer for parsing */
/* Validate all the parameters used in the function. Proceed only if they
* are valid */
if(endpointObj != NULL)
{
/* Check if any IRPs are assigned on this endpoint and abort them */
if(endpointObj->irpQueue != NULL)
{
/* Scan for all the IRPs on this endpoint. Cancel the IRP and
* deallocate driver IRP objects */
iterator = endpointObj->irpQueue;
while(iterator != NULL)
{
iterator->status = status;
if(iterator->callback != NULL)
{
iterator->callback((USB_DEVICE_IRP *)iterator);
}
iterator = iterator->next;
}
}
/* Set the head pointer to NULL */
endpointObj->irpQueue = NULL;
}
}/* end of _DRV_USBDP_IRPQueueFlush() */
// *****************************************************************************
/* Function: