Add LUFA-111009-BETA tag.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / V2Protocol.c
index f6ea477..65e34fa 100644 (file)
@@ -44,27 +44,22 @@ bool MustLoadExtendedAddress;
 
 
 /** ISR to manage timeouts whilst processing a V2Protocol command */
 
 
 /** ISR to manage timeouts whilst processing a V2Protocol command */
-ISR(TIMER0_COMPA_vect, ISR_NOBLOCK)
+ISR(WDT_vect, ISR_BLOCK)
 {
 {
-       if (TimeoutTicksRemaining)
-         TimeoutTicksRemaining--;
+       TimeoutExpired = true;
+       wdt_disable();
 }
 
 }
 
-/** 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)
 {
        #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(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();
        
        #if defined(ENABLE_ISP_PROTOCOL)
        V2Params_LoadNonVolatileParamValues();
        
        #if defined(ENABLE_ISP_PROTOCOL)
@@ -78,11 +73,12 @@ void V2Protocol_Init(void)
  */
 void V2Protocol_ProcessCommand(void)
 {
  */
 void V2Protocol_ProcessCommand(void)
 {
-       uint8_t V2Command = Endpoint_Read_Byte();
+       uint8_t V2Command = Endpoint_Read_8();
 
 
-       /* Start the timeout management timer */
-       TimeoutTicksRemaining = COMMAND_TIMEOUT_TICKS;
-       TCCR0B = ((1 << CS02) | (1 << CS00));
+       /* Start the watchdog with timeout interrupt enabled to manage the timeout */
+       TimeoutExpired = false;
+       wdt_enable(WDTO_1S);
+       WDTCSR |= (1 << WDIE);
 
        switch (V2Command)
        {
 
        switch (V2Command)
        {
@@ -144,8 +140,8 @@ void V2Protocol_ProcessCommand(void)
                        break;
        }
 
                        break;
        }
 
-       /* Disable the timeout management timer */
-       TCCR0B = 0;
+       /* Disable the timeout management watchdog timer */
+       wdt_disable();
 
        Endpoint_WaitUntilReady();
        Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);
 
        Endpoint_WaitUntilReady();
        Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);
@@ -170,8 +166,8 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        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();
 }
 
@@ -182,9 +178,9 @@ static void V2Protocol_SignOn(void)
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        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_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_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NULL);
        Endpoint_ClearIN();
 }
@@ -198,8 +194,8 @@ static void V2Protocol_ResetProtection(void)
        Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);
 
        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_8(CMD_RESET_PROTECTION);
+       Endpoint_Write_8(STATUS_CMD_OK);
        Endpoint_ClearIN();
 }
 
        Endpoint_ClearIN();
 }
 
@@ -211,33 +207,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;
 
        if (V2Command == CMD_SET_PARAMETER)
        uint8_t ParamValue;
 
        if (V2Command == CMD_SET_PARAMETER)
-         ParamValue = Endpoint_Read_Byte();
+         ParamValue = Endpoint_Read_8();
 
        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(V2Command);
+       Endpoint_Write_8(V2Command);
 
        uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
 
        if ((V2Command == CMD_SET_PARAMETER) && (ParamPrivs & PARAM_PRIV_WRITE))
        {
 
        uint8_t ParamPrivs = V2Params_GetParameterPrivileges(ParamID);
 
        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();
@@ -258,8 +254,8 @@ static void V2Protocol_LoadAddress(void)
        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();
 }