Fixed CDC and USBtoSerial demos freezing where buffers were full while still transmit...
[pub/USBasp.git] / Demos / Device / CDC / CDC.c
index 3dc6240..29bc003 100644 (file)
@@ -300,14 +300,22 @@ TASK(CDC_Task)
                /* Write the String to the Endpoint */\r
                Endpoint_Write_Stream_LE(ReportString, strlen(ReportString));\r
                \r
+               /* Remember if the packet to send completely fills the endpoint */\r
+               bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);\r
+\r
                /* Finalize the stream transfer to send the last packet */\r
                Endpoint_ClearIN();\r
 \r
-               /* Wait until the endpoint is ready for another packet */\r
-               while (!(Endpoint_IsINReady()));\r
-               \r
-               /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
-               Endpoint_ClearIN();\r
+               /* If the last packet filled the endpoint, send an empty packet to release the buffer on \r
+                * the receiver (otherwise all data will be cached until a non-full packet is received) */\r
+               if (IsFull)\r
+               {\r
+                       /* Wait until the endpoint is ready for another packet */\r
+                       while (!(Endpoint_IsINReady()));\r
+                       \r
+                       /* Send an empty packet to ensure that the host does not buffer data sent to it */\r
+                       Endpoint_ClearIN();\r
+               }\r
        }\r
 \r
        /* Select the Serial Rx Endpoint */\r