From 9d0d5d61586cd285fb838be1a6c17923fc75984e Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Thu, 2 Jul 2015 15:43:40 +0200
Subject: [PATCH] fix back-to-back SerialUSB test

---
 cores/arduino/USB/CDC.cpp     |  7 +++++--
 cores/arduino/USB/USBCore.cpp | 24 +++++++++++++-----------
 2 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/cores/arduino/USB/CDC.cpp b/cores/arduino/USB/CDC.cpp
index d68bd8c4..0ecee14a 100644
--- a/cores/arduino/USB/CDC.cpp
+++ b/cores/arduino/USB/CDC.cpp
@@ -25,7 +25,7 @@
 
 #ifdef CDC_ENABLED
 
-#define CDC_SERIAL_BUFFER_SIZE	64
+#define CDC_SERIAL_BUFFER_SIZE	256
 
 /* For information purpose only since RTS is not always handled by the terminal application */
 #define CDC_LINESTATE_DTR		0x01 // Data Terminal Ready
@@ -175,9 +175,12 @@ void Serial_::accept(void)
 int Serial_::available(void)
 {
 	ring_buffer *buffer = &cdc_rx_buffer;
-	if (buffer->full)
+	if (buffer->full) {
+		USB->DEVICE.DeviceEndpoint[2].EPINTENSET.reg = ~USB_DEVICE_EPINTENCLR_RXSTP;
 		return CDC_SERIAL_BUFFER_SIZE;
+	}
 	if (buffer->head == buffer->tail) {
+		USB->DEVICE.DeviceEndpoint[2].EPINTENSET.reg = USB_DEVICE_EPINTENCLR_RXSTP;
 		USB->DEVICE.DeviceEndpoint[2].EPINTENSET.reg = USB_DEVICE_EPINTENCLR_TRCPT(1);
 	}
 	return (uint32_t)(CDC_SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % CDC_SERIAL_BUFFER_SIZE;
diff --git a/cores/arduino/USB/USBCore.cpp b/cores/arduino/USB/USBCore.cpp
index 472efc42..3852e30f 100644
--- a/cores/arduino/USB/USBCore.cpp
+++ b/cores/arduino/USB/USBCore.cpp
@@ -68,10 +68,10 @@ const DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00, 0x00, 0x00, 64, USB
 volatile uint32_t _usbConfiguration = 0;
 volatile uint32_t _usbSetInterface = 0;
 
-static __attribute__((__aligned__(8))) //__attribute__((__section__(".bss_hram0")))
+static __attribute__((__aligned__(4))) //__attribute__((__section__(".bss_hram0")))
 uint8_t udd_ep_out_cache_buffer[4][64];
 
-static __attribute__((__aligned__(8))) //__attribute__((__section__(".bss_hram0")))
+static __attribute__((__aligned__(4))) //__attribute__((__section__(".bss_hram0")))
 uint8_t udd_ep_in_cache_buffer[4][64];
 
 //==================================================================
@@ -502,17 +502,19 @@ uint32_t USBDeviceClass::recv(uint32_t ep, void *_data, uint32_t len)
 	// NAK on endpoint OUT, the bank is full.
 	//usbd.epBank0SetReady(CDC_ENDPOINT_OUT);
 
-	uint8_t *buffer = udd_ep_out_cache_buffer[ep];
-	uint8_t *data = reinterpret_cast<uint8_t *>(_data);
-	for (uint32_t i=0; i<len; i++) {
-		data[i] = buffer[i];
-	}
+	memcpy(_data, udd_ep_out_cache_buffer[ep], len);
+
+	// uint8_t *buffer = udd_ep_out_cache_buffer[ep];
+	// uint8_t *data = reinterpret_cast<uint8_t *>(_data);
+	// for (uint32_t i=0; i<len; i++) {
+	// 	data[i] = buffer[i];
+	// }
 
 	// release empty buffer
 	if (len && !available(ep)) {
 		// The RAM Buffer is empty: we can receive data
 		usbd.epBank0ResetReady(ep);
-		
+
 		// Clear Transfer complete 0 flag
 		usbd.epBank0AckTransferComplete(ep);
 	}
@@ -553,9 +555,9 @@ uint8_t USBDeviceClass::armRecvCtrlOUT(uint32_t ep, uint32_t len)
 
 uint8_t USBDeviceClass::armRecv(uint32_t ep, uint32_t len)
 {
-	usbd.epBank0SetSize(ep, 64);
-	usbd.epBank0SetAddress(ep, &udd_ep_out_cache_buffer[ep]);
-	usbd.epBank0SetMultiPacketSize(ep, 64); // XXX: Should be "len"?
+	//usbd.epBank0SetSize(ep, 64);
+	//usbd.epBank0SetAddress(ep, &udd_ep_out_cache_buffer[ep]);
+	//usbd.epBank0SetMultiPacketSize(ep, 64); // XXX: Should be "len"?
 	uint16_t count = usbd.epBank0ByteCount(ep);
 	if (count >= 64) {
 		usbd.epBank0SetByteCount(ep, count - 64);
-- 
GitLab