From 3ae9e6042fc1ebc876e32142dde606f7890518b2 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Wed, 6 Oct 2010 10:16:02 +0000
Subject: [PATCH] Cache the USB_DeviceState variable internally in the library
 core when multiple checks are required in a single loop iteration, to reduce
 compiled code size.

---
 .../Template/Template_Endpoint_Control_R.c    | 15 ++++++++-----
 .../Template/Template_Endpoint_Control_W.c    | 22 ++++++++++---------
 LUFA/Drivers/USB/LowLevel/Endpoint.c          |  8 ++++---
 LUFA/ManPages/ChangeLog.txt                   |  4 ++--
 LUFA/ManPages/SoftwareBootloaderJump.txt      |  2 +-
 LUFA/ManPages/WhyUseLUFA.txt                  |  2 +-
 6 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_R.c b/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_R.c
index 43abe6e1e..c8bfaeb70 100644
--- a/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_R.c
+++ b/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_R.c
@@ -8,13 +8,14 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer,
 	
 	while (Length)
 	{
-		if (Endpoint_IsSETUPReceived())
-		  return ENDPOINT_RWCSTREAM_HostAborted;
+		uint8_t USB_DeviceState_LCL = USB_DeviceState;
 
-		if (USB_DeviceState == DEVICE_STATE_Unattached)
+		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
 		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
-		else if (USB_DeviceState == DEVICE_STATE_Suspended)
+		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
 		  return ENDPOINT_RWCSTREAM_BusSuspended;
+		else if (Endpoint_IsSETUPReceived())
+		  return ENDPOINT_RWCSTREAM_HostAborted;
 		  
 		if (Endpoint_IsOUTReceived())
 		{
@@ -30,9 +31,11 @@ uint8_t TEMPLATE_FUNC_NAME (void* Buffer,
 	
 	while (!(Endpoint_IsINReady()))
 	{
-		if (USB_DeviceState == DEVICE_STATE_Unattached)
+		uint8_t USB_DeviceState_LCL = USB_DeviceState;
+
+		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
 		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
-		else if (USB_DeviceState == DEVICE_STATE_Suspended)
+		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
 		  return ENDPOINT_RWCSTREAM_BusSuspended;
 	}
 	
diff --git a/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_W.c b/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_W.c
index c9d81f3db..0ae7febc4 100644
--- a/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_W.c
+++ b/LUFA/Drivers/USB/HighLevel/Template/Template_Endpoint_Control_W.c
@@ -11,17 +11,17 @@ uint8_t TEMPLATE_FUNC_NAME (const void* Buffer,
 
 	while (Length || LastPacketFull)
 	{
-		if (Endpoint_IsSETUPReceived())
-		  return ENDPOINT_RWCSTREAM_HostAborted;
-
-		if (Endpoint_IsOUTReceived())
-		  break;
+		uint8_t USB_DeviceState_LCL = USB_DeviceState;
 		
-		if (USB_DeviceState == DEVICE_STATE_Unattached)
+		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
 		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
-		else if (USB_DeviceState == DEVICE_STATE_Suspended)
+		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
 		  return ENDPOINT_RWCSTREAM_BusSuspended;
-		  
+		else if (Endpoint_IsSETUPReceived())
+		  return ENDPOINT_RWCSTREAM_HostAborted;
+		else if (Endpoint_IsOUTReceived())
+		  break;
+
 		if (Endpoint_IsINReady())
 		{
 			uint16_t BytesInEndpoint = Endpoint_BytesInEndpoint();
@@ -40,9 +40,11 @@ uint8_t TEMPLATE_FUNC_NAME (const void* Buffer,
 	
 	while (!(Endpoint_IsOUTReceived()))
 	{
-		if (USB_DeviceState == DEVICE_STATE_Unattached)
+		uint8_t USB_DeviceState_LCL = USB_DeviceState;
+
+		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
 		  return ENDPOINT_RWCSTREAM_DeviceDisconnected;
-		else if (USB_DeviceState == DEVICE_STATE_Suspended)
+		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
 		  return ENDPOINT_RWCSTREAM_BusSuspended;
 	}
 
diff --git a/LUFA/Drivers/USB/LowLevel/Endpoint.c b/LUFA/Drivers/USB/LowLevel/Endpoint.c
index fc54e115a..fc2bff154 100644
--- a/LUFA/Drivers/USB/LowLevel/Endpoint.c
+++ b/LUFA/Drivers/USB/LowLevel/Endpoint.c
@@ -115,10 +115,12 @@ uint8_t Endpoint_WaitUntilReady(void)
 			  return ENDPOINT_READYWAIT_NoError;
 		}
 		
-		if (USB_DeviceState == DEVICE_STATE_Unattached)
+		uint8_t USB_DeviceState_LCL = USB_DeviceState;
+
+		if (USB_DeviceState_LCL == DEVICE_STATE_Unattached)
 		  return ENDPOINT_READYWAIT_DeviceDisconnected;
-		else if (USB_DeviceState == DEVICE_STATE_Suspended)
-		  return ENDPOINT_READYWAIT_BusSuspended;
+		else if (USB_DeviceState_LCL == DEVICE_STATE_Suspended)
+		  return ENDPOINT_READYWAIT_BusSuspended;		
 		else if (Endpoint_IsStalled())
 		  return ENDPOINT_READYWAIT_EndpointStalled;
 
diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index 805b3e165..4aec15397 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -18,7 +18,7 @@
   *    handling of USB Start of Frame events while in USB Host mode
   *  - Added new PRNT_Host_BytesReceived(), PRNT_Host_ReceiveByte(), PRNT_Host_SendByte() and PRNT_Host_Flush() functions to the
   *    Print Host Class driver
-  *  - Added class specific descriptor type defines with standard USB-IF element naming
+  *  - Added class specific descriptor alternative struct type defines with standard USB-IF element naming
   *  - Added new project makefile template to the library and moved board driver stub files into in a new "CodeTemplates" directory
   *  - Added board hardware driver support for the Linnix UDIP development board
   *  - Added board hardware driver support for the Busware BUI development board
@@ -42,7 +42,7 @@
   *  - Changed over all demos, drivers and internal functions to use the current frame number over the Start of Frame flag where possible
   *    to free up the Start of Frame flag for interrupt use in the user application
   *  - All project makefiles now correctly clean intermediate build files from assembly and C++ sources (thanks to Daniel Czigany)
-  *  - Removed the EVENT_USB_InitFailure() event, not specifying a USB mode now defaults to UID selection mode
+  *  - Removed the EVENT_USB_InitFailure() event, not specifying a USB mode correctly now defaults to UID selection mode
   *  - Renamed and moved class driver common constant definitions to make the naming scheme more uniform
   *  - Changed default value for the reset polarity parameter in the AVRISP-MKII project so that it defaults to active low drive
   *  - Changed configuration descriptor parser for all host mode projects and class drivers to ensure better compatibility with devices
diff --git a/LUFA/ManPages/SoftwareBootloaderJump.txt b/LUFA/ManPages/SoftwareBootloaderJump.txt
index 6e7ea584e..574398c8f 100644
--- a/LUFA/ManPages/SoftwareBootloaderJump.txt
+++ b/LUFA/ManPages/SoftwareBootloaderJump.txt
@@ -35,7 +35,7 @@
  *  void Bootloader_Jump_Check(void)
  *  {
  *      // If the reset source was the bootloader and the key is correct, clear it and jump to the bootloader
- *      if ((MCUSR & (1<<WDRF)) && (Boot_Key == MAGIC_BOOT_KEY))
+ *      if ((MCUSR & (1 << WDRF)) && (Boot_Key == MAGIC_BOOT_KEY))
  *      {
  *          Boot_Key = 0;
  *          ((void (*)(void))BOOTLOADER_START_ADDRESS)(); 
diff --git a/LUFA/ManPages/WhyUseLUFA.txt b/LUFA/ManPages/WhyUseLUFA.txt
index 093a025e8..b6c93580b 100644
--- a/LUFA/ManPages/WhyUseLUFA.txt
+++ b/LUFA/ManPages/WhyUseLUFA.txt
@@ -40,7 +40,7 @@
  *    into difficulties or need some advice. In addition, you can also email the library author to receive personalized
  *    support when you need it (subject to author's schedule).
  *
- *   <small>* Atmel Stack Mouse Device Demo 4292 bytes, LUFA Mouse Low Level Device Demo 3372 bytes, under identical build
+ *   <small>* Atmel Stack Mouse Device Demo 4292 bytes, LUFA Mouse Low Level Device Demo 3332 bytes, under identical build
  *   environments</small>
  */
  
-- 
GitLab