From 5265e1215af6b7072482c27ffd6dd78a8a3b20fc Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Wed, 24 Aug 2016 12:37:27 +0200
Subject: [PATCH] USB: EP0 ::recv method has been merged with ::read method

It makes no sense to have ::recv calling repeatedly ::read in this
case.
---
 cores/arduino/USB/SAMD21_USBDevice.h | 37 ++++++++++++----------------
 1 file changed, 16 insertions(+), 21 deletions(-)

diff --git a/cores/arduino/USB/SAMD21_USBDevice.h b/cores/arduino/USB/SAMD21_USBDevice.h
index a6259f90..7d4c7b81 100644
--- a/cores/arduino/USB/SAMD21_USBDevice.h
+++ b/cores/arduino/USB/SAMD21_USBDevice.h
@@ -253,17 +253,23 @@ public:
 		release();
 	}
 
-	// Read one byte from the buffer, if the buffer is empty -1 is returned
-	int read() {
+	virtual uint32_t recv(void *_data, uint32_t len)
+	{
+		uint8_t *data = reinterpret_cast<uint8_t *>(_data);
+
 		// R/W: current, first0/1, ready0/1, notify
 		// R  : last0/1, data0/1
 		if (current == 0) {
 			synchronized {
 				if (!ready0) {
-					return -1;
+					return 0;
 				}
 			}
 			// when ready0==true the buffer is not being filled and last0 is constant
+			uint32_t i;
+			for (i=0; i<len && first0 < last0; i++) {
+				data[i] = data0[first0++];
+			}
 			if (first0 == last0) {
 				first0 = 0;
 				current = 1;
@@ -274,16 +280,19 @@ public:
 						release();
 					}
 				}
-				return -1;
 			}
-			return data0[first0++];
+			return i;
 		} else {
 			synchronized {
 				if (!ready1) {
-					return -1;
+					return 0;
 				}
 			}
 			// when ready1==true the buffer is not being filled and last1 is constant
+			uint32_t i;
+			for (i=0; i<len && first1 < last1; i++) {
+				data[i] = data1[first1++];
+			}
 			if (first1 == last1) {
 				first1 = 0;
 				current = 0;
@@ -294,9 +303,8 @@ public:
 						release();
 					}
 				}
-				return -1;
 			}
-			return data1[first1++];
+			return i;
 		}
 	}
 
@@ -340,19 +348,6 @@ public:
 		}
 	}
 
-	virtual uint32_t recv(void *_data, uint32_t len)
-	{
-		// TODO Write an optimized version of this one
-		uint8_t *data = reinterpret_cast<uint8_t *>(_data);
-		uint32_t i;
-		for (i=0; i<len; i++) {
-			int c = read();
-			if (c == -1) break;
-			data[i] = c;
-		}
-		return i;
-	}
-
 	// Returns how many bytes are stored in the buffers
 	virtual uint32_t available() const {
 		return (last0 - first0) + (last1 - first1);
-- 
GitLab