32
32
#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE
33
33
#endif
34
34
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
+
35
44
/* USBD_CDC Private Variables */
36
45
/* USB Device Core CDC handle declaration */
37
46
USBD_HandleTypeDef hUSBD_Device_CDC ;
@@ -43,6 +52,7 @@ CDC_TransmitQueue_TypeDef TransmitQueue;
43
52
CDC_ReceiveQueue_TypeDef ReceiveQueue ;
44
53
__IO uint32_t lineState = 0 ;
45
54
__IO bool receivePended = true;
55
+ static uint32_t transmitStart = 0 ;
46
56
47
57
48
58
/** USBD_CDC Private Function Prototypes */
@@ -212,6 +222,7 @@ static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len)
212
222
213
223
static int8_t USBD_CDC_Transferred (void )
214
224
{
225
+ transmitStart = 0 ;
215
226
CDC_TransmitQueue_CommitRead (& TransmitQueue );
216
227
CDC_continue_transmit ();
217
228
return (USBD_OK );
@@ -247,7 +258,13 @@ void CDC_deInit(void)
247
258
248
259
bool CDC_connected ()
249
260
{
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 ;
251
268
}
252
269
253
270
void CDC_continue_transmit (void )
@@ -266,6 +283,7 @@ void CDC_continue_transmit(void)
266
283
if (hcdc -> TxState == 0U ) {
267
284
buffer = CDC_TransmitQueue_ReadBlock (& TransmitQueue , & size );
268
285
if (size > 0 ) {
286
+ transmitStart = HAL_GetTick ();
269
287
USBD_CDC_SetTxBuffer (& hUSBD_Device_CDC , buffer , size );
270
288
/*
271
289
* size never exceed PMA buffer and USBD_CDC_TransmitPacket make full
0 commit comments