X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/d860e9e842c05e759214f5170f78783decae9956..b462f2d457ec2f0cfa22a1c3db198cb22f6809a1:/LUFA/Drivers/USB/LowLevel/Endpoint.c diff --git a/LUFA/Drivers/USB/LowLevel/Endpoint.c b/LUFA/Drivers/USB/LowLevel/Endpoint.c index efa1a2db4..1ff66ddfa 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,11 +71,14 @@ void Endpoint_ClearEndpoints(void) } } +#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 (;;) { @@ -118,7 +110,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 ) { @@ -127,7 +119,47 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); + + if (Length >= 8) + { + Length -= BytesRemToAlignment; + + 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); + } + } + #endif + + while (Length) { if (!(Endpoint_IsReadWriteAllowed())) { @@ -135,7 +167,7 @@ uint8_t Endpoint_Discard_Stream(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())) @@ -144,15 +176,16 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length else { Endpoint_Discard_Byte(); + Length--; } } - return ENDPOINT_RWSTREAM_ERROR_NoError; + return ENDPOINT_RWSTREAM_NoError; } uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif ) { @@ -162,7 +195,47 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); + + if (Length >= 8) + { + Length -= BytesRemToAlignment; + + switch (BytesRemToAlignment) + { + default: + do + { + if (!(Endpoint_IsReadWriteAllowed())) + { + Endpoint_ClearIN(); + + #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_Write_Byte(*(DataStream++)); + case 7: Endpoint_Write_Byte(*(DataStream++)); + case 6: Endpoint_Write_Byte(*(DataStream++)); + case 5: Endpoint_Write_Byte(*(DataStream++)); + case 4: Endpoint_Write_Byte(*(DataStream++)); + case 3: Endpoint_Write_Byte(*(DataStream++)); + case 2: Endpoint_Write_Byte(*(DataStream++)); + case 1: Endpoint_Write_Byte(*(DataStream++)); + } while (Length >= 8); + } + } + #endif + + while (Length) { if (!(Endpoint_IsReadWriteAllowed())) { @@ -170,7 +243,7 @@ uint8_t Endpoint_Write_Stream_LE(const 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())) @@ -179,15 +252,16 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length else { Endpoint_Write_Byte(*(DataStream++)); + Length--; } } - - return ENDPOINT_RWSTREAM_ERROR_NoError; + + return ENDPOINT_RWSTREAM_NoError; } uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif ) { @@ -197,7 +271,47 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); + + if (Length >= 8) + { + Length -= BytesRemToAlignment; + + switch (BytesRemToAlignment) + { + default: + do + { + if (!(Endpoint_IsReadWriteAllowed())) + { + Endpoint_ClearIN(); + + #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_Write_Byte(*(DataStream--)); + case 7: Endpoint_Write_Byte(*(DataStream--)); + case 6: Endpoint_Write_Byte(*(DataStream--)); + case 5: Endpoint_Write_Byte(*(DataStream--)); + case 4: Endpoint_Write_Byte(*(DataStream--)); + case 3: Endpoint_Write_Byte(*(DataStream--)); + case 2: Endpoint_Write_Byte(*(DataStream--)); + case 1: Endpoint_Write_Byte(*(DataStream--)); + } while (Length >= 8); + } + } + #endif + + while (Length) { if (!(Endpoint_IsReadWriteAllowed())) { @@ -205,7 +319,7 @@ uint8_t Endpoint_Write_Stream_BE(const 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())) @@ -214,15 +328,16 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length else { Endpoint_Write_Byte(*(DataStream--)); + Length--; } } - - return ENDPOINT_RWSTREAM_ERROR_NoError; + + return ENDPOINT_RWSTREAM_NoError; } uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif ) { @@ -232,7 +347,47 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); + + if (Length >= 8) + { + Length -= BytesRemToAlignment; + + 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; + + *(DataStream++) = Endpoint_Read_Byte(); + case 7: *(DataStream++) = Endpoint_Read_Byte(); + case 6: *(DataStream++) = Endpoint_Read_Byte(); + case 5: *(DataStream++) = Endpoint_Read_Byte(); + case 4: *(DataStream++) = Endpoint_Read_Byte(); + case 3: *(DataStream++) = Endpoint_Read_Byte(); + case 2: *(DataStream++) = Endpoint_Read_Byte(); + case 1: *(DataStream++) = Endpoint_Read_Byte(); + } while (Length >= 8); + } + } + #endif + + while (Length) { if (!(Endpoint_IsReadWriteAllowed())) { @@ -240,7 +395,7 @@ uint8_t Endpoint_Read_Stream_LE(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())) @@ -249,15 +404,16 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length else { *(DataStream++) = Endpoint_Read_Byte(); + Length--; } } - - return ENDPOINT_RWSTREAM_ERROR_NoError; + + return ENDPOINT_RWSTREAM_NoError; } uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif ) { @@ -267,7 +423,47 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); + + if (Length >= 8) + { + Length -= BytesRemToAlignment; + + 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; + + *(DataStream--) = Endpoint_Read_Byte(); + case 7: *(DataStream--) = Endpoint_Read_Byte(); + case 6: *(DataStream--) = Endpoint_Read_Byte(); + case 5: *(DataStream--) = Endpoint_Read_Byte(); + case 4: *(DataStream--) = Endpoint_Read_Byte(); + case 3: *(DataStream--) = Endpoint_Read_Byte(); + case 2: *(DataStream--) = Endpoint_Read_Byte(); + case 1: *(DataStream--) = Endpoint_Read_Byte(); + } while (Length >= 8); + } + } + #endif + + while (Length) { if (!(Endpoint_IsReadWriteAllowed())) { @@ -275,7 +471,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,17 +480,21 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length else { *(DataStream--) = Endpoint_Read_Byte(); + Length--; } } - - return ENDPOINT_RWSTREAM_ERROR_NoError; + + return ENDPOINT_RWSTREAM_NoError; } +#endif uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) { - uint8_t* DataStream = (uint8_t*)Buffer; + uint8_t* DataStream = (uint8_t*)Buffer; bool LastPacketFull = false; - bool ShortTransfer = (Length < USB_ControlRequest.wLength); + + if (Length > USB_ControlRequest.wLength) + Length = USB_ControlRequest.wLength; while (Length && !(Endpoint_IsOUTReceived())) { @@ -303,7 +503,6 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) { Endpoint_Write_Byte(*(DataStream++)); - Length--; } @@ -312,9 +511,9 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) } if (Endpoint_IsOUTReceived()) - return ENDPOINT_RWCSTREAM_ERROR_HostAborted; + return ENDPOINT_RWCSTREAM_HostAborted; - if (LastPacketFull || ShortTransfer) + if (LastPacketFull) { while (!(Endpoint_IsINReady())); Endpoint_ClearIN(); @@ -322,34 +521,36 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length) while (!(Endpoint_IsOUTReceived())); - return ENDPOINT_RWCSTREAM_ERROR_NoError; + return ENDPOINT_RWCSTREAM_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); + if (Length > USB_ControlRequest.wLength) + Length = USB_ControlRequest.wLength; + while (Length && !(Endpoint_IsOUTReceived())) { - while (!(Endpoint_IsINReady())); - - while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) + if (Endpoint_IsINReady()) { - Endpoint_Write_Byte(*(DataStream--)); + while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize)) + { + Endpoint_Write_Byte(*(DataStream--)); + Length--; + } - Length--; + LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize); + Endpoint_ClearIN(); } - - LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize); - Endpoint_ClearIN(); } if (Endpoint_IsOUTReceived()) - return ENDPOINT_RWCSTREAM_ERROR_HostAborted; + return ENDPOINT_RWCSTREAM_HostAborted; - if (LastPacketFull || ShortTransfer) + if (LastPacketFull) { while (!(Endpoint_IsINReady())); Endpoint_ClearIN(); @@ -357,7 +558,7 @@ uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length) while (!(Endpoint_IsOUTReceived())); - return ENDPOINT_RWCSTREAM_ERROR_NoError; + return ENDPOINT_RWCSTREAM_NoError; } uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) @@ -366,21 +567,21 @@ uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length) while (Length) { - while (!(Endpoint_IsOUTReceived())); - - while (Length && Endpoint_BytesInEndpoint()) + if (Endpoint_IsOUTReceived()) { - *(DataStream++) = Endpoint_Read_Byte(); + while (Length && Endpoint_BytesInEndpoint()) + { + *(DataStream++) = Endpoint_Read_Byte(); + Length--; + } - Length--; + Endpoint_ClearOUT(); } - - Endpoint_ClearOUT(); } while (!(Endpoint_IsINReady())); - return ENDPOINT_RWCSTREAM_ERROR_NoError; + return ENDPOINT_RWCSTREAM_NoError; } uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) @@ -389,21 +590,21 @@ uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length) while (Length) { - while (!(Endpoint_IsOUTReceived())); - - while (Length && Endpoint_BytesInEndpoint()) + if (Endpoint_IsOUTReceived()) { - *(DataStream--) = Endpoint_Read_Byte(); + while (Length && Endpoint_BytesInEndpoint()) + { + *(DataStream--) = Endpoint_Read_Byte(); + Length--; + } - Length--; + Endpoint_ClearOUT(); } - - Endpoint_ClearOUT(); } while (!(Endpoint_IsINReady())); - return ENDPOINT_RWCSTREAM_ERROR_NoError; + return ENDPOINT_RWCSTREAM_NoError; } #endif