diff --git a/libraries/USBDevice/inc/usbd_cdc.h b/libraries/USBDevice/inc/usbd_cdc.h index 3c32c7c9c2..8db2fbc648 100644 --- a/libraries/USBDevice/inc/usbd_cdc.h +++ b/libraries/USBDevice/inc/usbd_cdc.h @@ -148,12 +148,10 @@ uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint32_t length, uint8_t ClassId); uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, uint8_t ClassId); -uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev, uint8_t ClassId); #else uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, uint32_t length); uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev); -uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev); #endif /* USE_USBD_COMPOSITE */ uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff); uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev); diff --git a/libraries/USBDevice/src/cdc/usbd_cdc.c b/libraries/USBDevice/src/cdc/usbd_cdc.c index d2e8cf7dfb..f3f7200a75 100644 --- a/libraries/USBDevice/src/cdc/usbd_cdc.c +++ b/libraries/USBDevice/src/cdc/usbd_cdc.c @@ -1019,24 +1019,6 @@ uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) return (uint8_t)USBD_OK; } -#ifdef USE_USBD_COMPOSITE -uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev, uint8_t ClassId) -{ - /* Suspend or Resume USB Out process */ - if (pdev->pClassDataCmsit[classId] != NULL) { -#else -uint8_t USBD_CDC_ClearBuffer(USBD_HandleTypeDef *pdev) -{ - /* Suspend or Resume USB Out process */ - if (pdev->pClassDataCmsit[pdev->classId] != NULL) { -#endif /* USE_USBD_COMPOSITE */ - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, 0, 0); - return (uint8_t)USBD_OK; - } else { - return (uint8_t)USBD_FAIL; - } -} #endif /* USBD_USE_CDC */ #endif /* USBCON */ diff --git a/libraries/USBDevice/src/cdc/usbd_cdc_if.c b/libraries/USBDevice/src/cdc/usbd_cdc_if.c index 922b7d57e3..2fe9614fb0 100644 --- a/libraries/USBDevice/src/cdc/usbd_cdc_if.c +++ b/libraries/USBDevice/src/cdc/usbd_cdc_if.c @@ -240,10 +240,15 @@ static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len) /* It always contains required amount of free space for writing */ CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len)); receivePended = false; - /* If enough space in the queue for a full buffer then continue receive */ - if (!CDC_resume_receive()) { - USBD_CDC_ClearBuffer(&hUSBD_Device_CDC); - } + /* + * If there is enough space in the queue for a full packet, continue receive. + * Else leave the OUT endpoint unarmed: it then NAKs and the host retries. + * USBSerial::read() and the readBytes() family call CDC_resume_receive() + * after dequeuing, so the endpoint is armed again with a valid block as soon + * as the sketch drains data. Arming it with a NULL buffer instead makes the + * low level driver copy the retried packet to address 0. + */ + (void)CDC_resume_receive(); return ((int8_t)USBD_OK); }