X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/a504a3a010ec2441dda0209f195492fb36e7c97b..5de364163f5a7597ba2f54e37cdea493fbd1e7ff:/LUFA/Drivers/USB/LowLevel/Endpoint.c?ds=inline diff --git a/LUFA/Drivers/USB/LowLevel/Endpoint.c b/LUFA/Drivers/USB/LowLevel/Endpoint.c index 4615baa69..6d59510b8 100644 --- a/LUFA/Drivers/USB/LowLevel/Endpoint.c +++ b/LUFA/Drivers/USB/LowLevel/Endpoint.c @@ -71,12 +71,38 @@ void Endpoint_ClearEndpoints(void) } } +void Endpoint_ClearStatusStage(void) +{ + if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST) + { + while (!(Endpoint_IsOUTReceived())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + + Endpoint_ClearOUT(); + } + else + { + while (!(Endpoint_IsINReady())) + { + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; + } + + Endpoint_ClearIN(); + } +} + #if !defined(CONTROL_ONLY_DEVICE) uint8_t Endpoint_WaitUntilReady(void) { + #if (USB_STREAM_TIMEOUT_MS < 0xFF) + uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS; + #else uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS; - - USB_INT_Clear(USB_INT_SOFI); + #endif for (;;) { @@ -91,7 +117,7 @@ uint8_t Endpoint_WaitUntilReady(void) return ENDPOINT_READYWAIT_NoError; } - if (!(USB_IsConnected)) + if (USB_DeviceState == DEVICE_STATE_Unattached) return ENDPOINT_READYWAIT_DeviceDisconnected; else if (Endpoint_IsStalled()) return ENDPOINT_READYWAIT_EndpointStalled; @@ -108,7 +134,7 @@ uint8_t Endpoint_WaitUntilReady(void) uint8_t Endpoint_Discard_Stream(uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif ) { @@ -117,147 +143,47 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) - { - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearOUT(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - } - else - { - Endpoint_Discard_Byte(); - } - } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} - -uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length -#if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) -#endif - ) -{ - uint8_t* DataStream = (uint8_t*)Buffer; - uint8_t ErrorCode; - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - - while (Length--) - { - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearIN(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - } - else - { - Endpoint_Write_Byte(*(DataStream++)); - } - } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); -uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length -#if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) -#endif - ) -{ - uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1); - uint8_t ErrorCode; - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - - while (Length--) + if (Length >= 8) { - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearIN(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif + Length -= BytesRemToAlignment; - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - } - else + switch (BytesRemToAlignment) { - Endpoint_Write_Byte(*(DataStream--)); + default: + do + { + if (!(Endpoint_IsReadWriteAllowed())) + { + Endpoint_ClearOUT(); + + #if !defined(NO_STREAM_CALLBACKS) + if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) + return ENDPOINT_RWSTREAM_CallbackAborted; + #endif + + if ((ErrorCode = Endpoint_WaitUntilReady())) + return ErrorCode; + } + + Length -= 8; + + Endpoint_Discard_Byte(); + case 7: Endpoint_Discard_Byte(); + case 6: Endpoint_Discard_Byte(); + case 5: Endpoint_Discard_Byte(); + case 4: Endpoint_Discard_Byte(); + case 3: Endpoint_Discard_Byte(); + case 2: Endpoint_Discard_Byte(); + case 1: Endpoint_Discard_Byte(); + } while (Length >= 8); } } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} + #endif -uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length -#if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) -#endif - ) -{ - uint8_t* DataStream = (uint8_t*)Buffer; - uint8_t ErrorCode; - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - - while (Length--) - { - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearOUT(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - } - else - { - *(DataStream++) = Endpoint_Read_Byte(); - } - } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} - -uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length -#if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) -#endif - ) -{ - uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1); - uint8_t ErrorCode; - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - - while (Length--) + while (Length) { if (!(Endpoint_IsReadWriteAllowed())) { @@ -265,7 +191,7 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length #if !defined(NO_STREAM_CALLBACKS) if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; + return ENDPOINT_RWSTREAM_CallbackAborted; #endif if ((ErrorCode = Endpoint_WaitUntilReady())) @@ -273,128 +199,134 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length } else { - *(DataStream--) = Endpoint_Read_Byte(); - } - } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} -#endif - -uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) -{ - uint8_t* DataStream = (uint8_t*)Buffer; - bool LastPacketFull = false; - bool ShortTransfer = (Length < USB_ControlRequest.wLength); - - while (Length && !(Endpoint_IsOUTReceived())) - { - while (!(Endpoint_IsINReady())); - - while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) - { - Endpoint_Write_Byte(*(DataStream++)); - - Length--; - } - - LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize); - Endpoint_ClearIN(); - } - - if (Endpoint_IsOUTReceived()) - return ENDPOINT_RWCSTREAM_ERROR_HostAborted; - - if (LastPacketFull || ShortTransfer) - { - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); - } - - while (!(Endpoint_IsOUTReceived())); - - return ENDPOINT_RWCSTREAM_ERROR_NoError; -} - -uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length) -{ - uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1); - bool LastPacketFull = false; - bool ShortTransfer = (Length < USB_ControlRequest.wLength); - - while (Length && !(Endpoint_IsOUTReceived())) - { - while (!(Endpoint_IsINReady())); - - while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) - { - Endpoint_Write_Byte(*(DataStream--)); - + Endpoint_Discard_Byte(); Length--; } - - LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize); - Endpoint_ClearIN(); } - if (Endpoint_IsOUTReceived()) - return ENDPOINT_RWCSTREAM_ERROR_HostAborted; - - if (LastPacketFull || ShortTransfer) - { - while (!(Endpoint_IsINReady())); - Endpoint_ClearIN(); - } - - while (!(Endpoint_IsOUTReceived())); - - return ENDPOINT_RWCSTREAM_ERROR_NoError; + return ENDPOINT_RWSTREAM_NoError; } -uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) -{ - uint8_t* DataStream = (uint8_t*)Buffer; - - while (Length) - { - while (!(Endpoint_IsOUTReceived())); - - while (Length && Endpoint_BytesInEndpoint()) - { - *(DataStream++) = Endpoint_Read_Byte(); - - Length--; - } - - Endpoint_ClearOUT(); - } - - while (!(Endpoint_IsINReady())); - - return ENDPOINT_RWCSTREAM_ERROR_NoError; -} +#define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_LE +#define TEMPLATE_BUFFER_TYPE const void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN() +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++)) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_LE +#define TEMPLATE_BUFFER_TYPE const void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN() +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++)) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_LE +#define TEMPLATE_BUFFER_TYPE const void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN() +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++)) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_Stream_BE +#define TEMPLATE_BUFFER_TYPE const void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN() +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--)) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_EStream_BE +#define TEMPLATE_BUFFER_TYPE const void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN() +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--)) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_PStream_BE +#define TEMPLATE_BUFFER_TYPE const void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearIN() +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--)) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_LE +#define TEMPLATE_BUFFER_TYPE void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT() +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte() +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_LE +#define TEMPLATE_BUFFER_TYPE void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT() +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte()) +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_Stream_BE +#define TEMPLATE_BUFFER_TYPE void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT() +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte() +#include "Template/Template_Endpoint_RW.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_EStream_BE +#define TEMPLATE_BUFFER_TYPE void* +#define TEMPLATE_CLEAR_ENDPOINT() Endpoint_ClearOUT() +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte()) +#include "Template/Template_Endpoint_RW.c" -uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) -{ - uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1); - - while (Length) - { - while (!(Endpoint_IsOUTReceived())); - - while (Length && Endpoint_BytesInEndpoint()) - { - *(DataStream--) = Endpoint_Read_Byte(); - - Length--; - } - - Endpoint_ClearOUT(); - } - - while (!(Endpoint_IsINReady())); +#endif - return ENDPOINT_RWCSTREAM_ERROR_NoError; -} +#define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_LE +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr++)) +#include "Template/Template_Endpoint_Control_W.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_LE +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++)) +#include "Template/Template_Endpoint_Control_W.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_LE +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++)) +#include "Template/Template_Endpoint_Control_W.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_Control_Stream_BE +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(*((uint8_t*)BufferPtr--)) +#include "Template/Template_Endpoint_Control_W.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_Control_PStream_BE +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--)) +#include "Template/Template_Endpoint_Control_W.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Write_Control_EStream_BE +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--)) +#include "Template/Template_Endpoint_Control_W.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_LE +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte() +#include "Template/Template_Endpoint_Control_R.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_LE +#define TEMPLATE_BUFFER_OFFSET(Length) 0 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte()) +#include "Template/Template_Endpoint_Control_R.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_Control_Stream_BE +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte() +#include "Template/Template_Endpoint_Control_R.c" + +#define TEMPLATE_FUNC_NAME Endpoint_Read_Control_EStream_BE +#define TEMPLATE_BUFFER_OFFSET(Length) Length - 1 +#define TEMPLATE_TRANSFER_BYTE(BufferPtr) eeprom_write_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte()) +#include "Template/Template_Endpoint_Control_R.c" #endif