📄 usb_9.c
字号:
UsbEp0SetupWrite();
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbRecipientEndpoint:
if(UsbEpStatus(EpCtrlDataBuf,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
{
UsbEp0SetupWrite();
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
default:
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbClearFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Clear Device feature implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbClearDevFeature(Int16U Feature,Int16U Index)
{
if((Index != 0) || (Feature != UsbDeviceRemoteWakeupSelector))
{
return(UsbFault);
}
UsbDevCtrl.Feature.RemoteWakeupEnable = 0;
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbClearInterfaceFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Clear Interface feature implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbClearInterfaceFeature(Int16U Feature,Int16U Index)
{
return(UsbFault);
}
/*************************************************************************
* Function Name: UsbClearEpFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Clear Ep feature implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbClearEpFeature(Int16U Feature,Int16U Index)
{
USB_Endpoint_t UsbEpAdd;
if(((Index & 0x7F) > 16) || (Feature != UsbEpHaltSelector))
{
return(UsbFault);
}
UsbEpAdd = USB_EpLogToPhysAdd(Index);
USB_SetStallEP(UsbEpAdd,FALSE);
if (UsbUserFun[UsbEpAdd] != NULL)
{
UsbUserFun[UsbEpAdd]((void *)UsbEpUnstall);
}
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbClearFeature
* Parameters: none
*
* Return: none
*
* Description: USB Clear feature implement
*
*************************************************************************/
inline
void UsbClearFeature (void)
{
if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
// Length must be 0
(UsbEp0SetupPacket.wLength.Word == 0))
{
switch(UsbEp0SetupPacket.mRequestType.Recipient)
{
case UsbRecipientDevice:
if(UsbClearDevFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbRecipientInterface:
if((UsbDevCtrl.State.DS > UsbDevStatusAddress) &&
(UsbClearInterfaceFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbRecipientEndpoint:
if(((UsbDevCtrl.State.DS > UsbDevStatusAddress) || ((UsbEp0SetupPacket.wIndex.Word & 0x7F) == 0)) &&
(UsbClearEpFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
default:
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSetFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Set Device feature implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbSetDevFeature(Int16U Feature,Int16U Index)
{
if((Index != 0) || (Feature != UsbDeviceRemoteWakeupSelector))
{
return(UsbFault);
}
UsbDevCtrl.Feature.RemoteWakeupEnable = 1;
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbSetInterfaceFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Set Interface feature implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbSetInterfaceFeature(Int16U Feature,Int16U Index)
{
return(UsbFault);
}
/*************************************************************************
* Function Name: UsbSetEpFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Set Ep feature implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbSetEpFeature(Int16U Feature,Int16U Index)
{
USB_Endpoint_t UsbEpAdd;
if(((Index & 0x7F) > 16) || (Feature != UsbEpHaltSelector))
{
return(UsbFault);
}
UsbEpAdd = USB_EpLogToPhysAdd(Index);
USB_SetStallEP(UsbEpAdd,TRUE);
if (UsbUserFun[UsbEpAdd] != NULL)
{
UsbUserFun[UsbEpAdd]((void *)UsbEpStall);
}
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbSetFeature
* Parameters: none
*
* Return: none
*
* Description: USB Set feature implement
*
*************************************************************************/
inline
void UsbSetFeature (void)
{
if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
// Length must be 0
(UsbEp0SetupPacket.wLength.Word == 0))
{
switch(UsbEp0SetupPacket.mRequestType.Recipient)
{
case UsbRecipientDevice:
if(UsbSetDevFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass)
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbRecipientInterface:
if((UsbDevCtrl.State.DS > UsbDevStatusAddress) &&
(UsbSetInterfaceFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbRecipientEndpoint:
if(((UsbDevCtrl.State.DS > UsbDevStatusAddress) || ((UsbEp0SetupPacket.wIndex.Lo & 0x7F) == 0)) &&
(UsbSetEpFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusIn;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
default:
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSetAddress
* Parameters: none
*
* Return: none
*
* Description: USB Set address request implement
*
*************************************************************************/
inline
void UsbSetAddress (void)
{
if((UsbDevCtrl.State.DS >= UsbDevStatusDefault) &&
(UsbEp0SetupPacket.mRequestType.Recipient == 0) &&
(UsbEp0SetupPacket.wIndex.Word == 0) &&
(UsbEp0SetupPacket.wLength.Word == 0) &&
(UsbEp0SetupPacket.wValue.Word < 128))
{
USB_EpWrite(CTRL_ENP_IN,NULL,0);
UsbEp0Ctrl.EpStatus.Status = UsbEpStatusInSetAdd;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbGetDescriptorDevice
* Parameters: Int8U Index - must be 0
*
* Return: UsbCommStatus_t
*
* Description: USB get device's descriptor request implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbGetDescriptorDevice(Int8U Index)
{
if(Index == 0)
{
UsbEp0Ctrl.Counter = UsbStandardDeviceDescriptorStr.bLength;
UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
{
UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
}
UsbEp0Ctrl.pData = (Int8U *)&UsbStandardDeviceDescriptorStr;
UsbEp0SetupWrite();
return(UsbPass);
}
return(UsbFault);
}
/*************************************************************************
* Function Name: UsbGetDescriptorConfiguration
* Parameters: Int8U Index
*
* Return: UsbCommStatus_t
*
* Description: USB get configuration's descriptor request implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbGetDescriptorConfiguration(Int8U Index)
{
UsbStandardConfigurationDescriptor_t *pData;
for(pData = (UsbStandardConfigurationDescriptor_t *)UsbStandardConfigurationDescriptor;
pData->bLength;
pData=(UsbStandardConfigurationDescriptor_t*)((Int8U *)pData + pData->bLength))
{
if((pData->bDescriptorType == UsbDescriptorConfiguration) &&
((UsbStandardDeviceDescriptorStr.bNumConfigurations == 1) ||
(pData->bConfigurationValue == Index)))
{
break;
}
}
if(pData->bLength == 0)
{
return(UsbFault);
}
UsbEp0Ctrl.Counter = ((UsbStandardConfigurationDescriptor_t*)pData)->wTotalLength;
UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
if(UsbEp0Ctrl.Counter > UsbEp0SetupPacket.wLength.Word)
{
UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
}
UsbEp0Ctrl.pData = (Int8U *)pData;
UsbEp0SetupWrite();
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbGetDescriptorString
* Parameters: Int8U Index
*
* Return: UsbCommStatus_t
*
* Description: USB get string's descriptor request implement
*
*************************************************************************/
inline
UsbCommStatus_t UsbGetDescriptorString(Int8U Index)
{
Int8U **pData;
Int32U i;
if(Index == 0)
{
UsbEp0Ctrl.Counter = UsbLanguagesStr[UsbDescLengthOffset];
UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
{
UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
}
UsbEp0Ctrl.pData = (Int8U *)&UsbLanguagesStr;
UsbEp0SetupWrite();
return(UsbPass);
}
else
{
Int32U LanguagesOffset = 0;
// Find Languages index
for(i = 2; i < UsbLanguagesStr[UsbDescLengthOffset]; i+=2)
{
if((UsbLanguagesStr[i] == UsbEp0SetupPacket.wIndex.Lo) &&
(UsbLanguagesStr[i+1] == UsbEp0SetupPacket.wIndex.Hi))
{
break;
}
++LanguagesOffset;
}
if(i >= UsbLanguagesStr[UsbDescLengthOffset])
{
return(UsbFault);
}
// Find String index
for(pData = (Int8U **)UsbString[LanguagesOffset]; pData ; ++pData)
{
if(--Index == 0)
{
break;
}
}
if(pData == NULL)
{
return(UsbFault);
}
UsbEp0Ctrl.Counter = *(*pData+UsbDescLengthOffset);
UsbEp0Ctrl.EpStatus.NoZeroLength = FALSE;
if(UsbEp0Ctrl.Counter >= UsbEp0SetupPacket.wLength.Word)
{
UsbEp0Ctrl.Counter = UsbEp0SetupPacket.wLength.Word;
UsbEp0Ctrl.EpStatus.NoZeroLength = TRUE;
}
UsbEp0Ctrl.pData = *pData;
UsbEp0SetupWrite();
}
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbGetDescriptor
* Parameters: none
*
* Return: none
*
* Description: USB get descriptor request implement
*
*************************************************************************/
inline
void UsbGetDescriptor (void)
{
if(UsbDevCtrl.State.DS >= UsbDevStatusDefault)
{
switch(UsbEp0SetupPacket.wValue.Hi)
{
case UsbDescriptorDevice:
if(UsbGetDescriptorDevice(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbDescriptorConfiguration:
if(UsbGetDescriptorConfiguration(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
case UsbDescriptorString:
if(UsbGetDescriptorString(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
break;
default:
if ((UsbCoreUserFun[UsbUserGetDescriptor] == NULL) ||
((Int32U)UsbCoreUserFun[UsbUserGetDescriptor](&UsbEp0Ctrl) != UsbPass))
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSetDescriptor
* Parameters: none
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -