Skip to content

Commit fbb88e6

Browse files
committed
[AVR] Send USB ZLP if required
Fixes arduino#5732
1 parent f757ba1 commit fbb88e6

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

hardware/arduino/avr/cores/arduino/USBAPI.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ int USB_RecvControl(void* d, int len);
196196
int USB_RecvControlLong(void* d, int len);
197197

198198
uint8_t USB_Available(uint8_t ep);
199-
uint8_t USB_SendSpace(uint8_t ep);
199+
int8_t USB_SendSpace(uint8_t ep);
200200
int USB_Send(uint8_t ep, const void* data, int len); // blocking
201201
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
202202
int USB_Recv(uint8_t ep); // non-blocking

hardware/arduino/avr/cores/arduino/USBCore.cpp

+17-9
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,12 @@ int USB_Recv(u8 ep)
251251
}
252252

253253
// Space in send EP
254-
u8 USB_SendSpace(u8 ep)
254+
int8_t USB_SendSpace(u8 ep)
255255
{
256256
LockEP lock(ep);
257257
if (!ReadWriteAllowed())
258-
return 0;
259-
// subtract 1 from the EP size to never send a full packet,
260-
// this avoids dealing with ZLP's in USB_Send
261-
return USB_EP_SIZE - 1 - FifoByteCount();
258+
return -1;
259+
return USB_EP_SIZE - FifoByteCount();
262260
}
263261

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

275273
int r = len;
274+
275+
bool sendZlp = (len % USB_EP_SIZE) == 0;
276+
276277
const u8* data = (const u8*)d;
277278
u8 timeout = 250; // 250ms timeout on send? TODO
278-
while (len)
279+
while (len || sendZlp)
279280
{
280-
u8 n = USB_SendSpace(ep);
281-
if (n == 0)
281+
int8_t n = USB_SendSpace(ep);
282+
if (n < 0)
282283
{
283284
if (!(--timeout))
284285
return -1;
@@ -293,6 +294,13 @@ int USB_Send(u8 ep, const void* d, int len)
293294
// Frame may have been released by the SOF interrupt handler
294295
if (!ReadWriteAllowed())
295296
continue;
297+
298+
if (len == 0 && sendZlp) {
299+
// empty transfer sent
300+
Send8(0);
301+
sendZlp = false;
302+
}
303+
296304
len -= n;
297305
if (ep & TRANSFER_ZERO)
298306
{
@@ -475,7 +483,7 @@ static
475483
bool SendConfiguration(int maxlen)
476484
{
477485
// Count and measure interfaces
478-
InitControl(0);
486+
InitControl(0);
479487
u8 interfaces = SendInterfaces();
480488
ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
481489

0 commit comments

Comments
 (0)