Skip to content

Commit 82d932f

Browse files
committed
Check if the last packet we tried to transfer is taking too long
1 parent 5e0da73 commit 82d932f

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

Diff for: cores/arduino/USBSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ size_t USBSerial::write(const uint8_t *buffer, size_t size)
8484
// After storing data, start transmitting process
8585
CDC_continue_transmit();
8686
} else if (!CDC_connected()) {
87-
return size - rest;
87+
break;
8888
}
8989
}
90-
return size;
90+
return size - rest;
9191
}
9292

9393
int USBSerial::available(void)

Diff for: cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@
3232
#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE
3333
#endif
3434

35+
/*
36+
* The value USB_CDC_TRANSMIT_TIMEOUT is defined in terms of HAL_GetTick() units.
37+
* Typically it is 1ms value. The timeout determines when we would consider the
38+
* host "too slow" and threat the USB CDC port as disconnected.
39+
*/
40+
#ifndef
41+
#define USB_CDC_TRANSMIT_TIMEOUT 3
42+
#endif
43+
3544
/* USBD_CDC Private Variables */
3645
/* USB Device Core CDC handle declaration */
3746
USBD_HandleTypeDef hUSBD_Device_CDC;
@@ -43,6 +52,7 @@ CDC_TransmitQueue_TypeDef TransmitQueue;
4352
CDC_ReceiveQueue_TypeDef ReceiveQueue;
4453
__IO uint32_t lineState = 0;
4554
__IO bool receivePended = true;
55+
static uint32_t transmitStart = 0;
4656

4757

4858
/** USBD_CDC Private Function Prototypes */
@@ -212,6 +222,7 @@ static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len)
212222

213223
static int8_t USBD_CDC_Transferred(void)
214224
{
225+
transmitStart = 0;
215226
CDC_TransmitQueue_CommitRead(&TransmitQueue);
216227
CDC_continue_transmit();
217228
return (USBD_OK);
@@ -247,7 +258,13 @@ void CDC_deInit(void)
247258

248259
bool CDC_connected()
249260
{
250-
return hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED && lineState;
261+
uint32_t transmitTime = 0;
262+
if (transmitStart) {
263+
transmitTime = HAL_GetTick() - transmitStart;
264+
}
265+
return hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED
266+
&& transmitTime < USB_CDC_TRANSMIT_TIMEOUT
267+
&& lineState;
251268
}
252269

253270
void CDC_continue_transmit(void)
@@ -266,6 +283,7 @@ void CDC_continue_transmit(void)
266283
if (hcdc->TxState == 0U) {
267284
buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size);
268285
if (size > 0) {
286+
transmitStart = HAL_GetTick();
269287
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, buffer, size);
270288
/*
271289
* size never exceed PMA buffer and USBD_CDC_TransmitPacket make full

0 commit comments

Comments
 (0)