Skip to content

Commit d7a5d8c

Browse files
hasenbanckfpistm
authored andcommitted
Add fixes for CDC library
Added fixes as advised by @ktand * If the USB packet to be sent is equal to the USB buffer size (64 bytes), a Zero Length Packet must be sent. Otherwise the USB CDC connection will fail. * Add check for linestate in CDC_Flush and in CDC_TIM_PeriodElapsedCallback to ignore transmission if the device is disconnected.
1 parent daade74 commit d7a5d8c

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+28-21
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ __IO uint32_t GetRxCount;
5151
__IO uint32_t lineState = 0;
5252
uint8_t cptlineState = 0;
5353
volatile uint32_t USB_received = 0;
54-
uint8_t USBBuffer[64];
54+
uint8_t USBBuffer[USB_MAX_EP0_SIZE];
5555
uint8_t USBPackSize;
56+
bool sendZLP = false;
5657

5758
stimer_t CDC_TimHandle;
5859

@@ -293,6 +294,7 @@ void CDC_TIM_PeriodElapsedCallback(stimer_t *htim)
293294
{
294295
UNUSED(htim);
295296
uint8_t status;
297+
uint16_t transferSize;
296298

297299
if(USB_received) {
298300
USB_received = 0;
@@ -311,33 +313,38 @@ void CDC_TIM_PeriodElapsedCallback(stimer_t *htim)
311313
USBD_CDC_ReceivePacket(&hUSBD_Device_CDC);
312314
}
313315

314-
if(UserTxBufPtrOut != UserTxBufPtrIn) {
315-
if(UserTxBufPtrOut > UserTxBufPtrIn) { /* Roll-back */
316-
memcpy((uint8_t*)&StackTxBuffer[0], (uint8_t*)&UserTxBuffer[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
316+
if(UserTxBufPtrOut > UserTxBufPtrIn) { /* Roll-back */
317+
memcpy((uint8_t*)&StackTxBuffer[0], (uint8_t*)&UserTxBuffer[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
318+
memcpy((uint8_t*)&StackTxBuffer[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBuffer[0], UserTxBufPtrIn);
317319

318-
memcpy((uint8_t*)&StackTxBuffer[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBuffer[0], UserTxBufPtrIn);
320+
transferSize = (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn);
319321

320-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBuffer[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
322+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBuffer[0], transferSize);
323+
}
324+
else if (UserTxBufPtrOut != UserTxBufPtrIn) {
325+
transferSize = (UserTxBufPtrIn - UserTxBufPtrOut);
321326

322-
do {
323-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
324-
} while(status == USBD_BUSY);
327+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBuffer[UserTxBufPtrOut], transferSize);
328+
}
329+
else if (sendZLP) {
330+
transferSize = 0;
325331

326-
if(status == USBD_OK) {
327-
UserTxBufPtrOut = UserTxBufPtrIn;
328-
}
329-
}
330-
else {
331-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBuffer[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
332+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, NULL, 0);
333+
} else {
334+
return;
335+
}
332336

333-
do {
337+
do {
338+
if (lineState == 0) // Device disconnected
339+
status = USBD_OK;
340+
else
334341
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
335-
} while(status == USBD_BUSY);
342+
} while(status == USBD_BUSY);
336343

337-
if(status == USBD_OK) {
338-
UserTxBufPtrOut = UserTxBufPtrIn;
339-
}
340-
}
344+
if(status == USBD_OK) {
345+
UserTxBufPtrOut = UserTxBufPtrIn;
346+
347+
sendZLP = transferSize%USB_MAX_EP0_SIZE == 0;
341348
}
342349
}
343350

0 commit comments

Comments
 (0)