📄 usbio.c
字号:
/* Send data to the host if it has been */
/* requested. Otherwise, it will be sent at */
/* the next IN request on the endpoint. */
/********************************************/
if( USBMAIN_GetEpDataRequested( USB_EP1) == TRUE )
{
USBMAIN_SendDataHandler( USB_EP1 );
USBMAIN_SetEpDataRequested( USB_EP1, FALSE );
}
return PASS;
}
/****************************************************************************/
/* Get a single packet of a peer message request. */
/****************************************************************************/
uint16 usbIO_GetPeerPacket( uint08 *pPkt, uint32 waitMs )
{
uint16 pktSize;
if( RTA_SUCCESS != RTA_EventWait( ID_USBPeer_Event, TRUE, TMR_ConvertMSToTicks( waitMs )))
return 0; /* zero bytes received */
pktSize = Ep_Data[USB_EP2].recv_actual_len; /* number of bytes waiting */
USBMAIN_TranslateEpdataToMsg( USB_EP2, pPkt, pktSize ); /* copy caller */
RTA_EventClear( ID_USBPeer_Event ); /* clear event to receive next pkt */
RTA_SemRelease( ID_USBPeer_Sem ); /* release buffer */
return pktSize;
}
/****************************************************************************/
/* Send a complete peer message response. */
/****************************************************************************/
uint16 usbIO_PutPeerResponse( uint16 nbt, uint08 *pRsp )
{
dbmsg_ftrace( DBM_USB, "PutPeerResponse %d bytes\r\n", nbt );
USBMAIN_TranslateMsgToEpdata( USB_EP2, pRsp, nbt );
/********************************************/
/* Send data to the host if it has been */
/* requested. Otherwise, it will be sent at */
/* the next IN request on the endpoint. */
/********************************************/
if( USBMAIN_GetEpDataRequested (USB_EP2) == TRUE )
{
USBMAIN_SendDataHandler( USB_EP2 );
USBMAIN_SetEpDataRequested( USB_EP2, FALSE );
}
return PASS;
}
/****************************************************************************/
/* Process factory mode USB event. */
/****************************************************************************/
void USBMAIN_FactoryModeProcessEvent( void )
{
int08 result;
uint32 event;
uint16 rxData_Size;
BOOL rxData_SetupData;
/********************************************/
/* Get the state of the USB group event and */
/* handle the indicated events. All event */
/* bits are tested since more than one can */
/* be asserted at a time. */
/********************************************/
RTA_GroupEventGetState( ID_USBEvent, &event );
/********************************************/
/* Factory mode RESET */
/********************************************/
if( event & USB_DEV_RESET_DETECTED )
{
dbmsg_trace( DBM_USB, "USB_RESET" );
RTA_GroupEventClear( ID_USBEvent, USB_DEV_RESET_DETECTED );
USBMAIN_SetEpDataRequested( USB_EP1, FALSE ); /*PC request */
USBMAIN_SetEpDataRequested( USB_EP2, FALSE ); /* RFC request */
USBMAIN_SetEpDataRequested( USB_EP3, FALSE ); /* mouse request */
USBMAIN_SetEpDataRequested( USB_EP4, FALSE ); /* keyboard request */
USB_Reset();
result = USBMAIN_SetConfiguration();
result = USBMAIN_ConfigEps_FactoryMode();
if( MOUSE_HID_Timer_Id != NULL ) /* if mouse ide reporting enabled */
{
TMR_Delete( MOUSE_HID_Timer_Id ); /* free the timer */
MOUSE_HID_Timer_Id = NULL;
MOUSE_HID_Idle_Rate = 0; /*reset report rate */
}
if( KEYBOARD_HID_Timer_Id != NULL ) /* if KB ide reporting enabled */
{
TMR_Delete( KEYBOARD_HID_Timer_Id ); /* free the timer */
KEYBOARD_HID_Timer_Id = NULL;
KEYBOARD_HID_Idle_Rate = 0; /*reset report rate */
}
USB_FlushRxFIFO( USB_EP0 ); /* Flush all the FIFOs */
USB_FlushRxFIFO( USB_EP1 );
USB_FlushRxFIFO( USB_EP2 );
USB_FlushTxFIFO( USB_EP0 );
USB_FlushTxFIFO( USB_EP1 );
USB_FlushTxFIFO( USB_EP2 );
USB_FlushTxFIFO( USB_EP3 );
USB_FlushTxFIFO( USB_EP4 );
}
/********************************************/
/* Factory mode SUSPEND_DETECTED */
/********************************************/
if( event & USB_DEV_SUSPEND_DETECTED )
{
RTA_GroupEventClear( ID_USBEvent, USB_DEV_SUSPEND_DETECTED );
dbmsg_ftrace( DBM_USB, "USB_DEV_SUSPEND: state=%d\r\n",
USB_GetDeviceState());
}
/********************************************/
/* Factory mode SOF_DETECTED */
/********************************************/
if( event & USB_DEV_SOF_DETECTED )
{
RTA_GroupEventClear( ID_USBEvent, USB_DEV_SOF_DETECTED );
dbmsg_ftrace( DBM_USB, "USB_DEV_SOF: frame=%d\r\n",
USB_GetFrameNumber());
}
/********************************************/
/* Factory mode CONFIG_RECV */
/********************************************/
if( event & USB_DEV_SET_CONFIGURATION_RECV )
{
RTA_GroupEventClear( ID_USBEvent, USB_DEV_SET_CONFIGURATION_RECV );
dbmsg_ftrace( DBM_USB, "USB_SET_CONFIGURATION: config=%d state=%d\r\n",
USB_GetConfiguration(), USB_GetDeviceState());
iox_pinSet( IOX_D1_RED, TRUE ); /* configuration received LED */
}
/********************************************/
/* Factory mode SET_INTERFACE */
/* */
/* There is no alternate setting, so this */
/* isn't needed for this application. */
/********************************************/
if( event & USB_DEV_SET_INTERFACE_RECV )
{
uint08 interfaceNum;
uint08 alternateNum;
RTA_GroupEventClear( ID_USBEvent, USB_DEV_SET_INTERFACE_RECV );
USB_GetInterface( &interfaceNum, &alternateNum );
dbmsg_ftrace( DBM_USB, "USB_SET_INTERFACE: interface=%d alt=%d\r\n",
interfaceNum, alternateNum );
}
/********************************************/
/* Factory mode EP0_OUT_DATA */
/********************************************/
if( event & USB_EP0_OUT_DATA_RECV )
{
USB_DisableInterrupts();
result = USB_GetReceivedDataInfo( USB_EP0, &rxData_Size, &rxData_SetupData );
dbmsg_trace( DBM_USB, "USB_EP0_OUT_DATA\r\n" );
if( result == USB_NO_DATA_IN_RXFIFO ) /* if the rxFIFO is empty */
{
RTA_GroupEventClear( ID_USBEvent, USB_EP0_OUT_DATA_RECV );
USB_EnableInterrupts();
}
else /* if data received */
{
USB_EnableInterrupts();
if( TRUE == rxData_SetupData )
{
result = USBMAIN_RecvSetupDataHandler( USB_EP0, 8 );
}
else
{
/************************************************************/
/* Three stage control transfers with OUT data are not */
/* supported (i.e. Set_Report for HID), so just ignore this */
/* data. The status stage requires the USB device to send a */
/* zero byte write in response to the next IN packet. This */
/* has already been loaded in the TxFIFO when the */
/* Set_XX request was originally received. */
/************************************************************/
USBMAIN_RecvDataHandler( USB_EP0, rxData_Size );
}
}
}
/********************************************/
/* Factory mode EP1_OUT_DATA (pCtl) */
/* - Obtain the semaphore, indicating that */
/* the task buffer is free. */
/* - Signal the event, indicating that the */
/* task buffer contains data. */
/********************************************/
if( event & USB_EP1_OUT_DATA_RECV )
{
USB_DisableInterrupts();
result = USB_GetReceivedDataInfo(USB_EP1, &rxData_Size, &rxData_SetupData);
dbmsg_trace( DBM_USB, "USB_EP1_OUT: pCtl\r\n" );
if( result == USB_NO_DATA_IN_RXFIFO ) /* if the rxFIFO is empty */
{
RTA_GroupEventClear( ID_USBEvent, USB_EP1_OUT_DATA_RECV );
USB_EnableInterrupts();
dbmsg_trace( DBM_USB, "EP1: no Rx data in FIFO\r\n" );
}
else /* if data received */
{
USB_EnableInterrupts();
if( RTA_SUCCESS == RTA_SemReserve( ID_USBCmd_Sem, TMR_ConvertMSToTicks( 1000 )))
{
USBMAIN_RecvDataHandler( USB_EP1, rxData_Size );/* HW copy */
RTA_EventSet( ID_USBCmd_Event ); /* signal available */
}
else
{
dbmsg_trace( DBM_USB, "USB command packet transfer timeout\r\n" );
}
}
}
/********************************************/
/* Factory mode EP2_OUT_DATA (RFC) */
/* - Obtain the semaphore, indicating that */
/* the task buffer is free. */
/* - Signal the event, indicating that the */
/* task buffer contains data. */
/********************************************/
if( event & USB_EP2_OUT_DATA_RECV )
{
USB_DisableInterrupts();
result = USB_GetReceivedDataInfo( USB_EP2, &rxData_Size, &rxData_SetupData );
dbmsg_trace( DBM_USB, "USB_EP2_OUT: RFC\r\n" );
if( result == USB_NO_DATA_IN_RXFIFO ) /* if the rxFIFO is empty */
{
RTA_GroupEventClear( ID_USBEvent, USB_EP2_OUT_DATA_RECV );
USB_EnableInterrupts();
dbmsg_trace( DBM_USB, "EP2: no Rx data in FIFO\r\n" );
}
else /* if data received */
{
USB_EnableInterrupts();
if( RTA_SUCCESS == RTA_SemReserve( ID_USBPeer_Sem, TMR_ConvertMSToTicks( 100
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -