From 1686f7804877ba0c4f05270a3628747f65519831 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 4 Jan 2017 17:10:09 +0100 Subject: [PATCH 1/2] Revert "Subtract one from USB_EP_SIZE in USB_SendSpace" This reverts commit 817d700a7503b269f986075cad637ce56c657e37. --- hardware/arduino/avr/cores/arduino/USBCore.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/USBCore.cpp b/hardware/arduino/avr/cores/arduino/USBCore.cpp index 723edb3c828..efac25ed65a 100644 --- a/hardware/arduino/avr/cores/arduino/USBCore.cpp +++ b/hardware/arduino/avr/cores/arduino/USBCore.cpp @@ -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 From a540126c930f0c4510ae58a400a16c7db6fa5b41 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 4 Jan 2017 18:40:13 +0100 Subject: [PATCH 2/2] [AVR] USB send ZLP when needed See #5732 #4864 #4138 #3946 --- .../arduino/avr/cores/arduino/USBCore.cpp | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/hardware/arduino/avr/cores/arduino/USBCore.cpp b/hardware/arduino/avr/cores/arduino/USBCore.cpp index efac25ed65a..e85c6131b1a 100644 --- a/hardware/arduino/avr/cores/arduino/USBCore.cpp +++ b/hardware/arduino/avr/cores/arduino/USBCore.cpp @@ -273,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) @@ -284,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) { @@ -307,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 @@ -473,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);