X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/7d4cccc22d60125fac111819df48af1873d11018..03ee87b35abdb8b92e8b55ec040fa943f9a6786c:/LUFA/Drivers/USB/LowLevel/Endpoint.c diff --git a/LUFA/Drivers/USB/LowLevel/Endpoint.c b/LUFA/Drivers/USB/LowLevel/Endpoint.c index aa18358c8..03be1185e 100644 --- a/LUFA/Drivers/USB/LowLevel/Endpoint.c +++ b/LUFA/Drivers/USB/LowLevel/Endpoint.c @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2009. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ /* - Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -39,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) { @@ -82,15 +71,53 @@ 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; + #endif - USB_INT_Clear(USB_INT_SOFI); - - while (!(Endpoint_ReadWriteAllowed())) + for (;;) { - if (!(USB_IsConnected)) + if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN) + { + if (Endpoint_IsINReady()) + return ENDPOINT_READYWAIT_NoError; + } + else + { + 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; @@ -103,13 +130,11 @@ uint8_t Endpoint_WaitUntilReady(void) return ENDPOINT_READYWAIT_Timeout; } } - - return ENDPOINT_READYWAIT_NoError; } uint8_t Endpoint_Discard_Stream(uint16_t Length #if !defined(NO_STREAM_CALLBACKS) - , uint8_t (* const Callback)(void) + , StreamCallbackPtr_t Callback #endif ) { @@ -118,271 +143,193 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length if ((ErrorCode = Endpoint_WaitUntilReady())) return ErrorCode; - while (Length--) - { - if (!(Endpoint_ReadWriteAllowed())) - { - 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; - } - - 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; + #if defined(FAST_STREAM_TRANSFERS) + uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07); - while (Length--) + if (Length >= 8) { - if (!(Endpoint_ReadWriteAllowed())) - { - Endpoint_ClearCurrentBank(); - - #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; - } - - 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())) + switch (BytesRemToAlignment) { - 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; + 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); } - - Endpoint_Write_Byte(*(DataStream--)); } - - 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_ReadWriteAllowed())) - { - 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; - } - - *(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_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)) + 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_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)) - { - 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_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; -} +/* The following abuses the C preprocessor in order to copy-past common code with slight alterations, + * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */ + +#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