Update Studio Integration DLL, to include package logging.
[pub/USBasp.git] / Projects / AVRISP-MKII / AVRISP-MKII.c
index 7293906..6d4bc89 100644 (file)
@@ -1,24 +1,24 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2015.
+
   dean [at] fourwalledcubicle [dot] com
   dean [at] fourwalledcubicle [dot] com
-      www.fourwalledcubicle.com
+           www.lufa-lib.org
 */
 
 /*
 */
 
 /*
-  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2015  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
 
-  Permission to use, copy, modify, distribute, and sell this 
+  Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
   software and its documentation for any purpose is hereby granted
-  without fee, provided that the above copyright notice appear in 
+  without fee, provided that the above copyright notice appear in
   all copies and that both that the copyright notice and this
   all copies and that both that the copyright notice and this
-  permission notice and warranty disclaimer appear in supporting 
-  documentation, and that the name of the author not be used in 
-  advertising or publicity pertaining to distribution of the 
+  permission notice and warranty disclaimer appear in supporting
+  documentation, and that the name of the author not be used in
+  advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   software without specific, written prior permission.
 
-  The author disclaim all warranties with regard to this
+  The author disclaims all warranties with regard to this
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
   software, including all implied warranties of merchantability
   and fitness.  In no event shall the author be liable for any
   special, indirect or consequential damages or any damages
 
 #include "AVRISP-MKII.h"
 
 
 #include "AVRISP-MKII.h"
 
+#if (BOARD != BOARD_NONE)
+  /* Some board hardware definitions (e.g. the Arduino Micro) have their LEDs defined on the same pins
+     as the ISP, PDI or TPI interfaces (see the accompanying project documentation). If a board other
+     than NONE is selected (to enable the LED driver with the programmer) you should double-check that
+     no conflicts will occur. If there is a conflict, turn off the LEDs (set BOARD to NONE in the makefile)
+     or define a custom board driver (see the LUFA manual) with alternative LED mappings.
+  */
+  #warning Board specific drivers have been selected; make sure the board LED driver does not conflict with the programmer ISP/PDI/TPI interfaces.
+#endif
+
 /** Main program entry point. This routine contains the overall program flow, including initial
  *  setup of all components and the main program loop.
  */
 /** Main program entry point. This routine contains the overall program flow, including initial
  *  setup of all components and the main program loop.
  */
@@ -43,12 +53,18 @@ int main(void)
 {
        SetupHardware();
        V2Protocol_Init();
 {
        SetupHardware();
        V2Protocol_Init();
-       
+
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
        LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
-       sei();
+       GlobalInterruptEnable();
 
        for (;;)
        {
 
        for (;;)
        {
+               #if (BOARD == BOARD_USBTINYMKII)
+               /* On the USBTINY-MKII target, there is a secondary LED which indicates the current selected power
+                  mode - either VBUS, or sourced from the VTARGET pin of the programming connectors */
+               LEDs_ChangeLEDs(LEDMASK_VBUSPOWER, (PIND & (1 << 0)) ? 0 : LEDMASK_VBUSPOWER);
+               #endif
+
                AVRISP_Task();
                USB_USBTask();
        }
                AVRISP_Task();
                USB_USBTask();
        }
@@ -57,15 +73,22 @@ int main(void)
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
 /** Configures the board hardware and chip peripherals for the demo's functionality. */
 void SetupHardware(void)
 {
+#if (ARCH == ARCH_AVR8)
        /* Disable watchdog if enabled by bootloader/fuses */
        MCUSR &= ~(1 << WDRF);
        wdt_disable();
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
        /* Disable watchdog if enabled by bootloader/fuses */
        MCUSR &= ~(1 << WDRF);
        wdt_disable();
 
        /* Disable clock division */
        clock_prescale_set(clock_div_1);
+#endif
 
        /* Hardware Initialization */
        LEDs_Init();
 
        /* Hardware Initialization */
        LEDs_Init();
+       #if defined(RESET_TOGGLES_LIBUSB_COMPAT)
+       UpdateCurrentCompatibilityMode();
+       #endif
+
+       /* USB Stack Initialization */
        USB_Init();
 }
 
        USB_Init();
 }
 
@@ -84,25 +107,17 @@ void EVENT_USB_Device_Disconnect(void)
 /** Event handler for the library USB Configuration Changed event. */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
 /** Event handler for the library USB Configuration Changed event. */
 void EVENT_USB_Device_ConfigurationChanged(void)
 {
-       /* Indicate USB connected and ready */
-       LEDs_SetAllLEDs(LEDMASK_USB_READY);
+       bool ConfigSuccess = true;
 
 
-       /* Setup AVRISP data Endpoints */
-       if (!(Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPNUM, EP_TYPE_BULK,
-                                        ENDPOINT_DIR_OUT, AVRISP_DATA_EPSIZE,
-                                        ENDPOINT_BANK_SINGLE)))
-       {
-               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
-       }
+       /* Setup AVRISP Data OUT endpoint */
+       ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_OUT_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
 
 
-       #if defined(LIBUSB_DRIVER_COMPAT)
-       if (!(Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPNUM, EP_TYPE_BULK,
-                                        ENDPOINT_DIR_IN, AVRISP_DATA_EPSIZE,
-                                        ENDPOINT_BANK_SINGLE)))
-       {
-               LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
-       }
-       #endif
+       /* Setup AVRISP Data IN endpoint if it is using a physically different endpoint */
+       if ((AVRISP_DATA_IN_EPADDR & ENDPOINT_EPNUM_MASK) != (AVRISP_DATA_OUT_EPADDR & ENDPOINT_EPNUM_MASK))
+         ConfigSuccess &= Endpoint_ConfigureEndpoint(AVRISP_DATA_IN_EPADDR, EP_TYPE_BULK, AVRISP_DATA_EPSIZE, 1);
+
+       /* Indicate endpoint configuration success or failure */
+       LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR);
 }
 
 /** Processes incoming V2 Protocol commands from the host, returning a response when required. */
 }
 
 /** Processes incoming V2 Protocol commands from the host, returning a response when required. */
@@ -114,8 +129,8 @@ void AVRISP_Task(void)
 
        V2Params_UpdateParamValues();
 
 
        V2Params_UpdateParamValues();
 
-       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);
-       
+       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
+
        /* Check to see if a V2 Protocol command has been received */
        if (Endpoint_IsOUTReceived())
        {
        /* Check to see if a V2 Protocol command has been received */
        if (Endpoint_IsOUTReceived())
        {
@@ -128,3 +143,24 @@ void AVRISP_Task(void)
        }
 }
 
        }
 }
 
+/** This function is called by the library when in device mode, and must be overridden (see library "USB Descriptors"
+ *  documentation) by the application code so that the address and size of a requested descriptor can be given
+ *  to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
+ *  is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
+ *  USB host.
+ *
+ *  \param[in]  wValue                 Descriptor type and index to retrieve
+ *  \param[in]  wIndex                 Sub-index to retrieve (such as a localized string language)
+ *  \param[out] DescriptorAddress      Address of the retrieved descriptor
+ *  \param[out] DescriptorMemorySpace  Memory space that the descriptor is stored in
+ *
+ *  \return Length of the retrieved descriptor in bytes, or NO_DESCRIPTOR if the descriptor was not found
+ */
+uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
+                                    const uint8_t wIndex,
+                                    const void** const DescriptorAddress,
+                                    uint8_t* DescriptorMemorySpace)
+{
+       return AVRISP_GetDescriptor(wValue, wIndex, DescriptorAddress, DescriptorMemorySpace);
+}
+