Skip to content

Commit

Permalink
Check if the last packet we tried to transfer is taking too long
Browse files Browse the repository at this point in the history
  • Loading branch information
ghent360 committed Mar 9, 2019
1 parent 5e0da73 commit 82d932f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cores/arduino/USBSerial.cpp
Expand Up @@ -84,10 +84,10 @@ size_t USBSerial::write(const uint8_t *buffer, size_t size)
// After storing data, start transmitting process
CDC_continue_transmit();
} else if (!CDC_connected()) {
return size - rest;
break;
}
}
return size;
return size - rest;
}

int USBSerial::available(void)
Expand Down
20 changes: 19 additions & 1 deletion cores/arduino/stm32/usb/cdc/usbd_cdc_if.c
Expand Up @@ -32,6 +32,15 @@
#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE
#endif

/*
* The value USB_CDC_TRANSMIT_TIMEOUT is defined in terms of HAL_GetTick() units.
* Typically it is 1ms value. The timeout determines when we would consider the
* host "too slow" and threat the USB CDC port as disconnected.
*/
#ifndef
#define USB_CDC_TRANSMIT_TIMEOUT 3
#endif

/* USBD_CDC Private Variables */
/* USB Device Core CDC handle declaration */
USBD_HandleTypeDef hUSBD_Device_CDC;
Expand All @@ -43,6 +52,7 @@ CDC_TransmitQueue_TypeDef TransmitQueue;
CDC_ReceiveQueue_TypeDef ReceiveQueue;
__IO uint32_t lineState = 0;
__IO bool receivePended = true;
static uint32_t transmitStart = 0;


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

static int8_t USBD_CDC_Transferred(void)
{
transmitStart = 0;
CDC_TransmitQueue_CommitRead(&TransmitQueue);
CDC_continue_transmit();
return (USBD_OK);
Expand Down Expand Up @@ -247,7 +258,13 @@ void CDC_deInit(void)

bool CDC_connected()
{
return hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED && lineState;
uint32_t transmitTime = 0;
if (transmitStart) {
transmitTime = HAL_GetTick() - transmitStart;
}
return hUSBD_Device_CDC.dev_state == USBD_STATE_CONFIGURED
&& transmitTime < USB_CDC_TRANSMIT_TIMEOUT
&& lineState;
}

void CDC_continue_transmit(void)
Expand All @@ -266,6 +283,7 @@ void CDC_continue_transmit(void)
if (hcdc->TxState == 0U) {
buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size);
if (size > 0) {
transmitStart = HAL_GetTick();
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, buffer, size);
/*
* size never exceed PMA buffer and USBD_CDC_TransmitPacket make full
Expand Down

0 comments on commit 82d932f

Please sign in to comment.