diff options
author | Dean Camera <dean@fourwalledcubicle.com> | 2009-04-17 04:10:30 +0000 |
---|---|---|
committer | Dean Camera <dean@fourwalledcubicle.com> | 2009-04-17 04:10:30 +0000 |
commit | 6380d057f8911f5d09bdffff4220aa9602df49e2 (patch) | |
tree | 12456118c9c91c9521abfe3475d9d025d56aa38b /Demos | |
parent | 11bb2f21720c2af4b29732ca128963869e5c512c (diff) | |
download | lufa-6380d057f8911f5d09bdffff4220aa9602df49e2.tar.gz lufa-6380d057f8911f5d09bdffff4220aa9602df49e2.tar.bz2 lufa-6380d057f8911f5d09bdffff4220aa9602df49e2.zip |
Fixed CDC and USBtoSerial demos freezing where buffers were full while still transmitting or receiving (thanks to Peter Hand).
Diffstat (limited to 'Demos')
-rw-r--r-- | Demos/Device/CDC/CDC.c | 18 | ||||
-rw-r--r-- | Demos/Device/USBtoSerial/USBtoSerial.c | 38 |
2 files changed, 38 insertions, 18 deletions
diff --git a/Demos/Device/CDC/CDC.c b/Demos/Device/CDC/CDC.c index 3dc624057..29bc00373 100644 --- a/Demos/Device/CDC/CDC.c +++ b/Demos/Device/CDC/CDC.c @@ -300,14 +300,22 @@ TASK(CDC_Task) /* Write the String to the Endpoint */
Endpoint_Write_Stream_LE(ReportString, strlen(ReportString));
+ /* Remember if the packet to send completely fills the endpoint */
+ bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
+
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
- /* Wait until the endpoint is ready for another packet */
- while (!(Endpoint_IsINReady()));
-
- /* Send an empty packet to ensure that the host does not buffer data sent to it */
- Endpoint_ClearIN();
+ /* If the last packet filled the endpoint, send an empty packet to release the buffer on
+ * the receiver (otherwise all data will be cached until a non-full packet is received) */
+ if (IsFull)
+ {
+ /* Wait until the endpoint is ready for another packet */
+ while (!(Endpoint_IsINReady()));
+
+ /* Send an empty packet to ensure that the host does not buffer data sent to it */
+ Endpoint_ClearIN();
+ }
}
/* Select the Serial Rx Endpoint */
diff --git a/Demos/Device/USBtoSerial/USBtoSerial.c b/Demos/Device/USBtoSerial/USBtoSerial.c index 87d531c4d..1d238aa11 100644 --- a/Demos/Device/USBtoSerial/USBtoSerial.c +++ b/Demos/Device/USBtoSerial/USBtoSerial.c @@ -246,18 +246,19 @@ TASK(CDC_Task) if (Endpoint_IsOUTReceived())
{
- /* Read the received data endpoint into the transmission buffer */
- while (Endpoint_BytesInEndpoint())
+ /* Read the bytes in from the endpoint into the buffer while space is available */
+ while (Endpoint_BytesInEndpoint() && (BUFF_STATICSIZE - Rx_Buffer.Elements))
{
- /* Wait until the buffer has space for a new character */
- while (!((BUFF_STATICSIZE - Rx_Buffer.Elements)));
-
/* Store each character from the endpoint */
Buffer_StoreElement(&Rx_Buffer, Endpoint_Read_Byte());
}
- /* Clear the endpoint buffer */
- Endpoint_ClearOUT();
+ /* Check to see if all bytes in the current packet have been read */
+ if (!(Endpoint_BytesInEndpoint()))
+ {
+ /* Clear the endpoint buffer */
+ Endpoint_ClearOUT();
+ }
}
/* Check if Rx buffer contains data */
@@ -280,18 +281,29 @@ TASK(CDC_Task) /* Wait until Serial Tx Endpoint Ready for Read/Write */
while (!(Endpoint_IsReadWriteAllowed()));
- /* Write the transmission buffer contents to the received data endpoint */
+ /* Write the bytes from the buffer to the endpoint while space is available */
while (Tx_Buffer.Elements && (Endpoint_BytesInEndpoint() < CDC_TXRX_EPSIZE))
- Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer));
+ {
+ /* Write each byte retreived from the buffer to the endpoint */
+ Endpoint_Write_Byte(Buffer_GetElement(&Tx_Buffer));
+ }
+
+ /* Remember if the packet to send completely fills the endpoint */
+ bool IsFull = (Endpoint_BytesInEndpoint() == CDC_TXRX_EPSIZE);
/* Send the data */
Endpoint_ClearIN();
- /* Wait until Serial Tx Endpoint Ready for Read/Write */
- while (!(Endpoint_IsReadWriteAllowed()));
+ /* If no more data to send and the last packet filled the endpoint, send an empty packet to release
+ * the buffer on the receiver (otherwise all data will be cached until a non-full packet is received) */
+ if (IsFull && !(Tx_Buffer.Elements))
+ {
+ /* Wait until Serial Tx Endpoint Ready for Read/Write */
+ while (!(Endpoint_IsReadWriteAllowed()));
- /* Send an empty packet to terminate the transfer */
- Endpoint_ClearIN();
+ /* Send an empty packet to terminate the transfer */
+ Endpoint_ClearIN();
+ }
}
}
}
|