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 microcontroller devices only. Nothing else
14 * 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 *----------------------------------------------------------------------------*/
24 #include "projectconfig.h"
62 uint16_t USB_DeviceStatus
;
63 uint8_t USB_DeviceAddress
;
64 volatile uint8_t USB_Configuration
;
65 uint32_t USB_EndPointMask
;
66 uint32_t USB_EndPointHalt
;
67 uint32_t USB_EndPointStall
; /* EP must stay stalled */
68 uint8_t USB_NumInterfaces
;
69 uint8_t USB_AltSetting
[USB_IF_NUM
];
71 uint8_t EP0Buf
[USB_MAX_PACKET0
];
76 USB_SETUP_PACKET SetupPacket
;
85 void USB_ResetCore (void) {
87 USB_DeviceStatus
= USB_POWER
;
88 USB_DeviceAddress
= 0;
89 USB_Configuration
= 0;
90 USB_EndPointMask
= 0x00010001;
91 USB_EndPointHalt
= 0x00000000;
92 USB_EndPointStall
= 0x00000000;
97 * USB Request - Setup Stage
98 * Parameters: None (global SetupPacket)
102 void USB_SetupStage (void) {
103 USB_ReadEP(0x00, (uint8_t *)&SetupPacket
);
108 * USB Request - Data In Stage
109 * Parameters: None (global EP0Data)
113 void USB_DataInStage (void) {
116 if (EP0Data
.Count
> USB_MAX_PACKET0
) {
117 cnt
= USB_MAX_PACKET0
;
121 cnt
= USB_WriteEP(0x80, EP0Data
.pData
, cnt
);
122 EP0Data
.pData
+= cnt
;
123 EP0Data
.Count
-= cnt
;
128 * USB Request - Data Out Stage
129 * Parameters: None (global EP0Data)
133 void USB_DataOutStage (void) {
136 cnt
= USB_ReadEP(0x00, EP0Data
.pData
);
137 EP0Data
.pData
+= cnt
;
138 EP0Data
.Count
-= cnt
;
143 * USB Request - Status In Stage
148 void USB_StatusInStage (void) {
149 USB_WriteEP(0x80, NULL
, 0);
154 * USB Request - Status Out Stage
159 void USB_StatusOutStage (void) {
160 USB_ReadEP(0x00, EP0Buf
);
165 * Get Status USB Request
166 * Parameters: None (global SetupPacket)
167 * Return Value: TRUE - Success, FALSE - Error
170 static inline uint32_t USB_ReqGetStatus (void) {
172 uint16_t* ep0
= (uint16_t __attribute__((packed
)) *)EP0Buf
;
174 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
175 case REQUEST_TO_DEVICE
:
176 EP0Data
.pData
= (uint8_t *)&USB_DeviceStatus
;
178 case REQUEST_TO_INTERFACE
:
179 if ((USB_Configuration
!= 0) && (SetupPacket
.wIndex
.WB
.L
< USB_NumInterfaces
)) {
180 //*((uint16_t __attribute__((packed)) *)EP0Buf) = 0;
182 EP0Data
.pData
= EP0Buf
;
187 case REQUEST_TO_ENDPOINT
:
188 n
= SetupPacket
.wIndex
.WB
.L
& 0x8F;
189 m
= (n
& 0x80) ? ((1 << 16) << (n
& 0x0F)) : (1 << n
);
190 if (((USB_Configuration
!= 0) || ((n
& 0x0F) == 0)) && (USB_EndPointMask
& m
)) {
191 // *((uint16_t __attribute__((packed)) *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
192 *ep0
= (USB_EndPointHalt
& m
) ? 1 : 0;
193 EP0Data
.pData
= EP0Buf
;
206 * Set/Clear Feature USB Request
207 * Parameters: sc: 0 - Clear, 1 - Set
208 * (global SetupPacket)
209 * Return Value: TRUE - Success, FALSE - Error
212 static inline uint32_t USB_ReqSetClrFeature (uint32_t sc
) {
215 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
216 case REQUEST_TO_DEVICE
:
217 if (SetupPacket
.wValue
.W
== USB_FEATURE_REMOTE_WAKEUP
) {
220 USB_DeviceStatus
|= USB_GETSTATUS_REMOTE_WAKEUP
;
222 USB_WakeUpCfg(FALSE
);
223 USB_DeviceStatus
&= ~USB_GETSTATUS_REMOTE_WAKEUP
;
229 case REQUEST_TO_INTERFACE
:
231 case REQUEST_TO_ENDPOINT
:
232 n
= SetupPacket
.wIndex
.WB
.L
& 0x8F;
233 m
= (n
& 0x80) ? ((1 << 16) << (n
& 0x0F)) : (1 << n
);
234 if ((USB_Configuration
!= 0) && ((n
& 0x0F) != 0) && (USB_EndPointMask
& m
)) {
235 if (SetupPacket
.wValue
.W
== USB_FEATURE_ENDPOINT_STALL
) {
238 USB_EndPointHalt
|= m
;
240 if ((USB_EndPointStall
& m
) != 0) {
245 if ((n
== MSC_EP_IN
) && ((USB_EndPointHalt
& m
) != 0)) {
246 /* Compliance Test: rewrite CSW after unstall */
247 if (CSW
.dSignature
== MSC_CSW_Signature
) {
248 USB_WriteEP(MSC_EP_IN
, (uint8_t *)&CSW
, sizeof(CSW
));
252 USB_EndPointHalt
&= ~m
;
269 * Set Address USB Request
270 * Parameters: None (global SetupPacket)
271 * Return Value: TRUE - Success, FALSE - Error
274 static inline uint32_t USB_ReqSetAddress (void) {
276 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
277 case REQUEST_TO_DEVICE
:
278 USB_DeviceAddress
= 0x80 | SetupPacket
.wValue
.WB
.L
;
288 * Get Descriptor USB Request
289 * Parameters: None (global SetupPacket)
290 * Return Value: TRUE - Success, FALSE - Error
293 static inline uint32_t USB_ReqGetDescriptor (void) {
297 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
298 case REQUEST_TO_DEVICE
:
299 switch (SetupPacket
.wValue
.WB
.H
) {
300 case USB_DEVICE_DESCRIPTOR_TYPE
:
301 EP0Data
.pData
= (uint8_t *)USB_DeviceDescriptor
;
302 len
= USB_DEVICE_DESC_SIZE
;
304 case USB_CONFIGURATION_DESCRIPTOR_TYPE
:
305 pD
= (uint8_t *)USB_ConfigDescriptor
;
306 for (n
= 0; n
!= SetupPacket
.wValue
.WB
.L
; n
++) {
307 if (((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->bLength
!= 0) {
308 pD
+= ((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->wTotalLength
;
311 if (((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->bLength
== 0) {
315 len
= ((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->wTotalLength
;
317 case USB_STRING_DESCRIPTOR_TYPE
:
318 pD
= (uint8_t *)USB_StringDescriptor
;
319 for (n
= 0; n
!= SetupPacket
.wValue
.WB
.L
; n
++) {
320 if (((USB_STRING_DESCRIPTOR
*)pD
)->bLength
!= 0) {
321 pD
+= ((USB_STRING_DESCRIPTOR
*)pD
)->bLength
;
324 if (((USB_STRING_DESCRIPTOR
*)pD
)->bLength
== 0) {
328 len
= ((USB_STRING_DESCRIPTOR
*)EP0Data
.pData
)->bLength
;
334 case REQUEST_TO_INTERFACE
:
335 switch (SetupPacket
.wValue
.WB
.H
) {
337 case HID_HID_DESCRIPTOR_TYPE
:
338 if (SetupPacket
.wIndex
.WB
.L
!= USB_HID_IF_NUM
) {
339 return (FALSE
); /* Only Single HID Interface is supported */
341 EP0Data
.pData
= (uint8_t *)USB_ConfigDescriptor
+ HID_DESC_OFFSET
;
344 case HID_REPORT_DESCRIPTOR_TYPE
:
345 if (SetupPacket
.wIndex
.WB
.L
!= USB_HID_IF_NUM
) {
346 return (FALSE
); /* Only Single HID Interface is supported */
348 EP0Data
.pData
= (uint8_t *)HID_ReportDescriptor
;
349 len
= HID_ReportDescSize
;
351 case HID_PHYSICAL_DESCRIPTOR_TYPE
:
352 return (FALSE
); /* HID Physical Descriptor is not supported */
362 if (EP0Data
.Count
> len
) {
371 * Get Configuration USB Request
372 * Parameters: None (global SetupPacket)
373 * Return Value: TRUE - Success, FALSE - Error
376 static inline uint32_t USB_ReqGetConfiguration (void) {
378 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
379 case REQUEST_TO_DEVICE
:
380 // Added cast to avoid warnings due to USB_Configuration being volatile (KTownsend)
381 EP0Data
.pData
= (uint8_t *)&USB_Configuration
;
382 //EP0Data.pData = &USB_Configuration;
391 * Set Configuration USB Request
392 * Parameters: None (global SetupPacket)
393 * Return Value: TRUE - Success, FALSE - Error
396 static inline uint32_t USB_ReqSetConfiguration (void) {
397 USB_COMMON_DESCRIPTOR
*pD
;
401 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
402 case REQUEST_TO_DEVICE
:
404 if (SetupPacket
.wValue
.WB
.L
) {
405 pD
= (USB_COMMON_DESCRIPTOR
*)USB_ConfigDescriptor
;
406 while (pD
->bLength
) {
407 switch (pD
->bDescriptorType
) {
408 case USB_CONFIGURATION_DESCRIPTOR_TYPE
:
409 if (((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->bConfigurationValue
== SetupPacket
.wValue
.WB
.L
) {
410 USB_Configuration
= SetupPacket
.wValue
.WB
.L
;
411 USB_NumInterfaces
= ((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->bNumInterfaces
;
412 for (n
= 0; n
< USB_IF_NUM
; n
++) {
413 USB_AltSetting
[n
] = 0;
415 for (n
= 1; n
< 16; n
++) {
416 if (USB_EndPointMask
& (1 << n
)) {
419 if (USB_EndPointMask
& ((1 << 16) << n
)) {
420 USB_DisableEP(n
| 0x80);
423 USB_EndPointMask
= 0x00010001;
424 USB_EndPointHalt
= 0x00000000;
425 USB_EndPointStall
= 0x00000000;
427 if (((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->bmAttributes
& USB_CONFIG_POWERED_MASK
) {
428 USB_DeviceStatus
|= USB_GETSTATUS_SELF_POWERED
;
430 USB_DeviceStatus
&= ~USB_GETSTATUS_SELF_POWERED
;
433 UsbAddPtr((void **)&pD
, ((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->wTotalLength
);
437 case USB_INTERFACE_DESCRIPTOR_TYPE
:
438 alt
= ((USB_INTERFACE_DESCRIPTOR
*)pD
)->bAlternateSetting
;
440 case USB_ENDPOINT_DESCRIPTOR_TYPE
:
442 n
= ((USB_ENDPOINT_DESCRIPTOR
*)pD
)->bEndpointAddress
& 0x8F;
443 m
= (n
& 0x80) ? ((1 << 16) << (n
& 0x0F)) : (1 << n
);
444 USB_EndPointMask
|= m
;
445 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR
*)pD
);
451 UsbAddPtr((void **)&pD
, pD
->bLength
);
455 USB_Configuration
= 0;
456 for (n
= 1; n
< 16; n
++) {
457 if (USB_EndPointMask
& (1 << n
)) {
460 if (USB_EndPointMask
& ((1 << 16) << n
)) {
461 USB_DisableEP(n
| 0x80);
464 USB_EndPointMask
= 0x00010001;
465 USB_EndPointHalt
= 0x00000000;
466 USB_EndPointStall
= 0x00000000;
467 USB_Configure(FALSE
);
470 if (USB_Configuration
!= SetupPacket
.wValue
.WB
.L
) {
482 * Get Interface USB Request
483 * Parameters: None (global SetupPacket)
484 * Return Value: TRUE - Success, FALSE - Error
487 static inline uint32_t USB_ReqGetInterface (void) {
489 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
490 case REQUEST_TO_INTERFACE
:
491 if ((USB_Configuration
!= 0) && (SetupPacket
.wIndex
.WB
.L
< USB_NumInterfaces
)) {
492 EP0Data
.pData
= USB_AltSetting
+ SetupPacket
.wIndex
.WB
.L
;
505 * Set Interface USB Request
506 * Parameters: None (global SetupPacket)
507 * Return Value: TRUE - Success, FALSE - Error
510 static inline uint32_t USB_ReqSetInterface (void) {
511 USB_COMMON_DESCRIPTOR
*pD
;
512 uint32_t ifn
= 0, alt
= 0, old
= 0, msk
= 0;
516 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
517 case REQUEST_TO_INTERFACE
:
518 if (USB_Configuration
== 0) return (FALSE
);
520 pD
= (USB_COMMON_DESCRIPTOR
*)USB_ConfigDescriptor
;
521 while (pD
->bLength
) {
522 switch (pD
->bDescriptorType
) {
523 case USB_CONFIGURATION_DESCRIPTOR_TYPE
:
524 if (((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->bConfigurationValue
!= USB_Configuration
) {
525 UsbAddPtr((void **)&pD
, ((USB_CONFIGURATION_DESCRIPTOR
*)pD
)->wTotalLength
);
529 case USB_INTERFACE_DESCRIPTOR_TYPE
:
530 ifn
= ((USB_INTERFACE_DESCRIPTOR
*)pD
)->bInterfaceNumber
;
531 alt
= ((USB_INTERFACE_DESCRIPTOR
*)pD
)->bAlternateSetting
;
533 if ((ifn
== SetupPacket
.wIndex
.WB
.L
) && (alt
== SetupPacket
.wValue
.WB
.L
)) {
535 old
= USB_AltSetting
[ifn
];
536 USB_AltSetting
[ifn
] = (uint8_t)alt
;
539 case USB_ENDPOINT_DESCRIPTOR_TYPE
:
540 if (ifn
== SetupPacket
.wIndex
.WB
.L
) {
541 n
= ((USB_ENDPOINT_DESCRIPTOR
*)pD
)->bEndpointAddress
& 0x8F;
542 m
= (n
& 0x80) ? ((1 << 16) << (n
& 0x0F)) : (1 << n
);
543 if (alt
== SetupPacket
.wValue
.WB
.L
) {
544 USB_EndPointMask
|= m
;
545 USB_EndPointHalt
&= ~m
;
546 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR
*)pD
);
551 else if ((alt
== old
) && ((msk
& m
) == 0)) {
552 USB_EndPointMask
&= ~m
;
553 USB_EndPointHalt
&= ~m
;
559 UsbAddPtr((void **)&pD
, pD
->bLength
);
571 * USB Endpoint 0 Event Callback
576 void USB_EndPoint0 (uint32_t event
) {
581 USB_DirCtrlEP(SetupPacket
.bmRequestType
.BM
.Dir
);
582 EP0Data
.Count
= SetupPacket
.wLength
; /* Number of bytes to transfer */
583 switch (SetupPacket
.bmRequestType
.BM
.Type
) {
585 case REQUEST_STANDARD
:
586 switch (SetupPacket
.bRequest
) {
587 case USB_REQUEST_GET_STATUS
:
588 if (!USB_ReqGetStatus()) {
594 case USB_REQUEST_CLEAR_FEATURE
:
595 if (!USB_ReqSetClrFeature(0)) {
599 #if USB_FEATURE_EVENT
604 case USB_REQUEST_SET_FEATURE
:
605 if (!USB_ReqSetClrFeature(1)) {
609 #if USB_FEATURE_EVENT
614 case USB_REQUEST_SET_ADDRESS
:
615 if (!USB_ReqSetAddress()) {
621 case USB_REQUEST_GET_DESCRIPTOR
:
622 if (!USB_ReqGetDescriptor()) {
628 case USB_REQUEST_SET_DESCRIPTOR
:
629 /*stall_o:*/ USB_SetStallEP(0x00); /* not supported */
633 case USB_REQUEST_GET_CONFIGURATION
:
634 if (!USB_ReqGetConfiguration()) {
640 case USB_REQUEST_SET_CONFIGURATION
:
641 if (!USB_ReqSetConfiguration()) {
645 #if USB_CONFIGURE_EVENT
646 USB_Configure_Event();
650 case USB_REQUEST_GET_INTERFACE
:
651 if (!USB_ReqGetInterface()) {
657 case USB_REQUEST_SET_INTERFACE
:
658 if (!USB_ReqSetInterface()) {
662 #if USB_INTERFACE_EVENT
663 USB_Interface_Event();
670 break; /* end case REQUEST_STANDARD */
674 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
676 case REQUEST_TO_DEVICE
:
677 goto stall_i
; /* not supported */
679 case REQUEST_TO_INTERFACE
:
681 if (SetupPacket
.wIndex
.WB
.L
== USB_HID_IF_NUM
) { /* IF number correct? */
682 switch (SetupPacket
.bRequest
) {
683 case HID_REQUEST_GET_REPORT
:
684 if (HID_GetReport()) {
685 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
686 USB_DataInStage(); /* send requested data */
690 case HID_REQUEST_SET_REPORT
:
691 EP0Data
.pData
= EP0Buf
; /* data to be received */
693 case HID_REQUEST_GET_IDLE
:
695 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
696 USB_DataInStage(); /* send requested data */
700 case HID_REQUEST_SET_IDLE
:
702 USB_StatusInStage(); /* send Acknowledge */
706 case HID_REQUEST_GET_PROTOCOL
:
707 if (HID_GetProtocol()) {
708 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
709 USB_DataInStage(); /* send requested data */
713 case HID_REQUEST_SET_PROTOCOL
:
714 if (HID_SetProtocol()) {
715 USB_StatusInStage(); /* send Acknowledge */
723 if (SetupPacket
.wIndex
.WB
.L
== USB_MSC_IF_NUM
) { /* IF number correct? */
724 switch (SetupPacket
.bRequest
) {
725 case MSC_REQUEST_RESET
:
726 if ((SetupPacket
.wValue
.W
== 0) && /* RESET with invalid parameters -> STALL */
727 (SetupPacket
.wLength
== 0)) {
734 case MSC_REQUEST_GET_MAX_LUN
:
735 if ((SetupPacket
.wValue
.W
== 0) && /* GET_MAX_LUN with invalid parameters -> STALL */
736 (SetupPacket
.wLength
== 1)) {
737 if (MSC_GetMaxLUN()) {
738 EP0Data
.pData
= EP0Buf
;
748 if ((SetupPacket
.wIndex
.WB
.L
== USB_ADC_CIF_NUM
) || /* IF number correct? */
749 (SetupPacket
.wIndex
.WB
.L
== USB_ADC_SIF1_NUM
) ||
750 (SetupPacket
.wIndex
.WB
.L
== USB_ADC_SIF2_NUM
)) {
751 switch (SetupPacket
.bRequest
) {
752 case AUDIO_REQUEST_GET_CUR
:
753 case AUDIO_REQUEST_GET_MIN
:
754 case AUDIO_REQUEST_GET_MAX
:
755 case AUDIO_REQUEST_GET_RES
:
756 if (ADC_IF_GetRequest()) {
757 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
758 USB_DataInStage(); /* send requested data */
762 case AUDIO_REQUEST_SET_CUR
:
763 // case AUDIO_REQUEST_SET_MIN:
764 // case AUDIO_REQUEST_SET_MAX:
765 // case AUDIO_REQUEST_SET_RES:
766 EP0Data
.pData
= EP0Buf
; /* data to be received */
770 #endif /* USB_AUDIO */
772 if ((SetupPacket
.wIndex
.WB
.L
== USB_CDC_CIF_NUM
) || /* IF number correct? */
773 (SetupPacket
.wIndex
.WB
.L
== USB_CDC_DIF_NUM
)) {
774 switch (SetupPacket
.bRequest
) {
775 case CDC_SEND_ENCAPSULATED_COMMAND
:
776 EP0Data
.pData
= EP0Buf
; /* data to be received, see USB_EVT_OUT */
778 case CDC_GET_ENCAPSULATED_RESPONSE
:
779 if (CDC_GetEncapsulatedResponse()) {
780 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
781 USB_DataInStage(); /* send requested data */
785 case CDC_SET_COMM_FEATURE
:
786 EP0Data
.pData
= EP0Buf
; /* data to be received, see USB_EVT_OUT */
788 case CDC_GET_COMM_FEATURE
:
789 if (CDC_GetCommFeature(SetupPacket
.wValue
.W
)) {
790 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
791 USB_DataInStage(); /* send requested data */
795 case CDC_CLEAR_COMM_FEATURE
:
796 if (CDC_ClearCommFeature(SetupPacket
.wValue
.W
)) {
797 USB_StatusInStage(); /* send Acknowledge */
801 case CDC_SET_LINE_CODING
:
802 EP0Data
.pData
= EP0Buf
; /* data to be received, see USB_EVT_OUT */
804 case CDC_GET_LINE_CODING
:
805 if (CDC_GetLineCoding()) {
806 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
807 USB_DataInStage(); /* send requested data */
811 case CDC_SET_CONTROL_LINE_STATE
:
812 if (CDC_SetControlLineState(SetupPacket
.wValue
.W
)) {
813 USB_StatusInStage(); /* send Acknowledge */
818 if (CDC_SendBreak(SetupPacket
.wValue
.W
)) {
819 USB_StatusInStage(); /* send Acknowledge */
826 goto stall_i
; /* not supported */
827 /* end case REQUEST_TO_INTERFACE */
829 case REQUEST_TO_ENDPOINT
:
831 switch (SetupPacket
.bRequest
) {
832 case AUDIO_REQUEST_GET_CUR
:
833 case AUDIO_REQUEST_GET_MIN
:
834 case AUDIO_REQUEST_GET_MAX
:
835 case AUDIO_REQUEST_GET_RES
:
836 if (ADC_EP_GetRequest()) {
837 EP0Data
.pData
= EP0Buf
; /* point to data to be sent */
838 USB_DataInStage(); /* send requested data */
842 case AUDIO_REQUEST_SET_CUR
:
843 // case AUDIO_REQUEST_SET_MIN:
844 // case AUDIO_REQUEST_SET_MAX:
845 // case AUDIO_REQUEST_SET_RES:
846 EP0Data
.pData
= EP0Buf
; /* data to be received */
849 #endif /* USB_AUDIO */
851 /* end case REQUEST_TO_ENDPOINT */
856 setup_class_ok
: /* request finished successfully */
857 break; /* end case REQUEST_CLASS */
858 #endif /* USB_CLASS */
862 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
864 case REQUEST_TO_DEVICE
:
865 if (!USB_ReqVendorDev(TRUE
)) {
866 goto stall_i
; /* not supported */
870 case REQUEST_TO_INTERFACE
:
871 if (!USB_ReqVendorIF(TRUE
)) {
872 goto stall_i
; /* not supported */
876 case REQUEST_TO_ENDPOINT
:
877 if (!USB_ReqVendorEP(TRUE
)) {
878 goto stall_i
; /* not supported */
886 if (SetupPacket
.wLength
) {
887 if (SetupPacket
.bmRequestType
.BM
.Dir
== REQUEST_DEVICE_TO_HOST
) {
894 break; /* end case REQUEST_VENDOR */
895 #endif /* USB_VENDOR */
898 stall_i
: USB_SetStallEP(0x80);
902 break; /* end case USB_EVT_SETUP */
905 if (SetupPacket
.bmRequestType
.BM
.Dir
== REQUEST_HOST_TO_DEVICE
) {
906 if (EP0Data
.Count
) { /* still data to receive ? */
907 USB_DataOutStage(); /* receive data */
908 if (EP0Data
.Count
== 0) { /* data complete ? */
909 switch (SetupPacket
.bmRequestType
.BM
.Type
) {
911 case REQUEST_STANDARD
:
912 goto stall_i
; /* not supported */
916 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
917 case REQUEST_TO_DEVICE
:
918 goto stall_i
; /* not supported */
920 case REQUEST_TO_INTERFACE
:
922 if (SetupPacket
.wIndex
.WB
.L
== USB_HID_IF_NUM
) { /* IF number correct? */
923 switch (SetupPacket
.bRequest
) {
924 case HID_REQUEST_SET_REPORT
:
925 if (HID_SetReport()) {
926 USB_StatusInStage(); /* send Acknowledge */
934 if ((SetupPacket
.wIndex
.WB
.L
== USB_ADC_CIF_NUM
) || /* IF number correct? */
935 (SetupPacket
.wIndex
.WB
.L
== USB_ADC_SIF1_NUM
) ||
936 (SetupPacket
.wIndex
.WB
.L
== USB_ADC_SIF2_NUM
)) {
937 switch (SetupPacket
.bRequest
) {
938 case AUDIO_REQUEST_SET_CUR
:
939 // case AUDIO_REQUEST_SET_MIN:
940 // case AUDIO_REQUEST_SET_MAX:
941 // case AUDIO_REQUEST_SET_RES:
942 if (ADC_IF_SetRequest()) {
943 USB_StatusInStage(); /* send Acknowledge */
949 #endif /* USB_AUDIO */
951 if ((SetupPacket
.wIndex
.WB
.L
== USB_CDC_CIF_NUM
) || /* IF number correct? */
952 (SetupPacket
.wIndex
.WB
.L
== USB_CDC_DIF_NUM
)) {
953 switch (SetupPacket
.bRequest
) {
954 case CDC_SEND_ENCAPSULATED_COMMAND
:
955 if (CDC_SendEncapsulatedCommand()) {
956 USB_StatusInStage(); /* send Acknowledge */
960 case CDC_SET_COMM_FEATURE
:
961 if (CDC_SetCommFeature(SetupPacket
.wValue
.W
)) {
962 USB_StatusInStage(); /* send Acknowledge */
966 case CDC_SET_LINE_CODING
:
967 if (CDC_SetLineCoding()) {
968 USB_StatusInStage(); /* send Acknowledge */
976 /* end case REQUEST_TO_INTERFACE */
978 case REQUEST_TO_ENDPOINT
:
980 switch (SetupPacket
.bRequest
) {
981 case AUDIO_REQUEST_SET_CUR
:
982 // case AUDIO_REQUEST_SET_MIN:
983 // case AUDIO_REQUEST_SET_MAX:
984 // case AUDIO_REQUEST_SET_RES:
985 if (ADC_EP_SetRequest()) {
986 USB_StatusInStage(); /* send Acknowledge */
991 #endif /* USB_AUDIO */
993 /* end case REQUEST_TO_ENDPOINT */
998 out_class_ok
: /* request finished successfully */
999 break; /* end case REQUEST_CLASS */
1000 #endif /* USB_CLASS */
1003 case REQUEST_VENDOR
:
1004 switch (SetupPacket
.bmRequestType
.BM
.Recipient
) {
1006 case REQUEST_TO_DEVICE
:
1007 if (!USB_ReqVendorDev(FALSE
)) {
1008 goto stall_i
; /* not supported */
1012 case REQUEST_TO_INTERFACE
:
1013 if (!USB_ReqVendorIF(FALSE
)) {
1014 goto stall_i
; /* not supported */
1018 case REQUEST_TO_ENDPOINT
:
1019 if (!USB_ReqVendorEP(FALSE
)) {
1020 goto stall_i
; /* not supported */
1028 USB_StatusInStage();
1030 break; /* end case REQUEST_VENDOR */
1031 #endif /* USB_VENDOR */
1039 USB_StatusOutStage(); /* receive Acknowledge */
1041 break; /* end case USB_EVT_OUT */
1044 if (SetupPacket
.bmRequestType
.BM
.Dir
== REQUEST_DEVICE_TO_HOST
) {
1045 USB_DataInStage(); /* send data */
1047 if (USB_DeviceAddress
& 0x80) {
1048 USB_DeviceAddress
&= 0x7F;
1049 USB_SetAddress(USB_DeviceAddress
);
1052 break; /* end case USB_EVT_IN */
1054 case USB_EVT_OUT_STALL
:
1055 USB_ClrStallEP(0x00);
1058 case USB_EVT_IN_STALL
:
1059 USB_ClrStallEP(0x80);