From 34047efc7ee3bd765574d2ce709712ff27c7b2fd Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Thu, 13 May 2010 12:57:49 +0000
Subject: [PATCH] Remove incorrect check for the current device state in the
 Set Configuration request handler of DevChapter9, which broke Set
 Configuration requests to the device under most circumstances.

Move out the SPI prescaler list to a PROGMEM module-level variable in ISPTarget.c.
---
 LUFA/Drivers/USB/LowLevel/DevChapter9.c  |  3 --
 LUFA/ManPages/ChangeLog.txt              |  2 +
 Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c | 49 ++++++++++++------------
 Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h |  1 +
 4 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/LUFA/Drivers/USB/LowLevel/DevChapter9.c b/LUFA/Drivers/USB/LowLevel/DevChapter9.c
index f44cf0ba41..1529b6f138 100644
--- a/LUFA/Drivers/USB/LowLevel/DevChapter9.c
+++ b/LUFA/Drivers/USB/LowLevel/DevChapter9.c
@@ -145,9 +145,6 @@ static void USB_Device_SetAddress(void)
 
 static void USB_Device_SetConfiguration(void)
 {
-	if (USB_DeviceState != DEVICE_STATE_Addressed)
-	  return;
-	
 #if defined(FIXED_NUM_CONFIGURATIONS)
 	if ((uint8_t)USB_ControlRequest.wValue > FIXED_NUM_CONFIGURATIONS)
 	  return;
diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index eb8c1e4a6c..f04fbe437f 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -20,6 +20,8 @@
   *  - Fixed AVRISP-MKII clone project not correctly issuing LOAD EXTENDED ADDRESS commands when the extended address
   *    boundary is crossed during programming or readback (thanks to Gerard Sexton)
   *  - Fixed warnings when building the AVRISP-MKII clone project with the ENABLE_XPROG_PROTOCOL compile time option disabled
+  *  - Remove incorrect check for the current device state in the Set Configuration request handler of DevChapter9, which broke
+  *    Set Configuration requests to the device under most circumstances.
   *
   *  \section Sec_ChangeLog100512 Version 100512
   *
diff --git a/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c b/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c
index d7c6882cd3..9a74aabb22 100644
--- a/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c
+++ b/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c
@@ -37,6 +37,30 @@
 
 #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
 
+/** List of SPI prescaler masks for possible AVRStudio ISP programming speeds. */
+static uint8_t SPIMaskFromSCKDuration[] PROGMEM =
+{
+#if (F_CPU == 8000000)
+	SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   4MHz SPI
+	SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
+	SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
+	SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
+	SPI_SPEED_FCPU_DIV_16,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
+	SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
+	SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
+#elif (F_CPU == 16000000)
+	SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   8MHz SPI
+	SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
+	SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
+	SPI_SPEED_FCPU_DIV_16,   // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
+	SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
+	SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
+	SPI_SPEED_FCPU_DIV_128   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
+#else
+	#error No SPI prescaler masks for chosen F_CPU speed.
+#endif
+};
+
 /** Converts the given AVR Studio SCK duration parameter (set by a SET PARAM command from the host) to the nearest
  *  possible SPI clock prescaler mask for passing to the SPI_Init() routine.
  *
@@ -44,35 +68,12 @@
  */
 uint8_t ISPTarget_GetSPIPrescalerMask(void)
 {
-	static const uint8_t SPIMaskFromSCKDuration[] =
-	{
-	#if (F_CPU == 8000000)
-		SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   4MHz SPI
-		SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
-		SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
-		SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
-		SPI_SPEED_FCPU_DIV_16,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
-		SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
-		SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
-	#elif (F_CPU == 16000000)
-		SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   8MHz SPI
-		SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
-		SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
-		SPI_SPEED_FCPU_DIV_16,   // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
-		SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
-		SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
-		SPI_SPEED_FCPU_DIV_128   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
-	#else
-		#error No SPI prescaler masks for chosen F_CPU speed.
-	#endif
-	};
-
 	uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);
 
 	if (SCKDuration >= sizeof(SPIMaskFromSCKDuration))
 	  SCKDuration = (sizeof(SPIMaskFromSCKDuration) - 1);
 	  
-	return SPIMaskFromSCKDuration[SCKDuration];
+	return pgm_read_byte(&SPIMaskFromSCKDuration[SCKDuration]);
 }
 
 /** Asserts or deasserts the target's reset line, using the correct polarity as set by the host using a SET PARAM command.
diff --git a/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h b/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h
index d6e943582a..705dd39908 100644
--- a/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h
+++ b/Projects/AVRISP-MKII/Lib/ISP/ISPTarget.h
@@ -38,6 +38,7 @@
 
 	/* Includes: */
 		#include <avr/io.h>
+		#include <avr/pgmspace.h>
 		#include <util/delay.h>
 
 		#include <LUFA/Drivers/USB/USB.h>