uint8_t USB_Host_SendControlRequest(void* const BufferPtr) { uint8_t* DataStream = (uint8_t*)BufferPtr; bool BusSuspended = USB_Host_IsBusSuspended(); uint8_t ReturnStatus = HOST_SENDCONTROL_Successful; uint16_t DataLen = USB_ControlRequest.wLength; USB_Host_ResumeBus(); if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful) goto End_Of_Control_Send; Pipe_SetPipeToken(PIPE_TOKEN_SETUP); Pipe_ClearError(); Pipe_Unfreeze(); Pipe_Write_8(USB_ControlRequest.bmRequestType); Pipe_Write_8(USB_ControlRequest.bRequest); Pipe_Write_16_LE(USB_ControlRequest.wValue); Pipe_Write_16_LE(USB_ControlRequest.wIndex); Pipe_Write_16_LE(USB_ControlRequest.wLength); Pipe_ClearSETUP(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; Pipe_Freeze(); if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful) goto End_Of_Control_Send; if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST) { Pipe_SetPipeToken(PIPE_TOKEN_IN); if (DataStream != NULL) { while (DataLen) { Pipe_Unfreeze(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; if (!(Pipe_BytesInPipe())) DataLen = 0; while (Pipe_BytesInPipe() && DataLen) { *(DataStream++) = Pipe_Read_8(); DataLen--; } Pipe_Freeze(); Pipe_ClearIN(); } } Pipe_SetPipeToken(PIPE_TOKEN_OUT); Pipe_Unfreeze(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; Pipe_ClearOUT(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; } else { if (DataStream != NULL) { Pipe_SetPipeToken(PIPE_TOKEN_OUT); Pipe_Unfreeze(); while (DataLen) { if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; while (DataLen && (Pipe_BytesInPipe() < USB_Host_ControlPipeSize)) { Pipe_Write_8(*(DataStream++)); DataLen--; } Pipe_ClearOUT(); } if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; Pipe_Freeze(); } Pipe_SetPipeToken(PIPE_TOKEN_IN); Pipe_Unfreeze(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful) goto End_Of_Control_Send; Pipe_ClearIN(); } End_Of_Control_Send: Pipe_Freeze(); if (BusSuspended) USB_Host_SuspendBus(); Pipe_ResetPipe(PIPE_CONTROLPIPE); return ReturnStatus; }
static uint8_t USB_Host_SendControlRequest_PRV(void* const BufferPtr) { uint8_t* DataStream = (uint8_t*)BufferPtr; uint8_t ReturnStatus = HOST_SENDCONTROL_Successful; uint16_t DataLen = USB_ControlRequest.wLength; USB_Host_ResumeBus(); if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful) return ReturnStatus; Pipe_SetPipeToken(PIPE_TOKEN_SETUP); Pipe_ClearError(); Pipe_Unfreeze(); #if defined(ARCH_BIG_ENDIAN) Pipe_Write_8(USB_ControlRequest.bmRequestType); Pipe_Write_8(USB_ControlRequest.bRequest); Pipe_Write_16_LE(USB_ControlRequest.wValue); Pipe_Write_16_LE(USB_ControlRequest.wIndex); Pipe_Write_16_LE(USB_ControlRequest.wLength); #else uint8_t* HeaderStream = (uint8_t*)&USB_ControlRequest; for (uint8_t HeaderByte = 0; HeaderByte < sizeof(USB_Request_Header_t); HeaderByte++) Pipe_Write_8(*(HeaderStream++)); #endif Pipe_ClearSETUP(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_SetupSent)) != HOST_SENDCONTROL_Successful) return ReturnStatus; Pipe_Freeze(); if ((ReturnStatus = USB_Host_WaitMS(1)) != HOST_WAITERROR_Successful) return ReturnStatus; if ((USB_ControlRequest.bmRequestType & CONTROL_REQTYPE_DIRECTION) == REQDIR_DEVICETOHOST) { Pipe_SetPipeToken(PIPE_TOKEN_IN); if (DataStream != NULL) { while (DataLen) { Pipe_Unfreeze(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful) return ReturnStatus; if (!(Pipe_BytesInPipe())) DataLen = 0; while (Pipe_BytesInPipe() && DataLen) { *(DataStream++) = Pipe_Read_8(); DataLen--; } Pipe_Freeze(); Pipe_ClearIN(); } } Pipe_SetPipeToken(PIPE_TOKEN_OUT); Pipe_Unfreeze(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) return ReturnStatus; Pipe_ClearOUT(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) return ReturnStatus; } else { if (DataStream != NULL) { Pipe_SetPipeToken(PIPE_TOKEN_OUT); Pipe_Unfreeze(); while (DataLen) { if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) return ReturnStatus; while (DataLen && (Pipe_BytesInPipe() < USB_Host_ControlPipeSize)) { Pipe_Write_8(*(DataStream++)); DataLen--; } Pipe_ClearOUT(); } if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_OutReady)) != HOST_SENDCONTROL_Successful) return ReturnStatus; Pipe_Freeze(); } Pipe_SetPipeToken(PIPE_TOKEN_IN); Pipe_Unfreeze(); if ((ReturnStatus = USB_Host_WaitForIOS(USB_HOST_WAITFOR_InReceived)) != HOST_SENDCONTROL_Successful) return ReturnStatus; Pipe_ClearIN(); } return ReturnStatus; }