- /* Check if the Tx buffer contains anything to be sent to the host */\r
- if (Tx_Buffer.Elements)\r
+ /* Check if the Tx buffer contains anything to be sent to the host */\r
+ if (Tx_Buffer.Elements)\r
+ {\r
+ /* Wait until Serial Tx Endpoint Ready for Read/Write */\r
+ while (!(Endpoint_IsReadWriteAllowed()))\r
+ {\r
+ if (USB_DeviceState == DEVICE_STATE_Unattached)\r
+ return;\r
+ }\r
+ \r
+ /* Write the bytes from the buffer to the endpoint while space is available */\r
+ while (Tx_Buffer.Elements && Endpoint_IsReadWriteAllowed())\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
+ /* 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