Oops - restore deleted MissileLauncher project, fix all spelling errors for "missile...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
index e196b6a..dca7554 100644 (file)
 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
-       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
-#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
@@ -66,7 +56,6 @@ bool Endpoint_ConfigureEndpointStatic(const uint8_t Number, const uint8_t UECFG0
 \r
        return Endpoint_IsConfigured();\r
 }\r
-#endif\r
 \r
 void Endpoint_ClearEndpoints(void)\r
 {\r
@@ -82,11 +71,14 @@ void Endpoint_ClearEndpoints(void)
        }\r
 }\r
 \r
+#if !defined(CONTROL_ONLY_DEVICE)\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
-\r
-       USB_INT_Clear(USB_INT_SOFI);\r
+       #endif\r
 \r
        for (;;)\r
        {\r
@@ -118,7 +110,7 @@ uint8_t Endpoint_WaitUntilReady(void)
 \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
@@ -127,7 +119,7 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -135,7 +127,7 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
 \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
@@ -144,15 +136,16 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
                else\r
                {\r
                        Endpoint_Discard_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_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
+                                 , StreamCallbackPtr_t Callback\r
 #endif\r
                                                                 )\r
 {\r
@@ -162,7 +155,7 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -170,7 +163,7 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
                        \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
@@ -179,15 +172,16 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_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
+                                 , StreamCallbackPtr_t Callback\r
 #endif\r
                                                                 )\r
 {\r
@@ -197,7 +191,7 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -205,7 +199,7 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
 \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
@@ -214,15 +208,16 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream--));\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\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
+                                 , StreamCallbackPtr_t Callback\r
 #endif\r
                                                                 )\r
 {\r
@@ -232,7 +227,7 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -240,7 +235,7 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
 \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
@@ -249,15 +244,16 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
                else\r
                {\r
                        *(DataStream++) = Endpoint_Read_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_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
+                                 , StreamCallbackPtr_t Callback\r
 #endif\r
                                                                 )\r
 {\r
@@ -267,7 +263,7 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -275,7 +271,7 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
 \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
@@ -284,16 +280,21 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
                else\r
                {\r
                        *(DataStream--) = Endpoint_Read_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \r
-       return ENDPOINT_RWSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWSTREAM_NoError;\r
 }\r
+#endif\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
+       uint8_t* DataStream     = (uint8_t*)Buffer;\r
+       bool     LastPacketFull = false;\r
+       \r
+       if (Length > USB_ControlRequest.wLength)\r
+         Length = USB_ControlRequest.wLength;\r
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
@@ -302,18 +303,17 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
-                       \r
                        Length--;\r
                }\r
                \r
-               SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+               LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
                Endpoint_ClearIN();\r
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
+         return ENDPOINT_RWCSTREAM_HostAborted;\r
        \r
-       if (SendZLP)\r
+       if (LastPacketFull)\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
@@ -321,33 +321,36 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
        \r
        while (!(Endpoint_IsOUTReceived()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\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
+       uint8_t* DataStream     = (uint8_t*)(Buffer + Length - 1);\r
+       bool     LastPacketFull = false;\r
        \r
+       if (Length > USB_ControlRequest.wLength)\r
+         Length = USB_ControlRequest.wLength;\r
+\r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
-               while (!(Endpoint_IsINReady()));\r
-               \r
-               while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+               if (Endpoint_IsINReady())\r
                {\r
-                       Endpoint_Write_Byte(*(DataStream--));\r
+                       while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+                       {\r
+                               Endpoint_Write_Byte(*(DataStream--));\r
+                               Length--;\r
+                       }\r
                        \r
-                       Length--;\r
+                       LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+                       Endpoint_ClearIN();\r
                }\r
-               \r
-               SendZLP = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearIN();\r
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
+         return ENDPOINT_RWCSTREAM_HostAborted;\r
        \r
-       if (SendZLP)\r
+       if (LastPacketFull)\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
@@ -355,7 +358,7 @@ uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)
        \r
        while (!(Endpoint_IsOUTReceived()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)\r
@@ -364,21 +367,21 @@ uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
+               if (Endpoint_IsOUTReceived())\r
                {\r
-                       *(DataStream++) = Endpoint_Read_Byte();\r
+                       while (Length && Endpoint_BytesInEndpoint())\r
+                       {\r
+                               *(DataStream++) = Endpoint_Read_Byte();\r
+                               Length--;\r
+                       }\r
                        \r
-                       Length--;\r
+                       Endpoint_ClearOUT();\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
        \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)\r
@@ -387,21 +390,21 @@ uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
+               if (Endpoint_IsOUTReceived())\r
                {\r
-                       *(DataStream--) = Endpoint_Read_Byte();\r
+                       while (Length && Endpoint_BytesInEndpoint())\r
+                       {\r
+                               *(DataStream--) = Endpoint_Read_Byte();\r
+                               Length--;\r
+                       }\r
                        \r
-                       Length--;\r
+                       Endpoint_ClearOUT();\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 #endif\r