Fix up the incomplete Webserver project so that it integrates with the uIP stack...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
index aa18358..03be118 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
 uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;\r
 #endif\r
 \r
 uint8_t USB_ControlEndpointSize = ENDPOINT_CONTROLEP_DEFAULT_SIZE;\r
 #endif\r
 \r
-#if !defined(STATIC_ENDPOINT_CONFIGURATION)\r
-bool Endpoint_ConfigureEndpoint(const uint8_t  Number, const uint8_t Type, const uint8_t Direction,\r
-                                           const uint16_t Size, const uint8_t Banks)\r
+uint8_t Endpoint_BytesToEPSizeMaskDynamic(const uint16_t Size)\r
 {\r
 {\r
-       Endpoint_SelectEndpoint(Number);\r
-       Endpoint_EnableEndpoint();\r
-\r
-       UECFG1X = 0;    \r
-\r
-       UECFG0X = ((Type << EPTYPE0) | Direction);\r
-       UECFG1X = ((1 << ALLOC) | Banks | Endpoint_BytesToEPSizeMask(Size));\r
-\r
-       return Endpoint_IsConfigured();\r
+       return Endpoint_BytesToEPSizeMask(Size);\r
 }\r
 }\r
-#else\r
-bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData)\r
+\r
+bool Endpoint_ConfigureEndpoint_Prv(const uint8_t Number, const uint8_t UECFG0XData, const uint8_t UECFG1XData)\r
 {\r
        Endpoint_SelectEndpoint(Number);\r
        Endpoint_EnableEndpoint();\r
 \r
 {\r
        Endpoint_SelectEndpoint(Number);\r
        Endpoint_EnableEndpoint();\r
 \r
-       UECFG1X = 0;    \r
+       UECFG1X = 0;\r
 \r
        UECFG0X = UECFG0XData;\r
        UECFG1X = UECFG1XData;\r
 \r
        return Endpoint_IsConfigured();\r
 }\r
 \r
        UECFG0X = UECFG0XData;\r
        UECFG1X = UECFG1XData;\r
 \r
        return Endpoint_IsConfigured();\r
 }\r
-#endif\r
 \r
 void Endpoint_ClearEndpoints(void)\r
 {\r
 \r
 void Endpoint_ClearEndpoints(void)\r
 {\r
@@ -82,15 +71,53 @@ void Endpoint_ClearEndpoints(void)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
+void Endpoint_ClearStatusStage(void)\r
+{\r
+       if (USB_ControlRequest.bmRequestType & REQDIR_DEVICETOHOST)\r
+       {\r
+               while (!(Endpoint_IsOUTReceived()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
+\r
+               Endpoint_ClearOUT();\r
+       }\r
+       else\r
+       {\r
+               while (!(Endpoint_IsINReady()))\r
+               {\r
+                       if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+                         return;\r
+               }\r
+               \r
+               Endpoint_ClearIN();\r
+       }\r
+}\r
+\r
+#if !defined(CONTROL_ONLY_DEVICE)\r
 uint8_t Endpoint_WaitUntilReady(void)\r
 {\r
 uint8_t Endpoint_WaitUntilReady(void)\r
 {\r
+       #if (USB_STREAM_TIMEOUT_MS < 0xFF)\r
+       uint8_t  TimeoutMSRem = USB_STREAM_TIMEOUT_MS;  \r
+       #else\r
        uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
        uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
+       #endif\r
 \r
 \r
-       USB_INT_Clear(USB_INT_SOFI);\r
-\r
-       while (!(Endpoint_ReadWriteAllowed()))\r
+       for (;;)\r
        {\r
        {\r
-               if (!(USB_IsConnected))\r
+               if (Endpoint_GetEndpointDirection() == ENDPOINT_DIR_IN)\r
+               {\r
+                       if (Endpoint_IsINReady())\r
+                         return ENDPOINT_READYWAIT_NoError;\r
+               }\r
+               else\r
+               {\r
+                       if (Endpoint_IsOUTReceived())\r
+                         return ENDPOINT_READYWAIT_NoError;            \r
+               }\r
+               \r
+               if (USB_DeviceState == DEVICE_STATE_Unattached)\r
                  return ENDPOINT_READYWAIT_DeviceDisconnected;\r
                else if (Endpoint_IsStalled())\r
                  return ENDPOINT_READYWAIT_EndpointStalled;\r
                  return ENDPOINT_READYWAIT_DeviceDisconnected;\r
                else if (Endpoint_IsStalled())\r
                  return ENDPOINT_READYWAIT_EndpointStalled;\r
@@ -103,13 +130,11 @@ uint8_t Endpoint_WaitUntilReady(void)
                          return ENDPOINT_READYWAIT_Timeout;\r
                }\r
        }\r
                          return ENDPOINT_READYWAIT_Timeout;\r
                }\r
        }\r
-       \r
-       return ENDPOINT_READYWAIT_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Discard_Stream(uint16_t Length\r
 #if !defined(NO_STREAM_CALLBACKS)\r
 }\r
 \r
 uint8_t Endpoint_Discard_Stream(uint16_t Length\r
 #if !defined(NO_STREAM_CALLBACKS)\r
-                                , uint8_t (* const Callback)(void)\r
+                                , StreamCallbackPtr_t Callback\r
 #endif\r
                                                                )\r
 {\r
 #endif\r
                                                                )\r
 {\r
@@ -118,271 +143,193 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
-       {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
-               {\r
-                       Endpoint_ClearCurrentBank();\r
-\r
-                       #if !defined(NO_STREAM_CALLBACKS)\r
-                       if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
-                       #endif\r
-\r
-                       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-                         return ErrorCode;\r
-               }\r
-\r
-               Endpoint_Discard_Byte();\r
-       }\r
-       \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
-}\r
-\r
-uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length\r
-#if !defined(NO_STREAM_CALLBACKS)\r
-                                 , uint8_t (* const Callback)(void)\r
-#endif\r
-                                                                )\r
-{\r
-       uint8_t* DataStream   = (uint8_t*)Buffer;\r
-       uint8_t  ErrorCode;\r
-       \r
-       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-         return ErrorCode;\r
+       #if defined(FAST_STREAM_TRANSFERS)\r
+       uint8_t BytesRemToAlignment = (Endpoint_BytesInEndpoint() & 0x07);\r
 \r
 \r
-       while (Length--)\r
+       if (Length >= 8)\r
        {\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
-               {\r
-                       Endpoint_ClearCurrentBank();\r
-                       \r
-                       #if !defined(NO_STREAM_CALLBACKS)\r
-                       if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
-                       #endif\r
+               Length -= BytesRemToAlignment;\r
 \r
 \r
-                       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-                         return ErrorCode;\r
-               }\r
-\r
-               Endpoint_Write_Byte(*(DataStream++));\r
-       }\r
-       \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
-}\r
-\r
-uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length\r
-#if !defined(NO_STREAM_CALLBACKS)\r
-                                 , uint8_t (* const Callback)(void)\r
-#endif\r
-                                                                )\r
-{\r
-       uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);\r
-       uint8_t  ErrorCode;\r
-       \r
-       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-         return ErrorCode;\r
-\r
-       while (Length--)\r
-       {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               switch (BytesRemToAlignment)\r
                {\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
-\r
-                       #if !defined(NO_STREAM_CALLBACKS)\r
-                       if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
-                       #endif\r
-\r
-                       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-                         return ErrorCode;\r
+                       default:\r
+                               do\r
+                               {\r
+                                       if (!(Endpoint_IsReadWriteAllowed()))\r
+                                       {\r
+                                               Endpoint_ClearOUT();\r
+\r
+                                               #if !defined(NO_STREAM_CALLBACKS)\r
+                                               if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
+                                                 return ENDPOINT_RWSTREAM_CallbackAborted;\r
+                                               #endif\r
+\r
+                                               if ((ErrorCode = Endpoint_WaitUntilReady()))\r
+                                                 return ErrorCode;\r
+                                       }\r
+\r
+                                       Length -= 8;\r
+                                       \r
+                                       Endpoint_Discard_Byte();\r
+                       case 7: Endpoint_Discard_Byte();\r
+                       case 6: Endpoint_Discard_Byte();\r
+                       case 5: Endpoint_Discard_Byte();\r
+                       case 4: Endpoint_Discard_Byte();\r
+                       case 3: Endpoint_Discard_Byte();\r
+                       case 2: Endpoint_Discard_Byte();\r
+                       case 1: Endpoint_Discard_Byte();\r
+                               } while (Length >= 8);  \r
                }\r
                }\r
-\r
-               Endpoint_Write_Byte(*(DataStream--));\r
        }\r
        }\r
-       \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
-}\r
+       #endif\r
 \r
 \r
-uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length\r
-#if !defined(NO_STREAM_CALLBACKS)\r
-                                 , uint8_t (* const Callback)(void)\r
-#endif\r
-                                                                )\r
-{\r
-       uint8_t* DataStream = (uint8_t*)Buffer;\r
-       uint8_t  ErrorCode;\r
-       \r
-       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-         return ErrorCode;\r
-\r
-       while (Length--)\r
-       {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
-               {\r
-                       Endpoint_ClearCurrentBank();\r
-\r
-                       #if !defined(NO_STREAM_CALLBACKS)\r
-                       if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
-                       #endif\r
-\r
-                       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-                         return ErrorCode;\r
-               }\r
-               \r
-               *(DataStream++) = Endpoint_Read_Byte();\r
-       }\r
-       \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
-}\r
-\r
-uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length\r
-#if !defined(NO_STREAM_CALLBACKS)\r
-                                 , uint8_t (* const Callback)(void)\r
-#endif\r
-                                                                )\r
-{\r
-       uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);\r
-       uint8_t  ErrorCode;\r
-       \r
-       if ((ErrorCode = Endpoint_WaitUntilReady()))\r
-         return ErrorCode;\r
-\r
-       while (Length--)\r
+       while (Length)\r
        {\r
        {\r
-               if (!(Endpoint_ReadWriteAllowed()))\r
+               if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
                {\r
-                       Endpoint_ClearCurrentBank();\r
+                       Endpoint_ClearOUT();\r
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
 \r
                        #if !defined(NO_STREAM_CALLBACKS)\r
                        if ((Callback != NULL) && (Callback() == STREAMCALLBACK_Abort))\r
-                         return ENDPOINT_RWSTREAM_ERROR_CallbackAborted;\r
+                         return ENDPOINT_RWSTREAM_CallbackAborted;\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
                          return ErrorCode;\r
                }\r
-               \r
-               *(DataStream--) = Endpoint_Read_Byte();\r
-       }\r
-       \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
-}\r
-\r
-uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)\r
-{\r
-       uint8_t* DataStream = (uint8_t*)Buffer;\r
-       bool     SendZLP    = true;\r
-       \r
-       while (Length && !(Endpoint_IsSetupOUTReceived()))\r
-       {\r
-               while (!(Endpoint_IsSetupINReady()));\r
-               \r
-               while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+               else\r
                {\r
                {\r
-                       Endpoint_Write_Byte(*(DataStream++));\r
-                       \r
+                       Endpoint_Discard_Byte();\r
                        Length--;\r
                }\r
                        Length--;\r
                }\r
-               \r
-               SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearSetupIN();\r
-       }\r
-       \r
-       if (Endpoint_IsSetupOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
-       \r
-       if (SendZLP)\r
-       {\r
-               while (!(Endpoint_IsSetupINReady()));\r
-               Endpoint_ClearSetupIN();\r
        }\r
        \r
        }\r
        \r
-       while (!(Endpoint_IsSetupOUTReceived()));\r
-\r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
 \r
 }\r
 \r
-uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)\r
-{\r
-       uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);\r
-       bool     SendZLP    = true;\r
-       \r
-       while (Length && !(Endpoint_IsSetupOUTReceived()))\r
-       {\r
-               while (!(Endpoint_IsSetupINReady()));\r
-               \r
-               while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
-               {\r
-                       Endpoint_Write_Byte(*(DataStream--));\r
-                       \r
-                       Length--;\r
-               }\r
-               \r
-               SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearSetupIN();\r
-       }\r
-       \r
-       if (Endpoint_IsSetupOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
-       \r
-       if (SendZLP)\r
-       {\r
-               while (!(Endpoint_IsSetupINReady()));\r
-               Endpoint_ClearSetupIN();\r
-       }\r
-       \r
-       while (!(Endpoint_IsSetupOUTReceived()));\r
-\r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
-}\r
-\r
-uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)\r
-{\r
-       uint8_t* DataStream = (uint8_t*)Buffer;\r
-       \r
-       while (Length)\r
-       {\r
-               while (!(Endpoint_IsSetupOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
-               {\r
-                       *(DataStream++) = Endpoint_Read_Byte();\r
-                       \r
-                       Length--;\r
-               }\r
-               \r
-               Endpoint_ClearSetupOUT();\r
-       }\r
-       \r
-       while (!(Endpoint_IsSetupINReady()));\r
-       \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
-}\r
+/* The following abuses the C preprocessor in order to copy-past common code with slight alterations,\r
+ * so that the code needs to be written once. It is a crude form of templating to reduce code maintenance. */\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_LE\r
+#define  TEMPLATE_BUFFER_TYPE                      const void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_LE\r
+#define  TEMPLATE_BUFFER_TYPE                      const void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_LE\r
+#define  TEMPLATE_BUFFER_TYPE                      const void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Stream_BE\r
+#define  TEMPLATE_BUFFER_TYPE                      const void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_EStream_BE\r
+#define  TEMPLATE_BUFFER_TYPE                      const void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_PStream_BE\r
+#define  TEMPLATE_BUFFER_TYPE                      const void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearIN()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_LE\r
+#define  TEMPLATE_BUFFER_TYPE                      void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_LE\r
+#define  TEMPLATE_BUFFER_TYPE                      void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_write_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Stream_BE\r
+#define  TEMPLATE_BUFFER_TYPE                      void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()\r
+#include "Template/Template_Endpoint_RW.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_EStream_BE\r
+#define  TEMPLATE_BUFFER_TYPE                      void*\r
+#define  TEMPLATE_CLEAR_ENDPOINT()                 Endpoint_ClearOUT()\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_write_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())\r
+#include "Template/Template_Endpoint_RW.c"\r
 \r
 \r
-uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)\r
-{\r
-       uint8_t* DataStream = (uint8_t*)(Buffer + Length - 1);\r
-       \r
-       while (Length)\r
-       {\r
-               while (!(Endpoint_IsSetupOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
-               {\r
-                       *(DataStream--) = Endpoint_Read_Byte();\r
-                       \r
-                       Length--;\r
-               }\r
-               \r
-               Endpoint_ClearSetupOUT();\r
-       }\r
-       \r
-       while (!(Endpoint_IsSetupINReady()));\r
+#endif\r
 \r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
-}\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_LE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr++))\r
+#include "Template/Template_Endpoint_Control_W.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_LE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr++))\r
+#include "Template/Template_Endpoint_Control_W.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_LE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr++))\r
+#include "Template/Template_Endpoint_Control_W.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_Stream_BE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(*((uint8_t*)BufferPtr--))\r
+#include "Template/Template_Endpoint_Control_W.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_PStream_BE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(pgm_read_byte((uint8_t*)BufferPtr--))\r
+#include "Template/Template_Endpoint_Control_W.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Write_Control_EStream_BE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         Endpoint_Write_Byte(eeprom_read_byte((uint8_t*)BufferPtr--))\r
+#include "Template/Template_Endpoint_Control_W.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_LE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr++) = Endpoint_Read_Byte()\r
+#include "Template/Template_Endpoint_Control_R.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_LE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            0\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_write_byte((uint8_t*)BufferPtr++, Endpoint_Read_Byte())\r
+#include "Template/Template_Endpoint_Control_R.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_Stream_BE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         *((uint8_t*)BufferPtr--) = Endpoint_Read_Byte()\r
+#include "Template/Template_Endpoint_Control_R.c"\r
+\r
+#define  TEMPLATE_FUNC_NAME                        Endpoint_Read_Control_EStream_BE\r
+#define  TEMPLATE_BUFFER_OFFSET(Length)            (Length - 1)\r
+#define  TEMPLATE_TRANSFER_BYTE(BufferPtr)         eeprom_write_byte((uint8_t*)BufferPtr--, Endpoint_Read_Byte())\r
+#include "Template/Template_Endpoint_Control_R.c"\r
 \r
 #endif\r
 \r
 #endif\r