-
Notifications
You must be signed in to change notification settings - Fork 4
/
cy_usb_dev.c
2853 lines (2480 loc) · 105 KB
/
cy_usb_dev.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
/***************************************************************************//**
* \file cy_usb_dev.c
* \version 2.10
*
* Provides API implementation of the USBFS device middleware.
*
********************************************************************************
* \copyright
* (c) 2018-2021, Cypress Semiconductor Corporation (an Infineon company) or
* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/
#include "cy_usb_dev.h"
#if (defined(CY_IP_MXUSBFS) || defined(CY_IP_M0S8USBDSS))
/*******************************************************************************
* Internal Macro
*******************************************************************************/
#define GET_CFG_WORD(addr) CY_USB_DEV_GET_CFG_WORD(addr)
#define VAL2IDX(val) ( (uint32_t) (val) - 1U)
#define GET_UINT16(lsb, msb) ((lsb) | (uint16_t) (((uint16_t) (msb)) << 8UL))
/* Decode setup request bmRequestType */
#define SETUP_RQST_RCPT_Pos (0)
#define SETUP_RQST_RCPT_Msk (0x1FU)
#define SETUP_RQST_TYPE_Pos (5)
#define SETUP_RQST_TYPE_Msk (0x60u)
#define SETUP_RQST_DIR_Pos (7)
#define SETUP_RQST_DIR_Msk (0x80U)
/* Setup request layout in the buffer */
#define SETUP_RQST_POS (0)
#define SETUP_RQST_TYPE_POS (1)
#define SETUP_VALUE_LSB_POS (2)
#define SETUP_VALUE_MSB_POS (3)
#define SETUP_INDEX_LSB_POS (4)
#define SETUP_INDEX_MSB_POS (5)
#define SETUP_LENGTH_LSB_POS (6)
#define SETUP_LENGTH_MSB_POS (7)
/* Field position in the descriptors */
#define CONFIG_DESCR_LENGTH_LSB_POS (2) /* Configuration descriptor length (LSB) byte position */
#define CONFIG_DESCR_LENGTH_MSB_POS (3) /* Configuration descriptor length (MSB) byte position */
#define BOS_DESCR_LENGTH_LSB_POS (2) /* BOS descriptor length (LSB) byte position */
#define BOS_DESCR_LENGTH_MSB_POS (3) /* BOS descriptor length (MSB) byte position */
#define CONFIG_DESCR_ATTRIB_POS (7) /* Configuration descriptor attribute byte position */
#define DEVICE_DESCR_ISN_STRING_POS (16) /* Position of Serial Number in Device Descriptor */
#define STRING_DESCR_LENGTH_POS (0) /* Position inside string descriptor where length is stored */
#define STRING_DESCR_TYPE_POS (1) /* Position inside string descriptor where type is stored */
/* bmAttributes in configuration descriptor */
#define CONFIG_ATTR_SELF_POWERED_MASK (0x40U) /* Configuration attribute Self-Powered mask */
#define CONFIG_ATTR_REMOTE_WAKEUP_MASK (0x20U) /* Configuration attribute Remote Wakeup mask */
/* Fixed string descriptor indexes */
#define STRING_LANGID_INDEX (0U)
#define STRING_IMSOS_INDEX (0xEEU)
#define EXT_OS_DESC_LENGTH_BYTE0_POS (0x0U)
#define EXT_OS_DESC_LENGTH_BYTE1_POS (0x1U)
#define EXT_OS_DESC_LENGTH_BYTE2_POS (0x2U)
#define EXT_OS_DESC_LENGTH_BYTE3_POS (0x3U)
/*******************************************************************************
* Static Functions Prototypes
*******************************************************************************/
static int32_t HandleTimeout(int32_t milliseconds);
#if defined(CY_IP_MXUSBFS)
static void InitSerialNumberString(cy_stc_usb_dev_context_t *context);
#endif /* defined(CY_IP_MXUSBFS) */
static cy_en_usb_dev_status_t ConvertEndpointStateToStatus(cy_en_usb_dev_ep_state_t epState);
static void BusResetCallback(USBFS_Type *base, struct cy_stc_usbfs_dev_drv_context *drvContext);
static void Ep0SetupCallback(USBFS_Type *base, struct cy_stc_usbfs_dev_drv_context *drvContext);
static void Ep0InCallback (USBFS_Type *base, struct cy_stc_usbfs_dev_drv_context *drvContext);
static void Ep0OutCallback (USBFS_Type *base, struct cy_stc_usbfs_dev_drv_context *drvContext);
static cy_en_usb_dev_status_t HandleSetup(cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t HandleIn (cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t HandleOut (cy_stc_usb_dev_context_t *context);
static void DecodeSetupPacket (uint8_t const *data, cy_stc_usb_dev_setup_packet_t *packet);
static cy_en_usb_dev_status_t HandleStandardRequests (cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t GetDescriptorRequest (cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t GetConfigurationRequest(cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t GetInterfaceRequest (cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t GetStatusRequest (cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t const *context);
static cy_en_usb_dev_status_t ClearFeatureRequest (cy_stc_usb_dev_control_transfer_t const *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t SetAddressRequest (cy_stc_usb_dev_control_transfer_t const *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t SetConfigurationRequest(cy_stc_usb_dev_control_transfer_t const *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t SetInterfaceRequest (cy_stc_usb_dev_control_transfer_t const *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t SetFeatureRequest (cy_stc_usb_dev_control_transfer_t const *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t HandleClassRequests(cy_stc_usb_dev_class_ll_item_t *curItem,
cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t HandleClassRequestsCompleted(cy_stc_usb_dev_class_ll_item_t *curItem,
cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t GetExtOsStringDescriptors(cy_stc_usb_dev_ms_os_string_t const *msOsString,
cy_stc_usb_dev_control_transfer_t *transfer);
static cy_en_usb_dev_status_t HandleVendorRequests(cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t HandleVendorRequestsCompleted(cy_stc_usb_dev_control_transfer_t *transfer,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t CallSetInterfaceCallbacks(uint32_t interface,
uint32_t alternate,
cy_stc_usb_dev_class_ll_item_t *curItem,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t ConfigureDataEndpoints(uint32_t config, cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t InterfaceRemoveDataEndpoints(uint32_t numEndpoints,
cy_stc_usb_dev_endpoint_t const * const *epsPool,
cy_stc_usb_dev_context_t *context);
static cy_en_usb_dev_status_t InterfaceAddDataEndpoints(uint32_t numEndpoints,
cy_stc_usb_dev_endpoint_t const * const *epsPool,
cy_stc_usb_dev_context_t *context);
/*******************************************************************************
* Function Name: Cy_USB_Dev_Init
****************************************************************************//**
*
* Initialize the USB Device stack and underneath USBFS hardware driver.
*
* \param base
* The pointer to the USBFS instance.
*
* \param drvConfig
* The pointer to the USBFS driver configuration structure.
*
* \param drvContext
* The pointer to the USBFS driver context structure allocated by the user.
* The structure is used during the USBFS driver operation for internal
* configuration and data retention. The user must not modify anything in
* this structure.
*
* \param device
* The pointer to the device structure \ref cy_stc_usb_dev_device_t.
*
* \param config
* The pointer to the driver configuration structure \ref cy_stc_usb_dev_config_t.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for
* internal configuration and data retention. The user must not modify anything
* in this structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
* \note
* The configuration of USB clocks, pins, and interrupts is not handled by this
* function and must be done on the application level.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_Init(USBFS_Type *base,
struct cy_stc_usbfs_dev_drv_config const *drvConfig,
struct cy_stc_usbfs_dev_drv_context *drvContext,
cy_stc_usb_dev_device_t const *device,
cy_stc_usb_dev_config_t const *config,
cy_stc_usb_dev_context_t *context)
{
/* Input parameters verification */
if ((NULL == device) || (NULL == config) || (NULL == context) ||
(NULL == base) || (NULL == drvConfig) || (NULL == drvContext))
{
return CY_USB_DEV_BAD_PARAM;
}
cy_en_usb_dev_status_t retStatus = CY_USB_DEV_DRV_HW_ERROR;
/* Store driver information in device */
context->drvBase = base;
context->drvContext = drvContext;
/* Set default state */
context->state = CY_USB_DEV_DISABLED;
context->configuration = 0U;
context->classRoot = NULL;
/* Configure endpoint 0 buffer */
context->ControlTransfer.buffer = config->ep0Buffer;
context->ControlTransfer.bufferSize = config->ep0BufferSize;
/* Store link to descriptors */
context->devDescriptors = device;
/* Initialize delay function */
context->handleTimeout = &HandleTimeout;
/* Initialize event callback */
context->eventsCallback = NULL;
#if defined(CY_IP_MXUSBFS)
/* Initialize serial string descriptor and set pointer */
InitSerialNumberString(context);
#endif /* defined(CY_IP_MXUSBFS) */
context->getSerialNumString = NULL;
/* Initialize vendor-specific callbacks */
context->vndRequestReceived = NULL;
context->vndRequestCompleted = NULL;
/* Link driver and device context */
Cy_USBFS_Dev_Drv_SetDevContext(base, context, drvContext);
/* Configure driver */
retStatus = (cy_en_usb_dev_status_t) Cy_USBFS_Dev_Drv_Init(base, drvConfig, drvContext);
if (CY_USB_DEV_SUCCESS == retStatus)
{
/* Hook device handlers to be called by driver */
Cy_USBFS_Dev_Drv_RegisterServiceCallback(base, CY_USB_DEV_BUS_RESET, &BusResetCallback, drvContext);
Cy_USBFS_Dev_Drv_RegisterServiceCallback(base, CY_USB_DEV_EP0_SETUP, &Ep0SetupCallback, drvContext);
Cy_USBFS_Dev_Drv_RegisterServiceCallback(base, CY_USB_DEV_EP0_IN, &Ep0InCallback, drvContext);
Cy_USBFS_Dev_Drv_RegisterServiceCallback(base, CY_USB_DEV_EP0_OUT, &Ep0OutCallback, drvContext);
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_DeInit
****************************************************************************//**
*
* De-initialize the USB Device stack and underneath hardware driver.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
*******************************************************************************/
void Cy_USB_Dev_DeInit(cy_stc_usb_dev_context_t *context)
{
Cy_USBFS_Dev_Drv_DeInit(context->drvBase, context->drvContext);
/* IDe-initialize all callbacks */
context->classRoot = NULL;
context->eventsCallback = NULL;
context->getSerialNumString = NULL;
context->vndRequestReceived = NULL;
context->vndRequestCompleted = NULL;
}
/*******************************************************************************
* Function Name: HandleTimeout
****************************************************************************//**
*
* Waits for 1 millisecond and returns updated number of milliseconds that remain
* to wait before timeout expires.
*
* \param milliseconds
* Number of milliseconds that remain to wait before timeout expires.
*
* \return
* Updated number of milliseconds remain to wait.
*
*******************************************************************************/
static int32_t HandleTimeout(int32_t milliseconds)
{
Cy_SysLib_Delay(1U); /* Wait for 1 millisecond */
return (milliseconds - 1);
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_Connect
****************************************************************************//**
*
* Enables pull-up on D+ (hardware supports only full-speed device) line to
* signal USB Device connection on USB Bus.
*
* \param blocking
* Wait until device is configured.
*
* \param timeout
* Defines in milliseconds the time for which this function can block.
* If that time expires, the USB Device is disconnected and the function returns.
* To wait forever, pass \ref CY_USB_DEV_WAIT_FOREVER.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_Connect(bool blocking, int32_t timeout, cy_stc_usb_dev_context_t *context)
{
/* Returns SUCCESS except timeout when timeout is used */
cy_en_usb_dev_status_t retStatus = CY_USB_DEV_SUCCESS;
context->state = CY_USB_DEV_POWERED;
Cy_USBFS_Dev_Drv_Enable(context->drvBase, context->drvContext);
if (blocking)
{
if (CY_USB_DEV_WAIT_FOREVER == timeout)
{
/* Wait until device is configured */
while (CY_USB_DEV_CONFIGURED != context->state)
{
(void) context->handleTimeout(timeout);
}
}
else
{
/* Wait until device is configured or timeout */
while ((CY_USB_DEV_CONFIGURED != context->state) && (timeout > 0))
{
timeout = context->handleTimeout(timeout);
}
/* Timeout expired disconnect USB Device */
if (0 == timeout)
{
Cy_USB_Dev_Disconnect(context);
retStatus = CY_USB_DEV_TIMEOUT;
}
}
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_Disconnect
****************************************************************************//**
*
* Disables pull-up on D+ (hardware supports only full-speed device) line to
* signal USB Device disconnection on USB Bus.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
*******************************************************************************/
void Cy_USB_Dev_Disconnect(cy_stc_usb_dev_context_t *context)
{
Cy_USBFS_Dev_Drv_Disable(context->drvBase, context->drvContext);
/* Set device in the default state */
context->state = CY_USB_DEV_DISABLED;
context->configuration = 0U;
}
/*******************************************************************************
* Function Name: ConvertEndpointStateToStatus
****************************************************************************//**
*
* Converts endpoint state to the USB Device status code.
*
* \param epState
* Endpoint state cy_en_usb_dev_ep_state_t.
* The state CY_USB_DEV_EP_IDLE converted to \ref CY_USB_DEV_DRV_HW_DISABLED
* to indicate that current endpoint configuration was changed.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
*******************************************************************************/
static cy_en_usb_dev_status_t ConvertEndpointStateToStatus(cy_en_usb_dev_ep_state_t epState)
{
cy_en_usb_dev_status_t retStatus;
switch(epState)
{
case CY_USB_DEV_EP_COMPLETED:
retStatus = CY_USB_DEV_SUCCESS;
break;
case CY_USB_DEV_EP_PENDING:
retStatus = CY_USB_DEV_DRV_HW_BUSY;
break;
case CY_USB_DEV_EP_IDLE: /* Endpoint configuration is changed */
case CY_USB_DEV_EP_STALLED:
retStatus = CY_USB_DEV_DRV_HW_DISABLED;
break;
case CY_USB_DEV_EP_INVALID:
case CY_USB_DEV_EP_DISABLED:
retStatus = CY_USB_DEV_BAD_PARAM;
break;
default:
retStatus = CY_USB_DEV_BAD_PARAM;
break;
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_AbortEpTransfer
****************************************************************************//**
*
* Aborts pending read or write endpoint operation.
* If there is any bus activity after abort operation requested the function
* waits for its completion or timeout. The timeout is time to transfer
* bulk or interrupt packet of maximum playload size. If this bus activity is
* a transfer to the aborting endpoint the received data is lost and endpoint
* transfer completion callbacks is not invoked.
* After function returns new read or write endpoint operation can be submitted.
*
* \param endpoint
* The data endpoint number.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
* \note
* The abort operation is not supported for ISOC endpoints because
* these endpoints do not have handshake and are always accessible by the
* USB Host. Therefore, abort can cause unexpected behavior.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_AbortEpTransfer(uint32_t endpoint,
cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus;
/* Request abort operation, on exit abort complete */
retStatus = (cy_en_usb_dev_status_t) Cy_USBFS_Dev_Drv_Abort(context->drvBase, endpoint, context->drvContext);
if (CY_USB_DEV_SUCCESS != retStatus)
{
retStatus = CY_USB_DEV_DRV_HW_ERROR;
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_StartReadEp
****************************************************************************//**
*
* Start a reading on a certain endpoint.
*
* \param endpoint
* The OUT data endpoint number.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
* \note
* The read is not allowed for OUT endpoints after SET_CONFIGURATION or
* SET_INTERFACE request therefore this function must be called before reading data
* from OUT endpoints.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_StartReadEp(uint32_t endpoint, cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus;
cy_en_usb_dev_ep_state_t epState;
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
/* Check that endpoint is ready for read operation */
if ((CY_USB_DEV_EP_IDLE == epState) || (CY_USB_DEV_EP_COMPLETED == epState))
{
/* Enable endpoint to be written by host */
Cy_USBFS_Dev_Drv_EnableOutEndpoint(context->drvBase, endpoint, context->drvContext);
retStatus = CY_USB_DEV_SUCCESS;
}
else
{
/* Use endpoint state to get status */
retStatus = ConvertEndpointStateToStatus(epState);
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_ReadEpBlocking
****************************************************************************//**
*
* Read data received from USB Host from a certain endpoint. Before calling
* this function, \ref Cy_USB_Dev_StartReadEp must be called.
* This function is blocking and returns after successful USB Host transfer,
* or an error or timeout occurred.
*
* \param endpoint
* The OUT data endpoint number.
*
* \param buffer
* The pointer to buffer that stores data that was read. \n
* Allocate buffer using \ref CY_USB_DEV_ALLOC_ENDPOINT_BUFFER macro to make
* it USBFS driver configuration independent (See \ref group_usb_dev_ep_buf_alloc
* for more information).
*
* \param size
* The number of bytes to read.
* This value must be less or equal to endpoint maximum packet size.
*
* \param actSize
* The number of bytes that were actually read.
*
* \param timeout
* Defines in milliseconds the time for which this function can block.
* If that time expires the function returns.
* To wait forever pass \ref CY_USB_DEV_WAIT_FOREVER.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_ReadEpBlocking(uint32_t endpoint, uint8_t *buffer,
uint32_t size, uint32_t *actSize, int32_t timeout,
cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus = CY_USB_DEV_SUCCESS;
cy_en_usb_dev_ep_state_t epState;
/* Get endpoint state before check it */
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
if (CY_USB_DEV_WAIT_FOREVER == timeout)
{
/* Wait until transfer is completed */
while (CY_USB_DEV_EP_PENDING == epState)
{
(void) context->handleTimeout(timeout);
/* Update endpoint state */
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
}
}
else
{
/* Wait until transfer is completed or for timeout */
while ((CY_USB_DEV_EP_PENDING == epState) && (timeout > 0))
{
timeout = context->handleTimeout(timeout);
/* Update endpoint state */
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
}
/* Timeout expired */
if (0 == timeout)
{
/* Abort write operation */
(void) Cy_USB_Dev_AbortEpTransfer(endpoint, context);
retStatus = CY_USB_DEV_TIMEOUT;
}
}
/* Clear actual number of read bytes */
*actSize = 0U;
/* Read data from endpoint buffer after completion */
if (CY_USB_DEV_EP_COMPLETED == epState)
{
retStatus = (cy_en_usb_dev_status_t)
Cy_USBFS_Dev_Drv_ReadOutEndpoint(context->drvBase,
endpoint, buffer, size, actSize, context->drvContext);
if (CY_USB_DEV_SUCCESS != retStatus)
{
retStatus = CY_USB_DEV_DRV_HW_ERROR;
}
}
if ((CY_USB_DEV_TIMEOUT != retStatus) && (CY_USB_DEV_DRV_HW_ERROR != retStatus))
{
/* Use endpoint state to get status */
retStatus = ConvertEndpointStateToStatus(epState);
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_ReadEpNonBlocking
****************************************************************************//**
*
* Read data received from USB Host from a certain endpoint. Before calling
* this function, \ref Cy_USB_Dev_StartReadEp must be called.
*
* \param endpoint
* The OUT data endpoint number.
*
* \param buffer
* The pointer to buffer that stores data that was read. \n
* Allocate buffer using \ref CY_USB_DEV_ALLOC_ENDPOINT_BUFFER macro to make
* it USBFS driver configuration independent (See \ref group_usb_dev_ep_buf_alloc
* for more information).
*
* \param size
* The number of bytes to read.
* This value must be less than or equal to endpoint maximum packet size.
*
* \param actSize
* The number of bytes that were actually read.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_ReadEpNonBlocking(uint32_t endpoint, uint8_t *buffer,
uint32_t size, uint32_t *actSize, cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus;
cy_en_usb_dev_ep_state_t epState;
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
/* Read data from endpoint buffer after completion */
if (CY_USB_DEV_EP_COMPLETED == epState)
{
retStatus = (cy_en_usb_dev_status_t)
Cy_USBFS_Dev_Drv_ReadOutEndpoint(context->drvBase,
endpoint, buffer, size, actSize, context->drvContext);
if (CY_USB_DEV_SUCCESS != retStatus)
{
retStatus = CY_USB_DEV_DRV_HW_ERROR;
}
}
else
{
/* Clear actual number of read bytes */
*actSize = 0U;
/* Use endpoint state to get status */
retStatus = ConvertEndpointStateToStatus(epState);
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_WriteEpBlocking
****************************************************************************//**
*
* Write data to be transferred to USB Host from a certain endpoint.
* This function is blocking and returns after successful USB Host transfer,
* or an error or timeout occurred.
*
* \param endpoint
* The IN data endpoint number.
*
* \param buffer
* The pointer to the buffer containing data bytes to write. \n
* Allocate buffer using \ref CY_USB_DEV_ALLOC_ENDPOINT_BUFFER macro to make
* it USBFS driver configuration independent (See \ref group_usb_dev_ep_buf_alloc
* for more information).
*
* \param size
* The number of bytes to write.
* This value must be less than or equal to endpoint maximum packet size.
*
* \param timeout
* Defines in milliseconds the time for which this function can block.
* If that time expires, the function returns.
* To wait forever, pass \ref CY_USB_DEV_WAIT_FOREVER.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_WriteEpBlocking(uint32_t endpoint, uint8_t const *buffer,
uint32_t size, int32_t timeout, cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus = CY_USB_DEV_BAD_PARAM;
cy_en_usb_dev_ep_state_t epState;
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
/* Check that endpoint is ready for write operation */
if ((CY_USB_DEV_EP_IDLE == epState) || (CY_USB_DEV_EP_COMPLETED == epState))
{
retStatus = (cy_en_usb_dev_status_t) Cy_USBFS_Dev_Drv_LoadInEndpoint(context->drvBase,
endpoint, buffer, size, context->drvContext);
/* Check endpoint load status */
if (CY_USB_DEV_SUCCESS == retStatus)
{
/* Update endpoint state after load operation */
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
if (CY_USB_DEV_WAIT_FOREVER == timeout)
{
/* Wait until transfer is completed */
while (CY_USB_DEV_EP_PENDING == epState)
{
(void) context->handleTimeout(timeout);
/* Update endpoint state */
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
}
}
else
{
/* Wait until transfer is completed or for timeout */
while ((CY_USB_DEV_EP_PENDING == epState) && (timeout > 0))
{
timeout = context->handleTimeout(timeout);
/* Update endpoint state */
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
}
/* Timeout expired */
if (0 == timeout)
{
/* Abort write operation */
(void) Cy_USB_Dev_AbortEpTransfer(endpoint, context);
retStatus = CY_USB_DEV_TIMEOUT;
}
}
}
else
{
retStatus = CY_USB_DEV_DRV_HW_ERROR;
}
}
if ((CY_USB_DEV_TIMEOUT != retStatus) && (CY_USB_DEV_DRV_HW_ERROR != retStatus))
{
/* Use endpoint state to get status */
retStatus = ConvertEndpointStateToStatus(epState);
}
return retStatus;
}
/*******************************************************************************
* Function Name: Cy_USB_Dev_WriteEpNonBlocking
****************************************************************************//**
*
* Write data to be transferred to USB Host from a certain endpoint.
*
* \param endpoint
* The IN data endpoint number.
*
* \param buffer
* The pointer to the buffer containing data bytes to write. \n
* Allocate buffer using \ref CY_USB_DEV_ALLOC_ENDPOINT_BUFFER macro to make
* it USBFS driver configuration independent (See \ref group_usb_dev_ep_buf_alloc
* for more information).
*
* \param size
* The number of bytes to write.
* This value must be less than or equal to endpoint maximum packet size.
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
*******************************************************************************/
cy_en_usb_dev_status_t Cy_USB_Dev_WriteEpNonBlocking(uint32_t endpoint, uint8_t const *buffer,
uint32_t size, cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus;
cy_en_usb_dev_ep_state_t epState;
epState = Cy_USBFS_Dev_Drv_GetEndpointState(context->drvBase, endpoint, context->drvContext);
/* Check that endpoint is ready for operation */
if ((CY_USB_DEV_EP_IDLE == epState) || (CY_USB_DEV_EP_COMPLETED == epState))
{
retStatus = (cy_en_usb_dev_status_t)
Cy_USBFS_Dev_Drv_LoadInEndpoint(context->drvBase,
endpoint, buffer, size, context->drvContext);
/* Write data into the endpoint buffer */
if (CY_USB_DEV_SUCCESS != retStatus)
{
retStatus = CY_USB_DEV_DRV_HW_ERROR;
}
}
else
{
retStatus = ConvertEndpointStateToStatus(epState);
}
return retStatus;
}
/*******************************************************************************
* Function Name: BusResetCallback
****************************************************************************//**
*
* Handles Bus Reset interrupt.
*
* \param base
* The pointer to the USBFS instance.
*
* \param drvContext
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
*******************************************************************************/
static void BusResetCallback(USBFS_Type *base, struct cy_stc_usbfs_dev_drv_context *drvContext)
{
/* Get device context from the driver context */
cy_stc_usb_dev_context_t *context = (cy_stc_usb_dev_context_t *) Cy_USBFS_Dev_Drv_GetDevContext(base, drvContext);
/* Get linked list of classes */
cy_stc_usb_dev_class_ll_item_t *curItem = context->classRoot;
/* Set device in the DEFAULT state */
context->state = CY_USB_DEV_DEFAULT;
context->status = 0U; /* Reset remote wakeup and power state */
context->configuration = 0U;
/* Notify Bus Reset event for device instance */
if (NULL != context->eventsCallback)
{
/* Input parameters are zeros. Ignore return for Bus Reset event. */
(void) context->eventsCallback(CY_USB_DEV_EVENT_BUS_RESET, 0UL, 0UL, context);
}
/* Notify Bus Reset event for all class instances */
while (NULL != curItem)
{
if (NULL != curItem->classObj->busReset)
{
/* Execute callback */
curItem->classObj->busReset(curItem->classData, context);
}
/* Move to next element */
curItem = curItem->next;
}
}
/*******************************************************************************
* Function Name: DecodeSetupPacket
****************************************************************************//**
*
* Decodes setup packet (populates \ref cy_stc_usb_dev_setup_packet_t).
*
* \param data
* The pointer to buffer with setup packet (raw data).
*
* \param packet
* The pointer to structure that holds setup packet.
*
*******************************************************************************/
static void DecodeSetupPacket(uint8_t const *data, cy_stc_usb_dev_setup_packet_t *packet)
{
/* Fill elements of setup packet structure from raw data */
packet->bmRequestType.direction = (uint8_t) _FLD2VAL(SETUP_RQST_DIR, data[SETUP_RQST_POS]);
packet->bmRequestType.type = (uint8_t) _FLD2VAL(SETUP_RQST_TYPE, data[SETUP_RQST_POS]);
packet->bmRequestType.recipient = (uint8_t) _FLD2VAL(SETUP_RQST_RCPT, data[SETUP_RQST_POS]);
packet->bRequest = (uint8_t) data[SETUP_RQST_TYPE_POS];
packet->wValue = GET_UINT16(data[SETUP_VALUE_LSB_POS], data[SETUP_VALUE_MSB_POS]);
packet->wIndex = GET_UINT16(data[SETUP_INDEX_LSB_POS], data[SETUP_INDEX_MSB_POS]);
packet->wLength = GET_UINT16(data[SETUP_LENGTH_LSB_POS], data[SETUP_LENGTH_MSB_POS]);
}
/*******************************************************************************
* Function Name: HandleSetup
****************************************************************************//**
*
* Handles setup packet received event (generated from Endpoint 0 Interrupt).
*
* \param context
* The pointer to the context structure \ref cy_stc_usb_dev_context_t allocated
* by the user. The structure is used during the USB Device operation for internal
* configuration and data retention. The user must not modify anything in this
* structure.
*
* \return
* Status code of the function execution \ref cy_en_usb_dev_status_t.
*
*******************************************************************************/
static cy_en_usb_dev_status_t HandleSetup(cy_stc_usb_dev_context_t *context)
{
cy_en_usb_dev_status_t retStatus = CY_USB_DEV_REQUEST_NOT_HANDLED;
cy_stc_usb_dev_control_transfer_t *transfer = &context->ControlTransfer;
/* Get setup packet from hardware */
Cy_USBFS_Dev_Drv_Ep0GetSetup(context->drvBase, transfer->buffer, context->drvContext);
/* Decode setup packet */
DecodeSetupPacket(transfer->buffer, &transfer->setup);
/* Prepare for new transfer */
transfer->ptr = NULL;
transfer->remaining = 0U;
transfer->size = 0U;
transfer->direction = transfer->setup.bmRequestType.direction;
transfer->zlp = false;
transfer->notify = false;
/* Handle Setup request depends on type */
switch (transfer->setup.bmRequestType.type)
{
case CY_USB_DEV_STANDARD_TYPE:
case CY_USB_DEV_CLASS_TYPE:
{
if (CY_USB_DEV_STANDARD_TYPE == transfer->setup.bmRequestType.type)
{
/* Handle Standard requests */
retStatus = HandleStandardRequests(transfer, context);
}
/* Try handle by Class requests handler */
if (CY_USB_DEV_SUCCESS != retStatus)
{
retStatus = HandleClassRequests(context->classRoot, transfer, context);
}
}
break;
case CY_USB_DEV_VENDOR_TYPE:
retStatus = HandleVendorRequests(transfer, context);
break;