Skip to content

Commit efc22e3

Browse files
committed
stmhal: Fix 64-byte USB packet bug properly.
A 64-byte packet is now followed by a 0-byte packet if there is nothing more to send. This flushes the USB endpoint.
1 parent 2881772 commit efc22e3

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

‎stmhal/usbd_cdc_interface.c‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static uint16_t UserTxBufPtrIn = 0; // increment this pointer modulo APP_TX_DATA
6363
static __IO uint16_t UserTxBufPtrOut = 0; // increment this pointer modulo APP_TX_DATA_SIZE when data is drained
6464
static uint16_t UserTxBufPtrOutShadow = 0; // shadow of above
6565
static uint8_t UserTxBufPtrWaitCount = 0; // used to implement a timeout waiting for low-level USB driver
66+
static uint8_t UserTxNeedEmptyPacket = 0; // used to flush the USB IN endpoint if the last packet was exactly the endpoint packet size
6667

6768
static int user_interrupt_char = VCP_CHAR_NONE;
6869
static void *user_interrupt_data = NULL;
@@ -257,7 +258,7 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
257258
return;
258259
}
259260

260-
if (UserTxBufPtrOut == UserTxBufPtrIn) {
261+
if (UserTxBufPtrOut == UserTxBufPtrIn && !UserTxNeedEmptyPacket) {
261262
// No outstanding data to send
262263
return;
263264
}
@@ -278,7 +279,7 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
278279
UserTxBufPtrOut = UserTxBufPtrOutShadow;
279280
}
280281

281-
if (UserTxBufPtrOutShadow != UserTxBufPtrIn) {
282+
if (UserTxBufPtrOutShadow != UserTxBufPtrIn || UserTxNeedEmptyPacket) {
282283
uint32_t buffptr;
283284
uint32_t buffsize;
284285

@@ -290,14 +291,6 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
290291

291292
buffptr = UserTxBufPtrOutShadow;
292293

293-
// dpgeorge: For some reason that I don't understand, a packet size of 64 bytes
294-
// (CDC_DATA_FS_MAX_PACKET_SIZE) does not get through to the USB host until the
295-
// next packet is sent. To work around this, we just make sure that we never
296-
// send a packet 64 bytes in length.
297-
if (buffsize == CDC_DATA_FS_MAX_PACKET_SIZE) {
298-
buffsize -= 1;
299-
}
300-
301294
USBD_CDC_SetTxBuffer(&hUSBDDevice, (uint8_t*)&UserTxBuffer[buffptr], buffsize);
302295

303296
if (USBD_CDC_TransmitPacket(&hUSBDDevice) == USBD_OK) {
@@ -306,6 +299,14 @@ void USBD_CDC_HAL_TIM_PeriodElapsedCallback(void) {
306299
UserTxBufPtrOutShadow = 0;
307300
}
308301
UserTxBufPtrWaitCount = 0;
302+
303+
// According to the USB specification, a packet size of 64 bytes (CDC_DATA_FS_MAX_PACKET_SIZE)
304+
// gets held at the USB host until the next packet is sent. This is because a
305+
// packet of maximum size is considered to be part of a longer chunk of data, and
306+
// the host waits for all data to arrive (ie, waits for a packet < max packet size).
307+
// To flush a packet of exactly max packet size, we need to send a zero-size packet.
308+
// See eg http://www.cypress.com/?id=4&rID=92719
309+
UserTxNeedEmptyPacket = (buffsize == CDC_DATA_FS_MAX_PACKET_SIZE && UserTxBufPtrOutShadow == UserTxBufPtrIn);
309310
}
310311
}
311312
}

0 commit comments

Comments
 (0)