Skip to content

Add USB send timeout (take 2) #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 6 additions & 20 deletions cores/arduino/USB/CDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,28 +205,14 @@ void Serial_::flush(void)

size_t Serial_::write(const uint8_t *buffer, size_t size)
{
/* only try to send bytes if the high-level CDC connection itself
is open (not just the pipe) - the OS should set lineState when the port
is opened and clear lineState when the port is closed.
bytes sent before the user opens the connection or after
the connection is closed are lost - just like with a UART. */

// TODO - ZE - check behavior on different OSes and test what happens if an
// open connection isn't broken cleanly (cable is yanked out, host dies
// or locks up, or host virtual serial port hangs)
if (_usbLineInfo.lineState > 0) // Problem with Windows(R)
{
uint32_t r = usb.send(CDC_ENDPOINT_IN, buffer, size);
uint32_t r = usb.send(CDC_ENDPOINT_IN, buffer, size);

if (r > 0) {
return r;
} else {
setWriteError();
return 0;
}
if (r > 0) {
return r;
} else {
setWriteError();
return 0;
}
setWriteError();
return 0;
}

size_t Serial_::write(uint8_t c) {
Expand Down
40 changes: 36 additions & 4 deletions cores/arduino/USB/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,19 @@ uint8_t USBDeviceClass::armRecv(uint32_t ep)
return usbd.epBank0ByteCount(ep);
}

// Timeout for sends
#define TX_TIMEOUT_MS 70

static char LastTransmitTimedOut[7] = {
0,
0,
0,
0,
0,
0,
0
};

// Blocking Send of data to an endpoint
uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
{
Expand All @@ -619,6 +632,29 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
// Flash area
while (len != 0)
{
if (usbd.epBank1IsReady(ep)) {
// previous transfer is still not complete

// convert the timeout from microseconds to a number of times through
// the wait loop; it takes (roughly) 23 clock cycles per iteration.
uint32_t timeout = microsecondsToClockCycles(TX_TIMEOUT_MS * 1000) / 23;

// Wait for (previous) transfer to complete
// inspired by Paul Stoffregen's work on Teensy
while (!usbd.epBank1IsTransferComplete(ep)) {
if (LastTransmitTimedOut[ep] || timeout-- == 0) {
LastTransmitTimedOut[ep] = 1;

// set byte count to zero, so that ZLP is sent
// instead of stale data
usbd.epBank1SetByteCount(ep, 0);
return -1;
}
}
}

LastTransmitTimedOut[ep] = 0;

if (len >= EPX_SIZE) {
length = EPX_SIZE - 1;
} else {
Expand All @@ -637,10 +673,6 @@ 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);

// Wait for transfer to complete
while (!usbd.epBank1IsTransferComplete(ep)) {
; // need fire exit.
}
written += length;
len -= length;
data = (char *)data + length;
Expand Down