Oops - restore deleted MissileLauncher project, fix all spelling errors for "missile...
[pub/USBasp.git] / LUFA / Drivers / USB / LowLevel / Endpoint.c
index da2925e..dca7554 100644 (file)
 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
        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
 \r
        return Endpoint_IsConfigured();\r
 }\r
-#endif\r
 \r
 void Endpoint_ClearEndpoints(void)\r
 {\r
 \r
 void Endpoint_ClearEndpoints(void)\r
 {\r
@@ -85,9 +74,11 @@ void Endpoint_ClearEndpoints(void)
 #if !defined(CONTROL_ONLY_DEVICE)\r
 uint8_t Endpoint_WaitUntilReady(void)\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
        uint16_t TimeoutMSRem = USB_STREAM_TIMEOUT_MS;\r
-\r
-       USB_INT_Clear(USB_INT_SOFI);\r
+       #endif\r
 \r
        for (;;)\r
        {\r
 \r
        for (;;)\r
        {\r
@@ -119,7 +110,7 @@ uint8_t Endpoint_WaitUntilReady(void)
 \r
 uint8_t Endpoint_Discard_Stream(uint16_t Length\r
 #if !defined(NO_STREAM_CALLBACKS)\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
@@ -128,7 +119,7 @@ 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
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -136,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
 \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
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -145,15 +136,16 @@ uint8_t Endpoint_Discard_Stream(uint16_t Length
                else\r
                {\r
                        Endpoint_Discard_Byte();\r
                else\r
                {\r
                        Endpoint_Discard_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \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
 }\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
 #endif\r
                                                                 )\r
 {\r
@@ -163,7 +155,7 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -171,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
                        \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
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -180,15 +172,16 @@ uint8_t Endpoint_Write_Stream_LE(const void* Buffer, uint16_t Length
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
+                       Length--;\r
                }\r
        }\r
        \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
 }\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
 #endif\r
                                                                 )\r
 {\r
@@ -198,7 +191,7 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -206,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
 \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
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -215,15 +208,16 @@ uint8_t Endpoint_Write_Stream_BE(const void* Buffer, uint16_t Length
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream--));\r
                else\r
                {\r
                        Endpoint_Write_Byte(*(DataStream--));\r
+                       Length--;\r
                }\r
        }\r
        \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
 }\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
 #endif\r
                                                                 )\r
 {\r
@@ -233,7 +227,7 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -241,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
 \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
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -250,15 +244,16 @@ uint8_t Endpoint_Read_Stream_LE(void* Buffer, uint16_t Length
                else\r
                {\r
                        *(DataStream++) = Endpoint_Read_Byte();\r
                else\r
                {\r
                        *(DataStream++) = Endpoint_Read_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \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
 }\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
 #endif\r
                                                                 )\r
 {\r
@@ -268,7 +263,7 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
          return ErrorCode;\r
 \r
-       while (Length--)\r
+       while (Length)\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
        {\r
                if (!(Endpoint_IsReadWriteAllowed()))\r
                {\r
@@ -276,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
 \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
                        #endif\r
 \r
                        if ((ErrorCode = Endpoint_WaitUntilReady()))\r
@@ -285,18 +280,21 @@ uint8_t Endpoint_Read_Stream_BE(void* Buffer, uint16_t Length
                else\r
                {\r
                        *(DataStream--) = Endpoint_Read_Byte();\r
                else\r
                {\r
                        *(DataStream--) = Endpoint_Read_Byte();\r
+                       Length--;\r
                }\r
        }\r
        \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
 }\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
+       uint8_t* DataStream     = (uint8_t*)Buffer;\r
        bool     LastPacketFull = false;\r
        bool     LastPacketFull = false;\r
-       bool     ShortTransfer = (Length < USB_ControlRequest.wLength);\r
+       \r
+       if (Length > USB_ControlRequest.wLength)\r
+         Length = USB_ControlRequest.wLength;\r
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
        \r
        while (Length && !(Endpoint_IsOUTReceived()))\r
        {\r
@@ -305,7 +303,6 @@ 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
                while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
                {\r
                        Endpoint_Write_Byte(*(DataStream++));\r
-                       \r
                        Length--;\r
                }\r
                \r
                        Length--;\r
                }\r
                \r
@@ -314,9 +311,9 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
+         return ENDPOINT_RWCSTREAM_HostAborted;\r
        \r
        \r
-       if (LastPacketFull || ShortTransfer)\r
+       if (LastPacketFull)\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
@@ -324,34 +321,36 @@ uint8_t Endpoint_Write_Control_Stream_LE(const void* Buffer, uint16_t Length)
        \r
        while (!(Endpoint_IsOUTReceived()));\r
 \r
        \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     LastPacketFull = false;\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     LastPacketFull = false;\r
-       bool     ShortTransfer  = (Length < USB_ControlRequest.wLength);\r
        \r
        \r
+       if (Length > USB_ControlRequest.wLength)\r
+         Length = USB_ControlRequest.wLength;\r
+\r
        while (Length && !(Endpoint_IsOUTReceived()))\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
                {\r
-                       Endpoint_Write_Byte(*(DataStream--));\r
+                       while (Length && (Endpoint_BytesInEndpoint() < USB_ControlEndpointSize))\r
+                       {\r
+                               Endpoint_Write_Byte(*(DataStream--));\r
+                               Length--;\r
+                       }\r
                        \r
                        \r
-                       Length--;\r
+                       LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
+                       Endpoint_ClearIN();\r
                }\r
                }\r
-               \r
-               LastPacketFull = (Endpoint_BytesInEndpoint() == USB_ControlEndpointSize);\r
-               Endpoint_ClearIN();\r
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
        }\r
        \r
        if (Endpoint_IsOUTReceived())\r
-         return ENDPOINT_RWCSTREAM_ERROR_HostAborted;\r
+         return ENDPOINT_RWCSTREAM_HostAborted;\r
        \r
        \r
-       if (LastPacketFull || ShortTransfer)\r
+       if (LastPacketFull)\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
        {\r
                while (!(Endpoint_IsINReady()));\r
                Endpoint_ClearIN();\r
@@ -359,7 +358,7 @@ uint8_t Endpoint_Write_Control_Stream_BE(const void* Buffer, uint16_t Length)
        \r
        while (!(Endpoint_IsOUTReceived()));\r
 \r
        \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
 }\r
 \r
 uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)\r
@@ -368,21 +367,21 @@ uint8_t Endpoint_Read_Control_Stream_LE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
+               if (Endpoint_IsOUTReceived())\r
                {\r
                {\r
-                       *(DataStream++) = Endpoint_Read_Byte();\r
+                       while (Length && Endpoint_BytesInEndpoint())\r
+                       {\r
+                               *(DataStream++) = Endpoint_Read_Byte();\r
+                               Length--;\r
+                       }\r
                        \r
                        \r
-                       Length--;\r
+                       Endpoint_ClearOUT();\r
                }\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
        \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
 }\r
 \r
 uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)\r
@@ -391,21 +390,21 @@ uint8_t Endpoint_Read_Control_Stream_BE(void* Buffer, uint16_t Length)
        \r
        while (Length)\r
        {\r
        \r
        while (Length)\r
        {\r
-               while (!(Endpoint_IsOUTReceived()));\r
-               \r
-               while (Length && Endpoint_BytesInEndpoint())\r
+               if (Endpoint_IsOUTReceived())\r
                {\r
                {\r
-                       *(DataStream--) = Endpoint_Read_Byte();\r
+                       while (Length && Endpoint_BytesInEndpoint())\r
+                       {\r
+                               *(DataStream--) = Endpoint_Read_Byte();\r
+                               Length--;\r
+                       }\r
                        \r
                        \r
-                       Length--;\r
+                       Endpoint_ClearOUT();\r
                }\r
                }\r
-               \r
-               Endpoint_ClearOUT();\r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
 \r
        }\r
        \r
        while (!(Endpoint_IsINReady()));\r
 \r
-       return ENDPOINT_RWCSTREAM_ERROR_NoError;\r
+       return ENDPOINT_RWCSTREAM_NoError;\r
 }\r
 \r
 #endif\r
 }\r
 \r
 #endif\r