Fixed CDC and USBtoSerial demos freezing where buffers were full while still transmit...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 17 Apr 2009 04:10:30 +0000 (04:10 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 17 Apr 2009 04:10:30 +0000 (04:10 +0000)
Demos/Device/CDC/CDC.c
Demos/Device/USBtoSerial/USBtoSerial.c
LUFA/ChangeLog.txt

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
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
index 115a04a..e504c39 100644 (file)
@@ -48,6 +48,7 @@
   *  - Removed old endpoint and pipe aliased read/write/discard routines which did not have an explicit endian specifier for clarity\r
   *  - Removed the ButtLoadTag.h header file, as no one used for its intended purpose anyway\r
   *  - Renamed the main Drivers/AT90USBXXX directory to Drivers/Peripheral, renamed the Serial_Stream driver to SerialStream\r
+  *  - Fixed CDC and USBtoSerial demos freezing where buffers were full while still transmitting or receiving (thanks to Peter Hand)\r
   *    \r
   *\r
   *  \section Sec_ChangeLog090401 Version 090401\r