From 17d06b849440eaab17b0b3778e4dae12182eb98c Mon Sep 17 00:00:00 2001
From: Dean Camera <dean@fourwalledcubicle.com>
Date: Tue, 25 Aug 2009 14:02:47 +0000
Subject: [PATCH] Add VTARGET level detection to the AVRISP project on
 supported AVR models.

---
 Projects/AVRISP/AVRISP.c   | 15 +++++++++++++--
 Projects/AVRISP/AVRISP.h   | 10 +++++++---
 Projects/AVRISP/AVRISP.txt | 10 +++++++++-
 Projects/AVRISP/makefile   |  3 ++-
 4 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/Projects/AVRISP/AVRISP.c b/Projects/AVRISP/AVRISP.c
index f14b6545a..d4ae910f6 100644
--- a/Projects/AVRISP/AVRISP.c
+++ b/Projects/AVRISP/AVRISP.c
@@ -35,7 +35,6 @@
  */
 
 // TODO: Add in software SPI for lower programming speeds below 125KHz
-// TODO: Add in VTARGET detection
 // TODO: Add reversed target connector checks
 
 #include "AVRISP.h"
@@ -53,7 +52,12 @@ int main(void)
 
 	for (;;)
 	{
-		Process_AVRISP_Commands();
+		Process_AVRISP_Commands();
+		
+		#if defined(ADC)
+		/* Update VTARGET parameter with the latest ADC conversion of VTARGET on supported AVR models */
+		V2Params_SetParameterValue(PARAM_VTARGET, ((5 * 10 * ADC_GetResult()) / 1024));
+		#endif
 
 		USB_USBTask();
 	}
@@ -73,6 +77,13 @@ void SetupHardware(void)
 	LEDs_Init();
 	USB_Init();
 
+	#if defined(ADC)
+	/* Initialize the ADC converter for VTARGET level detection on supported AVR models */
+	ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);
+	ADC_SetupChannel(VTARGET_ADC_CHANNEL);
+	ADC_StartReading(VTARGET_ADC_CHANNEL | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC);
+	#endif
+	
 	/* Millisecond timer initialization for timeout checking */
 	OCR0A  = ((F_CPU / 64) / 1000);
 	TCCR0A = (1 << WGM01);
diff --git a/Projects/AVRISP/AVRISP.h b/Projects/AVRISP/AVRISP.h
index d37bf5320..e17fe3009 100644
--- a/Projects/AVRISP/AVRISP.h
+++ b/Projects/AVRISP/AVRISP.h
@@ -46,8 +46,12 @@
 
 		#include <LUFA/Version.h>
 		#include <LUFA/Drivers/Board/LEDs.h>
-		#include <LUFA/Drivers/USB/USB.h>
-
+		#include <LUFA/Drivers/USB/USB.h>
+		
+		#if defined(ADC)
+			#include <LUFA/Drivers/Peripheral/ADC.h>
+		#endif
+		
 		#include "Lib/V2Protocol.h"
 
 	/* Macros: */
@@ -61,7 +65,7 @@
 		#define LEDMASK_USB_READY        (LEDS_LED2 | LEDS_LED4)
 
 		/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
-		#define LEDMASK_USB_ERROR        (LEDS_LED1 | LEDS_LED3)		
+		#define LEDMASK_USB_ERROR        (LEDS_LED1 | LEDS_LED3)
 
 	/* Function Prototypes: */
 		void SetupHardware(void);
diff --git a/Projects/AVRISP/AVRISP.txt b/Projects/AVRISP/AVRISP.txt
index e26cfb11e..8d299e25a 100644
--- a/Projects/AVRISP/AVRISP.txt
+++ b/Projects/AVRISP/AVRISP.txt
@@ -60,9 +60,12 @@
  *
  *  Note that this design currently has several limitations:
  *    - Minimum target clock speed of 500KHz due to hardware SPI used
- *    - No VTARGET detection and notification
  *    - No reversed/shorted target connector detection and notification
  *
+ *  On AVR models with an ADC converter, ACC should be tied to 5V (e.g. VBUS) and the VTARGET_ADC_CHANNEL token should be
+ *  set to an appropriate ADC channel number in the project makefile for VTARGET detection to operate correctly. On models
+ *  without an ADC converter, VTARGET will report at a fixed 5V level.
+ *
  *  \section SSec_Options Project Options
  *
  *  The following defines can be found in this demo, which can control the demo behaviour when defined, or changed in value.
@@ -88,5 +91,10 @@
  *    <td>Makefile CDEFS</td>
  *    <td>Mask for the programmer's target RESET line on the chosen port.</td>
  *   </tr>
+ *   <tr>
+ *    <td>VTARGET_ADC_CHANNEL</td>
+ *    <td>Makefile CDEFS</td>
+ *    <td>ADC channel number (on supported AVRs) to use for VTARGET level detection.</td> 
+ *   </tr>
  *  </table>
  */
diff --git a/Projects/AVRISP/makefile b/Projects/AVRISP/makefile
index 84ca93ab6..5ea1176b6 100644
--- a/Projects/AVRISP/makefile
+++ b/Projects/AVRISP/makefile
@@ -60,7 +60,7 @@
 
 
 # MCU name
-MCU = at90usb647
+MCU = at90usb1287
 
 
 # Target board (see library "Board Types" documentation, USER or blank for projects not requiring
@@ -197,6 +197,7 @@ CDEFS  = -DF_CPU=$(F_CPU)UL -DF_CLOCK=$(F_CLOCK)UL -DBOARD=BOARD_$(BOARD) $(LUFA
 CDEFS += -DRESET_LINE_PORT=PORTB
 CDEFS += -DRESET_LINE_DDR=DDRB
 CDEFS += -DRESET_LINE_MASK="(1 << 4)"
+CDEFS += -DVTARGET_ADC_CHANNEL=2
 
 
 # Place -D or -U options here for ASM sources
-- 
GitLab