1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: USB Core Module
7 *----------------------------------------------------------------------------
8 * This software is supplied "AS IS" without any warranties, express,
9 * implied or statutory, including but not limited to the implied
10 * warranties of fitness for purpose, satisfactory quality and
11 * noninfringement. Keil extends you a royalty-free right to reproduce
12 * and distribute executable files created using this software for use
13 * on NXP Semiconductors LPC family microcontroller devices only. Nothing
14 * else gives you the right to use this software.
16 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
17 *----------------------------------------------------------------------------
19 * V1.20 Added vendor specific requests
20 * Changed string descriptor handling
22 * V1.00 Initial Version
23 *----------------------------------------------------------------------------*/
25 #include "lpc_types.h"
64 #pragma diag_suppress 111,177,1441
67 #if defined ( __GNUC__ )
68 #define __packed __attribute__((__packed__))
71 uint16_t USB_DeviceStatus;
72 uint8_t USB_DeviceAddress;
73 uint8_t USB_Configuration;
74 uint32_t USB_EndPointMask;
75 uint32_t USB_EndPointHalt;
76 uint32_t USB_EndPointStall; /* EP must stay stalled */
77 uint8_t USB_NumInterfaces;
78 uint8_t USB_AltSetting[USB_IF_NUM];
83 uint8_t EP0Buf[USB_MAX_PACKET0];
84 USB_SETUP_PACKET SetupPacket;
86 extern volatile uint32_t DevStatusFS2HS;
94 void USB_ResetCore (void) {
96 USB_DeviceStatus = USB_POWER;
97 USB_DeviceAddress = 0;
98 USB_Configuration = 0;
99 USB_EndPointMask = 0x00010001;
100 USB_EndPointHalt = 0x00000000;
101 USB_EndPointStall = 0x00000000;
106 * USB Request - Setup Stage
107 * Parameters: None (global SetupPacket)
111 void USB_SetupStage (void) {
112 USB_ReadSetupPkt(0x00, (uint32_t *)&SetupPacket);
117 * USB Request - Data In Stage
118 * Parameters: None (global EP0Data)
122 void USB_DataInStage (void) {
125 if (EP0Data.Count > USB_MAX_PACKET0) {
126 cnt = USB_MAX_PACKET0;
130 cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
131 EP0Data.pData += cnt;
132 EP0Data.Count -= cnt;
137 * USB Request - Data Out Stage
138 * Parameters: None (global EP0Data)
142 void USB_DataOutStage (void) {
145 cnt = USB_ReadEP(0x00, EP0Data.pData);
146 EP0Data.pData += cnt;
147 EP0Data.Count -= cnt;
152 * USB Request - Status In Stage
157 void USB_StatusInStage (void) {
158 USB_WriteEP(0x80, NULL, 0);
163 * USB Request - Status Out Stage
168 void USB_StatusOutStage (void) {
169 USB_ReadEP(0x00, EP0Buf);
174 * Get Status USB Request
175 * Parameters: None (global SetupPacket)
176 * Return Value: TRUE - Success, FALSE - Error
179 INLINE uint32_t USB_ReqGetStatus (void) {
182 switch (SetupPacket.bmRequestType.BM.Recipient) {
183 case REQUEST_TO_DEVICE:
184 EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
186 case REQUEST_TO_INTERFACE:
187 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
188 *((__packed uint16_t *)EP0Buf) = 0;
189 EP0Data.pData = EP0Buf;
194 case REQUEST_TO_ENDPOINT:
195 n = SetupPacket.wIndex.WB.L & 0x8F;
196 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
197 if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
198 *((__packed uint16_t *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
199 EP0Data.pData = EP0Buf;
212 * Set/Clear Feature USB Request
213 * Parameters: sc: 0 - Clear, 1 - Set
214 * (global SetupPacket)
215 * Return Value: TRUE - Success, FALSE - Error
218 INLINE uint32_t USB_ReqSetClrFeature (uint32_t sc) {
221 switch (SetupPacket.bmRequestType.BM.Recipient) {
222 case REQUEST_TO_DEVICE:
223 if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
226 USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
228 USB_WakeUpCfg(FALSE);
229 USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
231 } else if (SetupPacket.wValue.W == USB_FEATURE_TEST_MODE) {
232 return USB_SetTestMode(SetupPacket.wIndex.WB.H);
237 case REQUEST_TO_INTERFACE:
239 case REQUEST_TO_ENDPOINT:
240 n = SetupPacket.wIndex.WB.L & 0x8F;
241 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
242 if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
243 if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
246 USB_EndPointHalt |= m;
248 if ((USB_EndPointStall & m) != 0) {
253 if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
254 /* Compliance Test: rewrite CSW after unstall */
255 if (CSW.dSignature == MSC_CSW_Signature) {
256 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
260 USB_EndPointHalt &= ~m;
277 * Set Address USB Request
278 * Parameters: None (global SetupPacket)
279 * Return Value: TRUE - Success, FALSE - Error
282 INLINE uint32_t USB_ReqSetAddress (void) {
284 switch (SetupPacket.bmRequestType.BM.Recipient) {
285 case REQUEST_TO_DEVICE:
286 USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
296 * Get Descriptor USB Request
297 * Parameters: None (global SetupPacket)
298 * Return Value: TRUE - Success, FALSE - Error
301 INLINE uint32_t USB_ReqGetDescriptor (void) {
305 switch (SetupPacket.bmRequestType.BM.Recipient) {
306 case REQUEST_TO_DEVICE:
307 switch (SetupPacket.wValue.WB.H) {
308 case USB_DEVICE_DESCRIPTOR_TYPE:
309 EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
310 len = USB_DEVICE_DESC_SIZE;
312 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
313 if ( DevStatusFS2HS == FALSE ) {
314 pD = (uint8_t *)USB_FSConfigDescriptor;
316 pD = (uint8_t *)USB_HSConfigDescriptor;
318 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
319 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
320 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
323 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
327 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
329 case USB_STRING_DESCRIPTOR_TYPE:
330 pD = (uint8_t *)USB_StringDescriptor;
331 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
332 if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
333 pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
336 if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
340 len = ((USB_STRING_DESCRIPTOR *)pD)->bLength;
342 case USB_DEVICE_QUALIFIER_DESCRIPTOR_TYPE:
343 /* USB Chapter 9. page 9.6.2 */
344 if ( DevStatusFS2HS == FALSE ) {
349 EP0Data.pData = (uint8_t *)USB_DeviceQualifier;
350 len = USB_DEVICE_QUALI_SIZE;
353 case USB_OTHER_SPEED_CONFIG_DESCRIPTOR_TYPE:
354 if ( DevStatusFS2HS == TRUE ) {
355 pD = (uint8_t *)USB_FSOtherSpeedConfiguration;
357 pD = (uint8_t *)USB_HSOtherSpeedConfiguration;
360 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
361 if (((USB_OTHER_SPEED_CONFIGURATION *)pD)->bLength != 0) {
362 pD += ((USB_OTHER_SPEED_CONFIGURATION *)pD)->wTotalLength;
365 if (((USB_OTHER_SPEED_CONFIGURATION *)pD)->bLength == 0) {
369 len = ((USB_OTHER_SPEED_CONFIGURATION *)pD)->wTotalLength;
375 case REQUEST_TO_INTERFACE:
376 switch (SetupPacket.wValue.WB.H) {
378 case HID_HID_DESCRIPTOR_TYPE:
379 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
380 return (FALSE); /* Only Single HID Interface is supported */
382 if ( DevStatusFS2HS == FALSE ) {
383 EP0Data.pData = (uint8_t *)USB_FSConfigDescriptor + HID_DESC_OFFSET;
385 EP0Data.pData = (uint8_t *)USB_HSConfigDescriptor + HID_DESC_OFFSET;
389 case HID_REPORT_DESCRIPTOR_TYPE:
390 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
391 return (FALSE); /* Only Single HID Interface is supported */
393 EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
394 len = HID_ReportDescSize;
396 case HID_PHYSICAL_DESCRIPTOR_TYPE:
397 return (FALSE); /* HID Physical Descriptor is not supported */
407 if (EP0Data.Count > len) {
416 * Get Configuration USB Request
417 * Parameters: None (global SetupPacket)
418 * Return Value: TRUE - Success, FALSE - Error
421 INLINE uint32_t USB_ReqGetConfiguration (void) {
423 switch (SetupPacket.bmRequestType.BM.Recipient) {
424 case REQUEST_TO_DEVICE:
425 EP0Data.pData = &USB_Configuration;
435 * Set Configuration USB Request
436 * Parameters: None (global SetupPacket)
437 * Return Value: TRUE - Success, FALSE - Error
440 INLINE uint32_t USB_ReqSetConfiguration (void) {
441 USB_COMMON_DESCRIPTOR *pD;
445 switch (SetupPacket.bmRequestType.BM.Recipient) {
446 case REQUEST_TO_DEVICE:
448 if (SetupPacket.wValue.WB.L) {
449 if ( DevStatusFS2HS == FALSE ) {
450 pD = (USB_COMMON_DESCRIPTOR *)USB_FSConfigDescriptor;
452 pD = (USB_COMMON_DESCRIPTOR *)USB_HSConfigDescriptor;
454 while (pD->bLength) {
455 switch (pD->bDescriptorType) {
456 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
457 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
458 USB_Configuration = SetupPacket.wValue.WB.L;
459 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
460 for (n = 0; n < USB_IF_NUM; n++) {
461 USB_AltSetting[n] = 0;
463 for (n = 1; n < USB_EP_NUM; n++) {
464 if (USB_EndPointMask & (1 << n)) {
467 if (USB_EndPointMask & ((1 << 16) << n)) {
468 USB_DisableEP(n | 0x80);
471 USB_EndPointMask = 0x00010001;
472 USB_EndPointHalt = 0x00000000;
473 USB_EndPointStall= 0x00000000;
475 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
476 USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
478 USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
481 new_addr = (uint32_t)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
482 pD = (USB_COMMON_DESCRIPTOR*)new_addr;
486 case USB_INTERFACE_DESCRIPTOR_TYPE:
487 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
489 case USB_ENDPOINT_DESCRIPTOR_TYPE:
491 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
492 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
493 USB_EndPointMask |= m;
494 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
500 new_addr = (uint32_t)pD + pD->bLength;
501 pD = (USB_COMMON_DESCRIPTOR*)new_addr;
505 USB_Configuration = 0;
506 for (n = 1; n < USB_EP_NUM; n++) {
507 if (USB_EndPointMask & (1 << n)) {
510 if (USB_EndPointMask & ((1 << 16) << n)) {
511 USB_DisableEP(n | 0x80);
514 USB_EndPointMask = 0x00010001;
515 USB_EndPointHalt = 0x00000000;
516 USB_EndPointStall = 0x00000000;
517 USB_Configure(FALSE);
520 if (USB_Configuration != SetupPacket.wValue.WB.L) {
532 * Get Interface USB Request
533 * Parameters: None (global SetupPacket)
534 * Return Value: TRUE - Success, FALSE - Error
537 INLINE uint32_t USB_ReqGetInterface (void) {
539 switch (SetupPacket.bmRequestType.BM.Recipient) {
540 case REQUEST_TO_INTERFACE:
541 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
542 EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
555 * Set Interface USB Request
556 * Parameters: None (global SetupPacket)
557 * Return Value: TRUE - Success, FALSE - Error
560 INLINE uint32_t USB_ReqSetInterface (void) {
561 USB_COMMON_DESCRIPTOR *pD;
562 uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
564 uint32_t set, new_addr;
566 switch (SetupPacket.bmRequestType.BM.Recipient) {
567 case REQUEST_TO_INTERFACE:
568 if (USB_Configuration == 0) return (FALSE);
570 if ( DevStatusFS2HS == FALSE ) {
571 pD = (USB_COMMON_DESCRIPTOR *)USB_FSConfigDescriptor;
573 pD = (USB_COMMON_DESCRIPTOR *)USB_HSConfigDescriptor;
575 while (pD->bLength) {
576 switch (pD->bDescriptorType) {
577 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
578 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
579 new_addr = (uint32_t)pD + ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
580 pD = (USB_COMMON_DESCRIPTOR*)new_addr;
584 case USB_INTERFACE_DESCRIPTOR_TYPE:
585 ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
586 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
588 if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
590 old = USB_AltSetting[ifn];
591 USB_AltSetting[ifn] = (uint8_t)alt;
594 case USB_ENDPOINT_DESCRIPTOR_TYPE:
595 if (ifn == SetupPacket.wIndex.WB.L) {
596 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
597 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
598 if (alt == SetupPacket.wValue.WB.L) {
599 USB_EndPointMask |= m;
600 USB_EndPointHalt &= ~m;
601 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
606 else if ((alt == old) && ((msk & m) == 0)) {
607 USB_EndPointMask &= ~m;
608 USB_EndPointHalt &= ~m;
614 new_addr = (uint32_t)pD + pD->bLength;
615 pD = (USB_COMMON_DESCRIPTOR*)new_addr;
627 * USB Endpoint 0 Event Callback
632 void USB_EndPoint0 (uint32_t event) {
637 USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
638 EP0Data.Count = SetupPacket.wLength; /* Number of bytes to transfer */
639 switch (SetupPacket.bmRequestType.BM.Type) {
641 case REQUEST_STANDARD:
642 switch (SetupPacket.bRequest) {
643 case USB_REQUEST_GET_STATUS:
644 if (!USB_ReqGetStatus()) {
650 case USB_REQUEST_CLEAR_FEATURE:
651 if (!USB_ReqSetClrFeature(0)) {
655 #if USB_FEATURE_EVENT
660 case USB_REQUEST_SET_FEATURE:
661 if (!USB_ReqSetClrFeature(1)) {
665 #if USB_FEATURE_EVENT
670 case USB_REQUEST_SET_ADDRESS:
671 if (!USB_ReqSetAddress()) {
677 case USB_REQUEST_GET_DESCRIPTOR:
678 if (!USB_ReqGetDescriptor()) {
684 case USB_REQUEST_SET_DESCRIPTOR:
685 /*stall_o:*/ USB_SetStallEP(0x00); /* not supported */
689 case USB_REQUEST_GET_CONFIGURATION:
690 if (!USB_ReqGetConfiguration()) {
696 case USB_REQUEST_SET_CONFIGURATION:
697 if (!USB_ReqSetConfiguration()) {
701 #if USB_CONFIGURE_EVENT
702 USB_Configure_Event();
706 case USB_REQUEST_GET_INTERFACE:
707 if (!USB_ReqGetInterface()) {
713 case USB_REQUEST_SET_INTERFACE:
714 if (!USB_ReqSetInterface()) {
718 #if USB_INTERFACE_EVENT
719 USB_Interface_Event();
726 break; /* end case REQUEST_STANDARD */
730 switch (SetupPacket.bmRequestType.BM.Recipient) {
732 case REQUEST_TO_DEVICE:
733 goto stall_i; /* not supported */
735 case REQUEST_TO_INTERFACE:
737 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
738 switch (SetupPacket.bRequest) {
739 case HID_REQUEST_GET_REPORT:
740 if (HID_GetReport()) {
741 EP0Data.pData = EP0Buf; /* point to data to be sent */
742 USB_DataInStage(); /* send requested data */
746 case HID_REQUEST_SET_REPORT:
747 EP0Data.pData = EP0Buf; /* data to be received */
749 case HID_REQUEST_GET_IDLE:
751 EP0Data.pData = EP0Buf; /* point to data to be sent */
752 USB_DataInStage(); /* send requested data */
756 case HID_REQUEST_SET_IDLE:
758 USB_StatusInStage(); /* send Acknowledge */
762 case HID_REQUEST_GET_PROTOCOL:
763 if (HID_GetProtocol()) {
764 EP0Data.pData = EP0Buf; /* point to data to be sent */
765 USB_DataInStage(); /* send requested data */
769 case HID_REQUEST_SET_PROTOCOL:
770 if (HID_SetProtocol()) {
771 USB_StatusInStage(); /* send Acknowledge */
779 if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) { /* IF number correct? */
780 switch (SetupPacket.bRequest) {
781 case MSC_REQUEST_RESET:
782 if ((SetupPacket.wValue.W == 0) && /* RESET with invalid parameters -> STALL */
783 (SetupPacket.wLength == 0)) {
790 case MSC_REQUEST_GET_MAX_LUN:
791 if ((SetupPacket.wValue.W == 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
792 (SetupPacket.wLength == 1)) {
793 if (MSC_GetMaxLUN()) {
794 EP0Data.pData = EP0Buf;
804 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
805 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
806 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
807 switch (SetupPacket.bRequest) {
808 case AUDIO_REQUEST_GET_CUR:
809 case AUDIO_REQUEST_GET_MIN:
810 case AUDIO_REQUEST_GET_MAX:
811 case AUDIO_REQUEST_GET_RES:
812 if (ADC_IF_GetRequest()) {
813 EP0Data.pData = EP0Buf; /* point to data to be sent */
814 USB_DataInStage(); /* send requested data */
818 case AUDIO_REQUEST_SET_CUR:
819 // case AUDIO_REQUEST_SET_MIN:
820 // case AUDIO_REQUEST_SET_MAX:
821 // case AUDIO_REQUEST_SET_RES:
822 EP0Data.pData = EP0Buf; /* data to be received */
826 #endif /* USB_AUDIO */
828 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
829 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
830 switch (SetupPacket.bRequest) {
831 case CDC_SEND_ENCAPSULATED_COMMAND:
832 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
834 case CDC_GET_ENCAPSULATED_RESPONSE:
835 if (CDC_GetEncapsulatedResponse()) {
836 EP0Data.pData = EP0Buf; /* point to data to be sent */
837 USB_DataInStage(); /* send requested data */
841 case CDC_SET_COMM_FEATURE:
842 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
844 case CDC_GET_COMM_FEATURE:
845 if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
846 EP0Data.pData = EP0Buf; /* point to data to be sent */
847 USB_DataInStage(); /* send requested data */
851 case CDC_CLEAR_COMM_FEATURE:
852 if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
853 USB_StatusInStage(); /* send Acknowledge */
857 case CDC_SET_LINE_CODING:
858 EP0Data.pData = EP0Buf; /* data to be received, see USB_EVT_OUT */
860 case CDC_GET_LINE_CODING:
861 if (CDC_GetLineCoding()) {
862 EP0Data.pData = EP0Buf; /* point to data to be sent */
863 USB_DataInStage(); /* send requested data */
867 case CDC_SET_CONTROL_LINE_STATE:
868 if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
869 USB_StatusInStage(); /* send Acknowledge */
874 if (CDC_SendBreak(SetupPacket.wValue.W)) {
875 USB_StatusInStage(); /* send Acknowledge */
882 goto stall_i; /* not supported */
883 /* end case REQUEST_TO_INTERFACE */
885 case REQUEST_TO_ENDPOINT:
887 switch (SetupPacket.bRequest) {
888 case AUDIO_REQUEST_GET_CUR:
889 case AUDIO_REQUEST_GET_MIN:
890 case AUDIO_REQUEST_GET_MAX:
891 case AUDIO_REQUEST_GET_RES:
892 if (ADC_EP_GetRequest()) {
893 EP0Data.pData = EP0Buf; /* point to data to be sent */
894 USB_DataInStage(); /* send requested data */
898 case AUDIO_REQUEST_SET_CUR:
899 // case AUDIO_REQUEST_SET_MIN:
900 // case AUDIO_REQUEST_SET_MAX:
901 // case AUDIO_REQUEST_SET_RES:
902 EP0Data.pData = EP0Buf; /* data to be received */
905 #endif /* USB_AUDIO */
907 /* end case REQUEST_TO_ENDPOINT */
912 setup_class_ok: /* request finished successfully */
913 break; /* end case REQUEST_CLASS */
914 #endif /* USB_CLASS */
918 switch (SetupPacket.bmRequestType.BM.Recipient) {
920 case REQUEST_TO_DEVICE:
921 if (!USB_ReqVendorDev(TRUE)) {
922 goto stall_i; /* not supported */
926 case REQUEST_TO_INTERFACE:
927 if (!USB_ReqVendorIF(TRUE)) {
928 goto stall_i; /* not supported */
932 case REQUEST_TO_ENDPOINT:
933 if (!USB_ReqVendorEP(TRUE)) {
934 goto stall_i; /* not supported */
942 if (SetupPacket.wLength) {
943 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
950 break; /* end case REQUEST_VENDOR */
951 #endif /* USB_VENDOR */
954 stall_i: USB_SetStallEP(0x80);
958 break; /* end case USB_EVT_SETUP */
960 case USB_EVT_OUT_NAK:
961 if (SetupPacket.bmRequestType.BM.Dir == 0)
963 USB_ReadReqEP(0x00, EP0Data.pData, EP0Data.Count);
967 /* might be zero length pkt */
968 USB_ReadReqEP(0x00, EP0Data.pData, 0);
972 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
973 if (EP0Data.Count) { /* still data to receive ? */
974 USB_DataOutStage(); /* receive data */
975 if (EP0Data.Count == 0) { /* data complete ? */
976 switch (SetupPacket.bmRequestType.BM.Type) {
978 case REQUEST_STANDARD:
979 goto stall_i; /* not supported */
983 switch (SetupPacket.bmRequestType.BM.Recipient) {
984 case REQUEST_TO_DEVICE:
985 goto stall_i; /* not supported */
987 case REQUEST_TO_INTERFACE:
989 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) { /* IF number correct? */
990 switch (SetupPacket.bRequest) {
991 case HID_REQUEST_SET_REPORT:
992 if (HID_SetReport()) {
993 USB_StatusInStage(); /* send Acknowledge */
1001 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) || /* IF number correct? */
1002 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
1003 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
1004 switch (SetupPacket.bRequest) {
1005 case AUDIO_REQUEST_SET_CUR:
1006 // case AUDIO_REQUEST_SET_MIN:
1007 // case AUDIO_REQUEST_SET_MAX:
1008 // case AUDIO_REQUEST_SET_RES:
1009 if (ADC_IF_SetRequest()) {
1010 USB_StatusInStage(); /* send Acknowledge */
1016 #endif /* USB_AUDIO */
1018 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) || /* IF number correct? */
1019 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
1020 switch (SetupPacket.bRequest) {
1021 case CDC_SEND_ENCAPSULATED_COMMAND:
1022 if (CDC_SendEncapsulatedCommand()) {
1023 USB_StatusInStage(); /* send Acknowledge */
1027 case CDC_SET_COMM_FEATURE:
1028 if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
1029 USB_StatusInStage(); /* send Acknowledge */
1033 case CDC_SET_LINE_CODING:
1034 if (CDC_SetLineCoding()) {
1035 USB_StatusInStage(); /* send Acknowledge */
1041 #endif /* USB_CDC */
1043 /* end case REQUEST_TO_INTERFACE */
1045 case REQUEST_TO_ENDPOINT:
1047 switch (SetupPacket.bRequest) {
1048 case AUDIO_REQUEST_SET_CUR:
1049 // case AUDIO_REQUEST_SET_MIN:
1050 // case AUDIO_REQUEST_SET_MAX:
1051 // case AUDIO_REQUEST_SET_RES:
1052 if (ADC_EP_SetRequest()) {
1053 USB_StatusInStage(); /* send Acknowledge */
1058 #endif /* USB_AUDIO */
1060 /* end case REQUEST_TO_ENDPOINT */
1065 out_class_ok: /* request finished successfully */
1066 break; /* end case REQUEST_CLASS */
1067 #endif /* USB_CLASS */
1070 case REQUEST_VENDOR:
1071 switch (SetupPacket.bmRequestType.BM.Recipient) {
1073 case REQUEST_TO_DEVICE:
1074 if (!USB_ReqVendorDev(FALSE)) {
1075 goto stall_i; /* not supported */
1079 case REQUEST_TO_INTERFACE:
1080 if (!USB_ReqVendorIF(FALSE)) {
1081 goto stall_i; /* not supported */
1085 case REQUEST_TO_ENDPOINT:
1086 if (!USB_ReqVendorEP(FALSE)) {
1087 goto stall_i; /* not supported */
1095 USB_StatusInStage();
1097 break; /* end case REQUEST_VENDOR */
1098 #endif /* USB_VENDOR */
1106 USB_StatusOutStage(); /* receive Acknowledge */
1108 break; /* end case USB_EVT_OUT */
1111 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1112 USB_DataInStage(); /* send data */
1114 if (USB_DeviceAddress & 0x80) {
1115 USB_DeviceAddress &= 0x7F;
1116 USB_SetAddress(USB_DeviceAddress);
1119 break; /* end case USB_EVT_IN */
1121 case USB_EVT_OUT_STALL:
1122 USB_ClrStallEP(0x00);
1125 case USB_EVT_IN_STALL:
1126 USB_ClrStallEP(0x80);