From 90d2f8032a671c8250044464fda1d52c97609fe2 Mon Sep 17 00:00:00 2001
From: Martino Facchin <m.facchin@arduino.cc>
Date: Tue, 30 Jun 2015 18:08:49 +0200
Subject: [PATCH] USB: fix CDC endpoints

* fix in_endpoint buffer size (was too big)
* use the same EP for CDC_ENDPOINT_OUT and CDC_ENDPOINT_IN (different buffers)
* fix CDC_ENDPOINT_IN ep type
---
 cores/arduino/USB/USBCore.cpp | 20 +++++++++++++-------
 cores/arduino/USB/USBDesc.h   |  4 ++--
 2 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/cores/arduino/USB/USBCore.cpp b/cores/arduino/USB/USBCore.cpp
index cfcf101a..472efc42 100644
--- a/cores/arduino/USB/USBCore.cpp
+++ b/cores/arduino/USB/USBCore.cpp
@@ -68,11 +68,11 @@ const DeviceDescriptor USB_DeviceDescriptor = D_DEVICE(0x00, 0x00, 0x00, 64, USB
 volatile uint32_t _usbConfiguration = 0;
 volatile uint32_t _usbSetInterface = 0;
 
-static __attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/
+static __attribute__((__aligned__(8))) //__attribute__((__section__(".bss_hram0")))
 uint8_t udd_ep_out_cache_buffer[4][64];
 
-static __attribute__((__aligned__(4))) /*__attribute__((__section__(".bss_hram0")))*/
-uint8_t udd_ep_in_cache_buffer[4][128];
+static __attribute__((__aligned__(8))) //__attribute__((__section__(".bss_hram0")))
+uint8_t udd_ep_in_cache_buffer[4][64];
 
 //==================================================================
 
@@ -375,7 +375,7 @@ void USBDeviceClass::initEP(uint32_t ep, uint32_t config)
 {
 	if (config == (USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0)))
 	{
-		usbd.epBank1SetSize(ep, 8);
+		usbd.epBank1SetSize(ep, 64);
 		usbd.epBank1SetAddress(ep, &udd_ep_in_cache_buffer[ep]);
 		usbd.epBank1SetType(ep, 4); // INTERRUPT IN
 	}
@@ -580,7 +580,13 @@ uint32_t USBDeviceClass::send(uint32_t ep, const void *data, uint32_t len)
 	if (!_usbConfiguration)
 		return -1;
 
-	armSend(ep, data, len);
+	//armSend(ep, data, len);
+
+	/* memcopy could be safer in multithreaded environment */
+	memcpy(&udd_ep_in_cache_buffer[ep], data, len);
+
+	usbd.epBank1SetAddress(ep, &udd_ep_in_cache_buffer[ep]);
+	usbd.epBank1SetByteCount(ep, len);
 
 	// Clear the transfer complete flag
 	usbd.epBank1AckTransferComplete(ep);
@@ -712,9 +718,9 @@ bool USBDeviceClass::handleStandardSetup(Setup &setup)
 			#endif
 
 			#if defined(CDC_ENABLED)
-			initEP(CDC_ENDPOINT_ACM, USB_ENDPOINT_TYPE_BULK      | USB_ENDPOINT_IN(0));
+			initEP(CDC_ENDPOINT_ACM, USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0));
 			initEP(CDC_ENDPOINT_OUT, USB_ENDPOINT_TYPE_BULK      | USB_ENDPOINT_OUT(0));
-			initEP(CDC_ENDPOINT_IN,  USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0));
+			initEP(CDC_ENDPOINT_IN,  USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0));
 			#endif
 			_usbConfiguration = setup.wValueL;
 
diff --git a/cores/arduino/USB/USBDesc.h b/cores/arduino/USB/USBDesc.h
index 655fb6d0..4c326389 100644
--- a/cores/arduino/USB/USBDesc.h
+++ b/cores/arduino/USB/USBDesc.h
@@ -28,11 +28,11 @@
 #define CDC_DATA_INTERFACE	1	// CDC Data
 #define CDC_ENDPOINT_ACM	1
 #define CDC_ENDPOINT_OUT	2
-#define CDC_ENDPOINT_IN		3
+#define CDC_ENDPOINT_IN		2
 
 // HID
 #define HID_INTERFACE		2   // HID
-#define HID_ENDPOINT_INT	4
+#define HID_ENDPOINT_INT	3
 
 // Defined string description
 #define IMANUFACTURER	1
-- 
GitLab