From a9f313afa9a65a692718b8609fd47ae1dcb60523 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Tue, 16 Nov 2010 12:30:00 +0000
Subject: [PATCH] Slow AVRISP-MKII clone PDI/TPI programming speed back to
 250KHz due to issues with the XPLAINBridge when PDI programming at faster
 rates.

Speed up XMEGANVM_WaitWhileNVMControllerBusy() by preloading the status register address into the pointer register of the target's NVM controller and then reading via a faster indirect load command.
---
 LUFA/ManPages/ChangeLog.txt                  |  1 -
 Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c    | 10 ++++++----
 Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h |  2 +-
 Projects/AVRISP-MKII/makefile                |  6 +++---
 4 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/LUFA/ManPages/ChangeLog.txt b/LUFA/ManPages/ChangeLog.txt
index 0afdd4aba..1297202af 100644
--- a/LUFA/ManPages/ChangeLog.txt
+++ b/LUFA/ManPages/ChangeLog.txt
@@ -102,7 +102,6 @@
   *  - Fixed USBtoSerial and XPLAINBridge demos discarding data from the PC if the send buffer becomes full
   *  - Fixed broken input in the MagStripe reader project due to an incorrect HID report descriptor
   *  - Fixed incorrect PollingIntervalMS values in the demo/project/bootloader endpoint descriptors (thanks to MCS Electronics)
-  *  - Fixed PDI/TPI programming speed of ~250KHz in the AVRISP-MKII Clone project, instead of the desired 500KHz (thanks to Tom Light)
   *
   *  \section Sec_ChangeLog100807 Version 100807
   *  <b>New:</b>
diff --git a/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c b/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c
index ce02ebe95..60e755265 100644
--- a/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c
+++ b/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c
@@ -96,13 +96,15 @@ bool XMEGANVM_WaitWhileNVMBusBusy(void)
  */
 bool XMEGANVM_WaitWhileNVMControllerBusy(void)
 {
+	/* Preload the pointer register with the NVM STATUS register address to check the BUSY flag */
+	XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES);
+	XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS);
+
 	/* Poll the NVM STATUS register while the NVM controller is busy */
 	for (;;)
 	{
-		/* Send a LDS command to read the NVM STATUS register to check the BUSY flag */
-		XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));
-		XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS);
-
+		/* Fetch the current status value via the pointer register (without auto-increment afterwards) */
+		XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT << 2) | PDI_DATSIZE_1BYTE);
 		uint8_t StatusRegister = XPROGTarget_ReceiveByte();
 
 		/* We might have timed out waiting for the status register read response, check here */
diff --git a/Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h b/Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h
index 8d7181aa2..9f51b2738 100644
--- a/Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h
+++ b/Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.h
@@ -56,7 +56,7 @@
 		#endif
 
 		/** Serial carrier TPI/PDI speed when hardware TPI/PDI mode is used. */
-		#define XPROG_HARDWARE_SPEED       500000
+		#define XPROG_HARDWARE_SPEED       250000
 
 		/** Total number of bits in a single USART frame. */
 		#define BITS_IN_USART_FRAME        12
diff --git a/Projects/AVRISP-MKII/makefile b/Projects/AVRISP-MKII/makefile
index 5135722fd..28f893956 100644
--- a/Projects/AVRISP-MKII/makefile
+++ b/Projects/AVRISP-MKII/makefile
@@ -60,13 +60,13 @@
 
 
 # MCU name
-MCU = at90usb1287
+MCU = at90usb162
 
 
 # Target board (see library "Board Types" documentation, NONE for projects not requiring
 # LUFA board drivers). If USER is selected, put custom board drivers in a directory called
 # "Board" inside the application directory.
-BOARD = USBKEY
+BOARD = USBTINYMKII
 
 
 # Processor frequency.
@@ -80,7 +80,7 @@ BOARD = USBKEY
 #     does not *change* the processor frequency - it should merely be updated to
 #     reflect the processor speed set externally so that the code can use accurate
 #     software delays.
-F_CPU = 8000000
+F_CPU = 16000000
 
 
 # Input clock frequency.
-- 
GitLab