diff --git a/cores/arduino/USB/USBCore.cpp b/cores/arduino/USB/USBCore.cpp index 8d83b9df6..40f0f45d0 100644 --- a/cores/arduino/USB/USBCore.cpp +++ b/cores/arduino/USB/USBCore.cpp @@ -602,6 +602,9 @@ uint8_t USBDeviceClass::armRecv(uint32_t ep) return usbd.epBank0ByteCount(ep); } +// Timeout for sends +#define TX_TIMEOUT_MS 70 + // Blocking Send of data to an endpoint uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len) { @@ -672,9 +675,15 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len) // RAM buffer is full, we can send data (IN) usbd.epBank1SetReady(ep); + // convert the timeout from microseconds to a number of times through + // the wait loop; it takes (roughly) 16 clock cycles per iteration. + uint32_t timeout = microsecondsToClockCycles(TX_TIMEOUT_MS * 1000) / 16; + // Wait for transfer to complete while (!usbd.epBank1IsTransferComplete(ep)) { - ; // need fire exit. + if (timeout-- == 0) { + return -1; + } } written += length; len -= length;