Device mode class driver callbacks are now fired before the control request status...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / V2Protocol.c
index 4fcb5aa..096551d 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
@@ -43,13 +43,31 @@ uint32_t CurrentAddress;
 bool MustSetAddress;\r
 \r
 \r
 bool MustSetAddress;\r
 \r
 \r
-/** ISR for the management of the command execution timeout counter */\r
-ISR(TIMER0_COMPA_vect, ISR_BLOCK)\r
+/** ISR to manage timeouts whilst processing a V2Protocol command */\r
+ISR(TIMER0_COMPA_vect, ISR_NOBLOCK)\r
 {\r
        if (TimeoutMSRemaining)\r
          TimeoutMSRemaining--;\r
 }\r
 \r
 {\r
        if (TimeoutMSRemaining)\r
          TimeoutMSRemaining--;\r
 }\r
 \r
+/** Initializes the hardware and software associated with the V2 protocol command handling. */\r
+void V2Protocol_Init(void)\r
+{\r
+       #if defined(ADC)\r
+       /* Initialize the ADC converter for VTARGET level detection on supported AVR models */\r
+       ADC_Init(ADC_FREE_RUNNING | ADC_PRESCALE_128);\r
+       ADC_SetupChannel(VTARGET_ADC_CHANNEL);\r
+       ADC_StartReading(VTARGET_ADC_CHANNEL_MASK | ADC_RIGHT_ADJUSTED | ADC_REFERENCE_AVCC);\r
+       #endif\r
+       \r
+       /* Millisecond timer initialization for managing the command timeout counter */\r
+       OCR0A  = ((F_CPU / 64) / 1000);\r
+       TCCR0A = (1 << WGM01);\r
+       TIMSK0 = (1 << OCIE0A);\r
+       \r
+       V2Params_LoadNonVolatileParamValues();\r
+}\r
+\r
 /** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.\r
  *  This routine decodes the issued command and passes off the handling of the command to the\r
  *  appropriate function.\r
 /** Master V2 Protocol packet handler, for received V2 Protocol packets from a connected host.\r
  *  This routine decodes the issued command and passes off the handling of the command to the\r
  *  appropriate function.\r
@@ -58,9 +76,10 @@ void V2Protocol_ProcessCommand(void)
 {\r
        uint8_t V2Command = Endpoint_Read_Byte();\r
        \r
 {\r
        uint8_t V2Command = Endpoint_Read_Byte();\r
        \r
+       /* Start the timeout management timer */\r
        TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
        TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
-       TIMSK0 |= (1 << OCIE0A);\r
-\r
+       TCCR0B = ((1 << CS01) | (1 << CS00));\r
+       \r
        switch (V2Command)\r
        {\r
                case CMD_SIGN_ON:\r
        switch (V2Command)\r
        {\r
                case CMD_SIGN_ON:\r
@@ -120,10 +139,12 @@ void V2Protocol_ProcessCommand(void)
                        V2Protocol_UnknownCommand(V2Command);\r
                        break;\r
        }\r
                        V2Protocol_UnknownCommand(V2Command);\r
                        break;\r
        }\r
-               \r
-       TIMSK0 &= ~(1 << OCIE0A);\r
+       \r
+       /* Disable the timeout management timer */\r
+       TCCR0B = 0;\r
 \r
        Endpoint_WaitUntilReady();\r
 \r
        Endpoint_WaitUntilReady();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);\r
 }\r
 \r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);\r
 }\r
 \r
@@ -142,6 +163,7 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
        }\r
 \r
        Endpoint_ClearOUT();\r
        }\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        Endpoint_Write_Byte(V2Command);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        Endpoint_Write_Byte(V2Command);\r
@@ -153,21 +175,23 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
 static void V2Protocol_SignOn(void)\r
 {\r
        Endpoint_ClearOUT();\r
 static void V2Protocol_SignOn(void)\r
 {\r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        Endpoint_Write_Byte(CMD_SIGN_ON);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
        Endpoint_Write_Byte(sizeof(PROGRAMMER_ID) - 1);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        Endpoint_Write_Byte(CMD_SIGN_ON);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
        Endpoint_Write_Byte(sizeof(PROGRAMMER_ID) - 1);\r
-       Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1));\r
+       Endpoint_Write_Stream_LE(PROGRAMMER_ID, (sizeof(PROGRAMMER_ID) - 1), NO_STREAM_CALLBACK);\r
        Endpoint_ClearIN();\r
 }\r
 \r
        Endpoint_ClearIN();\r
 }\r
 \r
-/** Handler for the CMD_RESET_PROTECTION command, currently implemented as a dummy ACK function\r
- *  as no ISP short-circuit protection is currently implemented.\r
+/** Handler for the CMD_RESET_PROTECTION command, implemented as a dummy ACK function as\r
+ *  no target short-circuit protection is currently implemented.\r
  */\r
 static void V2Protocol_ResetProtection(void)\r
 {\r
        Endpoint_ClearOUT();\r
  */\r
 static void V2Protocol_ResetProtection(void)\r
 {\r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        Endpoint_Write_Byte(CMD_RESET_PROTECTION);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        Endpoint_Write_Byte(CMD_RESET_PROTECTION);\r
@@ -190,6 +214,7 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
          ParamValue = Endpoint_Read_Byte();\r
 \r
        Endpoint_ClearOUT();\r
          ParamValue = Endpoint_Read_Byte();\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        Endpoint_Write_Byte(V2Command);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        Endpoint_Write_Byte(V2Command);\r
@@ -220,9 +245,10 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
  */\r
 static void V2Protocol_LoadAddress(void)\r
 {\r
  */\r
 static void V2Protocol_LoadAddress(void)\r
 {\r
-       Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress));\r
+       Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NO_STREAM_CALLBACK);\r
 \r
        Endpoint_ClearOUT();\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        MustSetAddress = true;\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        MustSetAddress = true;\r