-
Notifications
You must be signed in to change notification settings - Fork 4
/
cy_usb_dev.h
1953 lines (1763 loc) · 83.8 KB
/
cy_usb_dev.h
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.h
* \version 2.10
*
* Provides API declarations 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.
*******************************************************************************/
/**
* \mainpage USB Device Middleware Library 2.10
*
* The USB Device middleware provides a full-speed USB 2.0 Chapter 9 specification
* -compliant device framework. It uses the USBFS driver from PDL to interface
* with the hardware. The middleware provides support for Audio, CDC, and HID
* classes. Also, it allows implementing other class support.
* The USB Configurator tool makes it easy to construct the USB
* Device descriptor.
*
* <b>Features:</b>
* * USB Full-Speed Device Framework
* * \ref group_usb_dev_cfg_tool
* * The following USB Classes are supported:
* * \ref group_usb_dev_audio_class_info
* * \ref group_usb_dev_cdc_class_info
* * \ref group_usb_dev_hid_class_info
*
********************************************************************************
* \section section_usb_dev_general General Description
********************************************************************************
*
* The USB Device structure is divided into layers. The implementation
* is event-driven: the USBFS driver receives interrupts from the hardware and
* provides callbacks to the USB Device layer which implements them and provides
* events to the Class layer. A simplified image is shown below. \n \n
* \image html usb_dev_solution_struct.png
*
* Include cy_usb_dev.h along with the header for the USB class to be used
* (cy_usb_dev_hid.h, cy_usb_dev_cdc.h, cy_usb_dev_audio.h) to get access to
* all the functions and other declarations in this library.
* If you use ModusToolbox USB Device Configurator, also include cycfg_usbdev.h.
*
********************************************************************************
* \section section_usb_dev_quick_start Quick Start Guide
********************************************************************************
*
* Cypress USB Device middleware can be used in various software environments.
* Refer to the \ref section_usb_dev_toolchain.
* The quickest way to get started is using the Code Examples.
* Cypress Semiconductor continuously extends its portfolio of code examples
* at <a href="http://www.cypress.com"><b>Cypress Semiconductor website</b></a>
* and at <a href="https://github.com/cypresssemiconductorco">
* <b>Cypress Semiconductor GitHub</b></a>.
*
* This quick start guide is for an environment configured to use for development
* MTB CAT1A Peripheral Driver Library (PSoC6) or MTB CAT2 Peripheral Driver Library
* (PSoC4 & PMG1) included in the project.
*
* To easily run a USB, use the ModusToolbox USBCommDevice or USBM project.
* The steps below show how to set up a USB Device based on a basic
* ModusToolbox project (like Hello_World).
*
* The following steps set up a USB device recognized as an standard HID device -
* USB mouse and moves it from right to the left, and vice-versa.
* \note Some steps contain a corresponding to the figure below number in
* brackets.
*
* \subsection subsection_qsg_step1 STEP 1: Enable the USB Device middleware.
*
* Launch ModusToolbox Library Manager and enable the USB Device middleware.
* This step is required only if the ModusToolbox IDE is used. Otherwise, ensure
* the USB Device Middleware is included in your project.
*
* \subsection subsection_qsg_step2 STEP 2: Generate initialization code.
*
* 1. Launch the ModusToolbox Device Configurator Tool and switch to the
* Peripherals tab (#1.1).
* 2. Enable the USB personality under Communication and enter Alias (#1.2).
* We use USBHID in \ref section_usb_dev_quick_start
* 3. Go to the Parameters pane and configure the USB personality: assign
* the peripheral clock divider (#1.3) for Clock (Bus Reset).
* Any available free divider can be used.
* This is not required to be done for the PMG1 family of devices.
* 4. Set Endpoint Mask to 1 to enable data endpoint 1 (#1.4) Enabled data
* endpoints must match the descriptor tree in the USB Configurator.
* \image html usb_dev_device_cfg.png
* 5. Switch to the System tab (#2.1).
* 6. Check the IMO clock is enabled (#2.2). Select Trim with USB (#2.3)
* 7. Select one of the PLLs, if your device supports more than one.
* Enable the PLL and set a frequency of 48 MHz (#2.4).
* 8. Select the CLK_HF3 USB clock (#2.5). Assign the source clock to
* the CLK_PATH connected to the configured previously PLL.<br>
* (Not required for PMG1 devices) (#2.6).
* 9. Check the FLL clock is enabled (Not required for PMG1 devices) (#2.7).
* 10. Select File->Save to generate initialization code.
* \image html usb_dev_system_cfg.png
*
* \subsection subsection_qsg_step3 STEP 3: Generate USB descriptors.
*
* 1. Run the USB Configurator.
* 2. In the Device Descriptor node, set bDeviceClass - 0x02(#3.1),
* iProduct - the device name to identify among connected devices. We use
* "USB Device Quick Start guide"(#3.2).
* 3. Remove default Alternate Settings.
* 4. Add HID Alternate Settings.
* 5. Add HID Descriptor and select 3-Button Mouse in HID Report (#3.3).
* 6. Add Endpoint Descriptor and set: direction - IN,
* Transfer Type - Interrupt, wMaxPacketSize - 3, bInterval - 10(#3.4).
* 7. Perform File->Save to generate initialization code. If configuration is
* saved for the first time, choose a name (like design.cyusbdev) and save it
* to the project root.
* \image html usb_dev_configurator.png
*
* \subsection subsection_qsg_step4 STEP 4: Update main.c
*
* 1. Include the USB headers to get access to the generated descriptor
* structures, USB driver, device, and class layers APIs.
* \snippet usb_dev/snippet/main.c snipped_cy_usb_dev_headers
* 2. Declare the USB context global variables:
* \snippet usb_dev/snippet/main.c snipped_cy_usb_dev_hid_globals
* 3. Configure the USB interrupt structures and declare interrupt handlers
* (refer to the Configure Interrupts section of the USBFS
* driver in the PDL API Reference).
* \snippet usb_dev/snippet/main.c snipped_cy_usb_dev_interrupt_configuration
* 4. Implement the interrupt handlers:
* \snippet usb_dev/snippet/main.c snipped_cy_usb_dev_interrupt_handlers
* 5. Update the main() function with the USB and interrupt initialization
* routines:
* \snippet usb_dev/snippet/main.c snipped_USB_Dev_InitEnableHID
* 6. Example of the routine to move mouse from right to the left,
* and vice-versa.
* \snippet usb_dev/snippet/main.c snipped_USB_Dev_MouseMove
*
* \subsection subsection_qsg_step5 STEP 5: Build and program the device.
*
* Connect the device to the Host PC. On the PC, verify a new USB device was
* enumerated as a mouse device. The mouse's cursor shall move left to
* right and vice-versa.
*
********************************************************************************
* \section group_usb_dev_configuration Configuration Considerations
********************************************************************************
*
* This section explains how to configure the USB Device for operation.
*
********************************************************************************
* \subsection group_usb_dev_config_drv Configure USBFS driver
********************************************************************************
*
* The driver and system resources configuration details are provided in the
* USBFS driver section Configuration Considerations in the PDL API Reference
* Manual. The provided code snippets expect that driver and system resources
* configuration is done.
*
********************************************************************************
* \subsection group_usb_dev_config_descr Construct USB Device Descriptors
********************************************************************************
*
* Run standalone USB Configurator tool to construct the USB Device descriptors.
* After USB Device descriptors are
* constructed, save generated source and header files. Add these files to your
* project. Open header files to get external definitions for:
* * USB Device configuration structure \ref cy_stc_usb_dev_config_t instance.
* * Array of USB Devices structures \ref cy_stc_usb_dev_device_t.
* * CDC and/or HID class configuration structure instances.
*
* These definitions will be required in the configuration steps provided below.
*
********************************************************************************
* \subsection group_usb_dev_config Configure USB Device
********************************************************************************
*
* To initialize the USB Device middleware, call \ref Cy_USB_Dev_Init function
* providing:
* * The pointer to the USBFS instance.
* * The pointer to the filled USBFS driver configuration structure cy_stc_usbfs_dev_drv_config_t.
* * The pointer to the allocated USBFS driver context structure cy_stc_usbfs_dev_drv_context_t.
* * The pointer to the generated middleware USB Device structure \ref cy_stc_usb_dev_device_t.
* * The pointer to the generated middleware USB Device configuration structure \ref cy_stc_usb_dev_config_t.
* * The pointer to the allocated middleware USB Device context structure \ref cy_stc_usb_dev_context_t.
*
********************************************************************************
* \subsection group_usb_dev_config_class Configure USB Classes
********************************************************************************
*
* The USB Device middleware provides support of Audio, HID, and CDC classes.
* Each class has own initialization function. This function must be called
* after \ref Cy_USB_Dev_Init to initialize class data and register it. The
* class-specific request will be passed to the class handler after registration.
* Note that the USB Configurator tool generates HID and CDC Class configuration
* structures that are required class initialization. Find these structure
* external declaration in the generated header file.
*
* To initialize the Audio Class, call \ref Cy_USB_Dev_Audio_Init function
* providing:
* * The NULL pointer (reserved for possible future use).
* * The pointer to the allocated Audio Class context structure \ref cy_stc_usb_dev_audio_context_t.
* * The pointer to the allocated USB Device context structure \ref cy_stc_usb_dev_context_t.
*
* To initialize the CDC Class, call \ref Cy_USB_Dev_CDC_Init function
* providing:
* * The pointer to the populated CDC Class configuration structure \ref cy_stc_usb_dev_cdc_config_t.
* * The pointer to the allocated CDC Class context structure \ref cy_stc_usb_dev_cdc_context_t.
* * The pointer to the allocated USB Device context structure \ref cy_stc_usb_dev_context_t.
*
* To initialize the HID Class, call \ref Cy_USB_Dev_HID_Init function
* providing:
* * The pointer to the populated HID Class configuration structure \ref cy_stc_usb_dev_hid_config_t.
* * The pointer to the allocated HID Class context structure \ref cy_stc_usb_dev_hid_context_t.
* * The pointer to the allocated USB Device context structure \ref cy_stc_usb_dev_context_t.
*
********************************************************************************
* \subsection group_usb_dev_config_enable Enable USB Device
********************************************************************************
*
* Finally, enable the USB Device operation calling \ref Cy_USB_Dev_Connect.
* This function call enables pull-up on D+ to signal USB Device connection on
* USB Bus. The USB Host detects device connection and starts device enumeration.
* It requests the device descriptors to define device capabilities and finally
* sets device configuration for the following operation.
* The \ref Cy_USB_Dev_Connect provides an argument to block until enumeration
* completes or exits after the USB Device is enabled.
*
* \snippet usb_dev/snippet/main.c snipped_Cy_USB_Dev_InitEnable
*
* \note
* The interrupts are mandatory for the USB Device operation. Therefore, USBFS
* interrupts must be enabled in the NVIC and global interrupts must be
* enabled as well.
*
********************************************************************************
* \section section_usb_dev_design Design Considerations
********************************************************************************
*
* The typical use case is that application calls the middleware API interface
* provided by the USB Device or Class layer to implement application logic.
* However, some features are provided only by the USBFS driver layer. Therefore, if
* the application needs them, the driver API interface must be used. The list of
* these features is provided in the section \ref group_usb_dev_drv_features.
*
********************************************************************************
* \subsection group_usb_dev_cfg_tool USB Configurator
********************************************************************************
*
* The standalone USB Configurator tool helps construct USB Device descriptors.
* The USB device descriptors
* provide to the USB Host complete information about the connected device. The tool
* output are generated source and header files that contain information about the
* USB Device: device descriptors plus structures that help access device
* descriptors. Generated files are mandatory for the middleware operation and must be
* added to your project. The header file provides access to
* instances of the USB Device configuration structure \ref cy_stc_usb_dev_config_t
* and array of the USB Device structures \ref cy_stc_usb_dev_device_t. Both these
* definitions are required for USB Device configuration. The tool also generates
* instances of configuration structures required for CDC and HID Class
* configuration.
*
* A detailed information about USB Descriptors is provided by the
* [USB Specification](http://www.usb.org/developers/docs/usb20_docs/)
*
* The USB Configurator tool provides the User Guide, which can be found in the
* documentation.
*
********************************************************************************
* \subsection group_usb_dev_std_requests Standard Request Support
********************************************************************************
*
* The USB Device supports standard requests listed in the table below.
*
* <table class="doxtable">
* <tr><th>Standard Request</th><th>Request Processing Description</th><th>USB Spec Reference</th></tr>
* <tr>
* <td>CLEAR_FEATURE</td>
* <td>Clears or disables a specific feature. Support recipients: Device,
* Interface and Endpoint. The TEST_MODE feature selector is not supported.</td>
* <td>9.4.1</td>
* </tr>
* <tr>
* <td>GET_CONFIGURATION</td>
* <td>Returns the current device configuration value.</td>
* <td>9.4.2</td>
* </tr>
* <tr>
* <td>GET_DESCRIPTOR</td>
* <td>Returns the specified descriptor if the descriptor exists.</td>
* <td>9.4.3</td>
* </tr>
* <tr>
* <td>GET_INTERFACE</td>
* <td>Returns the selected alternate interface setting for the specified
* interface.</td>
* <td>9.4.4</td>
* </tr>
* <tr>
* <td>GET_STATUS</td>
* <td>Returns status for the specified recipient. Support recipients:
* Device, Interface, and Endpoint.</td>
* <td>9.4.5</td>
* </tr>
* <tr>
* <td>SET_ADDRESS</td>
* <td>Sets the device address for all future device accesses.</td>
* <td>9.4.6</td>
* </tr>
* <tr>
* <td>SET_CONFIGURATION</td>
* <td>Sets the device configuration. After this request, the device is ready for
* communication.</td>
* <td>9.4.7</td>
* </tr>
* <tr>
* <td>SET_DESCRIPTOR</td>
* <td>Not supported (optional request).</td>
* <td>9.4.8</td>
* </tr>
* <tr>
* <td>SET_FEATURE</td>
* <td>Enables a specific feature. Support recipients: Device, Interface, and Endpoint.
* The TEST_MODE feature selector is not supported.</td>
* <td>9.4.9</td>
* </tr>
* <tr>
* <td>SET_INTERFACE</td>
* <td>Allows the USB Host to select an alternate setting for the specified
* interface.</td>
* <td>9.4.10</td>
* </tr>
* <tr>
* <td>SYNCH_FRAME</td>
* <td>Not supported.</td>
* <td>9.4.11</td>
* </tr>
* </table>
*
********************************************************************************
* \subsection group_usb_dev_audio_class_info Audio Class
********************************************************************************
*
* The USB Audio class can be used in a large amount of applications,
* either Made for iPod (MFI) or general USB Audio based. These applications
* consist of, but are not limited to, speakers, microphones, headsets, music
* creation tools (DJ equipment, guitar jacks, etc), and mixers.
* An additional application for the Audio class is in Musical Instrument
* Digital Interface (MIDI) applications. This interface uses a digital UART-like
* interface and allows the information to be sent across to the Host to
* be used with software applications, such as Apple Garage Band. Various
* instruments, such as electronic pianos, interface with MIDI.
*
* A detailed description about Audio Class is provided by the
* [USB Implementers Forum (USB-IF) Class Documentation]
* (http://www.usb.org/developers/docs/devclass_docs/)
*
* The Audio Class does not provide support any of Audio v1.0 or v2.0 requests
* processing and provides only the API interface to register Audio request handlers
* implemented on the application level. However, \ref group_usb_dev_cfg_tool
* supports Audio v1.0 or v2.0 descriptors.
*
* \note
* The MIDI Class support is not available in this version.
*
********************************************************************************
* \subsection group_usb_dev_cdc_class_info CDC: Communication Device Class
********************************************************************************
*
* Common use case for this class in a PSoC design is to replace a legacy serial
* (RS232) COM port with a USB connection. This allows customers to use
* legacy serial software while updating the communication interface to
* something more readily available on today's computers. The type of data
* that might be streamed across USB can vary depending on the end application.
* This could be as simple as streaming raw ADC counts to an entire command
* protocol. Additionally, CDC is extremely useful for debug purposes.
* Users can easily develop a USB interface that can send and receive
* information across CDC. On the Host side, GUI applications are widely
* available to view the data being transmitted, such as TeraTerm, Terminal,
* or Hyper-Terminal (depending on version of Microsoft Windows).
*
* A detailed description about CDC class is provided by the
* [USB Implementers Forum (USB-IF) Class Documentation]
* (http://www.usb.org/developers/docs/devclass_docs/)
*
* The CDC Class supports requests listed in the table below.
* <table class="doxtable">
* <tr><th>Class Request</th><th>Request Processing Description</th><th>Communications Class Subclass Specification for PSTN Devices</th></tr>
* <tr>
* <td>SET_LINE_CODING</td>
* <td>Allows the host to specify typical asynchronous line-character
* formatting properties such as: data terminal rate, number of stop bits,
* parity type and number of data bits. It applies to data transfers both
* from the Host to the Device and from the Device to the Host.</td>
* <td>6.3.10</td>
* </tr>
* <tr>
* <td>GET_LINE_CODING</td>
* <td>Allows the host to discover the currently configured line coding.</td>
* <td>6.3.11</td>
* </tr>
* <tr>
* <td>SET_CONTROL_LINE_STATE</td>
* <td>Generates RS-232/V.24 style control signals - RTS and DTR.</td>
* <td>6.3.12</td>
* </tr>
* </table>
*
* The CDC Class supports notifications listed in the table below.
* <table class="doxtable">
* <tr><th>Class Notification</th><th>Notification Processing Description</th><th>Communications Class Subclass Specification for PSTN Devices</th></tr>
* <tr>
* <td>SERIAL_STATE</td>
* <td>Allows the Host to read the current state of the carrier detect (CD),
* DSR, break, and ring signal (RI).</td>
* <td>6.3.4</td>
* </tr>
* </table>
*
********************************************************************************
* \subsection group_usb_dev_hid_class_info HID: Human Interface Device
********************************************************************************
*
* There are many possible use cases for HID depending on the end application.
* A keyboard/keypad is a common HID application that has been implemented
* previously with PSoC. Additionally, customers can use PSoC to implement a
* PC mouse or game controller device. A more generic use case seen is with
* regards to customers using USB as general-purpose interface between PSoC and
* the Host, without conforming to specific USAGE such as a Keyboard, Mouse, etc.
* Instead the user configures the HID descriptor to be a generic device, which
* allows them to transfer Vendor-Specific information, such as ADC data,
* button presses, etc., across the HID protocol. This allows customers to
* perform custom/generic data transfers over USB, without needing to provide
* an INF or SYS file during enumeration or worry about WHQL certification.
* All this is accomplished using the HID drivers that are built into all
* modern operation systems today. This includes Windows, Mac, and Linux.
*
* A detailed description about HID is provided by the
* [USB Implementers Forum (USB-IF) Class Documentation]
* (http://www.usb.org/developers/docs/devclass_docs/)
*
* The HID Class supports requests listed in the table below.
* <table class="doxtable">
* <tr><th>Class Request</th><th>Request Processing Description</th><th>HID Spec Reference</th></tr>
* <tr>
* <td>GET_REPORT</td>
* <td>Allows the USB Host to receive a report via the control pipe.</td>
* <td>7.2.1</td>
* </tr>
* <tr>
* <td>SET_REPORT</td>
* <td>Allows the USB Host to send a report via the control pipe (set a
* state of input, output, or feature report controls).</td>
* <td>7.2.2</td>
* </tr>
* <tr>
* <td>GET_IDLE</td>
* <td>Reads the current idle rate for a particular Input report. \n
* The recommended default idle rate (rate when the device is initialized)
* is 500 milliseconds for keyboards (delay before first repeat rate) and
* infinity for joysticks and mice.</td>
* <td>7.2.3</td>
* </tr>
* <tr>
* <td>SET_IDLE</td>
* <td>Sets idle rate for a particular report. This request is used to limit
* the reporting frequency of an interrupt in endpoint.\n
* When the idle rate byte is 0 (zero), the duration is indefinite.
* The endpoint reports only when a change is detected in the report data.
* When the idle rate byte is non-zero, then a fixed duration is
* used (defined by idle rate).</td>
* <td>7.2.4</td>
* </tr>
* <tr>
* <td>GET_PROTOCOL</td>
* <td>Reads which protocol is currently active (either the boot or the
* report protocol).</td>
* <td>7.2.5</td>
* </tr>
* <tr>
* <td>SET_PROTOCOL</td>
* <td>Switches between the boot protocol and the report protocol (or vice versa).</td>
* <td>7.2.6</td>
* </tr>
* </table>
*
********************************************************************************
* \subsection group_usb_dev_class_x Adding Custom Class
********************************************************************************
*
* The USB Device middleware provides API interface that allows the user to
* implement custom class support. The middleware notifies registered class about
* following events:
* * Bus Reset detected ( \ref cy_cb_usb_dev_bus_reset_t ).
* * Set Configuration request received ( \ref cy_cb_usb_dev_set_config_t ).
* * Set Interface request received ( \ref cy_cb_usb_dev_set_interface_t ).
* * Setup packet received ( \ref cy_cb_usb_dev_request_received_t ).
* * Data is received in response for current setup packet ( \ref cy_cb_usb_dev_request_cmplt_t ).
*
* To create a new custom Class, follow the steps below:
* 1. Implement functions to service events (from the list above) required
* for class operation. Typically, class should implement service of class-specific
* requests therefore needs to implement functions defined by \ref cy_cb_usb_dev_request_received_t
* and \ref cy_cb_usb_dev_request_cmplt_t.
* 2. Initialize instance of \ref cy_stc_usb_dev_class_t structure using implemented
* service functions. Provide NULL pointer as function pointer if class does not
* use this event.
* 3. Allocate instance of \ref cy_stc_usb_dev_class_ll_item_t structure to
* provide storage for a linked list item of the class.
* 4. The class might need a context to store class-specific data. If needed,
* define context type specific for this class and allocate it.
* 5. To enable class operation, this needs to be registered using
* \ref Cy_USB_Dev_RegisterClass function after USB Device middleware is
* initialized.
*
* Any of supported classes can be taken as an example to implement a custom class.
*
********************************************************************************
* \subsection group_usb_dev_vendor Vendor-Specific Requests Support
********************************************************************************
*
* The vendor-specific requests are STALLed by USB Device middleware by default.
* The exception is MS OS String vendor-specific request to retrieve an OS Feature
* Descriptor (Note that USB device must contain MS OS String descriptor to handle
* this vendor-specific by middleware).
* The middleware provides the \ref Cy_USB_Dev_RegisterVendorCallbacks function
* to register callbacks to handle vendor-specific requests.
*
********************************************************************************
* \subsection group_usb_dev_ep_buf_alloc Allocate Data Endpoint Buffer
********************************************************************************
*
* The application allocates buffers for data endpoints to operate. The buffer
* allocation depends on USBFS driver endpoint buffer access type configuration.
* It specifies which hardware register set 8-bit or 16-bit is used to access
* hardware endpoints buffer. The 16-bit access requires that the specific rules for
* the endpoints buffer allocation must be met (See Hardware Buffer Access
* section of the USBFS driver for more information). \n
* To make endpoint buffer allocation configuration independent, use
* the \ref CY_USB_DEV_ALLOC_ENDPOINT_BUFFER macro.
*
********************************************************************************
* \subsection group_usb_dev_self_powered_dev Self-Powered Devices
********************************************************************************
*
* The USB Device responds to GET_STATUS requests based on the status set with
* the \ref Cy_USB_Dev_SetPowerStatus function. To set the correct status,
* \ref Cy_USB_Dev_SetPowerStatus must be called during initialization if USB
* Device is configured as self-powered. The \ref Cy_USB_Dev_SetPowerStatus must
* be called any time the device changes status. A self-powered device also requires
* monitoring VBUS to control pull-up resistors. The pull-up resistor does
* not supply power to the data line until you call \ref Cy_USB_Dev_Connect.
* \ref Cy_USB_Dev_Disconnect disconnects the pull-up resistor from the data line.
* Find information about how to add VBUS monitoring in your application in the USBFS driver
* section VBUS Detection in the PDL API Reference Manual.
*
********************************************************************************
* \subsection group_usb_dev_std_timeout Timeout Function Redefinition
********************************************************************************
*
* The USB Device middleware provides following blocking functions: \ref Cy_USB_Dev_ReadEpBlocking,
* \ref Cy_USB_Dev_WriteEpBlocking, and \ref Cy_USB_Dev_Connect (the behavior
* defined by the blocking parameter of the connect function). The blocking functions
* parameter timeout defines how many milliseconds to wait before timeout.
* The SysLib driver function Cy_SysLib_Delay is used to implement a 1 millisecond
* wait cycle. The middleware provides function \ref Cy_USB_Dev_OverwriteHandleTimeout
* that allows overriding the wait function implementation. This might be
* useful when an operation system is used in your application.
*
* \note
* The blocking function must be used carefully to not cause application lock-up.
* The preferred solution is using non-blocking functions.
*
********************************************************************************
* \subsection group_usb_dev_drv_features Driver Features
********************************************************************************
*
* There are driver features that do not have corresponding an API
* interface provided in the middleware. However, the application might need
* these features for USB Device implementation. If there is such a need, the
* driver functions must be used. These features are listed below:
* * Data Endpoint 1 - 8 Completion, SOF received and LPM transfer ACKed events
* notification.
* * Low power support: Suspend / Resume, Remote wakeup signaling.
* * Link Power Management (LPM) support.
*
* Find more information in the appropriate section of the USBFS driver documentation
* provided in the PDL API Reference Manual.
*
********************************************************************************
* \section section_usb_dev_toolchain Supported Software and Tools
********************************************************************************
*
* For supported software and tools, refer to the Supported Software and Tools
* section in in the
* [RELEASE.md](https://github.com/Infineon/usbdev/blob/master/RELEASE.md#supported-software-and-tools)
*
********************************************************************************
* \section section_usb_dev_errata Errata
********************************************************************************
*
* This section lists the known problems with the USB Device middleware.
*
* <table class="doxtable">
* <tr><th>Cypress ID</th><th>Known Issue</th><th>Workaround</th></tr>
* <tr>
* <td>DRIVERS-1401</td>
* <td>
* The USB Device ignores LPM requests after wake up from Deep Sleep.
* </td>
* <td>
* Call USBFS driver Cy_USBFS_Dev_Drv_Lpm_SetResponse() after calling
* Cy_USBFS_Dev_Drv_Resume() to restore response to the LPM packets.
* </td>
* </tr>
* <tr>
* <td>DRIVERS-1427</td>
* <td>
* The USB Device modes with DMA do not work after wake up from Deep
* Sleep, due to incorrect restore of the ARB_CFG register.
* </td>
* <td>
* Save ARB_CFG values before entering Deep Sleep and restore it after
* calling of Cy_USBFS_Dev_Drv_Resume.
* \snippet usb_dev/snippet/main.c snippet_WorkaroundDmaResume
* </td>
* </tr>
* </table>
*
********************************************************************************
* \section group_usb_dev_changelog Changelog
********************************************************************************
*
* <table class="doxtable">
* <tr><th>Version</th><th>Changes</th><th>Reason for Change</th></tr>
* <tr>
* <td rowspan="4">2.10</td>
* <td>Updated Documentation</td>
* <td>PMG1 Integration</td>
* </tr>
* <tr>
* <td>Fixed vendor request handling</td>
* <td>Defect Fixes</td>
* </tr>
* <tr>
* <td>Added support for configurations without any data endpoints</td>
* <td>Defect Fixes </td>
* </tr>
* <tr>
* <td>Minor enhancements in several functions.</td>
* <td>Enabled compliance with MISRA-C:2012 standard.</td>
* </tr>
* </tr>
* <tr>
* <td rowspan="5">2.0</td>
* <td>Updated the internal processing to support USBFS driver updates.</td>
* <td>The USBFS driver is updated to v2.0.</td>
* </tr>
* <tr>
* <td>Moved the timeout from Cy_USB_Dev_AbortEpTransfer() to the driver layer.
* The maximum function's wait time is significantly reduced.</td>
* <td>Align with the changes of the USBFS driver.</td>
* </tr>
* <tr>
* <td>Changed the functions parameter name and the structure member name class to classObj.</td>
* <td>Fixed the ambiguity related to the usage of the C++ keyword class as a name.</td>
* </tr>
* <tr>
* <td>Enclosed middleware sources within a conditional compilation to
* exclude the USB Device middleware for devices without USB hardware.</td>
* <td>Fixed a compilation error for devices without USB hardware.</td>
* </tr>
* <tr>
* <td>Updated the major and minor version defines to follow the naming convention.</td>
* <td></td>
* </tr>
* <tr>
* <td>1.0</td>
* <td>The initial version.</td>
* <td></td>
* </tr>
* </table>
*
********************************************************************************
* \section group_usb_dev_more_information More Information
********************************************************************************
*
* For more information, refer to the links in the
* [README.md](https://github.com/Infineon/usbdev/blob/master/README.md#more-information)
*
* \note
* The links to the other software component's documentation (middleware and PDL)
* point to GitHub to the software latest available version.
* To get documentation of the specified version, download from GitHub and unzip
* the component archive. The documentation is available in the <i>docs</i> folder.
*
********************************************************************************
* \defgroup group_usb_device Device
* This section provides an API description for the core functionality of the
* USB Device: initialization, status information, data transfers, and class
* support.
* \{
* \defgroup group_usb_dev_macros Macros
* \{
* \defgroup group_usb_dev_macros_device_descr Descriptors
* \}
*
* \defgroup group_usb_dev_functions Functions
* \{
* \defgroup group_usb_dev_functions_common Initialization Functions
* \defgroup group_usb_dev_functions_service Service Functions
* \defgroup group_usb_dev_functions_data_transfer Data Transfer Functions
* \defgroup group_usb_dev_functions_vendor_support Vendor Request Support Functions
* \defgroup group_usb_dev_functions_class_support Class Support Functions
* \}
*
* \defgroup group_usb_dev_data_structures Data Structures
* \{
* \defgroup group_usb_dev_structures_device Device Information
* \defgroup group_usb_dev_structures_device_descr Device Descriptors
* \defgroup group_usb_dev_structures_control Control Transfer
* \defgroup group_usb_dev_structures_class Class Support
* \defgroup group_usb_dev_structures_func_ptr Function Pointers
* \}
*
* \defgroup group_usb_dev_enums Enumerated Types
* \}
*
* \defgroup group_usb_dev_audio Audio Class
* \defgroup group_usb_dev_cdc CDC Class
* \defgroup group_usb_dev_hid HID Class
*
*/
#if !defined(CY_USB_DEV_H)
#define CY_USB_DEV_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <string.h>
#include "cy_usbfs_dev_drv.h"
#include "cy_usb_dev_descr.h"
#if (defined(CY_IP_MXUSBFS) || defined(CY_IP_M0S8USBDSS))
#if defined(__cplusplus)
extern "C" {
#endif
/**
* \addtogroup group_usb_dev_macros
* \{
*/
/*******************************************************************************
* Middleware Version
*******************************************************************************/
/** USB Device Middleware major version */
#define CY_USB_DEV_MW_VERSION_MAJOR (2)
/** USB Device Middleware minor version */
#define CY_USB_DEV_MW_VERSION_MINOR (10)
/** USB Device Middleware identifier */
#define CY_USB_DEV_ID CY_PDL_DRV_ID(0x08U)
/** Maximum number of interfaces (this equals to the maximum number of hardware
* endpoints where each interface has at least one endpoint).
*/
#define CY_USB_DEV_NUM_INTERFACES_MAX CY_USBFS_DEV_DRV_NUM_EPS_MAX
/** USBFS Device endpoint 0 packet size */
#define CY_USB_DEV_EP0_PACKET_SIZE CY_USBFS_DEV_DRV_EP0_BUFFER_SIZE
/** Length of serial string number generated from silicon ID */
#define CY_USB_DEV_SN_STRING_LENGTH (32U)
/** Length of serial string number */
#define CY_USB_DEV_SN_STRING_DESR_LENGTH (CY_USB_DEV_SN_STRING_LENGTH + 2U)
/** \} group_usb_dev_macros */
/*******************************************************************************
* Enumerated Types
*******************************************************************************/
/**
* \addtogroup group_usb_dev_enums
* \{
*/
/** USBFS Device return codes */
typedef enum
{
/** Operation completed successfully */
CY_USB_DEV_SUCCESS = 0U,
/** One or more input parameters are invalid */
CY_USB_DEV_BAD_PARAM = (CY_USB_DEV_ID | CY_PDL_STATUS_ERROR | 1U),
/** The request is not handled */
CY_USB_DEV_REQUEST_NOT_HANDLED = (CY_USB_DEV_ID | CY_PDL_STATUS_ERROR | 2U),
/** Timeout occurred for the operation */
CY_USB_DEV_TIMEOUT = (CY_USB_DEV_ID | CY_PDL_STATUS_ERROR | 3U),
/** The hardware is busy executing previous operation */
CY_USB_DEV_DRV_HW_BUSY = (CY_USB_DEV_ID | CY_PDL_STATUS_ERROR | 4U),
/** The hardware is disabled */
CY_USB_DEV_DRV_HW_DISABLED = (CY_USB_DEV_ID | CY_PDL_STATUS_ERROR | 5U),
/** The hardware error occurred during operation */
CY_USB_DEV_DRV_HW_ERROR = (CY_USB_DEV_ID | CY_PDL_STATUS_ERROR | 6U),
} cy_en_usb_dev_status_t;
/** Device states */
typedef enum
{
CY_USB_DEV_DISABLED, /**< Device is disabled */
CY_USB_DEV_ATTACHED, /**< Device is attached */
CY_USB_DEV_POWERED, /**< Device is powered */
CY_USB_DEV_DEFAULT, /**< Device is in default state (after bus reset) */
CY_USB_DEV_ADDRESSED, /**< Device set address (device is ready to communicate using this address) */
CY_USB_DEV_CONFIGURED /**< Device is configured (device is ready to communicate using data endpoints) */
} cy_en_usb_dev_state_t;
/** Power Status */
typedef enum
{
CY_USB_DEV_STATUS_BUS_POWERED, /**< Device is bus powered */
CY_USB_DEV_STATUS_SELF_POWERED /**< Device is self powered */
} cy_en_usb_dev_power_status_t;
/** Callback events */
typedef enum
{
CY_USB_DEV_EVENT_BUS_RESET, /**< Bus Reset detected */
CY_USB_DEV_EVENT_SET_CONFIG, /**< SET_CONFIGURATION request received */
CY_USB_DEV_EVENT_SET_INTERFACE /**< SET_INTERFACE request received */
} cy_en_usb_dev_callback_events_t;
/** \} group_usb_dev_enums */
/*******************************************************************************
* Type Definitions
*******************************************************************************/
/**
* \addtogroup group_usb_dev_structures_control
* \{
*/
/** Parsed bmRequest */
typedef struct
{
uint8_t direction; /**< Transfer direction */
uint8_t type; /**< Transfer type */
uint8_t recipient; /**< Transfer recipient */
} cy_stc_usb_dev_bm_request;
/** Store request */
typedef struct
{
cy_stc_usb_dev_bm_request bmRequestType; /**< Request type */
uint8_t bRequest; /**< Request value */
uint16_t wValue; /**< Request value (2 bytes) */
uint16_t wIndex; /**< Request index (2 bytes) */
uint16_t wLength; /**< Request length (2 bytes) */
} cy_stc_usb_dev_setup_packet_t;
/** Execute transfer use this structure */
typedef struct
{
cy_stc_usb_dev_setup_packet_t setup; /**< Request packet */
uint8_t *ptr; /**< Pointer to data to transfer or space to store data */
uint8_t *buffer; /**< Pointer to buffer to store data send from host to device during control transfer */
uint16_t remaining; /**< Number of bytes remaining to complete send or receive */
uint16_t size; /**< Number of bytes to send or receive */
uint16_t bufferSize; /**< Size of the buffer to store data */
uint8_t direction; /**< Transfer direction */
bool zlp; /**< Defines whether zero length packet is needed to complete data stage */
bool notify; /**< Defines whether trigger callback that notifies end of data stage (device received data) */
} cy_stc_usb_dev_control_transfer_t;
/** \} group_usb_dev_structures_control */
/** \cond INTERNAL */
/** Get compiler know what is cy_stc_usb_dev_context.
* The cy_stc_usb_dev_context_t is defined below.
*/
struct cy_stc_usb_dev_context;
/** \endcond */
/**
* \addtogroup group_usb_dev_structures_func_ptr
* \{
*/
/**
* Pointer to a function that implements a callback for the following events:
* Bus Reset detected, SET_CONFIGURATION request received and SET_INTERFACE request
* received. The notified events are defined by event argument
* (see \ref cy_en_usb_dev_callback_events_t).
* Only one event is notified when callback invoked.
* This callback invoked in the USB interrupt handler context and must be short
* as possible. \n
* Input arguments:
* * Bus Reset detected event: ignore wValue and wIndex arguments.
* * SET_CONFIGURATION request received event: the wValue is new configuration
* value and wIndex can be ignored.
* * SET_CONFIGURATION request received event: the wValue is new alternative
* setting value and wIndex interface value to which alternate belongs. \n
*
* The returned status defines following operation:
* * Bus Reset detected event: status is ignored and event processing is passed to
* class event handlers.
* * SET_CONFIGURATION or SET_INTERFACE received event: the returned status defines
* following request processing. If returned status fail the request is
* STALLed and event notification stops.
* If returned status is success the event is notified to the appropriate class
* event handlers which status defines request response STALL or ACK.
*/
typedef cy_en_usb_dev_status_t (* cy_cb_usb_dev_events_callback_t)(cy_en_usb_dev_callback_events_t event,
uint32_t wValue, uint32_t wIndex,
struct cy_stc_usb_dev_context *devContext);
/**
* Pointer to a function that implements a timeout handler. The function accepts the
* number of milliseconds that remain to wait before timeout expires.
* This returns the number of milliseconds remaining to wait minus the milliseconds
* that have elapsed inside the function.
*/
typedef int32_t (* cy_fn_usb_dev_handle_timeout_ptr_t)(int32_t milliseconds);
/**
* Pointer to function that returns pointer to uint8_t array, which contains a serial number string
* and accepts the device context \ref cy_stc_usb_dev_context_t.
* This type is used to define a callback that returns serial number string.
*/
typedef uint8_t * (* cy_fn_usb_dev_sn_string_ptr_t)(void);
/**
* Pointer to function that returns nothing and accepts a pointer to void and a pointer to \ref cy_stc_usb_dev_context_t.
* This type is used to define a callback that notifies the class about a bus reset event.
*/
typedef void (* cy_cb_usb_dev_bus_reset_t)(void *classData, struct cy_stc_usb_dev_context *devContext);
/**
* Pointer to function that returns the status of operation and accepts received configuration number,
* pointer to class context, and pointer to device context \ref cy_stc_usb_dev_context_t.
* This type is used to define a callback that notifies the class about a set configuration request event.
*/
typedef cy_en_usb_dev_status_t (* cy_cb_usb_dev_set_config_t)(uint32_t configuration, void *classData,
struct cy_stc_usb_dev_context *devContext);
/**
* Pointer to function that returns the status of operation and accepts received interface
* alternate settings number, pointer to class context, and pointer to device context \ref cy_stc_usb_dev_context_t.
* This type is used to define a callback that notifies the class about a set interface request event.
*/
typedef cy_en_usb_dev_status_t (* cy_cb_usb_dev_set_interface_t)(uint32_t interface, uint32_t alternate,
void *classContext,
struct cy_stc_usb_dev_context *devContext);
/**
* Pointer to function that returns the status of operation and accepts pointer to received control transfer
* number, pointer to class context, and pointer to device context \ref cy_stc_usb_dev_context_t.
* This type is used to define a callback that notifies the class about a request received event.
*/
typedef cy_en_usb_dev_status_t (* cy_cb_usb_dev_request_received_t)(cy_stc_usb_dev_control_transfer_t *transfer,
void *classContext,
struct cy_stc_usb_dev_context *devContext);
/**
* Pointer to function that returns status of operation and accepts pointer to received control transfer
* number, pointer to class context, and pointer to device context \ref cy_stc_usb_dev_context_t.
* This type is used to define a callback that notifies the class that a request completed (data is received in
* response for current request) event.
*/
typedef cy_en_usb_dev_status_t (* cy_cb_usb_dev_request_cmplt_t)(cy_stc_usb_dev_control_transfer_t *transfer,
void *classContext,
struct cy_stc_usb_dev_context *devContext);
/** \} group_usb_dev_structures_func_ptr */
/**
* \addtogroup group_usb_dev_structures_class
* \{
*/