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 USB_CDC_TRANSMIT_TIMEOUT
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 */
@@ -169,6 +179,9 @@ static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length)
169
179
case CDC_SET_CONTROL_LINE_STATE :
170
180
lineState =
171
181
(((USBD_SetupReqTypedef * )pbuf )-> wValue & 0x01 ) != 0 ; // Check DTR state
182
+ if (lineState ) { // Reset the transmit timeout when the port is connected
183
+ transmitStart = 0 ;
184
+ }
172
185
break ;
173
186
174
187
case CDC_SEND_BREAK :
@@ -214,6 +227,7 @@ static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len)
214
227
215
228
static int8_t USBD_CDC_Transferred (void )
216
229
{
230
+ transmitStart = 0 ;
217
231
CDC_TransmitQueue_CommitRead (& TransmitQueue );
218
232
CDC_continue_transmit ();
219
233
return (USBD_OK );
@@ -247,6 +261,17 @@ void CDC_deInit(void)
247
261
}
248
262
}
249
263
264
+ bool CDC_connected ()
265
+ {
266
+ uint32_t transmitTime = 0 ;
267
+ if (transmitStart ) {
268
+ transmitTime = HAL_GetTick () - transmitStart ;
269
+ }
270
+ return hUSBD_Device_CDC .dev_state == USBD_STATE_CONFIGURED
271
+ && transmitTime < USB_CDC_TRANSMIT_TIMEOUT
272
+ && lineState ;
273
+ }
274
+
250
275
void CDC_continue_transmit (void )
251
276
{
252
277
uint16_t size ;
@@ -263,6 +288,7 @@ void CDC_continue_transmit(void)
263
288
if (hcdc -> TxState == 0U ) {
264
289
buffer = CDC_TransmitQueue_ReadBlock (& TransmitQueue , & size );
265
290
if (size > 0 ) {
291
+ transmitStart = HAL_GetTick ();
266
292
USBD_CDC_SetTxBuffer (& hUSBD_Device_CDC , buffer , size );
267
293
/*
268
294
* size never exceed PMA buffer and USBD_CDC_TransmitPacket make full
0 commit comments