Skip to content

Commit 3f1a0ff

Browse files
committed
Send a ZLP if data size is multiple of EPX_SIZE for USB sends, arduino#63
1 parent be488e5 commit 3f1a0ff

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

cores/arduino/USB/USBCore.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,10 @@ uint8_t USBDeviceClass::armRecv(uint32_t ep)
581581
uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
582582
{
583583
uint32_t length = 0;
584-
584+
// if len is a multiple of EPX_SIZE an ZLP needs to be sent
585+
// to indicate end of transfer
586+
bool sendZlp = (len % EPX_SIZE) == 0;
587+
585588
if (!_usbConfiguration)
586589
return -1;
587590
if (len > 16384)
@@ -621,10 +624,10 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
621624
#endif
622625

623626
// Flash area
624-
while (len != 0)
627+
while (len != 0 || sendZlp)
625628
{
626-
if (len >= 64) {
627-
length = 64;
629+
if (len >= EPX_SIZE) {
630+
length = EPX_SIZE;
628631
} else {
629632
length = len;
630633
}
@@ -646,6 +649,12 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
646649
; // need fire exit.
647650
}
648651
len -= length;
652+
653+
if (len == 0 && sendZlp) {
654+
// empty transfer sent
655+
sendZlp = false;
656+
}
657+
649658
data += length;
650659
}
651660
return len;
@@ -679,12 +688,12 @@ uint32_t USBDeviceClass::sendControl(const void* _data, uint32_t len)
679688
return length;
680689
}
681690

682-
while (len > 0)
683-
{
691+
while (len > 0)
692+
{
684693
sent = armSend(EP0, data + pos, len);
685694
pos += sent;
686695
len -= sent;
687-
}
696+
}
688697

689698
return length;
690699
}

0 commit comments

Comments
 (0)