62 uint16_t USB_DeviceStatus;
63 uint8_t USB_DeviceAddress;
64 uint8_t USB_Configuration;
65 uint32_t USB_EndPointMask;
66 uint32_t USB_EndPointHalt;
67 uint32_t USB_EndPointStall;
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;
102 void USB_SetupStage (
void) {
103 USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
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;
133 void USB_DataOutStage (
void) {
136 cnt = USB_ReadEP(0x00, EP0Data.pData);
137 EP0Data.pData += cnt;
138 EP0Data.Count -= cnt;
148 void USB_StatusInStage (
void) {
149 USB_WriteEP(0x80, NULL, 0);
159 void USB_StatusOutStage (
void) {
160 USB_ReadEP(0x00, EP0Buf);
170 __inline uint32_t USB_ReqGetStatus (
void) {
173 switch (SetupPacket.bmRequestType.BM.Recipient) {
174 case REQUEST_TO_DEVICE:
175 EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
177 case REQUEST_TO_INTERFACE:
178 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
180 EP0Data.pData = EP0Buf;
185 case REQUEST_TO_ENDPOINT:
186 n = SetupPacket.wIndex.WB.L & 0x8F;
187 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
188 if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
189 *((uint16_t
__attribute__((packed)) *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
190 EP0Data.pData = EP0Buf;
209 __inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
212 switch (SetupPacket.bmRequestType.BM.Recipient) {
213 case REQUEST_TO_DEVICE:
214 if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
217 USB_DeviceStatus |= USB_GETSTATUS_REMOTE_WAKEUP;
219 USB_WakeUpCfg(FALSE);
220 USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
226 case REQUEST_TO_INTERFACE:
228 case REQUEST_TO_ENDPOINT:
229 n = SetupPacket.wIndex.WB.L & 0x8F;
230 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
231 if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
232 if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
235 USB_EndPointHalt |= m;
237 if ((USB_EndPointStall & m) != 0) {
242 if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
244 if (CSW.dSignature == MSC_CSW_Signature) {
245 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW,
sizeof(CSW));
249 USB_EndPointHalt &= ~m;
271 __inline uint32_t USB_ReqSetAddress (
void) {
273 switch (SetupPacket.bmRequestType.BM.Recipient) {
274 case REQUEST_TO_DEVICE:
275 USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
290 __inline uint32_t USB_ReqGetDescriptor (
void) {
294 switch (SetupPacket.bmRequestType.BM.Recipient) {
295 case REQUEST_TO_DEVICE:
296 switch (SetupPacket.wValue.WB.H) {
297 case USB_DEVICE_DESCRIPTOR_TYPE:
298 EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
299 len = USB_DEVICE_DESC_SIZE;
301 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
302 pD = (uint8_t *)USB_ConfigDescriptor;
303 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
304 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
305 pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
308 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
312 len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
314 case USB_STRING_DESCRIPTOR_TYPE:
315 pD = (uint8_t *)USB_StringDescriptor;
316 for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
317 if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
318 pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
321 if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
325 len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
331 case REQUEST_TO_INTERFACE:
332 switch (SetupPacket.wValue.WB.H) {
334 case HID_HID_DESCRIPTOR_TYPE:
335 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
338 EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
341 case HID_REPORT_DESCRIPTOR_TYPE:
342 if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
345 EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
346 len = HID_ReportDescSize;
348 case HID_PHYSICAL_DESCRIPTOR_TYPE:
359 if (EP0Data.Count > len) {
373 __inline uint32_t USB_ReqGetConfiguration (
void) {
375 switch (SetupPacket.bmRequestType.BM.Recipient) {
376 case REQUEST_TO_DEVICE:
377 EP0Data.pData = &USB_Configuration;
398 __inline
void UsbAddPtr(
void **vpptr, uint32_t n)
407 bpptr = (uint8_t **)vpptr;
418 __inline uint32_t USB_ReqSetConfiguration (
void) {
419 USB_COMMON_DESCRIPTOR *pD;
423 switch (SetupPacket.bmRequestType.BM.Recipient) {
424 case REQUEST_TO_DEVICE:
426 if (SetupPacket.wValue.WB.L) {
427 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
428 while (pD->bLength) {
429 switch (pD->bDescriptorType) {
430 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
431 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
432 USB_Configuration = SetupPacket.wValue.WB.L;
433 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
434 for (n = 0; n < USB_IF_NUM; n++) {
435 USB_AltSetting[n] = 0;
437 for (n = 1; n < 16; n++) {
438 if (USB_EndPointMask & (1 << n)) {
441 if (USB_EndPointMask & ((1 << 16) << n)) {
442 USB_DisableEP(n | 0x80);
445 USB_EndPointMask = 0x00010001;
446 USB_EndPointHalt = 0x00000000;
447 USB_EndPointStall= 0x00000000;
449 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
450 USB_DeviceStatus |= USB_GETSTATUS_SELF_POWERED;
452 USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
455 UsbAddPtr((
void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
459 case USB_INTERFACE_DESCRIPTOR_TYPE:
460 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
462 case USB_ENDPOINT_DESCRIPTOR_TYPE:
464 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
465 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
466 USB_EndPointMask |= m;
467 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
473 UsbAddPtr((
void **)&pD, pD->bLength);
477 USB_Configuration = 0;
478 for (n = 1; n < 16; n++) {
479 if (USB_EndPointMask & (1 << n)) {
482 if (USB_EndPointMask & ((1 << 16) << n)) {
483 USB_DisableEP(n | 0x80);
486 USB_EndPointMask = 0x00010001;
487 USB_EndPointHalt = 0x00000000;
488 USB_EndPointStall = 0x00000000;
489 USB_Configure(FALSE);
492 if (USB_Configuration != SetupPacket.wValue.WB.L) {
509 __inline uint32_t USB_ReqGetInterface (
void) {
511 switch (SetupPacket.bmRequestType.BM.Recipient) {
512 case REQUEST_TO_INTERFACE:
513 if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
514 EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
532 __inline uint32_t USB_ReqSetInterface (
void) {
533 USB_COMMON_DESCRIPTOR *pD;
534 uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
538 switch (SetupPacket.bmRequestType.BM.Recipient) {
539 case REQUEST_TO_INTERFACE:
540 if (USB_Configuration == 0)
return (FALSE);
542 pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
543 while (pD->bLength) {
544 switch (pD->bDescriptorType) {
545 case USB_CONFIGURATION_DESCRIPTOR_TYPE:
546 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
547 UsbAddPtr((
void **)&pD, ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength);
551 case USB_INTERFACE_DESCRIPTOR_TYPE:
552 ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
553 alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
555 if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
557 old = USB_AltSetting[ifn];
558 USB_AltSetting[ifn] = (uint8_t)alt;
561 case USB_ENDPOINT_DESCRIPTOR_TYPE:
562 if (ifn == SetupPacket.wIndex.WB.L) {
563 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
564 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
565 if (alt == SetupPacket.wValue.WB.L) {
566 USB_EndPointMask |= m;
567 USB_EndPointHalt &= ~m;
568 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
573 else if ((alt == old) && ((msk & m) == 0)) {
574 USB_EndPointMask &= ~m;
575 USB_EndPointHalt &= ~m;
581 UsbAddPtr((
void **)&pD, pD->bLength);
598 void USB_EndPoint0 (uint32_t event) {
603 USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
604 EP0Data.Count = SetupPacket.wLength;
605 switch (SetupPacket.bmRequestType.BM.Type) {
607 case REQUEST_STANDARD:
608 switch (SetupPacket.bRequest) {
609 case USB_REQUEST_GET_STATUS:
610 if (!USB_ReqGetStatus()) {
616 case USB_REQUEST_CLEAR_FEATURE:
617 if (!USB_ReqSetClrFeature(0)) {
621 #if USB_FEATURE_EVENT
626 case USB_REQUEST_SET_FEATURE:
627 if (!USB_ReqSetClrFeature(1)) {
631 #if USB_FEATURE_EVENT
636 case USB_REQUEST_SET_ADDRESS:
637 if (!USB_ReqSetAddress()) {
643 case USB_REQUEST_GET_DESCRIPTOR:
644 if (!USB_ReqGetDescriptor()) {
650 case USB_REQUEST_SET_DESCRIPTOR:
651 USB_SetStallEP(0x00);
655 case USB_REQUEST_GET_CONFIGURATION:
656 if (!USB_ReqGetConfiguration()) {
662 case USB_REQUEST_SET_CONFIGURATION:
663 if (!USB_ReqSetConfiguration()) {
667 #if USB_CONFIGURE_EVENT
668 USB_Configure_Event();
672 case USB_REQUEST_GET_INTERFACE:
673 if (!USB_ReqGetInterface()) {
679 case USB_REQUEST_SET_INTERFACE:
680 if (!USB_ReqSetInterface()) {
684 #if USB_INTERFACE_EVENT
685 USB_Interface_Event();
696 switch (SetupPacket.bmRequestType.BM.Recipient) {
698 case REQUEST_TO_DEVICE:
701 case REQUEST_TO_INTERFACE:
703 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
704 switch (SetupPacket.bRequest) {
705 case HID_REQUEST_GET_REPORT:
706 if (HID_GetReport()) {
707 EP0Data.pData = EP0Buf;
712 case HID_REQUEST_SET_REPORT:
713 EP0Data.pData = EP0Buf;
715 case HID_REQUEST_GET_IDLE:
717 EP0Data.pData = EP0Buf;
722 case HID_REQUEST_SET_IDLE:
728 case HID_REQUEST_GET_PROTOCOL:
729 if (HID_GetProtocol()) {
730 EP0Data.pData = EP0Buf;
735 case HID_REQUEST_SET_PROTOCOL:
736 if (HID_SetProtocol()) {
745 if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {
746 switch (SetupPacket.bRequest) {
747 case MSC_REQUEST_RESET:
748 if ((SetupPacket.wValue.W == 0) &&
749 (SetupPacket.wLength == 0)) {
756 case MSC_REQUEST_GET_MAX_LUN:
757 if ((SetupPacket.wValue.W == 0) &&
758 (SetupPacket.wLength == 1)) {
759 if (MSC_GetMaxLUN()) {
760 EP0Data.pData = EP0Buf;
770 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) ||
771 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
772 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
773 switch (SetupPacket.bRequest) {
774 case AUDIO_REQUEST_GET_CUR:
775 case AUDIO_REQUEST_GET_MIN:
776 case AUDIO_REQUEST_GET_MAX:
777 case AUDIO_REQUEST_GET_RES:
778 if (ADC_IF_GetRequest()) {
779 EP0Data.pData = EP0Buf;
784 case AUDIO_REQUEST_SET_CUR:
788 EP0Data.pData = EP0Buf;
794 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) ||
795 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
796 switch (SetupPacket.bRequest) {
797 case CDC_SEND_ENCAPSULATED_COMMAND:
798 EP0Data.pData = EP0Buf;
800 case CDC_GET_ENCAPSULATED_RESPONSE:
801 if (CDC_GetEncapsulatedResponse()) {
802 EP0Data.pData = EP0Buf;
807 case CDC_SET_COMM_FEATURE:
808 EP0Data.pData = EP0Buf;
810 case CDC_GET_COMM_FEATURE:
811 if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
812 EP0Data.pData = EP0Buf;
817 case CDC_CLEAR_COMM_FEATURE:
818 if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
823 case CDC_SET_LINE_CODING:
824 EP0Data.pData = EP0Buf;
826 case CDC_GET_LINE_CODING:
827 if (CDC_GetLineCoding()) {
828 EP0Data.pData = EP0Buf;
833 case CDC_SET_CONTROL_LINE_STATE:
834 if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
840 if (CDC_SendBreak(SetupPacket.wValue.W)) {
851 case REQUEST_TO_ENDPOINT:
853 switch (SetupPacket.bRequest) {
854 case AUDIO_REQUEST_GET_CUR:
855 case AUDIO_REQUEST_GET_MIN:
856 case AUDIO_REQUEST_GET_MAX:
857 case AUDIO_REQUEST_GET_RES:
858 if (ADC_EP_GetRequest()) {
859 EP0Data.pData = EP0Buf;
864 case AUDIO_REQUEST_SET_CUR:
868 EP0Data.pData = EP0Buf;
884 switch (SetupPacket.bmRequestType.BM.Recipient) {
886 case REQUEST_TO_DEVICE:
887 if (!USB_ReqVendorDev(TRUE)) {
892 case REQUEST_TO_INTERFACE:
893 if (!USB_ReqVendorIF(TRUE)) {
898 case REQUEST_TO_ENDPOINT:
899 if (!USB_ReqVendorEP(TRUE)) {
908 if (SetupPacket.wLength) {
909 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
920 stall_i: USB_SetStallEP(0x80);
927 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
930 if (EP0Data.Count == 0) {
931 switch (SetupPacket.bmRequestType.BM.Type) {
933 case REQUEST_STANDARD:
938 switch (SetupPacket.bmRequestType.BM.Recipient) {
939 case REQUEST_TO_DEVICE:
942 case REQUEST_TO_INTERFACE:
944 if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {
945 switch (SetupPacket.bRequest) {
946 case HID_REQUEST_SET_REPORT:
947 if (HID_SetReport()) {
956 if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM) ||
957 (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
958 (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
959 switch (SetupPacket.bRequest) {
960 case AUDIO_REQUEST_SET_CUR:
964 if (ADC_IF_SetRequest()) {
973 if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM) ||
974 (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
975 switch (SetupPacket.bRequest) {
976 case CDC_SEND_ENCAPSULATED_COMMAND:
977 if (CDC_SendEncapsulatedCommand()) {
982 case CDC_SET_COMM_FEATURE:
983 if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
988 case CDC_SET_LINE_CODING:
989 if (CDC_SetLineCoding()) {
1000 case REQUEST_TO_ENDPOINT:
1002 switch (SetupPacket.bRequest) {
1003 case AUDIO_REQUEST_SET_CUR:
1007 if (ADC_EP_SetRequest()) {
1008 USB_StatusInStage();
1025 case REQUEST_VENDOR:
1026 switch (SetupPacket.bmRequestType.BM.Recipient) {
1028 case REQUEST_TO_DEVICE:
1029 if (!USB_ReqVendorDev(FALSE)) {
1034 case REQUEST_TO_INTERFACE:
1035 if (!USB_ReqVendorIF(FALSE)) {
1040 case REQUEST_TO_ENDPOINT:
1041 if (!USB_ReqVendorEP(FALSE)) {
1050 USB_StatusInStage();
1061 USB_StatusOutStage();
1066 if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
1069 if (USB_DeviceAddress & 0x80) {
1070 USB_DeviceAddress &= 0x7F;
1071 USB_SetAddress(USB_DeviceAddress);
1076 case USB_EVT_OUT_STALL:
1077 USB_ClrStallEP(0x00);
1080 case USB_EVT_IN_STALL:
1081 USB_ClrStallEP(0x80);