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 USB_CDC_TRANSMIT_TIMEOUT
41+ #define USB_CDC_TRANSMIT_TIMEOUT 3
42+ #endif
43+
3544/* USBD_CDC Private Variables */
3645/* USB Device Core CDC handle declaration */
3746USBD_HandleTypeDef hUSBD_Device_CDC ;
@@ -43,6 +52,7 @@ CDC_TransmitQueue_TypeDef TransmitQueue;
4352CDC_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
213223static 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
248259bool 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
253270void 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