Documentation improvements - put driver example code into its own section, fix incorr...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / V2Protocol.c
index 9e210c2..618dedb 100644 (file)
@@ -1,21 +1,21 @@
 /*
              LUFA Library
      Copyright (C) Dean Camera, 2010.
 /*
              LUFA Library
      Copyright (C) Dean Camera, 2010.
-              
+
   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 2010  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.
 
   The author disclaim all warranties with regard to this
   software without specific, written prior permission.
 
   The author disclaim all warranties with regard to this
@@ -50,22 +50,26 @@ ISR(TIMER0_COMPA_vect, ISR_NOBLOCK)
          TimeoutTicksRemaining--;
 }
 
          TimeoutTicksRemaining--;
 }
 
-/** Initializes the hardware and software associated with the V2 protocol command handling. */
+/** Initialises the hardware and software associated with the V2 protocol command handling. */
 void V2Protocol_Init(void)
 {
        #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);
 void V2Protocol_Init(void)
 {
        #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_MASK | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC);
+       ADC_StartReading(ADC_REFERENCE_AVCC | ADC_RIGHT_ADJUSTED | VTARGET_ADC_CHANNEL_MASK);
        #endif
        #endif
-       
+
        /* Timeout timer initialization (10ms period) */
        /* Timeout timer initialization (10ms period) */
-       OCR0A  = ((F_CPU / 1024) / 100);
+       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.
@@ -75,11 +79,11 @@ void V2Protocol_Init(void)
 void V2Protocol_ProcessCommand(void)
 {
        uint8_t V2Command = Endpoint_Read_Byte();
 void V2Protocol_ProcessCommand(void)
 {
        uint8_t V2Command = Endpoint_Read_Byte();
-       
+
        /* Start the timeout management timer */
        TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
        TCCR0B = ((1 << CS02) | (1 << CS00));
        /* Start the timeout management timer */
        TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
        TCCR0B = ((1 << CS02) | (1 << CS00));
-       
+
        switch (V2Command)
        {
                case CMD_SIGN_ON:
        switch (V2Command)
        {
                case CMD_SIGN_ON:
@@ -104,7 +108,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,7 +143,7 @@ void V2Protocol_ProcessCommand(void)
                        V2Protocol_UnknownCommand(V2Command);
                        break;
        }
                        V2Protocol_UnknownCommand(V2Command);
                        break;
        }
-       
+
        /* Disable the timeout management timer */
        TCCR0B = 0;
 
        /* Disable the timeout management timer */
        TCCR0B = 0;
 
@@ -193,10 +197,10 @@ static void V2Protocol_ResetProtection(void)
        Endpoint_ClearOUT();
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
        Endpoint_ClearOUT();
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
-       
+
        Endpoint_Write_Byte(CMD_RESET_PROTECTION);
        Endpoint_Write_Byte(STATUS_CMD_OK);
        Endpoint_Write_Byte(CMD_RESET_PROTECTION);
        Endpoint_Write_Byte(STATUS_CMD_OK);
-       Endpoint_ClearIN();     
+       Endpoint_ClearIN();
 }
 
 
 }
 
 
@@ -209,18 +213,18 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
 {
        uint8_t ParamID = Endpoint_Read_Byte();
        uint8_t ParamValue;
 {
        uint8_t ParamID = Endpoint_Read_Byte();
        uint8_t ParamValue;
-       
+
        if (V2Command == CMD_SET_PARAMETER)
          ParamValue = Endpoint_Read_Byte();
 
        Endpoint_ClearOUT();
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
        if (V2Command == CMD_SET_PARAMETER)
          ParamValue = Endpoint_Read_Byte();
 
        Endpoint_ClearOUT();
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
-       
+
        Endpoint_Write_Byte(V2Command);
        Endpoint_Write_Byte(V2Command);
-       
+
        uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
        uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
-       
+
        if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
        {
                Endpoint_Write_Byte(STATUS_CMD_OK);
        if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
        {
                Endpoint_Write_Byte(STATUS_CMD_OK);
@@ -232,7 +236,7 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
                Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID));
        }
        else
                Endpoint_Write_Byte(V2Params_GetParameterValue(ParamID));
        }
        else
-       {       
+       {
                Endpoint_Write_Byte(STATUS_CMD_FAILED);
        }
 
                Endpoint_Write_Byte(STATUS_CMD_FAILED);
        }
 
@@ -250,7 +254,7 @@ static void V2Protocol_LoadAddress(void)
        Endpoint_ClearOUT();
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
        Endpoint_ClearOUT();
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
-       
+
        if (CurrentAddress & (1UL << 31))
          MustLoadExtendedAddress = true;
 
        if (CurrentAddress & (1UL << 31))
          MustLoadExtendedAddress = true;
 
@@ -258,3 +262,4 @@ static void V2Protocol_LoadAddress(void)
        Endpoint_Write_Byte(STATUS_CMD_OK);
        Endpoint_ClearIN();
 }
        Endpoint_Write_Byte(STATUS_CMD_OK);
        Endpoint_ClearIN();
 }
+