From cce7119a2b8f3cf61bd25105c8d8939a504dd5fa Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Mon, 14 Apr 2014 01:46:25 +0100
Subject: [PATCH] stmhal: Work around crazy bug in USB CDC.

Packets of 64 bytes length are not send to the host until the following
packet is sent.  Fixed by never sending packets of 64 bytes length.
---
 stmhal/usbd_cdc_interface.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/stmhal/usbd_cdc_interface.c b/stmhal/usbd_cdc_interface.c
index 81f541df43..c6b6cf23f6 100644
--- a/stmhal/usbd_cdc_interface.c
+++ b/stmhal/usbd_cdc_interface.c
@@ -60,7 +60,7 @@ static uint16_t UserRxBufLen = 0; // counts number of valid characters in UserRx
 
 static uint8_t UserTxBuffer[APP_TX_DATA_SIZE]; // data for USB IN endpoind is stored in this buffer
 static uint16_t UserTxBufPtrIn = 0; // increment this pointer modulo APP_TX_DATA_SIZE when new data is available
-static uint16_t UserTxBufPtrOut = 0; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained
+static __IO uint16_t UserTxBufPtrOut = 0; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained
 
 static int user_interrupt_char = VCP_CHAR_NONE;
 static void *user_interrupt_data = NULL;
@@ -265,6 +265,14 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
     }
     
     buffptr = UserTxBufPtrOut;
+
+    // dpgeorge: For some reason that I don't understand, a packet size of 64 bytes
+    // (CDC_DATA_FS_MAX_PACKET_SIZE) does not get through to the USB host until the
+    // next packet is sent.  To work around this, we just make sure that we never
+    // send a packet 64 bytes in length.
+    if (buffsize == CDC_DATA_FS_MAX_PACKET_SIZE) {
+        buffsize -= 1;
+    }
     
     USBD_CDC_SetTxBuffer(&hUSBDDevice, (uint8_t*)&UserTxBuffer[buffptr], buffsize);