X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/6a10d6b465be27db090d760dc0fbe722c94e4344..5de364163f5a7597ba2f54e37cdea493fbd1e7ff:/LUFA/Drivers/USB/LowLevel/Endpoint.c diff --git a/LUFA/Drivers/USB/LowLevel/Endpoint.c b/LUFA/Drivers/USB/LowLevel/Endpoint.c index 273e1908a..6d59510b8 100644 --- a/LUFA/Drivers/USB/LowLevel/Endpoint.c +++ b/LUFA/Drivers/USB/LowLevel/Endpoint.c @@ -28,7 +28,8 @@ this software. */ -#include "USBMode.h" +#include "../HighLevel/USBMode.h" + #if defined(USB_CAN_BE_DEVICE) #define INCLUDE_FROM_ENDPOINT_C @@ -38,34 +39,23 @@ 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(); - UECFG1X = 0; + UECFG1X = 0; UECFG0X = UECFG0XData; UECFG1X = UECFG1XData; return Endpoint_IsConfigured(); } -#endif void Endpoint_ClearEndpoints(void) { @@ -81,307 +71,262 @@ void Endpoint_ClearEndpoints(void) } } -uint8_t Endpoint_WaitUntilReady(void) +void Endpoint_ClearStatusStage(void) { - uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS; - - USB_INT_Clear(USB_INT_SOFI); - - while (!(Endpoint_ReadWriteAllowed())) + if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST) { - if (!(USB_IsConnected)) - return ENDPOINT_READYWAIT_DeviceDisconnected; - else if (Endpoint_IsStalled()) - return ENDPOINT_READYWAIT_EndpointStalled; - - if (USB_INT_HasOccurred(USB_INT_SOFI)) + while (!(Endpoint_IsOUTReceived())) { - USB_INT_Clear(USB_INT_SOFI); - - if (!(TimeoutMSRem--)) - return ENDPOINT_READYWAIT_Timeout; + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; } - } - - return ENDPOINT_READYWAIT_NoError; -} -uint8_t Endpoint_Discard_Stream(uint16_t Length -#if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) -#endif - ) -{ - uint8_t ErrorCode; - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; - - while (Length--) + Endpoint_ClearOUT(); + } + else { - if (!(Endpoint_ReadWriteAllowed())) + while (!(Endpoint_IsINReady())) { - Endpoint_ClearCurrentBank(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; + if (USB_DeviceState == DEVICE_STATE_Unattached) + return; } - - Endpoint_Discard_Byte(); + + Endpoint_ClearIN(); } - - 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 - ) +#if !defined(CONTROL_ONLY_DEVICE) +uint8_t Endpoint_WaitUntilReady(void) { - uint8_t* DataStream = (uint8_t*)Buffer; - uint8_t ErrorCode; - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; + #if (USB_STREAM_TIMEOUT_MS < 0xFF) + uint8_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS; + #else + uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS; + #endif - while (Length--) + for (;;) { - if (!(Endpoint_ReadWriteAllowed())) + if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN) { - Endpoint_ClearCurrentBank(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; + if (Endpoint_IsINReady()) + return ENDPOINT_READYWAIT_NoError; } - - 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; - - while (Length--) - { - if (!(Endpoint_ReadWriteAllowed())) + else { - Endpoint_ClearCurrentBank(); - - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif - - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; + if (Endpoint_IsOUTReceived()) + return ENDPOINT_READYWAIT_NoError; } + + if (USB_DeviceState == DEVICE_STATE_Unattached) + return ENDPOINT_READYWAIT_DeviceDisconnected; + else if (Endpoint_IsStalled()) + return ENDPOINT_READYWAIT_EndpointStalled; + + if (USB_INT_HasOccurred(USB_INT_SOFI)) + { + USB_INT_Clear(USB_INT_SOFI); - Endpoint_Write_Byte(*(DataStream--)); + if (!(TimeoutMSRem--)) + return ENDPOINT_READYWAIT_Timeout; + } } - - return ENDPOINT_RWSTREAM_ERROR_NoError; } -uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length +uint8_t Endpoint_Discard_Stream(uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif - ) + ) { - uint8_t* DataStream = (uint8_t*)Buffer; uint8_t ErrorCode; if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) - { - if (!(Endpoint_ReadWriteAllowed())) - { - Endpoint_ClearCurrentBank(); + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); - #if !defined(NO_STREAM_CALLBACKS) - if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort)) - return ENDPOINT_RWSTREAM_ERROR_CallbackAborted; - #endif + if (Length >= 8) + { + Length -= BytesRemToAlignment; - if ((ErrorCode = Endpoint_WaitUntilReady())) - return ErrorCode; + switch (BytesRemToAlignment) + { + 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); } - - *(DataStream++) = Endpoint_Read_Byte(); } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} + #endif -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_ReadWriteAllowed())) + if (!(Endpoint_IsReadWriteAllowed())) { - Endpoint_ClearCurrentBank(); + Endpoint_ClearOUT(); #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())) return ErrorCode; } - - *(DataStream--) = Endpoint_Read_Byte(); - } - - return ENDPOINT_RWSTREAM_ERROR_NoError; -} - -uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) -{ - uint8_t* DataStream = (uint8_t*)Buffer; - bool SendZLP = true; - - while (Length && !(Endpoint_IsSetupOUTReceived())) - { - while (!(Endpoint_IsSetupINReady())); - - while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) - { - Endpoint_Write_Byte(*(DataStream++)); - - Length--; - } - - SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize); - Endpoint_ClearSetupIN(); - } - - if (Endpoint_IsSetupOUTReceived()) - return ENDPOINT_RWCSTREAM_ERROR_HostAborted; - - if (SendZLP) - { - while (!(Endpoint_IsSetupINReady())); - Endpoint_ClearSetupIN(); - } - - while (!(Endpoint_IsSetupOUTReceived())); - - 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 SendZLP = true; - - while (Length && !(Endpoint_IsSetupOUTReceived())) - { - while (!(Endpoint_IsSetupINReady())); - - while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) + else { - Endpoint_Write_Byte(*(DataStream--)); - + Endpoint_Discard_Byte(); Length--; } - - SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize); - Endpoint_ClearSetupIN(); - } - - if (Endpoint_IsSetupOUTReceived()) - return ENDPOINT_RWCSTREAM_ERROR_HostAborted; - - if (SendZLP) - { - while (!(Endpoint_IsSetupINReady())); - Endpoint_ClearSetupIN(); } - while (!(Endpoint_IsSetupOUTReceived())); - - 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_IsSetupOUTReceived())); - - while (Length && Endpoint_BytesInEndpoint()) - { - *(DataStream++) = Endpoint_Read_Byte(); - - Length--; - } - - Endpoint_ClearSetupOUT(); - } - - while (!(Endpoint_IsSetupINReady())); - - 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_IsSetupOUTReceived())); - - while (Length && Endpoint_BytesInEndpoint()) - { - *(DataStream--) = Endpoint_Read_Byte(); - - Length--; - } - - Endpoint_ClearSetupOUT(); - } - - while (!(Endpoint_IsSetupINReady())); +#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