📄 usb_common.c
字号:
{
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;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
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])
{
USB_T9W_TRACE_INFO("T9W UsbGetDescriptorString() can't find string\n\r");
return(UsbFault);
}
// Find String index
for(pData = (Int8U **)UsbString[LanguagesOffset]; pData ; ++pData)
{
if(--Index == 0)
{
break;
}
}
if(pData == NULL)
{
USB_T9E_TRACE_INFO("T9E UsbGetDescriptorString() - NULL \n\r");
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;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
}
return(UsbPass);
}
/*************************************************************************
* Function Name: UsbGetDescriptor
* Parameters: none
*
* Return: none
*
* Description: USB get descriptor request implement
*
*************************************************************************/
static 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;
}
else
{
USB_T9M_TRACE_INFO("T9M UsbGetDescriptor() UsbDescriptorDevice \n\r");
}
break;
case UsbDescriptorConfiguration:
if(UsbGetDescriptorConfiguration(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
else
{
USB_T9M_TRACE_INFO("T9M UsbGetDescriptor() UsbDescriptorConfiguration \n\r");
}
break;
case UsbDescriptorString:
if(UsbGetDescriptorString(UsbEp0SetupPacket.wValue.Lo) != UsbPass)
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
else
{
USB_T9M_TRACE_INFO("T9M UsbGetDescriptor() UsbDescriptorString \n\r");
}
break;
default:
if ((UsbUserFun[UsbUserGetDescriptor] == NULL) ||
((Int32U)UsbUserFun[UsbUserGetDescriptor](&UsbEp0Ctrl) != UsbPass))
{
USB_T9M_TRACE_INFO("T9M UsbGetDescriptor() User defined\n\r");
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
else
{
USB_T9W_TRACE_INFO("T9W UsbGetDescriptor() user defined fault\n\r");
}
}
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSetDescriptor
* Parameters: none
*
* Return: none
*
* Description: USB Set descriptor
*
*************************************************************************/
static inline
void UsbSetDescriptor(void)
{
USB_T9W_TRACE_INFO("T9W UsbSetDescriptor() unsupported\n\r");
if(UsbDevCtrl.State.DS >= UsbDevStatusAddress)
{
// This request is valid only for Address state and Configured state.
// If supported
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
else
{
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbGetConfiguration
* Parameters: none
*
* Return: none
*
* Description: USB Get configuration implement
*
*************************************************************************/
static inline
void UsbGetConfiguration(void)
{
if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
(UsbEp0SetupPacket.wValue.Word == 0) &&
(UsbEp0SetupPacket.wIndex.Word == 0) &&
(UsbEp0SetupPacket.wLength.Word == 1))
{
UsbEp0Ctrl.Counter = 1;
EpCtrlDataBuf[0] = UsbCoreReq(UsbCoreReqConfiquration);
UsbEp0Ctrl.pData = EpCtrlDataBuf;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
USB_T9M_TRACE_INFO("T9M UsbGetConfiguration()\n\r"
" UsbCoreReqConfiquration - %d\n\r",
EpCtrlDataBuf[0]);
}
else
{
USB_T9W_TRACE_INFO("T9W UsbGetConfiguration()\n\r"
" State.DS (>=4) - %d\n\r"
" wIndex.Word (==0) - %d\n\r"
" wLength.Word (==1) - %d\n\r"
" wValue.Wor (==0) - %d\n\r",
UsbDevCtrl.State.DS,UsbEp0SetupPacket.wIndex.Word,
UsbEp0SetupPacket.wLength.Word,UsbEp0SetupPacket.wValue.Word);
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSetConfiguration
* Parameters: none
*
* Return: none
*
* Description: USB Set configuration implement
*
*************************************************************************/
static inline
void UsbSetConfiguration(void)
{
if((UsbDevCtrl.State.DS >= UsbDevStatusAddress) &&
(UsbEp0SetupPacket.wIndex.Word == 0) &&
(UsbEp0SetupPacket.wLength.Word == 0) &&
(UsbSetConfigurtonState(UsbEp0SetupPacket.wValue.Lo) == UsbPass))
{
UsbEp0Ctrl.Counter = 0;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
}
else
{
USB_T9W_TRACE_INFO("T9W UsbSetConfiguration()\n\r"
" State.DS (==5) - %d\n\r"
" wIndex.Word - %d\n\r"
" wLength.Word (==0) - %d\n\r"
" wValue.Wor (==0) - %d\n\r",
UsbDevCtrl.State.DS,UsbEp0SetupPacket.wIndex.Word,
UsbEp0SetupPacket.wLength.Word,UsbEp0SetupPacket.wValue.Word);
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbGetInterface
* Parameters: none
*
* Return: none
*
* Description: Implement get interface request
*
*************************************************************************/
static inline
void UsbGetInterface (void)
{
if((UsbDevCtrl.State.DS == UsbDevStatusConfigured) &&
(UsbEp0SetupPacket.wIndex.Word == UsbDevCtrl.Interface) &&
(UsbEp0SetupPacket.wLength.Word == 1) &&
(UsbEp0SetupPacket.wValue.Word == 0))
{
USB_T9M_TRACE_INFO("T9M UsbGetInterface()\n\r");
UsbEp0Ctrl.Counter = 1;
EpCtrlDataBuf[0] = UsbDevCtrl.AlternateSetting;
UsbEp0Ctrl.pData = EpCtrlDataBuf;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
}
else
{
volatile Int32U Interface = UsbDevCtrl.Interface;
USB_T9W_TRACE_INFO("T9W UsbGetInterface()\n\r"
" State.DS (==5) - %d\n\r"
" wIndex.Word - %d\n\r"
" Interface - %d\n\r"
" wLength.Word (==1) - %d\n\r"
" wValue.Wor (==0) - %d\n\r",
UsbDevCtrl.State.DS,UsbEp0SetupPacket.wIndex.Word,
Interface,UsbEp0SetupPacket.wLength.Word,
UsbEp0SetupPacket.wValue.Word);
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSetInterface
* Parameters: none
*
* Return: none
*
* Description: Implement set interface request
*
*************************************************************************/
static inline
void UsbSetInterface (void)
{
if((UsbDevCtrl.State.DS == UsbDevStatusConfigured) &&
(UsbEp0SetupPacket.wLength.Word == 0))
{
UsbStandardConfigurationDescriptor_t * pData = UsbFindConfiguration(UsbDevCtrl.Configuration);
UsbDevCtrl_t UsbDevCtrlNew = UsbDevCtrl;
UsbDevCtrlNew.Interface = UsbEp0SetupPacket.wIndex.Word;
UsbDevCtrlNew.AlternateSetting = UsbEp0SetupPacket.wValue.Word;
UsbDevCtrlNew.Feature.RemoteWakeupEnable = 0;
// Whether the interface is exist into current configuration?
if(UsbFindInterface(pData,&UsbDevCtrlNew) != UsbPass)
{
USB_T9W_TRACE_INFO("T9W UsbSetInterface() can't find interface\n\r");
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
return;
}
USB_T9M_TRACE_INFO("T9M UsbSetInterface()\n\r");
UsbUserFun[UsbUserConfigure]((void *)&UsbDevCtrlNew);
UsbDevCtrl = UsbDevCtrlNew;
UsbEp0Ctrl.Counter = 0;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
}
else
{
USB_T9W_TRACE_INFO("T9W UsbSetInterface()\n\r"
" State.DS (==5) - %d\n\r"
" wLength.Word (==2) - %d\n\r",
UsbDevCtrl.State.DS,
UsbEp0SetupPacket.wLength.Word);
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbSynchFrame
* Parameters: none
*
* Return: none
*
* Description: Implement synch frame request
*
*************************************************************************/
static inline
void UsbSynchFrame (void)
{
if( USB_SOF_EVENT &&
(UsbDevCtrl.State.DS == UsbDevStatusConfigured) &&
(UsbEp0SetupPacket.wLength.Word == 2) &&
(UsbEp0SetupPacket.wValue.Word == 0))
{
Int32U FrameNumb = 0;
#if USB_SOF_EVENT > 0
FrameNumb = USB_GetFrameNumb();
#endif
UsbEp0Ctrl.Counter = 2;
EpCtrlDataBuf[0] = FrameNumb;
EpCtrlDataBuf[1] = FrameNumb>>8;
UsbEp0Ctrl.pData = EpCtrlDataBuf;
UsbEp0Ctrl.EpStatus.Status = UsbDataPhase;
USB_T9M_TRACE_INFO("T9M UsbSynchFrame() %d \n\r",FrameNumb);
}
else
{
USB_T9W_TRACE_INFO("T9W UsbSynchFrame()\n\r"
" USB_SOF_EVENT - %d\n\r"
" State.DS (==5) - %d\n\r"
" wLength.Word (==2) - %d\n\r"
" wValue.Word (==0) - %d\n\r",
USB_SOF_EVENT,UsbDevCtrl.State.DS,
UsbEp0SetupPacket.wLength.Word,
UsbEp0SetupPacket.wValue.Word);
UsbEp0Ctrl.EpStatus.Status = UsbEpStall;
}
}
/*************************************************************************
* Function Name: UsbDevConnectCallback
* Parameters: void * pArg
*
* Return: none
*
* Description: USB connect callback
*
*************************************************************************/
static inline
void UsbDevConnectCallback (Boolean Connect)
{
UsbSetDevState(Connect?UsbDevStatusAttached:UsbDevStatusUnknow);
if(UsbUserFun[UsbUserConnect] != NULL)
{
UsbUserFun[UsbUserConnect]((void*)Connect);
}
}
/*************************************************************************
* Function Name: UsbDevSuspendCallback
* Parameters: Boolean Suspend
*
* Return: none
*
* Description: USB suspend callback
*
*************************************************************************/
static inline
void UsbDevSuspendCallback (Boolean Suspend)
{
UsbSetDevSuspend(Suspend?UsbDevStatusSuspend:UsbDevStatusNoSuspend);
if(UsbUserFun[UsbUserSuspend] != NULL)
{
UsbUserFun[UsbUserSuspend]((void*)Suspend);
}
}
/*************************************************************************
* Function Name: UsbDevResetCallback
* Parameters: none
*
* Return: none
*
* Description: USB reset callback
*
*************************************************************************/
static inline
void UsbDevResetCallback (void)
{
UsbSetDevState(UsbDevStatusDefault);
if(UsbUserFun[UsbUserReset] != NULL)
{
UsbUserFun[UsbUserReset](NULL);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -