Skip to content

Commit

Permalink
[AVR] Send USB ZLP if required
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Jan 4, 2017
1 parent f757ba1 commit fbb88e6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion hardware/arduino/avr/cores/arduino/USBAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ int USB_RecvControl(void* d, int len);
int USB_RecvControlLong(void* d, int len);

uint8_t USB_Available(uint8_t ep);
uint8_t USB_SendSpace(uint8_t ep);
int8_t USB_SendSpace(uint8_t ep);
int USB_Send(uint8_t ep, const void* data, int len); // blocking
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
int USB_Recv(uint8_t ep); // non-blocking
Expand Down
26 changes: 17 additions & 9 deletions hardware/arduino/avr/cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,12 @@ int USB_Recv(u8 ep)
}

// Space in send EP
u8 USB_SendSpace(u8 ep)
int8_t 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 -1;
return USB_EP_SIZE - FifoByteCount();
}

// Blocking Send of data to an endpoint
Expand All @@ -273,12 +271,15 @@ int USB_Send(u8 ep, const void* d, int len)
}

int r = len;

bool sendZlp = (len % USB_EP_SIZE) == 0;

const u8* data = (const u8*)d;
u8 timeout = 250; // 250ms timeout on send? TODO
while (len)
while (len || sendZlp)
{
u8 n = USB_SendSpace(ep);
if (n == 0)
int8_t n = USB_SendSpace(ep);
if (n < 0)
{
if (!(--timeout))
return -1;
Expand All @@ -293,6 +294,13 @@ int USB_Send(u8 ep, const void* d, int len)
// Frame may have been released by the SOF interrupt handler
if (!ReadWriteAllowed())
continue;

if (len == 0 && sendZlp) {
// empty transfer sent
Send8(0);
sendZlp = false;
}

len -= n;
if (ep & TRANSFER_ZERO)
{
Expand Down Expand Up @@ -475,7 +483,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

0 comments on commit fbb88e6

Please sign in to comment.