X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/c20a94a4e84c89debf5e7109482ede708a694a0c..5de364163f5a7597ba2f54e37cdea493fbd1e7ff:/LUFA/Drivers/USB/LowLevel/Endpoint.c diff --git a/LUFA/Drivers/USB/LowLevel/Endpoint.c b/LUFA/Drivers/USB/LowLevel/Endpoint.c index da2925ec7..6d59510b8 100644 --- a/LUFA/Drivers/USB/LowLevel/Endpoint.c +++ b/LUFA/Drivers/USB/LowLevel/Endpoint.c @@ -39,22 +39,12 @@ uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE; #endif -#if !defined(STATIC_ENDPOINT_CONFIGURATION) -bool Endpoint_ConfigureEndpoint(const uint8_t Number, const uint8_t Type, const uint8_t Direction, - const uint16_t Size, const uint8_t Banks) +uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size) { - Endpoint_SelectEndpoint(Number); - Endpoint_EnableEndpoint(); - - UECFG1X = 0; - - UECFG0X = ((Type << EPTYPE0) | Direction); - UECFG1X = ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size)); - - return Endpoint_IsConfigured(); + return Endpoint_BytesToEPSizeMask(Size); } -#else -bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData) + +bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData) { Endpoint_SelectEndpoint(Number); Endpoint_EnableEndpoint(); @@ -66,7 +56,6 @@ bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0 return Endpoint_IsConfigured(); } -#endif void Endpoint_ClearEndpoints(void) { @@ -82,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 (;;) { @@ -102,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; @@ -119,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 ) { @@ -128,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; -} - -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; + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); - while (Length--) + if (Length >= 8) { - if (!(Endpoint_IsReadWriteAllowed())) - { - Endpoint_ClearIN(); + Length -= BytesRemToAlignment; - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - 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; -} - -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; + #endif - 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())) { @@ -276,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())) @@ -284,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