Skip to content

[AVR] Send USB ZLP if required #5802

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 2 commits into from
Mar 16, 2017
Merged
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: 19 additions & 7 deletions hardware/arduino/avr/cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,7 @@ u8 USB_SendSpace(u8 ep)
LockEP lock(ep);
if (!ReadWriteAllowed())
return 0;
// subtract 1 from the EP size to never send a full packet,
// this avoids dealing with ZLP's in USB_Send
return USB_EP_SIZE - 1 - FifoByteCount();
return USB_EP_SIZE - FifoByteCount();
}

// Blocking Send of data to an endpoint
Expand All @@ -275,7 +273,9 @@ int USB_Send(u8 ep, const void* d, int len)
int r = len;
const u8* data = (const u8*)d;
u8 timeout = 250; // 250ms timeout on send? TODO
while (len)
bool sendZlp = false;

while (len || sendZlp)
{
u8 n = USB_SendSpace(ep);
if (n == 0)
Expand All @@ -286,13 +286,16 @@ int USB_Send(u8 ep, const void* d, int len)
continue;
}

if (n > len)
if (n > len) {
n = len;
}

{
LockEP lock(ep);
// Frame may have been released by the SOF interrupt handler
if (!ReadWriteAllowed())
continue;

len -= n;
if (ep & TRANSFER_ZERO)
{
Expand All @@ -309,8 +312,17 @@ int USB_Send(u8 ep, const void* d, int len)
while (n--)
Send8(*data++);
}
if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer

if (sendZlp) {
ReleaseTX();
sendZlp = false;
} else if (!ReadWriteAllowed()) { // ...release if buffer is full...
ReleaseTX();
if (len == 0) sendZlp = true;
} else if ((len == 0) && (ep & TRANSFER_RELEASE)) { // ...or if forced with TRANSFER_RELEASE
// XXX: TRANSFER_RELEASE is never used can be removed?
ReleaseTX();
}
}
}
TXLED1; // light the TX LED
Expand Down Expand Up @@ -475,7 +487,7 @@ static
bool SendConfiguration(int maxlen)
{
// Count and measure interfaces
InitControl(0);
InitControl(0);
u8 interfaces = SendInterfaces();
ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);

Expand Down