These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers usbcore.c Source File

usbcore.c

00001 /*----------------------------------------------------------------------------
00002  *      U S B  -  K e r n e l
00003  *----------------------------------------------------------------------------
00004  * Name:    usbcore.c
00005  * Purpose: USB Core Module
00006  * Version: V1.20
00007  *----------------------------------------------------------------------------
00008  *      This software is supplied "AS IS" without any warranties, express,
00009  *      implied or statutory, including but not limited to the implied
00010  *      warranties of fitness for purpose, satisfactory quality and
00011  *      noninfringement. Keil extends you a royalty-free right to reproduce
00012  *      and distribute executable files created using this software for use
00013  *      on NXP Semiconductors LPC family microcontroller devices only. Nothing
00014  *      else gives you the right to use this software.
00015  *
00016  * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.
00017  *----------------------------------------------------------------------------
00018  * History:
00019  *          V1.20 Added vendor specific requests
00020  *                Changed string descriptor handling
00021  *                Reworked Endpoint0
00022  *          V1.00 Initial Version
00023  *----------------------------------------------------------------------------*/
00024 
00025 #include "lpc_types.h"
00026 
00027 #include "usb.h"
00028 #include "usbcfg.h"
00029 #include "usbhw.h"
00030 #include "usbcore.h"
00031 #include "usbdesc.h"
00032 #include "usbuser.h"
00033 
00034 #if (USB_CLASS)
00035 
00036 #if (USB_AUDIO)
00037 #include "audio.h"
00038 #include "adcuser.h"
00039 #endif
00040 
00041 #if (USB_HID)
00042 #include "hid.h"
00043 #include "hiduser.h"
00044 #endif
00045 
00046 #if (USB_MSC)
00047 #include "msc.h"
00048 #include "mscuser.h"
00049 extern MSC_CSW CSW;
00050 #endif
00051 
00052 #if (USB_CDC)
00053 #include "cdc.h"
00054 #include "cdcuser.h"
00055 #endif
00056 
00057 #endif
00058 
00059 #if (USB_VENDOR)
00060 #include "vendor.h"
00061 #endif
00062 
00063 #if defined (  __CC_ARM__  )
00064 #pragma diag_suppress 111,177,1441
00065 #endif
00066 
00067 #if defined   (  __GNUC__  )
00068 #define __packed __attribute__((__packed__))
00069 #endif
00070 
00071 #if defined   (  __IAR_SYSTEMS_ICC__  )
00072 #define   __inline     inline
00073 #endif
00074 
00075 
00076 uint16_t  USB_DeviceStatus;
00077 uint8_t  USB_DeviceAddress;
00078 uint8_t  USB_Configuration;
00079 uint32_t USB_EndPointMask;
00080 uint32_t USB_EndPointHalt;
00081 uint32_t USB_EndPointStall;                         /* EP must stay stalled */
00082 uint8_t  USB_NumInterfaces;
00083 uint8_t  USB_AltSetting[USB_IF_NUM];
00084 
00085 uint8_t  EP0Buf[USB_MAX_PACKET0];
00086 
00087 
00088 USB_EP_DATA EP0Data;
00089 
00090 USB_SETUP_PACKET SetupPacket;
00091 
00092 
00093 /*
00094  *  Reset USB Core
00095  *    Parameters:      None
00096  *    Return Value:    None
00097  */
00098 
00099 void USB_ResetCore (void) {
00100 
00101   USB_DeviceStatus  = USB_POWER;
00102   USB_DeviceAddress = 0;
00103   USB_Configuration = 0;
00104   USB_EndPointMask  = 0x00010001;
00105   USB_EndPointHalt  = 0x00000000;
00106   USB_EndPointStall = 0x00000000;
00107 }
00108 
00109 
00110 /*
00111  *  USB Request - Setup Stage
00112  *    Parameters:      None (global SetupPacket)
00113  *    Return Value:    None
00114  */
00115 
00116 void USB_SetupStage (void) {
00117   USB_ReadEP(0x00, (uint8_t *)&SetupPacket);
00118 }
00119 
00120 
00121 /*
00122  *  USB Request - Data In Stage
00123  *    Parameters:      None (global EP0Data)
00124  *    Return Value:    None
00125  */
00126 
00127 void USB_DataInStage (void) {
00128   uint32_t cnt;
00129 
00130   if (EP0Data.Count > USB_MAX_PACKET0) {
00131     cnt = USB_MAX_PACKET0;
00132   } else {
00133     cnt = EP0Data.Count;
00134   }
00135   cnt = USB_WriteEP(0x80, EP0Data.pData, cnt);
00136   EP0Data.pData += cnt;
00137   EP0Data.Count -= cnt;
00138 }
00139 
00140 
00141 /*
00142  *  USB Request - Data Out Stage
00143  *    Parameters:      None (global EP0Data)
00144  *    Return Value:    None
00145  */
00146 
00147 void USB_DataOutStage (void) {
00148   uint32_t cnt;
00149 
00150   cnt = USB_ReadEP(0x00, EP0Data.pData);
00151   EP0Data.pData += cnt;
00152   EP0Data.Count -= cnt;
00153 }
00154 
00155 
00156 /*
00157  *  USB Request - Status In Stage
00158  *    Parameters:      None
00159  *    Return Value:    None
00160  */
00161 
00162 void USB_StatusInStage (void) {
00163   USB_WriteEP(0x80, NULL, 0);
00164 }
00165 
00166 
00167 /*
00168  *  USB Request - Status Out Stage
00169  *    Parameters:      None
00170  *    Return Value:    None
00171  */
00172 
00173 void USB_StatusOutStage (void) {
00174   USB_ReadEP(0x00, EP0Buf);
00175 }
00176 
00177 
00178 /*
00179  *  Get Status USB Request
00180  *    Parameters:      None (global SetupPacket)
00181  *    Return Value:    TRUE - Success, FALSE - Error
00182  */
00183 
00184 __inline uint32_t USB_ReqGetStatus (void) {
00185   uint32_t n, m;
00186 
00187   switch (SetupPacket.bmRequestType.BM.Recipient) {
00188     case REQUEST_TO_DEVICE:
00189       EP0Data.pData = (uint8_t *)&USB_DeviceStatus;
00190       break;
00191     case REQUEST_TO_INTERFACE:
00192       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
00193         *((__packed uint16_t *)EP0Buf) = 0;
00194         EP0Data.pData = EP0Buf;
00195       } else {
00196         return (FALSE);
00197       }
00198       break;
00199     case REQUEST_TO_ENDPOINT:
00200       n = SetupPacket.wIndex.WB.L & 0x8F;
00201       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00202       if (((USB_Configuration != 0) || ((n & 0x0F) == 0)) && (USB_EndPointMask & m)) {
00203         *((__packed uint16_t *)EP0Buf) = (USB_EndPointHalt & m) ? 1 : 0;
00204         EP0Data.pData = EP0Buf;
00205       } else {
00206         return (FALSE);
00207       }
00208       break;
00209     default:
00210       return (FALSE);
00211   }
00212   return (TRUE);
00213 }
00214 
00215 
00216 /*
00217  *  Set/Clear Feature USB Request
00218  *    Parameters:      sc:    0 - Clear, 1 - Set
00219  *                            (global SetupPacket)
00220  *    Return Value:    TRUE - Success, FALSE - Error
00221  */
00222 
00223 __inline uint32_t USB_ReqSetClrFeature (uint32_t sc) {
00224   uint32_t n, m;
00225 
00226   switch (SetupPacket.bmRequestType.BM.Recipient) {
00227     case REQUEST_TO_DEVICE:
00228       if (SetupPacket.wValue.W == USB_FEATURE_REMOTE_WAKEUP) {
00229         if (sc) {
00230           USB_WakeUpCfg(TRUE);
00231           USB_DeviceStatus |=  USB_GETSTATUS_REMOTE_WAKEUP;
00232         } else {
00233           USB_WakeUpCfg(FALSE);
00234           USB_DeviceStatus &= ~USB_GETSTATUS_REMOTE_WAKEUP;
00235         }
00236       } else {
00237         return (FALSE);
00238       }
00239       break;
00240     case REQUEST_TO_INTERFACE:
00241       return (FALSE);
00242     case REQUEST_TO_ENDPOINT:
00243       n = SetupPacket.wIndex.WB.L & 0x8F;
00244       m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00245       if ((USB_Configuration != 0) && ((n & 0x0F) != 0) && (USB_EndPointMask & m)) {
00246         if (SetupPacket.wValue.W == USB_FEATURE_ENDPOINT_STALL) {
00247           if (sc) {
00248             USB_SetStallEP(n);
00249             USB_EndPointHalt |=  m;
00250           } else {
00251             if ((USB_EndPointStall & m) != 0) {
00252               return (TRUE);
00253             }
00254             USB_ClrStallEP(n);
00255 #if (USB_MSC)
00256             if ((n == MSC_EP_IN) && ((USB_EndPointHalt & m) != 0)) {
00257               /* Compliance Test: rewrite CSW after unstall */
00258               if (CSW.dSignature == MSC_CSW_Signature) {
00259                 USB_WriteEP(MSC_EP_IN, (uint8_t *)&CSW, sizeof(CSW));
00260               }
00261             }
00262 #endif
00263             USB_EndPointHalt &= ~m;
00264           }
00265         } else {
00266           return (FALSE);
00267         }
00268       } else {
00269         return (FALSE);
00270       }
00271       break;
00272     default:
00273       return (FALSE);
00274   }
00275   return (TRUE);
00276 }
00277 
00278 
00279 /*
00280  *  Set Address USB Request
00281  *    Parameters:      None (global SetupPacket)
00282  *    Return Value:    TRUE - Success, FALSE - Error
00283  */
00284 
00285 __inline uint32_t USB_ReqSetAddress (void) {
00286 
00287   switch (SetupPacket.bmRequestType.BM.Recipient) {
00288     case REQUEST_TO_DEVICE:
00289       USB_DeviceAddress = 0x80 | SetupPacket.wValue.WB.L;
00290       break;
00291     default:
00292       return (FALSE);
00293   }
00294   return (TRUE);
00295 }
00296 
00297 
00298 /*
00299  *  Get Descriptor USB Request
00300  *    Parameters:      None (global SetupPacket)
00301  *    Return Value:    TRUE - Success, FALSE - Error
00302  */
00303 
00304 __inline uint32_t USB_ReqGetDescriptor (void) {
00305   uint8_t  *pD;
00306   uint32_t len, n;
00307 
00308   switch (SetupPacket.bmRequestType.BM.Recipient) {
00309     case REQUEST_TO_DEVICE:
00310       switch (SetupPacket.wValue.WB.H) {
00311         case USB_DEVICE_DESCRIPTOR_TYPE:
00312           EP0Data.pData = (uint8_t *)USB_DeviceDescriptor;
00313           len = USB_DEVICE_DESC_SIZE;
00314           break;
00315         case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00316           pD = (uint8_t *)USB_ConfigDescriptor;
00317           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
00318             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength != 0) {
00319               pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00320             }
00321           }
00322           if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bLength == 0) {
00323             return (FALSE);
00324           }
00325           EP0Data.pData = pD;
00326           len = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00327           break;
00328         case USB_STRING_DESCRIPTOR_TYPE:
00329           pD = (uint8_t *)USB_StringDescriptor;
00330           for (n = 0; n != SetupPacket.wValue.WB.L; n++) {
00331             if (((USB_STRING_DESCRIPTOR *)pD)->bLength != 0) {
00332               pD += ((USB_STRING_DESCRIPTOR *)pD)->bLength;
00333             }
00334           }
00335           if (((USB_STRING_DESCRIPTOR *)pD)->bLength == 0) {
00336             return (FALSE);
00337           }
00338           EP0Data.pData = pD;
00339           len = ((USB_STRING_DESCRIPTOR *)EP0Data.pData)->bLength;
00340           break;
00341         default:
00342           return (FALSE);
00343       }
00344       break;
00345     case REQUEST_TO_INTERFACE:
00346       switch (SetupPacket.wValue.WB.H) {
00347 #if USB_HID
00348         case HID_HID_DESCRIPTOR_TYPE:
00349           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
00350             return (FALSE);    /* Only Single HID Interface is supported */
00351           }
00352           EP0Data.pData = (uint8_t *)USB_ConfigDescriptor + HID_DESC_OFFSET;
00353           len = HID_DESC_SIZE;
00354           break;
00355         case HID_REPORT_DESCRIPTOR_TYPE:
00356           if (SetupPacket.wIndex.WB.L != USB_HID_IF_NUM) {
00357             return (FALSE);    /* Only Single HID Interface is supported */
00358           }
00359           EP0Data.pData = (uint8_t *)HID_ReportDescriptor;
00360           len = HID_ReportDescSize;
00361           break;
00362         case HID_PHYSICAL_DESCRIPTOR_TYPE:
00363           return (FALSE);      /* HID Physical Descriptor is not supported */
00364 #endif
00365         default:
00366           return (FALSE);
00367       }
00368 //      break;
00369     default:
00370       return (FALSE);
00371   }
00372 
00373   if (EP0Data.Count > len) {
00374     EP0Data.Count = len;
00375   }
00376 
00377   return (TRUE);
00378 }
00379 
00380 
00381 /*
00382  *  Get Configuration USB Request
00383  *    Parameters:      None (global SetupPacket)
00384  *    Return Value:    TRUE - Success, FALSE - Error
00385  */
00386 
00387 __inline uint32_t USB_ReqGetConfiguration (void) {
00388 
00389   switch (SetupPacket.bmRequestType.BM.Recipient) {
00390     case REQUEST_TO_DEVICE:
00391       EP0Data.pData = &USB_Configuration;
00392       break;
00393     default:
00394       return (FALSE);
00395   }
00396   return (TRUE);
00397 }
00398 
00399 
00400 /*
00401  *  Set Configuration USB Request
00402  *    Parameters:      None (global SetupPacket)
00403  *    Return Value:    TRUE - Success, FALSE - Error
00404  */
00405 
00406 __inline uint32_t USB_ReqSetConfiguration (void) {
00407   USB_COMMON_DESCRIPTOR *pD;
00408   uint32_t alt = 0;
00409   uint32_t n, m;
00410   uint32_t tmp;
00411 
00412   switch (SetupPacket.bmRequestType.BM.Recipient) {
00413     case REQUEST_TO_DEVICE:
00414 
00415       if (SetupPacket.wValue.WB.L) {
00416         pD = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
00417         while (pD->bLength) {
00418           switch (pD->bDescriptorType) {
00419             case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00420               if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue == SetupPacket.wValue.WB.L) {
00421                 USB_Configuration = SetupPacket.wValue.WB.L;
00422                 USB_NumInterfaces = ((USB_CONFIGURATION_DESCRIPTOR *)pD)->bNumInterfaces;
00423                 for (n = 0; n < USB_IF_NUM; n++) {
00424                   USB_AltSetting[n] = 0;
00425                 }
00426                 for (n = 1; n < 16; n++) {
00427                   if (USB_EndPointMask & (1 << n)) {
00428                     USB_DisableEP(n);
00429                   }
00430                   if (USB_EndPointMask & ((1 << 16) << n)) {
00431                     USB_DisableEP(n | 0x80);
00432                   }
00433                 }
00434                 USB_EndPointMask = 0x00010001;
00435                 USB_EndPointHalt = 0x00000000;
00436                 USB_EndPointStall= 0x00000000;
00437                 USB_Configure(TRUE);
00438                 if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bmAttributes & USB_CONFIG_POWERED_MASK) {
00439                   USB_DeviceStatus |=  USB_GETSTATUS_SELF_POWERED;
00440                 } else {
00441                   USB_DeviceStatus &= ~USB_GETSTATUS_SELF_POWERED;
00442                 }
00443               } else {
00444 //                (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00445                 tmp = (uint32_t)pD;
00446                 tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00447                 pD = (USB_COMMON_DESCRIPTOR *)tmp;
00448                 continue;
00449               }
00450               break;
00451             case USB_INTERFACE_DESCRIPTOR_TYPE:
00452               alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
00453               break;
00454             case USB_ENDPOINT_DESCRIPTOR_TYPE:
00455               if (alt == 0) {
00456                 n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
00457                 m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00458                 USB_EndPointMask |= m;
00459                 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
00460                 USB_EnableEP(n);
00461                 USB_ResetEP(n);
00462               }
00463               break;
00464           }
00465 //          (uint8_t *)pD += pD->bLength;
00466             tmp = (uint32_t)pD;
00467             tmp += pD->bLength;
00468             pD = (USB_COMMON_DESCRIPTOR *)tmp;
00469         }
00470       }
00471       else {
00472         USB_Configuration = 0;
00473         for (n = 1; n < 16; n++) {
00474           if (USB_EndPointMask & (1 << n)) {
00475             USB_DisableEP(n);
00476           }
00477           if (USB_EndPointMask & ((1 << 16) << n)) {
00478             USB_DisableEP(n | 0x80);
00479           }
00480         }
00481         USB_EndPointMask  = 0x00010001;
00482         USB_EndPointHalt  = 0x00000000;
00483         USB_EndPointStall = 0x00000000;
00484         USB_Configure(FALSE);
00485       }
00486 
00487       if (USB_Configuration != SetupPacket.wValue.WB.L) {
00488         return (FALSE);
00489       }
00490       break;
00491     default:
00492       return (FALSE);
00493   }
00494   return (TRUE);
00495 }
00496 
00497 
00498 /*
00499  *  Get Interface USB Request
00500  *    Parameters:      None (global SetupPacket)
00501  *    Return Value:    TRUE - Success, FALSE - Error
00502  */
00503 
00504 __inline uint32_t USB_ReqGetInterface (void) {
00505 
00506   switch (SetupPacket.bmRequestType.BM.Recipient) {
00507     case REQUEST_TO_INTERFACE:
00508       if ((USB_Configuration != 0) && (SetupPacket.wIndex.WB.L < USB_NumInterfaces)) {
00509         EP0Data.pData = USB_AltSetting + SetupPacket.wIndex.WB.L;
00510       } else {
00511         return (FALSE);
00512       }
00513       break;
00514     default:
00515       return (FALSE);
00516   }
00517   return (TRUE);
00518 }
00519 
00520 
00521 /*
00522  *  Set Interface USB Request
00523  *    Parameters:      None (global SetupPacket)
00524  *    Return Value:    TRUE - Success, FALSE - Error
00525  */
00526 
00527 __inline uint32_t USB_ReqSetInterface (void) {
00528   USB_COMMON_DESCRIPTOR *pD;
00529   uint32_t ifn = 0, alt = 0, old = 0, msk = 0;
00530   uint32_t n, m;
00531   uint32_t set;
00532   uint32_t tmp;
00533 
00534   switch (SetupPacket.bmRequestType.BM.Recipient) {
00535     case REQUEST_TO_INTERFACE:
00536       if (USB_Configuration == 0) return (FALSE);
00537       set = FALSE;
00538       pD  = (USB_COMMON_DESCRIPTOR *)USB_ConfigDescriptor;
00539       while (pD->bLength) {
00540         switch (pD->bDescriptorType) {
00541           case USB_CONFIGURATION_DESCRIPTOR_TYPE:
00542             if (((USB_CONFIGURATION_DESCRIPTOR *)pD)->bConfigurationValue != USB_Configuration) {
00543 //              (uint8_t *)pD += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00544                 tmp = (uint32_t)pD;
00545                 tmp += ((USB_CONFIGURATION_DESCRIPTOR *)pD)->wTotalLength;
00546                 pD = (USB_COMMON_DESCRIPTOR *)tmp;
00547                 continue;
00548             }
00549             break;
00550           case USB_INTERFACE_DESCRIPTOR_TYPE:
00551             ifn = ((USB_INTERFACE_DESCRIPTOR *)pD)->bInterfaceNumber;
00552             alt = ((USB_INTERFACE_DESCRIPTOR *)pD)->bAlternateSetting;
00553             msk = 0;
00554             if ((ifn == SetupPacket.wIndex.WB.L) && (alt == SetupPacket.wValue.WB.L)) {
00555               set = TRUE;
00556               old = USB_AltSetting[ifn];
00557               USB_AltSetting[ifn] = (uint8_t)alt;
00558             }
00559             break;
00560           case USB_ENDPOINT_DESCRIPTOR_TYPE:
00561             if (ifn == SetupPacket.wIndex.WB.L) {
00562               n = ((USB_ENDPOINT_DESCRIPTOR *)pD)->bEndpointAddress & 0x8F;
00563               m = (n & 0x80) ? ((1 << 16) << (n & 0x0F)) : (1 << n);
00564               if (alt == SetupPacket.wValue.WB.L) {
00565                 USB_EndPointMask |=  m;
00566                 USB_EndPointHalt &= ~m;
00567                 USB_ConfigEP((USB_ENDPOINT_DESCRIPTOR *)pD);
00568                 USB_EnableEP(n);
00569                 USB_ResetEP(n);
00570                 msk |= m;
00571               }
00572               else if ((alt == old) && ((msk & m) == 0)) {
00573                 USB_EndPointMask &= ~m;
00574                 USB_EndPointHalt &= ~m;
00575                 USB_DisableEP(n);
00576               }
00577             }
00578            break;
00579         }
00580 //        (uint8_t *)pD += pD->bLength;
00581             tmp = (uint32_t)pD;
00582             tmp += pD->bLength;
00583             pD = (USB_COMMON_DESCRIPTOR *)tmp;
00584       }
00585       break;
00586     default:
00587       return (FALSE);
00588   }
00589 
00590   return (set);
00591 }
00592 
00593 
00594 /*
00595  *  USB Endpoint 0 Event Callback
00596  *    Parameters:      event
00597  *    Return Value:    none
00598  */
00599 
00600 void USB_EndPoint0 (uint32_t event) {
00601 
00602   switch (event) {
00603     case USB_EVT_SETUP:
00604       USB_SetupStage();
00605       USB_DirCtrlEP(SetupPacket.bmRequestType.BM.Dir);
00606       EP0Data.Count = SetupPacket.wLength;     /* Number of bytes to transfer */
00607       switch (SetupPacket.bmRequestType.BM.Type) {
00608 
00609         case REQUEST_STANDARD:
00610           switch (SetupPacket.bRequest) {
00611             case USB_REQUEST_GET_STATUS:
00612               if (!USB_ReqGetStatus()) {
00613                 goto stall_i;
00614               }
00615               USB_DataInStage();
00616               break;
00617 
00618             case USB_REQUEST_CLEAR_FEATURE:
00619               if (!USB_ReqSetClrFeature(0)) {
00620                 goto stall_i;
00621               }
00622               USB_StatusInStage();
00623 #if USB_FEATURE_EVENT
00624               USB_Feature_Event();
00625 #endif
00626               break;
00627 
00628             case USB_REQUEST_SET_FEATURE:
00629               if (!USB_ReqSetClrFeature(1)) {
00630                 goto stall_i;
00631               }
00632               USB_StatusInStage();
00633 #if USB_FEATURE_EVENT
00634               USB_Feature_Event();
00635 #endif
00636               break;
00637 
00638             case USB_REQUEST_SET_ADDRESS:
00639               if (!USB_ReqSetAddress()) {
00640                 goto stall_i;
00641               }
00642               USB_StatusInStage();
00643               break;
00644 
00645             case USB_REQUEST_GET_DESCRIPTOR:
00646               if (!USB_ReqGetDescriptor()) {
00647                 goto stall_i;
00648               }
00649               USB_DataInStage();
00650               break;
00651 
00652             case USB_REQUEST_SET_DESCRIPTOR:
00653 /*stall_o:*/  USB_SetStallEP(0x00);            /* not supported */
00654               EP0Data.Count = 0;
00655               break;
00656 
00657             case USB_REQUEST_GET_CONFIGURATION:
00658               if (!USB_ReqGetConfiguration()) {
00659                 goto stall_i;
00660               }
00661               USB_DataInStage();
00662               break;
00663 
00664             case USB_REQUEST_SET_CONFIGURATION:
00665               if (!USB_ReqSetConfiguration()) {
00666                 goto stall_i;
00667               }
00668               USB_StatusInStage();
00669 #if USB_CONFIGURE_EVENT
00670               USB_Configure_Event();
00671 #endif
00672               break;
00673 
00674             case USB_REQUEST_GET_INTERFACE:
00675               if (!USB_ReqGetInterface()) {
00676                 goto stall_i;
00677               }
00678               USB_DataInStage();
00679               break;
00680 
00681             case USB_REQUEST_SET_INTERFACE:
00682               if (!USB_ReqSetInterface()) {
00683                 goto stall_i;
00684               }
00685               USB_StatusInStage();
00686 #if USB_INTERFACE_EVENT
00687               USB_Interface_Event();
00688 #endif
00689               break;
00690 
00691             default:
00692               goto stall_i;
00693           }
00694           break;  /* end case REQUEST_STANDARD */
00695 
00696 #if USB_CLASS
00697         case REQUEST_CLASS:
00698           switch (SetupPacket.bmRequestType.BM.Recipient) {
00699 
00700             case REQUEST_TO_DEVICE:
00701               goto stall_i;                                              /* not supported */
00702 
00703             case REQUEST_TO_INTERFACE:
00704 #if USB_HID
00705               if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {           /* IF number correct? */
00706                 switch (SetupPacket.bRequest) {
00707                   case HID_REQUEST_GET_REPORT:
00708                     if (HID_GetReport()) {
00709                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00710                       USB_DataInStage();                                 /* send requested data */
00711                       goto setup_class_ok;
00712                     }
00713                     break;
00714                   case HID_REQUEST_SET_REPORT:
00715                     EP0Data.pData = EP0Buf;                              /* data to be received */
00716                     goto setup_class_ok;
00717                   case HID_REQUEST_GET_IDLE:
00718                     if (HID_GetIdle()) {
00719                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00720                       USB_DataInStage();                                 /* send requested data */
00721                       goto setup_class_ok;
00722                     }
00723                     break;
00724                   case HID_REQUEST_SET_IDLE:
00725                     if (HID_SetIdle()) {
00726                       USB_StatusInStage();                               /* send Acknowledge */
00727                       goto setup_class_ok;
00728                     }
00729                     break;
00730                   case HID_REQUEST_GET_PROTOCOL:
00731                     if (HID_GetProtocol()) {
00732                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00733                       USB_DataInStage();                                 /* send requested data */
00734                       goto setup_class_ok;
00735                     }
00736                     break;
00737                   case HID_REQUEST_SET_PROTOCOL:
00738                     if (HID_SetProtocol()) {
00739                       USB_StatusInStage();                               /* send Acknowledge */
00740                       goto setup_class_ok;
00741                     }
00742                     break;
00743                 }
00744               }
00745 #endif  /* USB_HID */
00746 #if USB_MSC
00747               if (SetupPacket.wIndex.WB.L == USB_MSC_IF_NUM) {           /* IF number correct? */
00748                 switch (SetupPacket.bRequest) {
00749                   case MSC_REQUEST_RESET:
00750                     if ((SetupPacket.wValue.W == 0) &&                   /* RESET with invalid parameters -> STALL */
00751                         (SetupPacket.wLength  == 0)) {
00752                       if (MSC_Reset()) {
00753                         USB_StatusInStage();
00754                         goto setup_class_ok;
00755                       }
00756                     }
00757                     break;
00758                   case MSC_REQUEST_GET_MAX_LUN:
00759                     if ((SetupPacket.wValue.W == 0) &&                   /* GET_MAX_LUN with invalid parameters -> STALL */
00760                         (SetupPacket.wLength  == 1)) {
00761                       if (MSC_GetMaxLUN()) {
00762                         EP0Data.pData = EP0Buf;
00763                         USB_DataInStage();
00764                         goto setup_class_ok;
00765                       }
00766                     }
00767                     break;
00768                 }
00769               }
00770 #endif  /* USB_MSC */
00771 #if USB_AUDIO
00772               if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  ||       /* IF number correct? */
00773                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
00774                   (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
00775                 switch (SetupPacket.bRequest) {
00776                   case AUDIO_REQUEST_GET_CUR:
00777                   case AUDIO_REQUEST_GET_MIN:
00778                   case AUDIO_REQUEST_GET_MAX:
00779                   case AUDIO_REQUEST_GET_RES:
00780                     if (ADC_IF_GetRequest()) {
00781                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00782                       USB_DataInStage();                                 /* send requested data */
00783                       goto setup_class_ok;
00784                     }
00785                     break;
00786                   case AUDIO_REQUEST_SET_CUR:
00787 //                case AUDIO_REQUEST_SET_MIN:
00788 //                case AUDIO_REQUEST_SET_MAX:
00789 //                case AUDIO_REQUEST_SET_RES:
00790                     EP0Data.pData = EP0Buf;                              /* data to be received */
00791                     goto setup_class_ok;
00792                 }
00793               }
00794 #endif  /* USB_AUDIO */
00795 #if USB_CDC
00796               if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  ||       /* IF number correct? */
00797                   (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
00798                 switch (SetupPacket.bRequest) {
00799                   case CDC_SEND_ENCAPSULATED_COMMAND:
00800                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
00801                     goto setup_class_ok;
00802                   case CDC_GET_ENCAPSULATED_RESPONSE:
00803                     if (CDC_GetEncapsulatedResponse()) {
00804                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00805                       USB_DataInStage();                                 /* send requested data */
00806                       goto setup_class_ok;
00807                     }
00808                     break;
00809                   case CDC_SET_COMM_FEATURE:
00810                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
00811                     goto setup_class_ok;
00812                   case CDC_GET_COMM_FEATURE:
00813                     if (CDC_GetCommFeature(SetupPacket.wValue.W)) {
00814                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00815                       USB_DataInStage();                                 /* send requested data */
00816                       goto setup_class_ok;
00817                     }
00818                     break;
00819                   case CDC_CLEAR_COMM_FEATURE:
00820                     if (CDC_ClearCommFeature(SetupPacket.wValue.W)) {
00821                       USB_StatusInStage();                               /* send Acknowledge */
00822                       goto setup_class_ok;
00823                     }
00824                     break;
00825                   case CDC_SET_LINE_CODING:
00826                     EP0Data.pData = EP0Buf;                              /* data to be received, see USB_EVT_OUT */
00827                     goto setup_class_ok;
00828                   case CDC_GET_LINE_CODING:
00829                     if (CDC_GetLineCoding()) {
00830                       EP0Data.pData = EP0Buf;                            /* point to data to be sent */
00831                       USB_DataInStage();                                 /* send requested data */
00832                       goto setup_class_ok;
00833                     }
00834                     break;
00835                   case CDC_SET_CONTROL_LINE_STATE:
00836                     if (CDC_SetControlLineState(SetupPacket.wValue.W)) {
00837                       USB_StatusInStage();                               /* send Acknowledge */
00838                       goto setup_class_ok;
00839                     }
00840                     break;
00841                   case CDC_SEND_BREAK:
00842                     if (CDC_SendBreak(SetupPacket.wValue.W)) {
00843                       USB_StatusInStage();                               /* send Acknowledge */
00844                       goto setup_class_ok;
00845                     }
00846                     break;
00847                 }
00848               }
00849 #endif  /* USB_CDC */
00850               goto stall_i;                                              /* not supported */
00851               /* end case REQUEST_TO_INTERFACE */
00852 
00853             case REQUEST_TO_ENDPOINT:
00854 #if USB_AUDIO
00855               switch (SetupPacket.bRequest) {
00856                 case AUDIO_REQUEST_GET_CUR:
00857                 case AUDIO_REQUEST_GET_MIN:
00858                 case AUDIO_REQUEST_GET_MAX:
00859                 case AUDIO_REQUEST_GET_RES:
00860                   if (ADC_EP_GetRequest()) {
00861                     EP0Data.pData = EP0Buf;                              /* point to data to be sent */
00862                     USB_DataInStage();                                   /* send requested data */
00863                     goto setup_class_ok;
00864                   }
00865                   break;
00866                 case AUDIO_REQUEST_SET_CUR:
00867 //              case AUDIO_REQUEST_SET_MIN:
00868 //              case AUDIO_REQUEST_SET_MAX:
00869 //              case AUDIO_REQUEST_SET_RES:
00870                   EP0Data.pData = EP0Buf;                                /* data to be received */
00871                   goto setup_class_ok;
00872               }
00873 #endif  /* USB_AUDIO */
00874               goto stall_i;
00875               /* end case REQUEST_TO_ENDPOINT */
00876 
00877             default:
00878               goto stall_i;
00879           }
00880 setup_class_ok:                                                          /* request finished successfully */
00881           break;  /* end case REQUEST_CLASS */
00882 #endif  /* USB_CLASS */
00883 
00884 #if USB_VENDOR
00885         case REQUEST_VENDOR:
00886           switch (SetupPacket.bmRequestType.BM.Recipient) {
00887 
00888             case REQUEST_TO_DEVICE:
00889               if (!USB_ReqVendorDev(TRUE)) {
00890                 goto stall_i;                                            /* not supported */
00891               }
00892               break;
00893 
00894             case REQUEST_TO_INTERFACE:
00895               if (!USB_ReqVendorIF(TRUE)) {
00896                 goto stall_i;                                            /* not supported */
00897               }
00898               break;
00899 
00900             case REQUEST_TO_ENDPOINT:
00901               if (!USB_ReqVendorEP(TRUE)) {
00902                 goto stall_i;                                            /* not supported */
00903               }
00904               break;
00905 
00906             default:
00907               goto stall_i;
00908           }
00909 
00910           if (SetupPacket.wLength) {
00911             if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
00912               USB_DataInStage();
00913             }
00914           } else {
00915             USB_StatusInStage();
00916           }
00917 
00918           break;  /* end case REQUEST_VENDOR */
00919 #endif  /* USB_VENDOR */
00920 
00921         default:
00922 stall_i:  USB_SetStallEP(0x80);
00923           EP0Data.Count = 0;
00924           break;
00925       }
00926       break;  /* end case USB_EVT_SETUP */
00927 
00928     case USB_EVT_OUT:
00929       if (SetupPacket.bmRequestType.BM.Dir == REQUEST_HOST_TO_DEVICE) {
00930         if (EP0Data.Count) {                                             /* still data to receive ? */
00931           USB_DataOutStage();                                            /* receive data */
00932           if (EP0Data.Count == 0) {                                      /* data complete ? */
00933             switch (SetupPacket.bmRequestType.BM.Type) {
00934 
00935               case REQUEST_STANDARD:
00936                 goto stall_i;                                            /* not supported */
00937 
00938 #if (USB_CLASS)
00939               case REQUEST_CLASS:
00940                 switch (SetupPacket.bmRequestType.BM.Recipient) {
00941                   case REQUEST_TO_DEVICE:
00942                     goto stall_i;                                        /* not supported */
00943 
00944                   case REQUEST_TO_INTERFACE:
00945 #if USB_HID
00946                     if (SetupPacket.wIndex.WB.L == USB_HID_IF_NUM) {     /* IF number correct? */
00947                       switch (SetupPacket.bRequest) {
00948                         case HID_REQUEST_SET_REPORT:
00949                           if (HID_SetReport()) {
00950                             USB_StatusInStage();                         /* send Acknowledge */
00951                             goto out_class_ok;
00952                           }
00953                           break;
00954                       }
00955                     }
00956 #endif  /* USB_HID */
00957 #if USB_AUDIO
00958                     if ((SetupPacket.wIndex.WB.L == USB_ADC_CIF_NUM)  || /* IF number correct? */
00959                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF1_NUM) ||
00960                         (SetupPacket.wIndex.WB.L == USB_ADC_SIF2_NUM)) {
00961                       switch (SetupPacket.bRequest) {
00962                         case AUDIO_REQUEST_SET_CUR:
00963 //                      case AUDIO_REQUEST_SET_MIN:
00964 //                      case AUDIO_REQUEST_SET_MAX:
00965 //                      case AUDIO_REQUEST_SET_RES:
00966                           if (ADC_IF_SetRequest()) {
00967                             USB_StatusInStage();                         /* send Acknowledge */
00968                             goto out_class_ok;
00969                           }
00970                           break;
00971                       }
00972                     }
00973 #endif  /* USB_AUDIO */
00974 #if USB_CDC
00975                     if ((SetupPacket.wIndex.WB.L == USB_CDC_CIF_NUM)  || /* IF number correct? */
00976                         (SetupPacket.wIndex.WB.L == USB_CDC_DIF_NUM)) {
00977                       switch (SetupPacket.bRequest) {
00978                         case CDC_SEND_ENCAPSULATED_COMMAND:
00979                           if (CDC_SendEncapsulatedCommand()) {
00980                             USB_StatusInStage();                         /* send Acknowledge */
00981                             goto out_class_ok;
00982                           }
00983                           break;
00984                         case CDC_SET_COMM_FEATURE:
00985                           if (CDC_SetCommFeature(SetupPacket.wValue.W)) {
00986                             USB_StatusInStage();                         /* send Acknowledge */
00987                             goto out_class_ok;
00988                           }
00989                           break;
00990                         case CDC_SET_LINE_CODING:
00991                           if (CDC_SetLineCoding()) {
00992                             USB_StatusInStage();                         /* send Acknowledge */
00993                             goto out_class_ok;
00994                           }
00995                           break;
00996                       }
00997                     }
00998 #endif  /* USB_CDC */
00999                     goto stall_i;
01000                     /* end case REQUEST_TO_INTERFACE */
01001 
01002                   case REQUEST_TO_ENDPOINT:
01003 #if USB_AUDIO
01004                     switch (SetupPacket.bRequest) {
01005                       case AUDIO_REQUEST_SET_CUR:
01006 //                    case AUDIO_REQUEST_SET_MIN:
01007 //                    case AUDIO_REQUEST_SET_MAX:
01008 //                    case AUDIO_REQUEST_SET_RES:
01009                         if (ADC_EP_SetRequest()) {
01010                           USB_StatusInStage();                           /* send Acknowledge */
01011                           goto out_class_ok;
01012                         }
01013                         break;
01014                     }
01015 #endif  /* USB_AUDIO */
01016                     goto stall_i;
01017                     /* end case REQUEST_TO_ENDPOINT */
01018 
01019                   default:
01020                     goto stall_i;
01021                 }
01022 out_class_ok:                                                            /* request finished successfully */
01023                 break; /* end case REQUEST_CLASS */
01024 #endif  /* USB_CLASS */
01025 
01026 #if USB_VENDOR
01027               case REQUEST_VENDOR:
01028                 switch (SetupPacket.bmRequestType.BM.Recipient) {
01029 
01030                   case REQUEST_TO_DEVICE:
01031                     if (!USB_ReqVendorDev(FALSE)) {
01032                       goto stall_i;                                      /* not supported */
01033                     }
01034                     break;
01035 
01036                   case REQUEST_TO_INTERFACE:
01037                     if (!USB_ReqVendorIF(FALSE)) {
01038                       goto stall_i;                                      /* not supported */
01039                     }
01040                     break;
01041 
01042                   case REQUEST_TO_ENDPOINT:
01043                     if (!USB_ReqVendorEP(FALSE)) {
01044                       goto stall_i;                                      /* not supported */
01045                     }
01046                     break;
01047 
01048                   default:
01049                     goto stall_i;
01050                 }
01051 
01052                 USB_StatusInStage();
01053 
01054                 break;  /* end case REQUEST_VENDOR */
01055 #endif  /* USB_VENDOR */
01056 
01057               default:
01058                 goto stall_i;
01059             }
01060           }
01061         }
01062       } else {
01063         USB_StatusOutStage();                                            /* receive Acknowledge */
01064       }
01065       break;  /* end case USB_EVT_OUT */
01066 
01067     case USB_EVT_IN :
01068       if (SetupPacket.bmRequestType.BM.Dir == REQUEST_DEVICE_TO_HOST) {
01069         USB_DataInStage();                                               /* send data */
01070       } else {
01071         if (USB_DeviceAddress & 0x80) {
01072           USB_DeviceAddress &= 0x7F;
01073           USB_SetAddress(USB_DeviceAddress);
01074         }
01075       }
01076       break;  /* end case USB_EVT_IN */
01077 
01078     case USB_EVT_OUT_STALL:
01079       USB_ClrStallEP(0x00);
01080       break;
01081 
01082     case USB_EVT_IN_STALL:
01083       USB_ClrStallEP(0x80);
01084       break;
01085 
01086   }
01087 }