From a868785f42b4d3de76476847836899262eeb0ad8 Mon Sep 17 00:00:00 2001
From: Jean-Christophe BUDA <jean-christophe.buda@atmel.com>
Date: Tue, 26 Aug 2014 11:49:15 +0200
Subject: [PATCH]  change define for PSTATUS SET / CLR and TCRP

---
 cores/arduino/USB/USBCore.cpp                 | 24 ++++++------
 cores/arduino/USB/samd21_device.c             | 38 ++++++++++---------
 cores/arduino/USB/samd21_device.h             | 35 ++++++++++-------
 .../build_as6/test.cppproj                    |  8 ----
 4 files changed, 54 insertions(+), 51 deletions(-)

diff --git a/cores/arduino/USB/USBCore.cpp b/cores/arduino/USB/USBCore.cpp
index 87b65bc2..4403959c 100644
--- a/cores/arduino/USB/USBCore.cpp
+++ b/cores/arduino/USB/USBCore.cpp
@@ -156,12 +156,12 @@ uint32_t USBD_Send(uint32_t ep, const void* d, uint32_t len)
 	UDD_Send(ep, data, len);
 
 	/* Clear the transfer complete flag  */
-	udd_clear_transf_cplt(ep);
+	udd_clear_IN_transf_cplt(ep);
 	/* Set the bank as ready */
-	udd_bk_rdy(ep);
+	udd_IN_transfer_allowed(ep);
 
 	/* Wait for transfer to complete */
-	while (! udd_is_transf_cplt(ep));  // need fire exit.
+	while (! udd_is_IN_transf_cplt(ep));  // need fire exit.
 	return r;
 }
 
@@ -206,7 +206,7 @@ static bool USB_SendStringDescriptor(const uint8_t *string, int wLength)
 
 uint32_t USBD_RecvControl(void* d, uint32_t len)
 {
-	udd_ack_out_received(0);
+	udd_OUT_transfer_allowed(0);
 
 	return len;
 }
@@ -410,7 +410,7 @@ void EndpointHandler(uint8_t bEndpoint)
 #ifdef CDC_ENABLED
 	if( bEndpoint == CDC_ENDPOINT_OUT )
 	{
-		udd_ack_out_received(CDC_ENDPOINT_OUT);
+		udd_OUT_transfer_allowed(CDC_ENDPOINT_OUT);
 
 		// Handle received bytes
 		if (USBD_Available(CDC_ENDPOINT_OUT))
@@ -420,16 +420,16 @@ void EndpointHandler(uint8_t bEndpoint)
 	}
 	if( bEndpoint == CDC_ENDPOINT_IN )
 	{
-		udd_ack_in_received(CDC_ENDPOINT_IN);
+		udd_IN_stop_transfer(CDC_ENDPOINT_IN);
 		/* Clear the transfer complete flag  */
-		udd_clear_transf_cplt(CDC_ENDPOINT_IN);
+		udd_clear_IN_transf_cplt(CDC_ENDPOINT_IN);
 
 	}
 	if( bEndpoint == CDC_ENDPOINT_ACM )
 	{
-		udd_ack_in_received(CDC_ENDPOINT_ACM);
+		udd_IN_stop_transfer(CDC_ENDPOINT_ACM);
 		/* Clear the transfer complete flag  */
-		udd_clear_transf_cplt(CDC_ENDPOINT_ACM);
+		udd_clear_IN_transf_cplt(CDC_ENDPOINT_ACM);
 	}
 #endif
 
@@ -490,7 +490,7 @@ void USB_ISR(void)
 			UDD_Recv(EP0, (uint8_t**)&pSetupData);
 
 			/* Clear the Bank 0 ready flag on Control OUT */
-			udd_ack_out_received(0);
+			udd_OUT_transfer_allowed(0);
 
 			bool ok = true;
 			if (REQUEST_STANDARD == (pSetupData->bmRequestType & REQUEST_TYPE))
@@ -600,8 +600,8 @@ void USB_ISR(void)
 
 #ifdef CDC_ENABLED
 						// Enable interrupt for CDC reception from host (OUT packet)
-						udd_ept_enable_it_transf_cplt_in(CDC_ENDPOINT_ACM);
-						udd_ept_enable_it_transf_cplt_out(CDC_ENDPOINT_OUT);
+						udd_ept_enable_it_IN_transf_cplt(CDC_ENDPOINT_ACM);
+						udd_ept_enable_it_OUT_transf_cplt(CDC_ENDPOINT_OUT);
 #endif
 						send_zlp();
 					}
diff --git a/cores/arduino/USB/samd21_device.c b/cores/arduino/USB/samd21_device.c
index 9d3bc68a..7ee6eb22 100644
--- a/cores/arduino/USB/samd21_device.c
+++ b/cores/arduino/USB/samd21_device.c
@@ -207,7 +207,7 @@ void UDD_InitEP( uint32_t ul_ep_nb, uint32_t ul_ep_cfg )
 		usb_endpoint_table[ul_ep_nb].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = 64;
 		usb_endpoint_table[ul_ep_nb].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
 		// NACK if not ready
-	    USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY;
+		udd_OUT_transfer_allowed(ul_ep_nb);
 	}
 	else if( ul_ep_cfg == (USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0)) )
 	{
@@ -218,14 +218,14 @@ void UDD_InitEP( uint32_t ul_ep_nb, uint32_t ul_ep_cfg )
 		/* Configure the data buffer */
 		usb_endpoint_table[ul_ep_nb].DeviceDescBank[1].ADDR.reg = (uint32_t)&udd_ep_in_cache_buffer[ul_ep_nb];
 		// NACK if not ready
-		USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
+		udd_IN_stop_transfer(ul_ep_nb);
 	}
 	else if( ul_ep_cfg == USB_ENDPOINT_TYPE_CONTROL )
 	{
 		/* Configure CONTROL endpoint */
 		USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPCFG.reg = USB_DEVICE_EPCFG_EPTYPE0(1) | USB_DEVICE_EPCFG_EPTYPE1(1);
-		USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
-		USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
+		udd_OUT_stop_transfer(ul_ep_nb);
+		udd_IN_stop_transfer(ul_ep_nb);
 
 		usb_endpoint_table[ul_ep_nb].DeviceDescBank[0].PCKSIZE.reg &= ~USB_DEVICE_PCKSIZE_AUTO_ZLP;
 		usb_endpoint_table[ul_ep_nb].DeviceDescBank[1].PCKSIZE.reg &= ~USB_DEVICE_PCKSIZE_AUTO_ZLP;
@@ -240,8 +240,8 @@ void UDD_InitEP( uint32_t ul_ep_nb, uint32_t ul_ep_cfg )
 		usb_endpoint_table[ul_ep_nb].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
 
 		// NACK if not ready
-		USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY;
-		USB->DEVICE.DeviceEndpoint[ul_ep_nb].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY;
+		udd_OUT_stop_transfer(ul_ep_nb);
+		udd_IN_stop_transfer(ul_ep_nb);
 	}
 }
 
@@ -249,7 +249,7 @@ void UDD_InitEP( uint32_t ul_ep_nb, uint32_t ul_ep_cfg )
 // Send packet.
 void UDD_ClearIN(void)
 {
-	USB->DEVICE.DeviceEndpoint[EP0].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK1RDY;
+	udd_IN_transfer_allowed(EP0);
 }
 
 
@@ -273,13 +273,13 @@ uint8_t UDD_Recv8(uint32_t ep)
     usb_endpoint_table[ep].DeviceDescBank[0].ADDR.reg = (uint32_t)&udd_ep_out_cache_buffer[ep];
 	usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.MULTI_PACKET_SIZE = 8;
 	usb_endpoint_table[ep].DeviceDescBank[0].PCKSIZE.bit.BYTE_COUNT = 0;
-	USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY;
+	udd_OUT_transfer_allowed(ep);
 	TRACE_DEVICE(printf("=> UDD_Recv8 : data=%lu\r\n", (unsigned long)data);)
 
 	/* Wait for transfer to complete */
-	while (!( USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT0 ));
+	while (!udd_is_OUT_transf_cplt(ep));
 	/* Clear Transfer complete 0 flag */
-	USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.TRCPT0 = 1;
+	udd_clear_OUT_transf_cplt(ep);
 
 	return udd_ep_out_cache_buffer[ep][0];
 }
@@ -305,15 +305,19 @@ uint32_t UDD_FifoByteCount(uint32_t ep)
 void UDD_ReleaseRX(uint32_t ep)
 {
 	TRACE_DEVICE(puts("=> UDD_ReleaseRX\r\n");)
-	USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY;
-    USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0;
+	// The RAM Buffer is empty: we can receive data
+	udd_OUT_transfer_allowed(ep);
+	/* Clear Transfer complete 0 flag */
+	udd_clear_OUT_transf_cplt(ep);
 }
 
 void UDD_ReleaseTX(uint32_t ep)
 {
 	TRACE_DEVICE(printf("=> UDD_ReleaseTX ep=%lu\r\n", (unsigned long)ep);)
-    USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK1RDY;
-    USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1;
+	// The RAM Buffer is full: we can send data
+    udd_IN_transfer_allowed(ep);
+ 	/* Clear the transfer complete flag  */
+    udd_clear_IN_transf_cplt(ep);
 }
 
 void UDD_SetAddress(uint32_t addr)
@@ -323,12 +327,12 @@ void UDD_SetAddress(uint32_t addr)
 	/* Set the byte count as zero */
 	usb_endpoint_table[0].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT = 0;
  	/* Clear the transfer complete flag  */
- 	USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.TRCPT1 = 1;
+    udd_clear_IN_transf_cplt(0);
 	/* Set the bank as ready */
-	USB->DEVICE.DeviceEndpoint[0].EPSTATUSSET.bit.BK1RDY = 1;
+	udd_IN_transfer_allowed(0);
 
 	/* Wait for transfer to complete */
-	while (!( USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1 )) {}
+	while (!udd_is_IN_transf_cplt(EP0)) {}
 
 	udd_configure_address(addr);
 }
diff --git a/cores/arduino/USB/samd21_device.h b/cores/arduino/USB/samd21_device.h
index 8150fb17..db7b4d1e 100644
--- a/cores/arduino/USB/samd21_device.h
+++ b/cores/arduino/USB/samd21_device.h
@@ -50,22 +50,29 @@ extern "C" {
 // configures the USB device address and enable it.
 #define udd_configure_address(address)            USB->DEVICE.DADD.reg = USB_DEVICE_DADD_ADDEN | address
 
-// Enables SETUP received interrupt
-#define udd_enable_setup_received_interrupt(ep)   USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.reg = USB_DEVICE_EPINTFLAG_RXSTP
-// ACKs OUT received
-#define udd_ack_out_received(ep)                  USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY
-// ACKs OUT received
-#define udd_ack_in_received(ep)                   USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY
-// Is transfert completed ?
-#define udd_is_transf_cplt(ep)                   (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1)
-// Clear the transfer complete flag
-#define udd_clear_transf_cplt(ep)                 USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.bit.TRCPT1 = 1
-// Set the bank as ready
-#define udd_bk_rdy(ep)                            USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.bit.BK1RDY = 1
+// Clear BK0RDY for know that the BANK0 ram buffer (udd_g_ep_table[ep].DeviceDescBank[0]) is empty and can receive data 
+#define udd_OUT_transfer_allowed(ep)              USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK0RDY
+// Set BK1RDY for know that the BANK1 ram buffer (udd_g_ep_table[ep].DeviceDescBank[1]) is full and can send data
+#define udd_IN_transfer_allowed(ep)               USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK1RDY
+
+// Stop OUT transfer
+#define udd_OUT_stop_transfer(ep)                 USB->DEVICE.DeviceEndpoint[ep].EPSTATUSSET.reg = USB_DEVICE_EPSTATUSSET_BK0RDY
+// Stop IN transfer
+#define udd_IN_stop_transfer(ep)                  USB->DEVICE.DeviceEndpoint[ep].EPSTATUSCLR.reg = USB_DEVICE_EPSTATUSCLR_BK1RDY
+
 #define udd_read_endpoint_flag(ep)                USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg
+// Is transfer completed ?
+#define udd_is_IN_transf_cplt(ep)                 (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT1)
+#define udd_is_OUT_transf_cplt(ep)                (USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg & USB_DEVICE_EPINTFLAG_TRCPT0)
+// Clear the transfer complete flag
+#define udd_clear_IN_transf_cplt(ep)              USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT1
+#define udd_clear_OUT_transf_cplt(ep)             USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT0
 // Enable interrupt transfer complete
-#define udd_ept_enable_it_transf_cplt_in(ep)      USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT1
-#define udd_ept_enable_it_transf_cplt_out(ep)     USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0
+#define udd_ept_enable_it_IN_transf_cplt(ep)      USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT1
+#define udd_ept_enable_it_OUT_transf_cplt(ep)     USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.reg = USB_DEVICE_EPINTENSET_TRCPT0
+
+// Enables SETUP received interrupt
+#define udd_enable_setup_received_interrupt(ep)   USB->DEVICE.DeviceEndpoint[ep].EPINTENSET.reg = USB_DEVICE_EPINTFLAG_RXSTP
 // Clear the stall flag
 #define udd_clear_stall_request(ep)			      USB->DEVICE.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_STALL1
 // Remove stall
diff --git a/cores/validation/validation_usb_device/build_as6/test.cppproj b/cores/validation/validation_usb_device/build_as6/test.cppproj
index 0d718425..9a11cc0d 100644
--- a/cores/validation/validation_usb_device/build_as6/test.cppproj
+++ b/cores/validation/validation_usb_device/build_as6/test.cppproj
@@ -341,14 +341,6 @@
       <SubType>compile</SubType>
       <Link>core\Stream.h</Link>
     </Compile>
-    <Compile Include="..\..\..\arduino\syscalls.c">
-      <SubType>compile</SubType>
-      <Link>core\syscalls.c</Link>
-    </Compile>
-    <Compile Include="..\..\..\arduino\syscalls.h">
-      <SubType>compile</SubType>
-      <Link>core\syscalls.h</Link>
-    </Compile>
     <Compile Include="..\..\..\arduino\Tone.h">
       <SubType>compile</SubType>
       <Link>core\Tone.h</Link>
-- 
GitLab