From 8cd7e118e950d18ec0dd0d716005930ec45fd299 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Sat, 26 Dec 2009 05:20:10 +0000
Subject: [PATCH] Fixed ISP mode in the AVRISP programmer project.

---
 LUFA/ManPages/ChangeLog.txt            |  1 +
 Projects/AVRISP/Lib/ISP/ISPProtocol.h  | 19 ++++---------------
 Projects/AVRISP/Lib/ISP/ISPTarget.c    |  3 +++
 Projects/AVRISP/Lib/V2ProtocolParams.c | 12 ++++++++----
 Projects/AVRISP/makefile               |  2 +-
 5 files changed, 17 insertions(+), 20 deletions(-)

diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index 48361e307..41e82ea0c 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -17,6 +17,7 @@
   *  - Slowed down bit-banged PDI programming in the AVRISP project slightly to prevent transmission errors
   *
   *  <b>Fixed:</b>
+  *  - Fixed AVRISP project not able to enter programming mode when ISP protocol is used
   *
   *  \section Sec_ChangeLog091223 Version 091223
   *
diff --git a/Projects/AVRISP/Lib/ISP/ISPProtocol.h b/Projects/AVRISP/Lib/ISP/ISPProtocol.h
index abbbf569d..127c8266c 100644
--- a/Projects/AVRISP/Lib/ISP/ISPProtocol.h
+++ b/Projects/AVRISP/Lib/ISP/ISPProtocol.h
@@ -38,6 +38,7 @@
 
 	/* Includes: */
 		#include <avr/io.h>
+		#include <util/delay.h>
 		
 		#include <LUFA/Drivers/USB/USB.h>
 
@@ -66,26 +67,14 @@
 		#define PROG_MODE_COMMIT_PAGE_MASK      (1 << 7)
 
 	/* Inline Functions: */
-		/** Blocking delay for a given number of milliseconds, via a hardware timer.
+		/** Blocking delay for a given number of milliseconds.
 		 *
 		 *  \param[in] DelayMS  Number of milliseconds to delay for
 		 */
 		static inline void ISPProtocol_DelayMS(uint8_t DelayMS)
 		{
-			OCR2A  = ((F_CPU / 64) / 1000);
-			TCCR2A = (1 << WGM01);
-			TCCR2B = ((1 << CS01) | (1 << CS00));			
-
-			while (DelayMS)
-			{
-				if (TIFR2 & (1 << OCF2A))
-				{
-					TIFR2 = (1 << OCF2A);
-					DelayMS--;
-				}
-			}
-			
-			TCCR2B = 0;			
+			while (DelayMS--)
+			  _delay_ms(1);
 		}
 
 	/* Function Prototypes: */
diff --git a/Projects/AVRISP/Lib/ISP/ISPTarget.c b/Projects/AVRISP/Lib/ISP/ISPTarget.c
index 29cb18e62..a3a8b2289 100644
--- a/Projects/AVRISP/Lib/ISP/ISPTarget.c
+++ b/Projects/AVRISP/Lib/ISP/ISPTarget.c
@@ -122,6 +122,9 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
 			break;
 		case PROG_MODE_WORD_VALUE_MASK:
 		case PROG_MODE_PAGED_VALUE_MASK:
+			TCNT0 = 0;
+			TIFR0 = (1 << OCF1A);
+			
 			do
 			{
 				SPI_SendByte(ReadMemCommand);
diff --git a/Projects/AVRISP/Lib/V2ProtocolParams.c b/Projects/AVRISP/Lib/V2ProtocolParams.c
index eacff0394..105633560 100644
--- a/Projects/AVRISP/Lib/V2ProtocolParams.c
+++ b/Projects/AVRISP/Lib/V2ProtocolParams.c
@@ -131,7 +131,7 @@ uint8_t V2Params_GetParameterValue(const uint8_t ParamID)
 {
 	ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
 	
-	if ((ParamInfo == NULL) || !(ParamInfo->ParamPrivileges & PARAM_PRIV_READ))
+	if (ParamInfo == NULL)
 	  return 0;
 	
 	return ParamInfo->ParamValue;
@@ -148,7 +148,7 @@ void V2Params_SetParameterValue(const uint8_t ParamID, const uint8_t Value)
 {
 	ParameterItem_t* ParamInfo = V2Params_GetParamFromTable(ParamID);
 
-	if ((ParamInfo == NULL) || !(ParamInfo->ParamPrivileges & PARAM_PRIV_WRITE))
+	if (ParamInfo == NULL)
 	  return;
 
 	ParamInfo->ParamValue = Value;
@@ -167,11 +167,15 @@ void V2Params_SetParameterValue(const uint8_t ParamID, const uint8_t Value)
  */
 static ParameterItem_t* V2Params_GetParamFromTable(const uint8_t ParamID)
 {
+	ParameterItem_t* CurrTableItem = ParameterTable;
+
 	/* Find the parameter in the parameter table if present */
 	for (uint8_t TableIndex = 0; TableIndex < (sizeof(ParameterTable) / sizeof(ParameterTable[0])); TableIndex++)
 	{
-		if (ParamID == ParameterTable[TableIndex].ParamID)
-		  return &ParameterTable[TableIndex];
+		if (ParamID == CurrTableItem->ParamID)
+		  return CurrTableItem;
+		
+		CurrTableItem++;
 	}
 
 	return NULL;
diff --git a/Projects/AVRISP/makefile b/Projects/AVRISP/makefile
index f5bb5b712..823535f45 100644
--- a/Projects/AVRISP/makefile
+++ b/Projects/AVRISP/makefile
@@ -60,7 +60,7 @@
 
 
 # MCU name
-MCU = at90usb1287
+MCU = at90usb162
 
 
 # Target board (see library "Board Types" documentation, USER or blank for projects not requiring
-- 
GitLab