usb_9.c
上传用户:sourcesun
上传日期:2013-09-23
资源大小:362k
文件大小:41k
源码类别:

DNA

开发平台:

Asm

  1. /*************************************************************************
  2.  *
  3.  *    Used with ICCARM and AARM.
  4.  *
  5.  *    (c) Copyright IAR Systems 2003
  6.  *
  7.  *    File name   : usb_9.c
  8.  *    Description : USB chapter 9 (device framework) module
  9.  *
  10.  *    History :
  11.  *    1. Data        : August 5, 2005
  12.  *       Author      : Stanimir Bonev
  13.  *       Description : Create
  14.  *    2. Data        : December 19, 2005
  15.  *       Author      : Stanimir Bonev
  16.  *       Description : Modify
  17.  *        Modify user callback function for EP0
  18.  *        Change prameter transfered to function form packet type to
  19.  *        pointer to UsbEp0Ctrl structure
  20.  *
  21.  *    $Revision: 1.1.2.1 $
  22. **************************************************************************/
  23. #include "usb_9.h"
  24. UsbEpCtrl_t UsbEp0Ctrl;
  25. UsbSetupPacket_t UsbEp0SetupPacket;
  26. #pragma data_alignment=4
  27. Int8U EpCtrlDataBuf[Ep0MaxSize];
  28. UsbDevCtrl_t UsbDevCtrl = {{UsbDevStatusUnknow,UsbDevStatusNoSuspend},0,0,0,0};
  29. // User funcrions
  30. CommUserFpnt_t UsbCoreUserFun[UsbUserLastInd] =
  31. {
  32.   // UsbUserClass
  33.   NULL,
  34.   // UsbUserVendor
  35.   NULL,
  36.   // UsbClassEp0OutPacket
  37.   NULL,
  38.   // UsbVendorEp0OutPacket
  39.   NULL,
  40.   // UsbUserConfigure
  41.   NULL,
  42.   // UsbUserReset
  43.   NULL,
  44.   // UsbUserConnect
  45.   NULL,
  46.   // UsbUserSuspend
  47.   NULL,
  48. };
  49. /*************************************************************************
  50.  * Function Name: UsbCoreInit
  51.  * Parameters:  CommUserFpnt_t UserCoreConfigure
  52.  *
  53.  * Return: none
  54.  *
  55.  * Description: USB init core
  56.  *
  57.  *************************************************************************/
  58. void UsbCoreInit (CommUserFpnt_t UserCoreConfigure)
  59. {
  60.   UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  61.   UserCoreConfigure(NULL);
  62.   UsbCoreUserFuncRegistered(UserCoreConfigure,UsbUserConfigure);
  63.   UsbSetDevState(UsbDevStatusUnknow);
  64.   // Registerd Core functions
  65.   USB_UserFuncRegistered(UsbDevResetCallback  ,UsbResetEvent  );
  66.   USB_UserFuncRegistered(UsbDevConnectCallback,UsbConnectEvent);
  67.   USB_UserFuncRegistered(UsbDevSuspendCallback,UsbSuspendEvent);
  68.   USB_UserFuncRegistered(UsbCtrlEpCallback    ,UsbEp0Out      );
  69.   USB_UserFuncRegistered(UsbCtrlEpCallback    ,UsbEp0In       );
  70. }
  71. /*************************************************************************
  72.  * Function Name: UsbCoreUserFuncRegistered
  73.  * Parameters: CommUserFpnt_t UserFunc, UsbCoreUserFuncInd_t UserFuncInd
  74.  *
  75.  * Return: CommUserFpnt_t
  76.  *
  77.  * Description: Registed User core callback function
  78.  *
  79.  *************************************************************************/
  80. CommUserFpnt_t UsbCoreUserFuncRegistered (CommUserFpnt_t UserCoreFunc,
  81.                                           UsbCoreUserFuncInd_t UserFuncInd)
  82. {
  83. CommUserFpnt_t PrevUserFunc = UsbCoreUserFun[UserFuncInd];
  84.   UsbCoreUserFun[UserFuncInd] = UserCoreFunc;
  85.   return(PrevUserFunc);
  86. }
  87. /*************************************************************************
  88.  * Function Name: UsbCoreReq
  89.  * Parameters:  UsbCoreReqType_t Type
  90.  *
  91.  * Return: Int32U
  92.  *
  93.  * Description: Return different device states
  94.  *
  95.  *************************************************************************/
  96. Int32U UsbCoreReq (UsbCoreReqType_t Type)
  97. {
  98.   switch(Type)
  99.   {
  100.   case UsbCoreReqDevState:
  101.     return(UsbDevCtrl.State.DS);
  102.   case UsbCoreReqDevSusState:
  103.     return(UsbDevCtrl.State.Suspend);
  104.   case UsbCoreReqConfiquration:
  105.     return(UsbDevCtrl.Configuration);
  106.   case UsbCoreReqInterface:
  107.     return(UsbDevCtrl.Interface);
  108.   case UsbCoreReqAlternateSetting:
  109.     return(UsbDevCtrl.AlternateSetting);
  110.   case UsbCoreReqDevOwnAddress:
  111.     return(UsbDevCtrl.DevAdd);
  112.   case UsbCoreReqWakeUpEnableStatus:
  113.     return(UsbDevCtrl.Feature.RemoteWakeupEnable);
  114.   case UsbCoreReqSelfPoweredStatus:
  115.     return(UsbDevCtrl.Feature.SelfPowered);
  116.   }
  117.   return((Int32U) -1);
  118. }
  119. /*************************************************************************
  120.  * Function Name: UsbWakeUp
  121.  * Parameters:  none
  122.  *
  123.  * Return: none
  124.  *
  125.  * Description: WakeUp device from suspend mode
  126.  *
  127.  *************************************************************************/
  128. void UsbWakeUp (void)
  129. {
  130.   if(UsbDevCtrl.Feature.RemoteWakeupEnable && UsbDevCtrl.State.Suspend)
  131.   {
  132.     USB_WakeUp();
  133.   }
  134. }
  135. /*************************************************************************
  136.  * Function Name: UsbSetDevState
  137.  * Parameters:  _UsbDevState_t DevState
  138.  *
  139.  * Return: none
  140.  *
  141.  * Description: Set device state
  142.  *
  143.  *************************************************************************/
  144. void UsbSetDevState (_UsbDevState_t DevState)
  145. {
  146.   UsbDevCtrl.State.DS = DevState;
  147.   switch (DevState)
  148.   {
  149.   case UsbDevStatusConfigured:
  150.     USB_Configure(TRUE);
  151.     break;
  152.   case UsbDevStatusAddress:
  153.     UsbDevCtrl.Configuration = UsbDevCtrl.AlternateSetting =
  154.     UsbDevCtrl.Interface = 0;
  155.     USB_Configure(FALSE);
  156.     break;
  157.   default:
  158.     UsbDevCtrl.DevAdd = UsbDevCtrl.Configuration = 
  159.     UsbDevCtrl.AlternateSetting = UsbDevCtrl.Interface = 0;
  160.     USB_Configure(FALSE);
  161.   }
  162. }
  163. /*************************************************************************
  164.  * Function Name: UsbGetDevState
  165.  * Parameters:  none
  166.  *
  167.  * Return: UsbDevState_t
  168.  *
  169.  * Description: Get device state
  170.  *
  171.  *************************************************************************/
  172. UsbDevState_t UsbGetDevState (void)
  173. {
  174.   return(UsbDevCtrl.State);
  175. }
  176. /*************************************************************************
  177.  * Function Name: UsbSetDevSuspend
  178.  * Parameters:  none
  179.  *
  180.  * Return: UsbDevState_t
  181.  *
  182.  * Description: Set device suspend mode
  183.  *
  184.  *************************************************************************/
  185. void UsbSetDevSuspend (Boolean Suspend)
  186. {
  187.   UsbDevCtrl.State.Suspend = Suspend ? UsbDevStatusSuspend : UsbDevStatusNoSuspend;
  188. }
  189. /*************************************************************************
  190.  * Function Name: UsbFindInterface
  191.  * Parameters:  UsbStandardConfigurationDescriptor_t * pConfiguration
  192.  *              Int32U Interface,Int32U AlternativeSetting
  193.  *
  194.  * Return: UsbCommStatus_t
  195.  *
  196.  * Description: Set Interface and  Alternative setting state
  197.  *
  198.  *************************************************************************/
  199. UsbCommStatus_t UsbFindInterface (UsbStandardConfigurationDescriptor_t * pConfiguration,
  200.                                   UsbDevCtrl_t * UsbDev)
  201. {
  202. UsbStandardInterfaceDescriptor_t *pData;
  203.   // Find Interface
  204.   for(pData = (UsbStandardInterfaceDescriptor_t *)pConfiguration;
  205.       (Int8U*)pData < (Int8U *)pConfiguration + pConfiguration->wTotalLength;
  206.       pData =(UsbStandardInterfaceDescriptor_t *)((Int8U *)pData + pData->bLength))
  207.   {
  208.     if((pData->bDescriptorType == UsbDescriptorInterface) &&
  209.        (pData->bInterfaceNumber == UsbDev->Interface) &&
  210.        (pData->bAlternateSetting == UsbDev->AlternateSetting))
  211.     {
  212.       return(UsbPass);
  213.     }
  214.   }
  215.   return(UsbFault);
  216. }
  217. /*************************************************************************
  218.  * Function Name: UsbFindConfiguration
  219.  * Parameters:  Int32U
  220.  *
  221.  * Return: UsbStandardConfigurationDescriptor_t *
  222.  *
  223.  * Description: Return pointer to current configuration
  224.  *
  225.  *************************************************************************/
  226. UsbStandardConfigurationDescriptor_t * UsbFindConfiguration(Int32U Configuration)
  227. {
  228. UsbStandardConfigurationDescriptor_t * pData =
  229.  (UsbStandardConfigurationDescriptor_t *) &UsbStandardConfigurationDescriptor[0];
  230.   while(pData->bLength)
  231.   {
  232.     if(pData->bDescriptorType == UsbDescriptorConfiguration)
  233.     {
  234.       if(pData->bConfigurationValue == Configuration)
  235.       {
  236.          return(pData);
  237.       }
  238.     }
  239.     pData = (UsbStandardConfigurationDescriptor_t *)((Int8U *)pData + pData->bLength);
  240.   }
  241.   return(NULL);
  242. }
  243. /*************************************************************************
  244.  * Function Name: UsbDevConnectCallback
  245.  * Parameters:  void * pArg
  246.  *
  247.  * Return: none
  248.  *
  249.  * Description: USB connect callback
  250.  *
  251.  *************************************************************************/
  252. void UsbDevConnectCallback (void * pArg)
  253. {
  254.   UsbSetDevState(pArg?UsbDevStatusAttached:UsbDevStatusUnknow);
  255.   if(UsbCoreUserFun[UsbUserConnect] != NULL)
  256.   {
  257.     UsbCoreUserFun[UsbUserConnect](pArg);
  258.   }
  259. }
  260. /*************************************************************************
  261.  * Function Name: UsbDevSuspendCallback
  262.  * Parameters:  void * pArg
  263.  *
  264.  * Return: none
  265.  *
  266.  * Description: USB suspend callback
  267.  *
  268.  *************************************************************************/
  269. void UsbDevSuspendCallback (void * pArg)
  270. {
  271.   UsbSetDevSuspend(pArg?UsbDevStatusSuspend:UsbDevStatusNoSuspend);
  272.   if(UsbCoreUserFun[UsbUserSuspend] != NULL)
  273.   {
  274.     UsbCoreUserFun[UsbUserSuspend](pArg);
  275.   }
  276. }
  277. /*************************************************************************
  278.  * Function Name: UsbDevResetCallback
  279.  * Parameters:  void * pArg
  280.  *
  281.  * Return: none
  282.  *
  283.  * Description: USB reset callback
  284.  *
  285.  *************************************************************************/
  286. void UsbDevResetCallback (void * pArg)
  287. {
  288.   UsbSetDevState(UsbDevStatusDefault);
  289.   UsbCoreUserFun[UsbUserConfigure](NULL);
  290.   if(UsbCoreUserFun[UsbUserReset] != NULL)
  291.   {
  292.     UsbCoreUserFun[UsbUserReset](pArg);
  293.   }
  294. }
  295. /*************************************************************************
  296.  * Function Name: UsbEp0SetupWrite
  297.  * Parameters:  none
  298.  *
  299.  * Return: none
  300.  *
  301.  * Description: USB EP Write implement (IN)
  302.  *
  303.  *************************************************************************/
  304. void UsbEp0SetupWrite (void)
  305. {
  306. Int32U CurrCount = Ep0MaxSize;
  307.   if((UsbEp0Ctrl.Counter == Ep0MaxSize) && UsbEp0Ctrl.EpStatus.NoZeroLength)
  308.   {
  309.     UsbEp0Ctrl.EpStatus.Status = UsbEpSetupLastIn;
  310.   }
  311.   else if(UsbEp0Ctrl.Counter < Ep0MaxSize)
  312.   {
  313.     CurrCount = UsbEp0Ctrl.Counter;
  314.     UsbEp0Ctrl.EpStatus.Status = UsbEpSetupLastIn;
  315.   }
  316.   else
  317.   {
  318.     UsbEp0Ctrl.EpStatus.Status = UsbEpSetupDataIn;
  319.   }
  320.   USB_EpWrite(CTRL_ENP_IN,(Int32U *)UsbEp0Ctrl.pData,CurrCount);
  321.   UsbEp0Ctrl.Counter -= CurrCount;
  322.   UsbEp0Ctrl.pData += CurrCount;
  323. }
  324. /*************************************************************************
  325.  * Function Name: UsbSetConfigurtonState
  326.  * Parameters:  Int32U
  327.  *
  328.  * Return: UsbCommStatus_t
  329.  *
  330.  * Description: Set configuration state
  331.  *
  332.  *************************************************************************/
  333. inline
  334. UsbCommStatus_t UsbSetConfigurtonState (Int32U Configuration)
  335. {
  336.   if(Configuration)
  337.   {
  338.     // Find configuration
  339.     UsbStandardConfigurationDescriptor_t * pData = UsbFindConfiguration(Configuration);
  340.     UsbDevCtrl_t UsbDevCtrlNew = UsbDevCtrl;
  341.     if(pData == NULL)
  342.     {
  343.       return(UsbFault);
  344.     }
  345.     // Set Interfce and Alternative Setting
  346.     UsbDevCtrlNew.Configuration = Configuration;
  347.     UsbDevCtrlNew.Interface = UsbDevCtrlNew.AlternateSetting = 0;
  348.     UsbDevCtrlNew.Feature.RemoteWakeupEnable = 0;
  349.     UsbDevCtrlNew.Feature.SelfPowered = pData->bmAttributes.SelfPowered?1:0;
  350.     // Whether the interface is exist into current conficuration?
  351.     if(UsbFindInterface(pData,&UsbDevCtrlNew) != UsbPass)
  352.     {
  353.       return(UsbFault);
  354.     }
  355.     // User function that realize EP and init buffers
  356.     UsbCoreUserFun[UsbUserConfigure]((void *)&UsbDevCtrlNew);
  357.     UsbDevCtrl = UsbDevCtrlNew;
  358.     UsbSetDevState(UsbDevStatusConfigured);
  359.   }
  360.   else
  361.   {
  362.     UsbCoreUserFun[UsbUserConfigure](NULL);
  363.     UsbSetDevState(UsbDevStatusAddress);
  364.   }
  365.   return(UsbPass);
  366. }
  367. /*************************************************************************
  368.  * Function Name: UsbDevStatus
  369.  * Parameters:  Int8U * pData, Int16U Index
  370.  *
  371.  * Return: UsbCommStatus_t
  372.  *
  373.  * Description: Usb Return device status into pData
  374.  *
  375.  *************************************************************************/
  376. inline
  377. UsbCommStatus_t UsbDevStatus(Int8U * pData, Int16U Index)
  378. {
  379.   if(Index == 0)
  380.   {
  381.     UsbEp0Ctrl.pData = pData;
  382.     UsbEp0Ctrl.Counter = 2;
  383.     *pData++ = UsbDevCtrl.Feature.Data;
  384.     *pData   = 0;
  385.     return(UsbPass);
  386.   }
  387.   return(UsbFault);
  388. }
  389. /*************************************************************************
  390.  * Function Name: UsbInterfaceStatus
  391.  * Parameters:  Int8U * pData, Int16U Index
  392.  *
  393.  * Return: UsbCommStatus_t
  394.  *
  395.  * Description: Usb Return interface status into pData
  396.  *
  397.  *************************************************************************/
  398. inline
  399. UsbCommStatus_t UsbInterfaceStatus(Int8U * pData, Int16U Index)
  400. {
  401.   UsbEp0Ctrl.pData = pData;
  402.   UsbEp0Ctrl.Counter = 2;
  403.   *pData++ = 0;
  404.   *pData   = 0;
  405.   return(UsbPass);
  406. }
  407. /*************************************************************************
  408.  * Function Name: UsbEpStatus
  409.  * Parameters:  Int8U * pData, Int16U Index
  410.  *
  411.  * Return: UsbCommStatus_t
  412.  *
  413.  * Description: Usb Return Ep status into pData
  414.  *
  415.  *************************************************************************/
  416. inline
  417. UsbCommStatus_t UsbEpStatus(Int8U * pData, Int16U Index)
  418. {
  419.   if((Index & 0x7F) > 16)
  420.   {
  421.     return(UsbFault);
  422.   }
  423.   UsbEp0Ctrl.pData = pData;
  424.   UsbEp0Ctrl.Counter = 2;
  425.   *pData++ = USB_GetStallEP(USB_EpLogToPhysAdd(Index)) ? 1:0;
  426.   *pData = 0;
  427.   return(UsbPass);
  428. }
  429. /*************************************************************************
  430.  * Function Name: UsbGetStatus
  431.  * Parameters:  none
  432.  *
  433.  * Return: none
  434.  *
  435.  * Description: Usb Get status implement
  436.  *
  437.  *************************************************************************/
  438. inline
  439. void UsbGetStatus (void)
  440. {
  441.   if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
  442.      // Length must be 2
  443.      (UsbEp0SetupPacket.wLength.Word == 2) &&
  444.      // Direction of data from device
  445.      (UsbEp0SetupPacket.mRequestType.Dir == 1) &&
  446.      // wValue mut be 0
  447.      (UsbEp0SetupPacket.wValue.Word == 0) )
  448.   {
  449.     // This request is valid only for Address and Configured device's states
  450.     switch(UsbEp0SetupPacket.mRequestType.Recipient)
  451.     {
  452.     case UsbRecipientDevice:
  453.       if(UsbDevStatus(EpCtrlDataBuf,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
  454.       {
  455.         UsbEp0SetupWrite();
  456.       }
  457.       else
  458.       {
  459.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  460.       }
  461.       break;
  462.     case UsbRecipientInterface:
  463.       if(UsbInterfaceStatus(EpCtrlDataBuf,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
  464.       {
  465.         UsbEp0SetupWrite();
  466.       }
  467.       else
  468.       {
  469.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  470.       }
  471.       break;
  472.     case UsbRecipientEndpoint:
  473.       if(UsbEpStatus(EpCtrlDataBuf,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
  474.       {
  475.         UsbEp0SetupWrite();
  476.       }
  477.       else
  478.       {
  479.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  480.       }
  481.       break;
  482.     default:
  483.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  484.     }
  485.   }
  486.   else
  487.   {
  488.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  489.   }
  490. }
  491. /*************************************************************************
  492.  * Function Name: UsbClearFeature
  493.  * Parameters:  Int16U Feature,Int16U Index
  494.  *
  495.  * Return: UsbCommStatus_t
  496.  *
  497.  * Description: USB Clear Device feature implement
  498.  *
  499.  *************************************************************************/
  500. inline
  501. UsbCommStatus_t UsbClearDevFeature(Int16U Feature,Int16U Index)
  502. {
  503.   if((Index != 0) || (Feature != UsbDeviceRemoteWakeupSelector))
  504.   {
  505.     return(UsbFault);
  506.   }
  507.   UsbDevCtrl.Feature.RemoteWakeupEnable = 0;
  508.   return(UsbPass);
  509. }
  510. /*************************************************************************
  511.  * Function Name: UsbClearInterfaceFeature
  512.  * Parameters:  Int16U Feature,Int16U Index
  513.  *
  514.  * Return: UsbCommStatus_t
  515.  *
  516.  * Description: USB Clear Interface feature implement
  517.  *
  518.  *************************************************************************/
  519. inline
  520. UsbCommStatus_t UsbClearInterfaceFeature(Int16U Feature,Int16U Index)
  521. {
  522.   return(UsbFault);
  523. }
  524. /*************************************************************************
  525.  * Function Name: UsbClearEpFeature
  526.  * Parameters:  Int16U Feature,Int16U Index
  527.  *
  528.  * Return: UsbCommStatus_t
  529.  *
  530.  * Description: USB Clear Ep feature implement
  531.  *
  532.  *************************************************************************/
  533. inline
  534. UsbCommStatus_t UsbClearEpFeature(Int16U Feature,Int16U Index)
  535. {
  536. USB_Endpoint_t UsbEpAdd;
  537.   if(((Index & 0x7F) > 16) || (Feature != UsbEpHaltSelector))
  538.   {
  539.     return(UsbFault);
  540.   }
  541.   UsbEpAdd = USB_EpLogToPhysAdd(Index);
  542.   USB_SetStallEP(UsbEpAdd,FALSE);
  543.   if (UsbUserFun[UsbEpAdd] != NULL)
  544.   {
  545.     UsbUserFun[UsbEpAdd]((void *)UsbEpUnstall);
  546.   }
  547.   return(UsbPass);
  548. }
  549. /*************************************************************************
  550.  * Function Name: UsbClearFeature
  551.  * Parameters:  none
  552.  *
  553.  * Return: none
  554.  *
  555.  * Description: USB Clear feature implement
  556.  *
  557.  *************************************************************************/
  558. inline
  559. void UsbClearFeature (void)
  560. {
  561.   if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
  562.      // Length must be 0
  563.      (UsbEp0SetupPacket.wLength.Word == 0))
  564.   {
  565.     switch(UsbEp0SetupPacket.mRequestType.Recipient)
  566.     {
  567.     case UsbRecipientDevice:
  568.       if(UsbClearDevFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
  569.       {
  570.         USB_EpWrite(CTRL_ENP_IN,NULL,0);
  571.         UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  572.       }
  573.       else
  574.       {
  575.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  576.       }
  577.       break;
  578.     case UsbRecipientInterface:
  579.       if((UsbDevCtrl.State.DS > UsbDevStatusAddress) &&
  580.          (UsbClearInterfaceFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
  581.       {
  582.         USB_EpWrite(CTRL_ENP_IN,NULL,0);
  583.         UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  584.       }
  585.       else
  586.       {
  587.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  588.       }
  589.       break;
  590.     case UsbRecipientEndpoint:
  591.       if(((UsbDevCtrl.State.DS > UsbDevStatusAddress) || ((UsbEp0SetupPacket.wIndex.Word & 0x7F) == 0)) &&
  592.          (UsbClearEpFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
  593.       {
  594.         USB_EpWrite(CTRL_ENP_IN,NULL,0);
  595.         UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  596.       }
  597.       else
  598.       {
  599.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  600.       }
  601.       break;
  602.     default:
  603.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  604.     }
  605.   }
  606.   else
  607.   {
  608.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  609.   }
  610. }
  611. /*************************************************************************
  612.  * Function Name: UsbSetFeature
  613.  * Parameters:  Int16U Feature,Int16U Index
  614.  *
  615.  * Return: UsbCommStatus_t
  616.  *
  617.  * Description: USB Set Device feature implement
  618.  *
  619.  *************************************************************************/
  620. inline
  621. UsbCommStatus_t UsbSetDevFeature(Int16U Feature,Int16U Index)
  622. {
  623.   if((Index != 0) || (Feature != UsbDeviceRemoteWakeupSelector))
  624.   {
  625.     return(UsbFault);
  626.   }
  627.   UsbDevCtrl.Feature.RemoteWakeupEnable = 1;
  628.   return(UsbPass);
  629. }
  630. /*************************************************************************
  631.  * Function Name: UsbSetInterfaceFeature
  632.  * Parameters:  Int16U Feature,Int16U Index
  633.  *
  634.  * Return: UsbCommStatus_t
  635.  *
  636.  * Description: USB Set Interface feature implement
  637.  *
  638.  *************************************************************************/
  639. inline
  640. UsbCommStatus_t UsbSetInterfaceFeature(Int16U Feature,Int16U Index)
  641. {
  642.   return(UsbFault);
  643. }
  644. /*************************************************************************
  645.  * Function Name: UsbSetEpFeature
  646.  * Parameters:  Int16U Feature,Int16U Index
  647.  *
  648.  * Return: UsbCommStatus_t
  649.  *
  650.  * Description: USB Set Ep feature implement
  651.  *
  652.  *************************************************************************/
  653. inline
  654. UsbCommStatus_t UsbSetEpFeature(Int16U Feature,Int16U Index)
  655. {
  656. USB_Endpoint_t UsbEpAdd;
  657.   if(((Index & 0x7F) > 16) || (Feature != UsbEpHaltSelector))
  658.   {
  659.     return(UsbFault);
  660.   }
  661.   UsbEpAdd = USB_EpLogToPhysAdd(Index);
  662.   USB_SetStallEP(UsbEpAdd,TRUE);
  663.   if (UsbUserFun[UsbEpAdd] != NULL)
  664.   {
  665.     UsbUserFun[UsbEpAdd]((void *)UsbEpStall);
  666.   }
  667.   return(UsbPass);
  668. }
  669. /*************************************************************************
  670.  * Function Name: UsbSetFeature
  671.  * Parameters:  none
  672.  *
  673.  * Return: none
  674.  *
  675.  * Description: USB Set feature implement
  676.  *
  677.  *************************************************************************/
  678. inline
  679. void UsbSetFeature (void)
  680. {
  681.   if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
  682.      // Length must be 0
  683.      (UsbEp0SetupPacket.wLength.Word == 0))
  684.   {
  685.     switch(UsbEp0SetupPacket.mRequestType.Recipient)
  686.     {
  687.     case UsbRecipientDevice:
  688.       if(UsbSetDevFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
  689.       {
  690.         USB_EpWrite(CTRL_ENP_IN,NULL,0);
  691.         UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  692.       }
  693.       else
  694.       {
  695.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  696.       }
  697.       break;
  698.     case UsbRecipientInterface:
  699.       if((UsbDevCtrl.State.DS > UsbDevStatusAddress) &&
  700.          (UsbSetInterfaceFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
  701.       {
  702.         USB_EpWrite(CTRL_ENP_IN,NULL,0);
  703.         UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  704.       }
  705.       else
  706.       {
  707.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  708.       }
  709.       break;
  710.     case UsbRecipientEndpoint:
  711.       if(((UsbDevCtrl.State.DS > UsbDevStatusAddress) || ((UsbEp0SetupPacket.wIndex.Lo & 0x7F) == 0)) &&
  712.          (UsbSetEpFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
  713.       {
  714.         USB_EpWrite(CTRL_ENP_IN,NULL,0);
  715.         UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  716.       }
  717.       else
  718.       {
  719.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  720.       }
  721.       break;
  722.     default:
  723.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  724.     }
  725.   }
  726.   else
  727.   {
  728.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  729.   }
  730. }
  731. /*************************************************************************
  732.  * Function Name: UsbSetAddress
  733.  * Parameters:  none
  734.  *
  735.  * Return: none
  736.  *
  737.  * Description: USB Set address request implement
  738.  *
  739.  *************************************************************************/
  740. inline
  741. void UsbSetAddress (void)
  742. {
  743.   if((UsbDevCtrl.State.DS >= UsbDevStatusDefault) &&
  744.      (UsbEp0SetupPacket.mRequestType.Recipient == 0) &&
  745.      (UsbEp0SetupPacket.wIndex.Word  == 0) &&
  746.      (UsbEp0SetupPacket.wLength.Word == 0) &&
  747.      (UsbEp0SetupPacket.wValue.Word < 128))
  748.   {
  749.     USB_EpWrite(CTRL_ENP_IN,NULL,0);
  750.     UsbEp0Ctrl.EpStatus.Status = UsbEpStatusInSetAdd;
  751.   }
  752.   else
  753.   {
  754.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  755.   }
  756. }
  757. /*************************************************************************
  758.  * Function Name: UsbGetDescriptorDevice
  759.  * Parameters:  Int8U Index - must be 0
  760.  *
  761.  * Return: UsbCommStatus_t
  762.  *
  763.  * Description: USB get device's descriptor request implement
  764.  *
  765.  *************************************************************************/
  766. inline
  767. UsbCommStatus_t UsbGetDescriptorDevice(Int8U Index)
  768. {
  769.   if(Index == 0)
  770.   {
  771.     UsbEp0Ctrl.Counter = UsbStandardDeviceDescriptorStr.bLength;
  772.     UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  773.     if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
  774.     {
  775.       UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  776.       UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  777.     }
  778.     UsbEp0Ctrl.pData = (Int8U *)&UsbStandardDeviceDescriptorStr;
  779.     UsbEp0SetupWrite();
  780.     return(UsbPass);
  781.   }
  782.   return(UsbFault);
  783. }
  784. /*************************************************************************
  785.  * Function Name: UsbGetDescriptorConfiguration
  786.  * Parameters:  Int8U Index
  787.  *
  788.  * Return: UsbCommStatus_t
  789.  *
  790.  * Description: USB get configuration's descriptor request implement
  791.  *
  792.  *************************************************************************/
  793. inline
  794. UsbCommStatus_t UsbGetDescriptorConfiguration(Int8U Index)
  795. {
  796. UsbStandardConfigurationDescriptor_t *pData;
  797.   for(pData = (UsbStandardConfigurationDescriptor_t *)UsbStandardConfigurationDescriptor;
  798.       pData->bLength;
  799.       pData=(UsbStandardConfigurationDescriptor_t*)((Int8U *)pData + pData->bLength))
  800.   {
  801.     if((pData->bDescriptorType == UsbDescriptorConfiguration) &&
  802.        ((UsbStandardDeviceDescriptorStr.bNumConfigurations == 1) ||
  803.        (pData->bConfigurationValue == Index)))
  804.     {
  805.       break;
  806.     }
  807.   }
  808.   if(pData->bLength == 0)
  809.   {
  810.      return(UsbFault);
  811.   }
  812.   UsbEp0Ctrl.Counter = ((UsbStandardConfigurationDescriptor_t*)pData)->wTotalLength;
  813.   UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  814.   if(UsbEp0Ctrl.Counter > UsbEp0SetupPacket.wLength.Word)
  815.   {
  816.     UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  817.     UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  818.   }
  819.   UsbEp0Ctrl.pData = (Int8U *)pData;
  820.   UsbEp0SetupWrite();
  821.   return(UsbPass);
  822. }
  823. /*************************************************************************
  824.  * Function Name: UsbGetDescriptorString
  825.  * Parameters:  Int8U Index
  826.  *
  827.  * Return: UsbCommStatus_t
  828.  *
  829.  * Description: USB get string's descriptor request implement
  830.  *
  831.  *************************************************************************/
  832. inline
  833. UsbCommStatus_t UsbGetDescriptorString(Int8U Index)
  834. {
  835. Int8U **pData;
  836. Int32U i;
  837.   if(Index == 0)
  838.   {
  839.     UsbEp0Ctrl.Counter = UsbLanguagesStr[UsbDescLengthOffset];
  840.     UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  841.     if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
  842.     {
  843.       UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  844.       UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  845.     }
  846.     UsbEp0Ctrl.pData = (Int8U *)&UsbLanguagesStr;
  847.     UsbEp0SetupWrite();
  848.     return(UsbPass);
  849.   }
  850.   else
  851.   {
  852.     Int32U LanguagesOffset = 0;
  853.     // Find Languages index
  854.     for(i = 2; i < UsbLanguagesStr[UsbDescLengthOffset]; i+=2)
  855.     {
  856.       if((UsbLanguagesStr[i] == UsbEp0SetupPacket.wIndex.Lo) &&
  857.          (UsbLanguagesStr[i+1] == UsbEp0SetupPacket.wIndex.Hi))
  858.       {
  859.         break;
  860.       }
  861.       ++LanguagesOffset;
  862.     }
  863.     if(i >= UsbLanguagesStr[UsbDescLengthOffset])
  864.     {
  865.       return(UsbFault);
  866.     }
  867.     // Find String index
  868.     for(pData = (Int8U **)UsbString[LanguagesOffset]; pData ; ++pData)
  869.     {
  870.       if(--Index == 0)
  871.       {
  872.         break;
  873.       }
  874.     }
  875.     if(pData == NULL)
  876.     {
  877.       return(UsbFault);
  878.     }
  879.     UsbEp0Ctrl.Counter = *(*pData+UsbDescLengthOffset);
  880.     UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  881.     if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
  882.     {
  883.       UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  884.       UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  885.     }
  886.     UsbEp0Ctrl.pData = *pData;
  887.     UsbEp0SetupWrite();
  888.   }
  889.   return(UsbPass);
  890. }
  891. /*************************************************************************
  892.  * Function Name: UsbGetDescriptor
  893.  * Parameters:  none
  894.  *
  895.  * Return: none
  896.  *
  897.  * Description: USB get descriptor request implement
  898.  *
  899.  *************************************************************************/
  900. inline
  901. void UsbGetDescriptor (void)
  902. {
  903.     if(UsbDevCtrl.State.DS >= UsbDevStatusDefault)
  904.     {
  905.       switch(UsbEp0SetupPacket.wValue.Hi)
  906.       {
  907.       case UsbDescriptorDevice:
  908.         if(UsbGetDescriptorDevice(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
  909.         {
  910.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  911.         }
  912.         break;
  913.       case UsbDescriptorConfiguration:
  914.         if(UsbGetDescriptorConfiguration(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
  915.         {
  916.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  917.         }
  918.         break;
  919.       case UsbDescriptorString:
  920.         if(UsbGetDescriptorString(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
  921.         {
  922.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  923.         }
  924.         break;
  925.       default:
  926.         if ((UsbCoreUserFun[UsbUserGetDescriptor] == NULL) ||
  927.             ((Int32U)UsbCoreUserFun[UsbUserGetDescriptor](&UsbEp0Ctrl) != UsbPass))
  928.         {
  929.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  930.         }
  931.       }
  932.     }
  933.     else
  934.     {
  935.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  936.     }
  937. }
  938. /*************************************************************************
  939.  * Function Name: UsbSetDescriptor
  940.  * Parameters:  none
  941.  *
  942.  * Return: none
  943.  *
  944.  * Description: USB Set descriptor
  945.  *
  946.  *************************************************************************/
  947. inline
  948. void UsbSetDescriptor(void)
  949. {
  950.   if(UsbDevCtrl.State.DS >= UsbDevStatusAddress)
  951.   {
  952.     // This request is valid only for Address state and Configured state.
  953.     // If supported
  954.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  955.   }
  956.   else
  957.   {
  958.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  959.   }
  960. }
  961. /*************************************************************************
  962.  * Function Name: UsbGetConfiguration
  963.  * Parameters:  none
  964.  *
  965.  * Return: none
  966.  *
  967.  * Description: USB Get configuration implement
  968.  *
  969.  *************************************************************************/
  970. inline
  971. void UsbGetConfiguration(void)
  972. {
  973.   if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
  974.      (UsbEp0SetupPacket.wValue.Word == 0) &&
  975.      (UsbEp0SetupPacket.wIndex.Word == 0) &&
  976.      (UsbEp0SetupPacket.wLength.Word == 1))
  977.   {
  978.     UsbEp0Ctrl.Counter = 1;
  979.     EpCtrlDataBuf[0] = UsbCoreReq(UsbCoreReqConfiquration);
  980.     UsbEp0Ctrl.pData = EpCtrlDataBuf;
  981.     UsbEp0SetupWrite();
  982.   }
  983.   else
  984.   {
  985.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  986.   }
  987. }
  988. /*************************************************************************
  989.  * Function Name: UsbSetConfiguration
  990.  * Parameters:  none
  991.  *
  992.  * Return: none
  993.  *
  994.  * Description: USB Set configuration implement
  995.  *
  996.  *************************************************************************/
  997. inline
  998. void UsbSetConfiguration(void)
  999. {
  1000.   if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
  1001.      (UsbEp0SetupPacket.wIndex.Word == 0) &&
  1002.      (UsbEp0SetupPacket.wLength.Word == 0) &&
  1003.      (UsbSetConfigurtonState(UsbEp0SetupPacket.wValue.Lo) == UsbPass))
  1004.   {
  1005.     USB_EpWrite(CTRL_ENP_IN,NULL,0);
  1006.     UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  1007.   }
  1008.   else
  1009.   {
  1010.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1011.   }
  1012. }
  1013. /*************************************************************************
  1014.  * Function Name: UsbGetInterface
  1015.  * Parameters:  none
  1016.  *
  1017.  * Return: none
  1018.  *
  1019.  * Description: Implement get interface request
  1020.  *
  1021.  *************************************************************************/
  1022. inline
  1023. void UsbGetInterface (void)
  1024. {
  1025.   if((UsbDevCtrl.State.DS == UsbDevStatusConfigured) &&
  1026.      (UsbEp0SetupPacket.wIndex.Word == UsbDevCtrl.Interface) &&
  1027.      (UsbEp0SetupPacket.wLength.Word == 1) &&
  1028.      (UsbEp0SetupPacket.wValue.Word == 0))
  1029.   {
  1030.     UsbEp0Ctrl.Counter = 1;
  1031.     EpCtrlDataBuf[0] = UsbDevCtrl.AlternateSetting;
  1032.     UsbEp0Ctrl.pData = EpCtrlDataBuf;
  1033.     UsbEp0SetupWrite();
  1034.   }
  1035.   else
  1036.   {
  1037.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1038.   }
  1039. }
  1040. /*************************************************************************
  1041.  * Function Name: UsbSetInterface
  1042.  * Parameters:  none
  1043.  *
  1044.  * Return: none
  1045.  *
  1046.  * Description: Implement set interface request
  1047.  *
  1048.  *************************************************************************/
  1049. inline
  1050. void UsbSetInterface (void)
  1051. {
  1052.   if((UsbDevCtrl.State.DS == UsbDevStatusConfigured) &&
  1053.      (UsbEp0SetupPacket.wLength.Word == 0))
  1054.   {
  1055.     UsbStandardConfigurationDescriptor_t * pData = UsbFindConfiguration(UsbDevCtrl.Configuration);
  1056.     UsbDevCtrl_t UsbDevCtrlNew = UsbDevCtrl;
  1057.     UsbDevCtrlNew.Interface = UsbEp0SetupPacket.wIndex.Word;
  1058.     UsbDevCtrlNew.AlternateSetting = UsbEp0SetupPacket.wValue.Word;
  1059.     UsbDevCtrlNew.Feature.RemoteWakeupEnable = 0;
  1060.     // Whether the interface is exist into current conficuration?
  1061.     if(UsbFindInterface(pData,&UsbDevCtrlNew) != UsbPass)
  1062.     {
  1063.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1064.       return;
  1065.     }
  1066.     UsbCoreUserFun[UsbUserConfigure]((void *)&UsbDevCtrlNew);
  1067.     UsbDevCtrl = UsbDevCtrlNew;
  1068.     USB_EpWrite(CTRL_ENP_IN,NULL,0);
  1069.     UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  1070.   }
  1071.   else
  1072.   {
  1073.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1074.   }
  1075. }
  1076. /*************************************************************************
  1077.  * Function Name: UsbSynchFrame
  1078.  * Parameters:  none
  1079.  *
  1080.  * Return: none
  1081.  *
  1082.  * Description: Implement synch frame request
  1083.  *
  1084.  *************************************************************************/
  1085. inline
  1086. void UsbSynchFrame (void)
  1087. {
  1088.   if( USB_SOF_EVENT &&
  1089.      (UsbDevCtrl.State.DS == UsbDevStatusConfigured) &&
  1090.      (UsbEp0SetupPacket.wLength.Word == 2) &&
  1091.      (UsbEp0SetupPacket.wValue.Word == 0))
  1092.   {
  1093.   Int32U FrameNumb = 0;
  1094.   #if USB_SOF_EVENT > 0
  1095.     FrameNumb = USB_GetFrameNumb();
  1096.   #endif
  1097.     UsbEp0Ctrl.Counter = 2;
  1098.     EpCtrlDataBuf[0]   = FrameNumb;
  1099.     EpCtrlDataBuf[1]   = FrameNumb>>8;
  1100.     UsbEp0Ctrl.pData   = EpCtrlDataBuf;
  1101.     UsbEp0SetupWrite();
  1102.   }
  1103.   else
  1104.   {
  1105.     UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1106.   }
  1107. }
  1108. /*************************************************************************
  1109.  * Function Name: UsbCtrlEpCallback
  1110.  * Parameters:  void * pArg
  1111.  *
  1112.  * Return: none
  1113.  *
  1114.  * Description: USB Ctrl EP Callback
  1115.  *
  1116.  *************************************************************************/
  1117. void UsbCtrlEpCallback (void * pArg)
  1118. {
  1119. Int32U PacketSize;
  1120.   switch((Int32U)pArg)
  1121.   {
  1122.   case UsbSetupPacket:
  1123. setup_packet_overwrited:
  1124.     UsbEp0Ctrl.pData   = EpCtrlDataBuf;
  1125.     UsbEp0Ctrl.Counter = USB_EpRead(CTRL_ENP_OUT,(Int32U *)EpCtrlDataBuf,Ep0MaxSize);
  1126.     if(UsbEp0Ctrl.EpStatus.Status != UsbEpPause)
  1127.     {
  1128.       // Copy new setup packet int setup buffer
  1129.       memcpy((Int8U *)&UsbEp0SetupPacket,EpCtrlDataBuf,sizeof(UsbEp0SetupPacket));
  1130.     }
  1131.     switch(UsbEp0SetupPacket.mRequestType.Type)
  1132.     {
  1133.     // Standart
  1134.     case UsbTypeStandart:
  1135.       // Decoding standart request
  1136.       switch (UsbEp0SetupPacket.bRequest)
  1137.       {
  1138.       case GET_STATUS:
  1139.         UsbGetStatus();
  1140.         break;
  1141.       case CLEAR_FEATURE:
  1142.         UsbClearFeature();
  1143.         break;
  1144.       case SET_FEATURE:
  1145.         UsbSetFeature();
  1146.         break;
  1147.       case SET_ADDRESS:
  1148.         UsbSetAddress();
  1149.         break;
  1150.       case GET_DESCRIPTOR:
  1151.         if(UsbEp0SetupPacket.mRequestType.Recipient == UsbRecipientDevice)
  1152.         {
  1153.           UsbGetDescriptor();
  1154.         }
  1155.         // Only get descriptof for device is standart request
  1156.         else if ((UsbCoreUserFun[UsbUserGetDescriptor] != NULL) &&
  1157.                  ((Int32U)UsbCoreUserFun[UsbUserGetDescriptor](&UsbEp0Ctrl) == UsbUserSendPacket))
  1158.         {
  1159.           if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
  1160.           {
  1161.             UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  1162.             UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  1163.           }
  1164.           else
  1165.           {
  1166.              UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  1167.           }
  1168.           UsbEp0SetupWrite();
  1169.         }
  1170.         else
  1171.         {
  1172.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1173.         }
  1174.         break;
  1175.       case SET_DESCRIPTOR:
  1176.         // Optional (only for configuration and string descriptors)
  1177.         UsbSetDescriptor();
  1178.         break;
  1179.       case GET_CONFIGURATION:
  1180.         UsbGetConfiguration();
  1181.         break;
  1182.       case SET_CONFIGURATION:
  1183.         UsbSetConfiguration();
  1184.         break;
  1185.       case GET_INTERFACE:
  1186.         UsbGetInterface();
  1187.         break;
  1188.       case SET_INTERFACE:
  1189.         UsbSetInterface();
  1190.         break;
  1191.       case SYNCH_FRAME:
  1192.         UsbSynchFrame();
  1193.         break;
  1194.       default:
  1195.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1196.       }
  1197.       break;
  1198.     // Class
  1199.     case UsbTypeClass:
  1200.       if(UsbCoreUserFun[UsbUserClass] != NULL)
  1201.       {
  1202.         switch((Int32U)UsbCoreUserFun[UsbUserClass](&UsbEp0Ctrl))
  1203.         {
  1204.         case UsbUserNone:
  1205.           break;
  1206.         case UsbUserSendPacket:
  1207.           if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
  1208.           {
  1209.             UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  1210.             UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  1211.           }
  1212.           else
  1213.           {
  1214.             UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  1215.           }
  1216.           UsbEp0SetupWrite();
  1217.           break;
  1218.         case UsbUserReceivePacket:
  1219.           if((UsbEp0Ctrl.Counter == UsbEp0SetupPacket.wLength.Word) &&
  1220.              (UsbCoreUserFun[UsbClassEp0OutPacket] != NULL))
  1221.           {
  1222.             UsbEp0Ctrl.EpStatus.Status = UsbEpClassDataOut;
  1223.           }
  1224.           else
  1225.           {
  1226.             UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1227.           }
  1228.           break;
  1229.         case UsbUserSendAckn:
  1230.           USB_EpWrite(CTRL_ENP_IN,NULL,0);
  1231.           UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  1232.           break;
  1233.         default :
  1234.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1235.         }
  1236.       }
  1237.       else
  1238.       {
  1239.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1240.       }
  1241.       break;
  1242.     // Vendor
  1243.     case UsbTypeVendor:
  1244.       if(UsbCoreUserFun[UsbUserVendor] != NULL)
  1245.       {
  1246.         switch((Int32U)UsbCoreUserFun[UsbUserClass](&UsbEp0Ctrl))
  1247.         {
  1248.         case UsbUserNone:
  1249.           break;
  1250.         case UsbUserSendPacket:
  1251.           if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
  1252.           {
  1253.             UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
  1254.             UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
  1255.           }
  1256.           else
  1257.           {
  1258.             UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
  1259.           }
  1260.           UsbEp0SetupWrite();
  1261.           break;
  1262.         case UsbUserReceivePacket:
  1263.           if((UsbEp0Ctrl.Counter == UsbEp0SetupPacket.wLength.Word) &&
  1264.              (UsbCoreUserFun[UsbVendorEp0OutPacket] != NULL))
  1265.           {
  1266.             UsbEp0Ctrl.EpStatus.Status = UsbEpVendorDataOut;
  1267.           }
  1268.           else
  1269.           {
  1270.             UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1271.           }
  1272.           break;
  1273.         case UsbUserSendAckn:
  1274.           USB_EpWrite(CTRL_ENP_IN,NULL,0);
  1275.           UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  1276.           break;
  1277.         default :
  1278.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1279.         }
  1280.       }
  1281.       else
  1282.       {
  1283.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1284.       }
  1285.       break;
  1286.     // Other
  1287.     default:
  1288.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1289.     }
  1290.     break;
  1291.   // Out Packet
  1292.   case UsbDataOutPacket:
  1293.     switch(UsbEp0Ctrl.EpStatus.Status)
  1294.     {
  1295.     case UsbEpSetupDataIn:
  1296.     case UsbEpSetupLastIn:
  1297.       // Clear output ep buffer
  1298.       UsbEp0Ctrl.EpStatus.Status = UsbEpStatusOut;
  1299.       UsbEp0Ctrl.Counter = USB_EpRead(CTRL_ENP_OUT,(Int32U *)EpCtrlDataBuf,Ep0MaxSize);
  1300.       if (UsbEp0Ctrl.Counter & 0x80000000)
  1301.       {
  1302.          goto setup_packet_overwrited;
  1303.       }
  1304.       break;
  1305.     case UsbEpWaitStatusOut:
  1306.       // Clear output ep buffer
  1307.       UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  1308.       UsbEp0Ctrl.Counter = USB_EpRead(CTRL_ENP_OUT,(Int32U *)EpCtrlDataBuf,Ep0MaxSize);
  1309.       if (UsbEp0Ctrl.Counter & 0x80000000)
  1310.       {
  1311.          goto setup_packet_overwrited;
  1312.       }
  1313.       break;
  1314.     case UsbEpSetupDataOut:
  1315.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1316.       break;
  1317.     case UsbEpClassDataOut:
  1318.       PacketSize = USB_EpRead(CTRL_ENP_OUT,(Int32U *)UsbEp0Ctrl.pData,Ep0MaxSize);
  1319.       if (UsbEp0Ctrl.Counter & 0x80000000)
  1320.       {
  1321.          goto setup_packet_overwrited;
  1322.       }
  1323.       UsbEp0Ctrl.pData += PacketSize;
  1324.       UsbEp0Ctrl.Counter -= PacketSize;
  1325.       if(UsbEp0Ctrl.Counter == 0)
  1326.       {
  1327.         switch((Int32U)UsbCoreUserFun[UsbClassEp0OutPacket](NULL))
  1328.         {
  1329.         case UsbUserNone:
  1330.           UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  1331.           break;
  1332.         case UsbUserSendAckn:
  1333.           USB_EpWrite(CTRL_ENP_IN,NULL,0);
  1334.           UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  1335.           break;
  1336.         default:
  1337.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1338.         }
  1339.       }
  1340.       else if (PacketSize != Ep0MaxSize)
  1341.       {
  1342.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1343.       }
  1344.       break;
  1345.     case UsbEpVendorDataOut:
  1346.       PacketSize = USB_EpRead(CTRL_ENP_OUT,(Int32U *)UsbEp0Ctrl.pData,Ep0MaxSize);
  1347.       if (UsbEp0Ctrl.Counter & 0x80000000)
  1348.       {
  1349.          goto setup_packet_overwrited;
  1350.       }
  1351.       UsbEp0Ctrl.pData += PacketSize;
  1352.       UsbEp0Ctrl.Counter -= PacketSize;
  1353.       if(UsbEp0Ctrl.Counter == 0)
  1354.       {
  1355.         switch((Int32U)UsbCoreUserFun[UsbVendorEp0OutPacket](NULL))
  1356.         {
  1357.         case UsbUserNone:
  1358.           UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  1359.           break;
  1360.         case UsbUserSendAckn:
  1361.           USB_EpWrite(CTRL_ENP_IN,NULL,0);
  1362.           UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
  1363.           break;
  1364.         default:
  1365.           UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1366.         }
  1367.       }
  1368.       else if (PacketSize != Ep0MaxSize)
  1369.       {
  1370.         UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1371.       }
  1372.       break;
  1373.     default:
  1374.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1375.     }
  1376.     break;
  1377.   // In Packet
  1378.   case UsbDataInPacket:
  1379.     switch(UsbEp0Ctrl.EpStatus.Status)
  1380.     {
  1381.     case UsbEpStatusOut:
  1382.     case UsbEpWaitStatusIn:
  1383.     case UsbEpStatusIn:
  1384.       UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  1385.       break;
  1386.     case UsbEpSetupDataIn:
  1387.       UsbEp0SetupWrite();
  1388.       break;
  1389.     case UsbEpSetupLastIn:
  1390.       UsbEp0Ctrl.EpStatus.Status = UsbEpWaitStatusOut;
  1391.       break;
  1392.     case UsbEpStatusInSetAdd:
  1393.       USB_SetAdd(UsbEp0SetupPacket.wValue.Lo);
  1394.       UsbDevCtrl.DevAdd = UsbEp0SetupPacket.wValue.Lo;
  1395.       if(UsbEp0SetupPacket.wValue.Lo)
  1396.       {
  1397.         UsbSetDevState(UsbDevStatusAddress);
  1398.         UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  1399.       }
  1400.       else
  1401.       {
  1402.         UsbSetDevState(UsbDevStatusDefault);
  1403.         UsbCoreUserFun[UsbUserConfigure](NULL);
  1404.       }
  1405.       break;
  1406.     default:
  1407.       UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
  1408.     }
  1409.     break;
  1410.   }
  1411.   if (UsbEp0Ctrl.EpStatus.Status == UsbEpStall)
  1412.   {
  1413.     USB_SetStallEP(CTRL_ENP_OUT,TRUE);
  1414.     USB_SetStallEP(CTRL_ENP_IN ,TRUE);
  1415.     UsbEp0Ctrl.EpStatus.Status = UsbSetupPacket;
  1416.   }
  1417. }