From b0d9f961acfff498b7c4f2dc4a54ad9513d09a3b Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Mon, 9 Mar 2009 11:50:43 +0000
Subject: [PATCH] Fixed interrupt driven HID device demos not clearing the
 interrupt flags in all circumstances.

---
 Demos/KeyboardFullInt/KeyboardFullInt.c | 24 ++++++++++++------------
 Demos/KeyboardViaInt/KeyboardViaInt.c   | 20 ++++++++++----------
 Demos/MouseFullInt/MouseFullInt.c       | 24 ++++++++++++------------
 Demos/MouseViaInt/MouseViaInt.c         | 18 +++++++++---------
 4 files changed, 43 insertions(+), 43 deletions(-)

diff --git a/Demos/KeyboardFullInt/KeyboardFullInt.c b/Demos/KeyboardFullInt/KeyboardFullInt.c
index 7e90b5ff2..44f983059 100644
--- a/Demos/KeyboardFullInt/KeyboardFullInt.c
+++ b/Demos/KeyboardFullInt/KeyboardFullInt.c
@@ -404,6 +404,15 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		USB_KeyboardReport_Data_t KeyboardReportData;
 		bool                      SendReport;
 	
+		/* Select the Keyboard Report Endpoint */
+		Endpoint_SelectEndpoint(KEYBOARD_EPNUM);
+
+		/* Clear the endpoint IN interrupt flag */
+		USB_INT_Clear(ENDPOINT_INT_IN);
+
+		/* Clear the Keyboard Report endpoint interrupt */
+		Endpoint_ClearEndpointInterrupt(KEYBOARD_EPNUM);
+
 		/* Create the next keyboard report for transmission to the host */
 		SendReport = GetNextReport(&KeyboardReportData);
 	
@@ -420,21 +429,12 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		/* Check to see if a report should be issued */
 		if (SendReport)
 		{
-			/* Select the Keyboard Report Endpoint */
-			Endpoint_SelectEndpoint(KEYBOARD_EPNUM);
-
-			/* Clear the endpoint IN interrupt flag */
-			USB_INT_Clear(ENDPOINT_INT_IN);
-
-			/* Clear the Keyboard Report endpoint interrupt */
-			Endpoint_ClearEndpointInterrupt(KEYBOARD_EPNUM);
-
 			/* Write Keyboard Report Data */
 			Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
-
-			/* Finalize the stream transfer to send the last packet */
-			Endpoint_ClearCurrentBank();
 		}
+
+		/* Finalize the stream transfer to send the last packet */
+		Endpoint_ClearCurrentBank();
 	}
 
 	/* Check if Keyboard LED status Endpoint has interrupted */
diff --git a/Demos/KeyboardViaInt/KeyboardViaInt.c b/Demos/KeyboardViaInt/KeyboardViaInt.c
index 7f6fcd8cd..791650675 100644
--- a/Demos/KeyboardViaInt/KeyboardViaInt.c
+++ b/Demos/KeyboardViaInt/KeyboardViaInt.c
@@ -391,7 +391,16 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 	{
 		USB_KeyboardReport_Data_t KeyboardReportData;
 		bool                      SendReport;
-	
+
+		/* Select the Keyboard Report Endpoint */
+		Endpoint_SelectEndpoint(KEYBOARD_EPNUM);
+
+		/* Clear the endpoint IN interrupt flag */
+		USB_INT_Clear(ENDPOINT_INT_IN);
+
+		/* Clear the Keyboard Report endpoint interrupt */
+		Endpoint_ClearEndpointInterrupt(KEYBOARD_EPNUM);
+
 		/* Create the next keyboard report for transmission to the host */
 		SendReport = GetNextReport(&KeyboardReportData);
 	
@@ -408,15 +417,6 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		/* Check to see if a report should be issued */
 		if (SendReport)
 		{
-			/* Select the Keyboard Report Endpoint */
-			Endpoint_SelectEndpoint(KEYBOARD_EPNUM);
-
-			/* Clear the endpoint IN interrupt flag */
-			USB_INT_Clear(ENDPOINT_INT_IN);
-
-			/* Clear the Keyboard Report endpoint interrupt */
-			Endpoint_ClearEndpointInterrupt(KEYBOARD_EPNUM);
-
 			/* Write Keyboard Report Data */
 			Endpoint_Write_Stream_LE(&KeyboardReportData, sizeof(KeyboardReportData));
 		}
diff --git a/Demos/MouseFullInt/MouseFullInt.c b/Demos/MouseFullInt/MouseFullInt.c
index c21959a2c..28267a14f 100644
--- a/Demos/MouseFullInt/MouseFullInt.c
+++ b/Demos/MouseFullInt/MouseFullInt.c
@@ -358,6 +358,15 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		USB_MouseReport_Data_t MouseReportData;
 		bool                   SendReport = true;
 		
+		/* Select the Mouse Report Endpoint */
+		Endpoint_SelectEndpoint(MOUSE_EPNUM);
+
+		/* Clear the endpoint IN interrupt flag */
+		USB_INT_Clear(ENDPOINT_INT_IN);
+
+		/* Clear the Mouse Report endpoint interrupt and select the endpoint */
+		Endpoint_ClearEndpointInterrupt(MOUSE_EPNUM);
+
 		/* Create the next mouse report for transmission to the host */
 		GetNextReport(&MouseReportData);
 		
@@ -380,20 +389,11 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		/* Check to see if a report should be issued */
 		if (SendReport)
 		{
-			/* Select the Mouse Report Endpoint */
-			Endpoint_SelectEndpoint(MOUSE_EPNUM);
-
-			/* Clear the endpoint IN interrupt flag */
-			USB_INT_Clear(ENDPOINT_INT_IN);
-
-			/* Clear the Mouse Report endpoint interrupt and select the endpoint */
-			Endpoint_ClearEndpointInterrupt(MOUSE_EPNUM);
-
 			/* Write Mouse Report Data */
 			Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
-
-			/* Finalize the stream transfer to send the last packet */
-			Endpoint_ClearCurrentBank();
 		}
+
+		/* Finalize the stream transfer to send the last packet */
+		Endpoint_ClearCurrentBank();
 	}
 }
diff --git a/Demos/MouseViaInt/MouseViaInt.c b/Demos/MouseViaInt/MouseViaInt.c
index 77e4c565e..a4b863c13 100644
--- a/Demos/MouseViaInt/MouseViaInt.c
+++ b/Demos/MouseViaInt/MouseViaInt.c
@@ -345,6 +345,15 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		USB_MouseReport_Data_t MouseReportData;
 		bool                   SendReport = true;
 		
+		/* Select the Mouse Report Endpoint */
+		Endpoint_SelectEndpoint(MOUSE_EPNUM);
+
+		/* Clear the endpoint IN interrupt flag */
+		USB_INT_Clear(ENDPOINT_INT_IN);
+
+		/* Clear the Mouse Report endpoint interrupt and select the endpoint */
+		Endpoint_ClearEndpointInterrupt(MOUSE_EPNUM);
+
 		/* Create the next mouse report for transmission to the host */
 		GetNextReport(&MouseReportData);
 		
@@ -367,15 +376,6 @@ ISR(ENDPOINT_PIPE_vect, ISR_BLOCK)
 		/* Check to see if a report should be issued */
 		if (SendReport)
 		{
-			/* Select the Mouse Report Endpoint */
-			Endpoint_SelectEndpoint(MOUSE_EPNUM);
-
-			/* Clear the endpoint IN interrupt flag */
-			USB_INT_Clear(ENDPOINT_INT_IN);
-
-			/* Clear the Mouse Report endpoint interrupt and select the endpoint */
-			Endpoint_ClearEndpointInterrupt(MOUSE_EPNUM);
-
 			/* Write Mouse Report Data */
 			Endpoint_Write_Stream_LE(&MouseReportData, sizeof(MouseReportData));
 		}
-- 
GitLab