Fixed CDC and USBtoSerial demos freezing where buffers were full while still transmit...
[pub/USBasp.git] / Demos / Device / USBtoSerial / USBtoSerial.c
index 87d531c..1d238aa 100644 (file)
@@ -246,18 +246,19 @@ TASK(CDC_Task)
                \r
                if (Endpoint_IsOUTReceived())\r
                {\r
-                       /* Read the received data endpoint into the transmission buffer */\r
-                       while (Endpoint_BytesInEndpoint())\r
+                       /* Read the bytes in from the endpoint into the buffer while space is available */\r
+                       while (Endpoint_BytesInEndpoint() && (BUFF_STATICSIZE - Rx_Buffer.Elements))\r
                        {\r
-                               /* Wait until the buffer has space for a new character */\r
-                               while (!((BUFF_STATICSIZE - Rx_Buffer.Elements)));\r
-                       \r
                                /* Store each character from the endpoint */\r
                                Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte());\r
                        }\r
                        \r
-                       /* Clear the endpoint buffer */\r
-                       Endpoint_ClearOUT();\r
+                       /* Check to see if all bytes in the current packet have been read */\r
+                       if (!(Endpoint_BytesInEndpoint()))\r
+                       {\r
+                               /* Clear the endpoint buffer */\r
+                               Endpoint_ClearOUT();\r
+                       }\r
                }\r
                \r
                /* Check if Rx buffer contains data */\r
@@ -280,18 +281,29 @@ TASK(CDC_Task)
                        /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
                        while (!(Endpoint_IsReadWriteAllowed()));\r
                        \r
-                       /* Write the transmission buffer contents to the received data endpoint */\r
+                       /* Write the bytes from the buffer to the endpoint while space is available */\r
                        while (Tx_Buffer.Elements && (Endpoint_BytesInEndpoint() < CDC_TXRX_EPSIZE))\r
-                         Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer));\r
+                       {\r
+                               /* Write each byte retreived from the buffer to the endpoint */\r
+                               Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer));\r
+                       }\r
+                       \r
+                       /* Remember if the packet to send completely fills the endpoint */\r
+                       bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);\r
                        \r
                        /* Send the data */\r
                        Endpoint_ClearIN();\r
 \r
-                       /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
-                       while (!(Endpoint_IsReadWriteAllowed()));\r
+                       /* If no more data to send and the last packet filled the endpoint, send an empty packet to release\r
+                        * the buffer on the receiver (otherwise all data will be cached until a non-full packet is received) */\r
+                       if (IsFull && !(Tx_Buffer.Elements))\r
+                       {\r
+                               /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
+                               while (!(Endpoint_IsReadWriteAllowed()));\r
 \r
-                       /* Send an empty packet to terminate the transfer */\r
-                       Endpoint_ClearIN();\r
+                               /* Send an empty packet to terminate the transfer */\r
+                               Endpoint_ClearIN();\r
+                       }\r
                }\r
        }\r
 }\r