Add VTARGET level detection to the AVRISP project on supported AVR models.
authorDean Camera <dean@fourwalledcubicle.com>
Tue, 25 Aug 2009 14:02:47 +0000 (14:02 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Tue, 25 Aug 2009 14:02:47 +0000 (14:02 +0000)
Projects/AVRISP/AVRISP.c
Projects/AVRISP/AVRISP.h
Projects/AVRISP/AVRISP.txt
Projects/AVRISP/makefile

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