@ -603,15 +603,14 @@ static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost)
{
{
case CTRL_SETUP :
case CTRL_SETUP :
/* send a SETUP packet */
/* send a SETUP packet */
USBH_CtlSendSetup ( phost ,
phost - > Control . timer = phost - > Timer ;
USBH_CtlSendSetup ( phost ,
( uint8_t * ) phost - > Control . setup . d8 ,
( uint8_t * ) phost - > Control . setup . d8 ,
phost - > Control . pipe_out ) ;
phost - > Control . pipe_out ) ;
phost - > Control . state = CTRL_SETUP_WAIT ;
phost - > Control . state = CTRL_SETUP_WAIT ;
break ;
break ;
case CTRL_SETUP_WAIT :
case CTRL_SETUP_WAIT :
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_out ) ;
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_out ) ;
/* case SETUP packet sent successfully */
/* case SETUP packet sent successfully */
if ( URB_Status = = USBH_URB_DONE )
if ( URB_Status = = USBH_URB_DONE )
@ -647,22 +646,24 @@ static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost)
phost - > Control . state = CTRL_STATUS_IN ;
phost - > Control . state = CTRL_STATUS_IN ;
}
}
}
}
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_ERROR )
else if ( URB_Status = = USBH_URB_ERROR )
{
{
phost - > Control . state = CTRL_ERROR ;
phost - > Control . state = CTRL_ERROR ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_NOTREADY )
else if ( URB_Status = = USBH_URB_NOTREADY )
{
{
//Some mice cause a transaction error interrupt (HCINT_TXERR) at this point,
//Some mice cause a transaction error interrupt (HCINT_TXERR) at this point.
//so we retry and hope for the best!
//Other mice just NAK our transaction.
phost - > Control . state = CTRL_SETUP ;
//Either way we need to retry, but keep our original timeout so we don't wait forever.
USBH_CtlSendSetup ( phost ,
( uint8_t * ) phost - > Control . setup . d8 ,
phost - > Control . pipe_out ) ;
}
if ( ( int32_t ) ( phost - > Timer - phost - > Control . timer ) > = USBH_CTRL_TRANSACTION_TIMEOUT_MS )
{
phost - > Control . state = CTRL_ERROR ;
}
}
break ;
break ;
@ -678,171 +679,139 @@ static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost)
break ;
break ;
case CTRL_DATA_IN_WAIT :
case CTRL_DATA_IN_WAIT :
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_in ) ;
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_in ) ;
/* check is DATA packet transferred successfully */
/* check is DATA packet transferred successfully */
if ( URB_Status = = USBH_URB_DONE )
if ( URB_Status = = USBH_URB_DONE )
{
{
phost - > Control . state = CTRL_STATUS_OUT ;
phost - > Control . state = CTRL_STATUS_OUT ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
/* manage error cases*/
/* manage error cases*/
if ( URB_Status = = USBH_URB_STALL )
if ( URB_Status = = USBH_URB_STALL )
{
{
/* In stall case, return to previous machine state*/
//Retry transaction
status = USBH_NOT_SUPPORTED ;
phost - > Control . state = CTRL_ERROR ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_ERROR )
else if ( URB_Status = = USBH_URB_ERROR )
{
{
/* Device error */
/* Device error */
phost - > Control . state = CTRL_ERROR ;
phost - > Control . state = CTRL_ERROR ;
# if (USBH_USE_OS == 1)
}
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
if ( ( int32_t ) ( phost - > Timer - phost - > Control . timer ) > = USBH_CTRL_TRANSACTION_TIMEOUT_MS )
{
phost - > Control . state = CTRL_ERROR ;
}
}
break ;
break ;
case CTRL_DATA_OUT :
case CTRL_DATA_OUT :
phost - > Control . timer = phost - > Timer ;
USBH_CtlSendData ( phost ,
USBH_CtlSendData ( phost ,
phost - > Control . buff ,
phost - > Control . buff ,
phost - > Control . length ,
phost - > Control . length ,
phost - > Control . pipe_out ,
phost - > Control . pipe_out ,
1 ) ;
1 ) ;
phost - > Control . timer = phost - > Timer ;
phost - > Control . state = CTRL_DATA_OUT_WAIT ;
phost - > Control . state = CTRL_DATA_OUT_WAIT ;
break ;
break ;
case CTRL_DATA_OUT_WAIT :
case CTRL_DATA_OUT_WAIT :
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_out ) ;
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_out ) ;
if ( URB_Status = = USBH_URB_DONE )
if ( URB_Status = = USBH_URB_DONE )
{ /* If the Setup Pkt is sent successful, then change the state */
{ /* If the Setup Pkt is sent successful, then change the state */
phost - > Control . state = CTRL_STATUS_IN ;
phost - > Control . state = CTRL_STATUS_IN ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
/* handle error cases */
/* handle error cases */
else if ( URB_Status = = USBH_URB_STALL )
else if ( URB_Status = = USBH_URB_STALL )
{
{
/* In stall case, return to previous machine state*/
//Retry transaction
phost - > Control . state = CTRL_STALLED ;
phost - > Control . state = CTRL_ERROR ;
status = USBH_NOT_SUPPORTED ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_NOTREADY )
else if ( URB_Status = = USBH_URB_NOTREADY )
{
{
/* Nack received from device */
/* Nack received from device */
phost - > Control . state = CTRL_DATA_OUT ;
phost - > Control . state = CTRL_DATA_OUT ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_ERROR )
else if ( URB_Status = = USBH_URB_ERROR )
{
{
/* device error */
/* device error */
phost - > Control . state = CTRL_ERROR ;
phost - > Control . state = CTRL_ERROR ;
status = USBH_FAIL ;
status = USBH_FAIL ;
}
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
if ( ( int32_t ) ( phost - > Timer - phost - > Control . timer ) > = USBH_CTRL_TRANSACTION_TIMEOUT_MS )
# endif
{
}
phost - > Control . state = CTRL_ERROR ;
}
break ;
break ;
case CTRL_STATUS_IN :
case CTRL_STATUS_IN :
/* Send 0 bytes out packet */
/* Send 0 bytes out packet */
phost - > Control . timer = phost - > Timer ;
USBH_CtlReceiveData ( phost ,
USBH_CtlReceiveData ( phost ,
0 ,
0 ,
0 ,
0 ,
phost - > Control . pipe_in ) ;
phost - > Control . pipe_in ) ;
phost - > Control . timer = phost - > Timer ;
phost - > Control . state = CTRL_STATUS_IN_WAIT ;
phost - > Control . state = CTRL_STATUS_IN_WAIT ;
break ;
break ;
case CTRL_STATUS_IN_WAIT :
case CTRL_STATUS_IN_WAIT :
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_in ) ;
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_in ) ;
if ( URB_Status = = USBH_URB_DONE )
if ( URB_Status = = USBH_URB_DONE )
{ /* Control transfers completed, Exit the State Machine */
{ /* Control transfers completed, Exit the State Machine */
phost - > Control . state = CTRL_COMPLETE ;
phost - > Control . state = CTRL_COMPLETE ;
status = USBH_OK ;
status = USBH_OK ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_ERROR )
else if ( URB_Status = = USBH_URB_ERROR )
{
{
phost - > Control . state = CTRL_ERROR ;
phost - > Control . state = CTRL_ERROR ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_STALL )
else if ( URB_Status = = USBH_URB_STALL )
{
{
/* Control transfers completed, Exit the State Machine */
//Retry transaction
status = USBH_NOT_SUPPORTED ;
phost - > Control . state = CTRL_ERROR ;
}
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
if ( ( int32_t ) ( phost - > Timer - phost - > Control . timer ) > = USBH_CTRL_TRANSACTION_TIMEOUT_MS )
# endif
{
phost - > Control . state = CTRL_ERROR ;
}
}
break ;
break ;
case CTRL_STATUS_OUT :
case CTRL_STATUS_OUT :
phost - > Control . timer = phost - > Timer ;
USBH_CtlSendData ( phost ,
USBH_CtlSendData ( phost ,
0 ,
0 ,
0 ,
0 ,
phost - > Control . pipe_out ,
phost - > Control . pipe_out ,
1 ) ;
1 ) ;
phost - > Control . timer = phost - > Timer ;
phost - > Control . state = CTRL_STATUS_OUT_WAIT ;
phost - > Control . state = CTRL_STATUS_OUT_WAIT ;
break ;
break ;
case CTRL_STATUS_OUT_WAIT :
case CTRL_STATUS_OUT_WAIT :
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_out ) ;
URB_Status = USBH_LL_GetURBState ( phost , phost - > Control . pipe_out ) ;
if ( URB_Status = = USBH_URB_DONE )
if ( URB_Status = = USBH_URB_DONE )
{
{
status = USBH_OK ;
status = USBH_OK ;
phost - > Control . state = CTRL_COMPLETE ;
phost - > Control . state = CTRL_COMPLETE ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_NOTREADY )
else if ( URB_Status = = USBH_URB_NOTREADY )
{
{
phost - > Control . state = CTRL_STATUS_OUT ;
phost - > Control . state = CTRL_STATUS_OUT ;
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
# endif
}
}
else if ( URB_Status = = USBH_URB_ERROR )
else if ( URB_Status = = USBH_URB_ERROR )
{
{
phost - > Control . state = CTRL_ERROR ;
phost - > Control . state = CTRL_ERROR ;
}
# if (USBH_USE_OS == 1)
osMessagePut ( phost - > os_event , USBH_CONTROL_EVENT , 0 ) ;
if ( ( int32_t ) ( phost - > Timer - phost - > Control . timer ) > = USBH_CTRL_TRANSACTION_TIMEOUT_MS )
# endif
{
phost - > Control . state = CTRL_ERROR ;
}
}
break ;
break ;