From 592d32e681a244049a99f26883e592443ce5cba9 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 4 Jul 2016 15:23:30 -0400 Subject: [PATCH] Add USB send timeout --- cores/arduino/USB/USBCore.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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;