Get rid of the redundant ATTR_NEVER_INLINE macro which translated to the same as...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / V2Protocol.c
index a955a95..83b3917 100644 (file)
@@ -1,13 +1,13 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2011.
+     Copyright (C) Dean Camera, 2013.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
 */
 
 /*
-  Copyright 2011  Dean Camera (dean [at] fourwalledcubicle [dot] com)
+  Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com)
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
 
   Permission to use, copy, modify, distribute, and sell this
   software and its documentation for any purpose is hereby granted
@@ -18,7 +18,7 @@
   advertising or publicity pertaining to distribution of the
   software without specific, written prior permission.
 
   advertising or publicity pertaining to distribution of the
   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
@@ -42,30 +42,33 @@ uint32_t CurrentAddress;
 /** Flag to indicate that the next read/write operation must update the device's current extended FLASH address */
 bool MustLoadExtendedAddress;
 
 /** Flag to indicate that the next read/write operation must update the device's current extended FLASH address */
 bool MustLoadExtendedAddress;
 
-/** Command timeout expiration flag. */
-volatile bool TimeoutExpired;
 
 
-#if (ARCH == ARCH_AVR8) // TODO: FIXME
 /** ISR to manage timeouts whilst processing a V2Protocol command */
 /** ISR to manage timeouts whilst processing a V2Protocol command */
-ISR(WDT_vect, ISR_BLOCK)
+ISR(TIMER0_COMPA_vect, ISR_NOBLOCK)
 {
 {
-       TimeoutExpired = true;
-       wdt_disable();
+       if (TimeoutTicksRemaining)
+         TimeoutTicksRemaining--;
+       else
+         TCCR0B = 0;
 }
 }
-#endif
 
 
-/** Initialises the hardware and software associated with the V2 protocol command handling. */
+/** Initializes the hardware and software associated with the V2 protocol command handling. */
 void V2Protocol_Init(void)
 {
 void V2Protocol_Init(void)
 {
-       #if defined(ADC)
+       #if defined(ADC) && !defined(NO_VTARGET_DETECT)
        /* 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);
        /* 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(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK);
+       ADC_StartReading(VTARGET_REF_MASK | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK);
        #endif
 
        #endif
 
+       /* Timeout timer initialization (~10ms period) */
+       OCR0A  = (((F_CPU / 1024) / 100) - 1);
+       TCCR0A = (1 << WGM01);
+       TIMSK0 = (1 << OCIE0A);
+
        V2Params_LoadNonVolatileParamValues();
        V2Params_LoadNonVolatileParamValues();
-       
+
        #if defined(ENABLE_ISP_PROTOCOL)
        ISPTarget_ConfigureRescueClock();
        #endif
        #if defined(ENABLE_ISP_PROTOCOL)
        ISPTarget_ConfigureRescueClock();
        #endif
@@ -79,12 +82,9 @@ void V2Protocol_ProcessCommand(void)
 {
        uint8_t V2Command = Endpoint_Read_8();
 
 {
        uint8_t V2Command = Endpoint_Read_8();
 
-       #if (ARCH == ARCH_AVR8) // TODO: FIXME
-       /* Start the watchdog with timeout interrupt enabled to manage the timeout */
-       TimeoutExpired = false;
-       wdt_enable(WDTO_1S);
-       WDTCSR |= (1 << WDIE);
-       #endif
+       /* Reset timeout counter duration and start the timer */
+       TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
+       TCCR0B = ((1 << CS02) | (1 << CS00));   
 
        switch (V2Command)
        {
 
        switch (V2Command)
        {
@@ -146,13 +146,11 @@ void V2Protocol_ProcessCommand(void)
                        break;
        }
 
                        break;
        }
 
-       #if (ARCH == ARCH_AVR8) // TODO: FIXME
-       /* Disable the timeout management watchdog timer */
-       wdt_disable();
-       #endif
+       /* Disable the timeout management timer */
+       TCCR0B = 0;
 
        Endpoint_WaitUntilReady();
 
        Endpoint_WaitUntilReady();
-       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
 }
 
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
 }
 
@@ -171,7 +169,7 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
        }
 
        Endpoint_ClearOUT();
        }
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(V2Command);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(V2Command);
@@ -183,7 +181,7 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
 static void V2Protocol_SignOn(void)
 {
        Endpoint_ClearOUT();
 static void V2Protocol_SignOn(void)
 {
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(CMD_SIGN_ON);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(CMD_SIGN_ON);
@@ -199,7 +197,7 @@ static void V2Protocol_SignOn(void)
 static void V2Protocol_ResetProtection(void)
 {
        Endpoint_ClearOUT();
 static void V2Protocol_ResetProtection(void)
 {
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(CMD_RESET_PROTECTION);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(CMD_RESET_PROTECTION);
@@ -222,7 +220,7 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
          ParamValue = Endpoint_Read_8();
 
        Endpoint_ClearOUT();
          ParamValue = Endpoint_Read_8();
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(V2Command);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_Write_8(V2Command);
@@ -256,7 +254,7 @@ static void V2Protocol_LoadAddress(void)
        Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NULL);
 
        Endpoint_ClearOUT();
        Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NULL);
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        if (CurrentAddress & (1UL << 31))
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        if (CurrentAddress & (1UL << 31))