From 021b1b567e8686d4addccb53511b7f5447392267 Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Wed, 16 Dec 2009 09:13:42 +0000
Subject: More speed and quality improvements to the software USART in the
 AVRISP project.

---
 Projects/AVRISP/Lib/NVMTarget.c | 18 +-----------------
 Projects/AVRISP/Lib/NVMTarget.h | 14 ++++++++++++++
 Projects/AVRISP/Lib/PDITarget.c | 17 ++++++++---------
 Projects/AVRISP/Lib/PDITarget.h | 12 +-----------
 Projects/AVRISP/makefile        |  2 +-
 5 files changed, 25 insertions(+), 38 deletions(-)

diff --git a/Projects/AVRISP/Lib/NVMTarget.c b/Projects/AVRISP/Lib/NVMTarget.c
index 10a911ff3..c4de1d2bd 100644
--- a/Projects/AVRISP/Lib/NVMTarget.c
+++ b/Projects/AVRISP/Lib/NVMTarget.c
@@ -48,23 +48,7 @@ void NVMTarget_SendNVMRegAddress(uint8_t Register)
 	uint32_t Address = XPROG_Param_NVMBase | Register;
 
 	/* Send the calculated 32-bit address to the target, LSB first */
-	PDITarget_SendByte(Address &  0xFF);
-	PDITarget_SendByte(Address >> 8);
-	PDITarget_SendByte(Address >> 16);
-	PDITarget_SendByte(Address >> 24);
-}
-
-/** Sends the given 32-bit absolute address to the target.
- *
- *  \param[in] AbsoluteAddress  Absolute address to send to the target
- */
-void NVMTarget_SendAddress(uint32_t AbsoluteAddress)
-{
-	/* Send the given 32-bit address to the target, LSB first */
-	PDITarget_SendByte(AbsoluteAddress &  0xFF);
-	PDITarget_SendByte(AbsoluteAddress >> 8);
-	PDITarget_SendByte(AbsoluteAddress >> 16);
-	PDITarget_SendByte(AbsoluteAddress >> 24);
+	NVMTarget_SendAddress(Address);
 }
 
 /** Waits while the target's NVM controller is busy performing an operation, exiting if the
diff --git a/Projects/AVRISP/Lib/NVMTarget.h b/Projects/AVRISP/Lib/NVMTarget.h
index e9acd4375..803ec3cb6 100644
--- a/Projects/AVRISP/Lib/NVMTarget.h
+++ b/Projects/AVRISP/Lib/NVMTarget.h
@@ -105,6 +105,20 @@
 		#define NVM_CMD_ERASEWRITEEEPROMPAGE   0x35
 		#define NVM_CMD_READEEPROM             0x06
 
+	/* Inline Functions: */
+		/** Sends the given 32-bit absolute address to the target.
+		 *
+		 *  \param[in] AbsoluteAddress  Absolute address to send to the target
+		 */
+		static inline void NVMTarget_SendAddress(uint32_t AbsoluteAddress)
+		{
+			/* Send the given 32-bit address to the target, LSB first */
+			PDITarget_SendByte(AbsoluteAddress &  0xFF);
+			PDITarget_SendByte(AbsoluteAddress >> 8);
+			PDITarget_SendByte(AbsoluteAddress >> 16);
+			PDITarget_SendByte(AbsoluteAddress >> 24);
+		}
+
 	/* Function Prototypes: */
 		void NVMTarget_SendNVMRegAddress(uint8_t Register);
 		void NVMTarget_SendAddress(uint32_t AbsoluteAddress);
diff --git a/Projects/AVRISP/Lib/PDITarget.c b/Projects/AVRISP/Lib/PDITarget.c
index e21942a1f..afcf8b81f 100644
--- a/Projects/AVRISP/Lib/PDITarget.c
+++ b/Projects/AVRISP/Lib/PDITarget.c
@@ -196,19 +196,18 @@ void PDITarget_SendByte(uint8_t Byte)
 		IsSending = true;
 	}
 
-	bool    EvenParityBit = false;
-	uint8_t ParityData    = Byte;
+	/* Calculate the new USART frame data here while while we wait for a previous byte (if any) to finish sending */
+	uint16_t NewUSARTData = ((1 << 11) | (1 << 10) | (0 << 9) | ((uint16_t)Byte << 1) | (0 << 0));
 
 	/* Compute Even parity - while a bit is still set, chop off lowest bit and toggle parity bit */
+	uint8_t ParityData    = Byte;
 	while (ParityData)
 	{
-		EvenParityBit ^= true;
-		ParityData    &= (ParityData - 1);
+		NewUSARTData ^= (1 << 9);
+		ParityData   &= (ParityData - 1);
 	}
 
-	/* Calculate the new USART frame data here while while we wait for a previous byte (if any) to finish sending */
-	uint16_t NewUSARTData = ((1 << 11) | (1 << 10) | ((uint16_t)EvenParityBit << 9) | ((uint16_t)Byte << 1) | (0 << 0));
-
+	/* Wait until transmitter is idle before writing new data */
 	while (SoftUSART_BitCount);
 
 	/* Data shifted out LSB first, START DATA PARITY STOP STOP */
@@ -258,7 +257,7 @@ uint8_t PDITarget_ReceiveByte(void)
 	SoftUSART_BitCount = BITS_IN_FRAME;
 	while (SoftUSART_BitCount);
 	
-	/* Throw away the start, parity and stop bits to leave only the data */
+	/* Throw away the parity and stop bits to leave only the data (start bit is already discarded) */
 	return (uint8_t)SoftUSART_Data;
 #endif
 }
@@ -280,7 +279,7 @@ void PDITarget_SendBreak(void)
 	}
 
 	/* Need to do nothing for a full frame to send a BREAK */
-	for (uint8_t i = 0; i <= BITS_IN_FRAME; i++)
+	for (uint8_t i = 0; i < BITS_IN_FRAME; i++)
 	{
 		/* Wait for a full cycle of the clock */
 		while (PIND & (1 << 5));
diff --git a/Projects/AVRISP/Lib/PDITarget.h b/Projects/AVRISP/Lib/PDITarget.h
index 2a704b3d8..03083fcb6 100644
--- a/Projects/AVRISP/Lib/PDITarget.h
+++ b/Projects/AVRISP/Lib/PDITarget.h
@@ -54,17 +54,7 @@
 
 	/* Defines: */
 		#if ((BOARD == BOARD_XPLAIN) || (BOARD == BOARD_XPLAIN_REV1))
-//			#define PDI_VIA_HARDWARE_USART
-
-			#define BITBANG_PDIDATA_PORT     PORTD
-			#define BITBANG_PDIDATA_DDR      DDRD
-			#define BITBANG_PDIDATA_PIN      PIND
-			#define BITBANG_PDIDATA_MASK     (1 << 3)
-			
-			#define BITBANG_PDICLOCK_PORT    PORTD
-			#define BITBANG_PDICLOCK_DDR     DDRD
-			#define BITBANG_PDICLOCK_PIN     PIND
-			#define BITBANG_PDICLOCK_MASK    (1 << 5)
+			#define PDI_VIA_HARDWARE_USART
 		#else
 			#define BITBANG_PDIDATA_PORT     PORTB
 			#define BITBANG_PDIDATA_DDR      DDRB
diff --git a/Projects/AVRISP/makefile b/Projects/AVRISP/makefile
index 23e27c52d..2b6a1e858 100644
--- a/Projects/AVRISP/makefile
+++ b/Projects/AVRISP/makefile
@@ -66,7 +66,7 @@ MCU = at90usb1287
 # Target board (see library "Board Types" documentation, USER or blank 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 = XPLAIN
+BOARD = USBKEY
 
 
 # Processor frequency.
-- 
cgit v1.2.3