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 45d840b..83b3917 100644 (file)
@@ -1,24 +1,24 @@
 /*
              LUFA Library
 /*
              LUFA Library
-     Copyright (C) Dean Camera, 2010.
-              
+     Copyright (C) Dean Camera, 2013.
+
   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 2013  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
@@ -48,24 +48,30 @@ ISR(TIMER0_COMPA_vect, ISR_NOBLOCK)
 {
        if (TimeoutTicksRemaining)
          TimeoutTicksRemaining--;
 {
        if (TimeoutTicksRemaining)
          TimeoutTicksRemaining--;
+       else
+         TCCR0B = 0;
 }
 
 }
 
-/** 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(VTARGET_ADC_CHANNEL_MASK | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC);
+       ADC_StartReading(VTARGET_REF_MASK | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK);
        #endif
        #endif
-       
-       /* Timeout timer initialization (10ms period) */
-       OCR0A  = ((F_CPU / 1024) / 100);
+
+       /* Timeout timer initialization (~10ms period) */
+       OCR0A  = (((F_CPU / 1024) / 100) - 1);
        TCCR0A = (1 << WGM01);
        TIMSK0 = (1 << OCIE0A);
        TCCR0A = (1 << WGM01);
        TIMSK0 = (1 << OCIE0A);
-       
+
        V2Params_LoadNonVolatileParamValues();
        V2Params_LoadNonVolatileParamValues();
+
+       #if defined(ENABLE_ISP_PROTOCOL)
+       ISPTarget_ConfigureRescueClock();
+       #endif
 }
 
 /** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.
 }
 
 /** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.
@@ -74,12 +80,12 @@ void V2Protocol_Init(void)
  */
 void V2Protocol_ProcessCommand(void)
 {
  */
 void V2Protocol_ProcessCommand(void)
 {
-       uint8_t V2Command = Endpoint_Read_Byte();
-       
-       /* Start the timeout management timer */
+       uint8_t V2Command = Endpoint_Read_8();
+
+       /* Reset timeout counter duration and start the timer */
        TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
        TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
-       TCCR0B = ((1 << CS02) | (1 << CS00));
-       
+       TCCR0B = ((1 << CS02) | (1 << CS00));   
+
        switch (V2Command)
        {
                case CMD_SIGN_ON:
        switch (V2Command)
        {
                case CMD_SIGN_ON:
@@ -104,7 +110,7 @@ void V2Protocol_ProcessCommand(void)
                        break;
                case CMD_PROGRAM_FLASH_ISP:
                case CMD_PROGRAM_EEPROM_ISP:
                        break;
                case CMD_PROGRAM_FLASH_ISP:
                case CMD_PROGRAM_EEPROM_ISP:
-                       ISPProtocol_ProgramMemory(V2Command);                   
+                       ISPProtocol_ProgramMemory(V2Command);
                        break;
                case CMD_READ_FLASH_ISP:
                case CMD_READ_EEPROM_ISP:
                        break;
                case CMD_READ_FLASH_ISP:
                case CMD_READ_EEPROM_ISP:
@@ -139,12 +145,12 @@ void V2Protocol_ProcessCommand(void)
                        V2Protocol_UnknownCommand(V2Command);
                        break;
        }
                        V2Protocol_UnknownCommand(V2Command);
                        break;
        }
-       
+
        /* Disable the timeout management timer */
        TCCR0B = 0;
 
        Endpoint_WaitUntilReady();
        /* Disable the timeout management timer */
        TCCR0B = 0;
 
        Endpoint_WaitUntilReady();
-       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
 }
 
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);
 }
 
@@ -163,11 +169,11 @@ 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_SetEndpointDirection(ENDPOINT_DIR_IN);
 
-       Endpoint_Write_Byte(V2Command);
-       Endpoint_Write_Byte(STATUS_CMD_UNKNOWN);
+       Endpoint_Write_8(V2Command);
+       Endpoint_Write_8(STATUS_CMD_UNKNOWN);
        Endpoint_ClearIN();
 }
 
        Endpoint_ClearIN();
 }
 
@@ -175,13 +181,13 @@ 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_SetEndpointDirection(ENDPOINT_DIR_IN);
 
-       Endpoint_Write_Byte(CMD_SIGN_ON);
-       Endpoint_Write_Byte(STATUS_CMD_OK);
-       Endpoint_Write_Byte(sizeof(PROGRAMMER_ID) - 1);
-       Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NO_STREAM_CALLBACK);
+       Endpoint_Write_8(CMD_SIGN_ON);
+       Endpoint_Write_8(STATUS_CMD_OK);
+       Endpoint_Write_8(sizeof(PROGRAMMER_ID) - 1);
+       Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NULL);
        Endpoint_ClearIN();
 }
 
        Endpoint_ClearIN();
 }
 
@@ -191,12 +197,12 @@ 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_SetEndpointDirection(ENDPOINT_DIR_IN);
-       
-       Endpoint_Write_Byte(CMD_RESET_PROTECTION);
-       Endpoint_Write_Byte(STATUS_CMD_OK);
-       Endpoint_ClearIN();     
+
+       Endpoint_Write_8(CMD_RESET_PROTECTION);
+       Endpoint_Write_8(STATUS_CMD_OK);
+       Endpoint_ClearIN();
 }
 
 
 }
 
 
@@ -207,33 +213,33 @@ static void V2Protocol_ResetProtection(void)
  */
 static void V2Protocol_GetSetParam(const uint8_t V2Command)
 {
  */
 static void V2Protocol_GetSetParam(const uint8_t V2Command)
 {
-       uint8_t ParamID = Endpoint_Read_Byte();
+       uint8_t ParamID = Endpoint_Read_8();
        uint8_t ParamValue;
        uint8_t ParamValue;
-       
+
        if (V2Command == CMD_SET_PARAMETER)
        if (V2Command == CMD_SET_PARAMETER)
-         ParamValue = Endpoint_Read_Byte();
+         ParamValue = Endpoint_Read_8();
 
        Endpoint_ClearOUT();
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
-       
-       Endpoint_Write_Byte(V2Command);
-       
+
+       Endpoint_Write_8(V2Command);
+
        uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
        uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
-       
+
        if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
        {
        if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
        {
-               Endpoint_Write_Byte(STATUS_CMD_OK);
+               Endpoint_Write_8(STATUS_CMD_OK);
                V2Params_SetParameterValue(ParamID, ParamValue);
        }
        else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ))
        {
                V2Params_SetParameterValue(ParamID, ParamValue);
        }
        else if ((V2Command == CMD_GET_PARAMETER) && (ParamPrivs & PARAM_PRIV_READ))
        {
-               Endpoint_Write_Byte(STATUS_CMD_OK);
-               Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID));
+               Endpoint_Write_8(STATUS_CMD_OK);
+               Endpoint_Write_8(V2Params_GetParameterValue(ParamID));
        }
        else
        }
        else
-       {       
-               Endpoint_Write_Byte(STATUS_CMD_FAILED);
+       {
+               Endpoint_Write_8(STATUS_CMD_FAILED);
        }
 
        Endpoint_ClearIN();
        }
 
        Endpoint_ClearIN();
@@ -245,16 +251,17 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
  */
 static void V2Protocol_LoadAddress(void)
 {
  */
 static void V2Protocol_LoadAddress(void)
 {
-       Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NO_STREAM_CALLBACK);
+       Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NULL);
 
        Endpoint_ClearOUT();
 
        Endpoint_ClearOUT();
-       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPADDR);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
-       
+
        if (CurrentAddress & (1UL << 31))
          MustLoadExtendedAddress = true;
 
        if (CurrentAddress & (1UL << 31))
          MustLoadExtendedAddress = true;
 
-       Endpoint_Write_Byte(CMD_LOAD_ADDRESS);
-       Endpoint_Write_Byte(STATUS_CMD_OK);
+       Endpoint_Write_8(CMD_LOAD_ADDRESS);
+       Endpoint_Write_8(STATUS_CMD_OK);
        Endpoint_ClearIN();
 }
        Endpoint_ClearIN();
 }
+