📄 usb_t9.c
字号:
static
pUsbStandardEpDescriptor_t UsbFindEP(pUsbStandardInterfaceDescriptor_t pInterface,
pUsbStandardEpDescriptor_t pEp)
{
pUsbStandardEpDescriptor_t pEpNext;
static Int32U ep_numbers;
assert(pInterface != NULL);
if (pEp == NULL)
{
ep_numbers = pInterface->bNumEndpoints;
pEpNext = (pUsbStandardEpDescriptor_t)(((pInt8U)pInterface)+pInterface->bLength);
}
else
{
pEpNext = (pUsbStandardEpDescriptor_t)(((pInt8U)pEp) + pEp->bLength);
}
// Find Interface
for(;ep_numbers != 0 && pEpNext->bLength;
pEpNext = (pUsbStandardEpDescriptor_t)(((pInt8U)pEpNext) + pEpNext->bLength))
{
if(pEpNext->bDescriptorType == UsbDescriptorEp)
{
--ep_numbers;
return(pEpNext);
}
else if (pEpNext->bDescriptorType == UsbDescriptorInterface ||
pEpNext->bDescriptorType == UsbDescriptorConfiguration)
{
break;
}
}
return(NULL);
}
/*************************************************************************
* Function Name: UsbSetConfigurtonState
* Parameters: Int32U
*
* Return: UsbCommStatus_t
*
* Description: Set configuration state
*
*************************************************************************/
inline static
UsbCommStatus_t UsbSetConfigurtonState (Int32U Configuration)
{
pUsbStandardConfigurationDescriptor_t pCnfg;
if(Configuration)
{
// Find configuration
pCnfg = UsbFindConfiguration(Configuration);
if(pCnfg == NULL)
{
return(UsbFault);
}
// deinit current configuration
UsbSetDevState(UsbDevStatusAddress);
USB_CONFIGURE_HOOK(NULL);
UsbDevCtrl.pCnfg = pCnfg;
// Set Interface and Alternative Setting
UsbDevCtrl.Configuration = Configuration;
UsbDevCtrl.Feature.RemoteWakeupEnable = 0;
UsbDevCtrl.Feature.SelfPowered = pCnfg->bmAttributes.SelfPowered?1:0;
// configure all existing interfaces alternative setting 0
// Whether the interface exist into current configuration?
UsbDevCtrl.Interfaces = pCnfg->bNumInterfaces;
for(Int32U i = 0; i < UsbDevCtrl.Interfaces; i++)
{
UsbDevCtrl.AlternateSetting[i] = 0;
}
UsbSetDevState(UsbDevStatusConfigured);
USB_CONFIGURE_HOOK(&UsbDevCtrl);
}
else
{
USB_CONFIGURE_HOOK(NULL);
UsbSetDevState(UsbDevStatusAddress);
}
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbDevStatus
* Parameters: Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: Usb Return device status
*
*************************************************************************/
inline static
UsbCommStatus_t UsbDevStatus(Int16U Index)
{
if(Index == 0)
{
InData = ((Int32U)UsbDevCtrl.Feature.Data) & 0xFF;
USB_IO_Data(CTRL_ENP_IN,(pInt8U)&InData,2,(void*)USB_StatusHandler);
return(UsbPass);
}
return(UsbFault);
}
/*************************************************************************
* Function Name: UsbInterfaceStatus
* Parameters: Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Return interface status into pData
*
*************************************************************************/
inline static
UsbCommStatus_t UsbInterfaceStatus(Int16U Index)
{
InData = 0;
USB_IO_Data(CTRL_ENP_IN,(pInt8U)&InData,2,(void*)USB_StatusHandler);
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbEpStatus
* Parameters: Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Return EP status into pData
*
*************************************************************************/
inline static
UsbCommStatus_t UsbEpStatus(Int16U Index)
{
if((Index & 0x7F) > 16)
{
return(UsbFault);
}
InData = 0;
USB_GetStallEP(USB_EpLogToPhysAdd(Index),(pBoolean)&InData);
USB_IO_Data(CTRL_ENP_IN,(pInt8U)&InData,2,(void*)USB_StatusHandler);
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbGetStatus
* Parameters: none
*
* Return: none
*
* Description: USB Get status implement
*
*************************************************************************/
inline static
void UsbGetStatus (void)
{
if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
// Length must be 2
(UsbEp0SetupPacket.wLength.Word == 2) &&
// Direction of data from device
(UsbEp0SetupPacket.mRequestType.Dir == 1) &&
// wValue must be 0
(UsbEp0SetupPacket.wValue.Word == 0) )
{
// This request is valid only for Address and Configured device's states
switch(UsbEp0SetupPacket.mRequestType.Recipient)
{
case UsbRecipientDevice:
if(UsbDevStatus(UsbEp0SetupPacket.wIndex.Word) != UsbPass)
{
USB_T9_ERROR_REQUEST();
}
break;
case UsbRecipientInterface:
if(UsbInterfaceStatus(UsbEp0SetupPacket.wIndex.Word) != UsbPass)
{
USB_T9_ERROR_REQUEST();
}
break;
case UsbRecipientEndpoint:
if(UsbEpStatus(UsbEp0SetupPacket.wIndex.Word) != UsbPass)
{
USB_T9_ERROR_REQUEST();
}
break;
default:
USB_T9_ERROR_REQUEST();
}
}
else
{
USB_T9_ERROR_REQUEST();
}
}
/*************************************************************************
* Function Name: UsbClearFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Clear Device feature implement
*
*************************************************************************/
inline static
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 static
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 static
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);
USB_EP_UNSTALL_HOOK(UsbEpAdd);
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbClearFeature
* Parameters: none
*
* Return: none
*
* Description: USB Clear feature implement
*
*************************************************************************/
inline static
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_StatusHandler(CTRL_ENP_OUT);
}
else
{
USB_T9_ERROR_REQUEST();
}
break;
case UsbRecipientInterface:
if((UsbDevCtrl.State.DS > UsbDevStatusAddress) &&
(UsbClearInterfaceFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_IO_Data(CTRL_ENP_IN,NULL,0,(void*)USB_StatusHandler);
}
else
{
USB_T9_ERROR_REQUEST();
}
break;
case UsbRecipientEndpoint:
if(((UsbDevCtrl.State.DS > UsbDevStatusAddress) || ((UsbEp0SetupPacket.wIndex.Word & 0x7F) == 0)) &&
(UsbClearEpFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_StatusHandler(CTRL_ENP_OUT);
}
else
{
USB_T9_ERROR_REQUEST();
}
break;
default:
USB_T9_ERROR_REQUEST();
}
}
else
{
USB_T9_ERROR_REQUEST();
}
}
/*************************************************************************
* Function Name: UsbSetFeature
* Parameters: Int16U Feature,Int16U Index
*
* Return: UsbCommStatus_t
*
* Description: USB Set Device feature implement
*
*************************************************************************/
inline static
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 static
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 static
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);
USB_EP_STALL_HOOK(UsbEpAdd);
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbSetFeature
* Parameters: none
*
* Return: none
*
* Description: USB Set feature implement
*
*************************************************************************/
inline static
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_StatusHandler(CTRL_ENP_OUT);
}
else
{
USB_T9_ERROR_REQUEST();
}
break;
case UsbRecipientInterface:
if((UsbDevCtrl.State.DS > UsbDevStatusAddress) &&
(UsbSetInterfaceFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_StatusHandler(CTRL_ENP_OUT);
}
else
{
USB_T9_ERROR_REQUEST();
}
break;
case UsbRecipientEndpoint:
if(((UsbDevCtrl.State.DS > UsbDevStatusAddress) || ((UsbEp0SetupPacket.wIndex.Word & 0x7F) == 0)) &&
(UsbSetEpFeature(UsbEp0SetupPacket.wValue.Word,UsbEp0SetupPacket.wIndex.Word) == UsbPass))
{
USB_StatusHandler(CTRL_ENP_OUT);
}
else
{
USB_T9_ERROR_REQUEST();
}
break;
default:
USB_T9_ERROR_REQUEST();
}
}
else
{
USB_T9_ERROR_REQUEST();
}
}
/*************************************************************************
* Function Name: UsbSetAddress
* Parameters: none
*
* Return: none
*
* Description: USB Set address request implement
*
*************************************************************************/
inline static
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_StatusHandler(CTRL_ENP_IN);
}
else
{
USB_T9_ERROR_REQUEST();
}
}
/*************************************************************************
* Function Name: UsbGetDescriptorDevice
* Parameters: Int8U Index - must be 0
*
* Return: UsbCommStatus_t
*
* Description: USB get device's descriptor request implement
*
*************************************************************************/
inline static
UsbCommStatus_t UsbGetDescriptorDevice(Int8U Index)
{
if(Index == 0)
{
USB_IO_Data(CTRL_ENP_IN,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -