Change over instances of "make" in the makefiles to "$(MAKE)" to allow for the make...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / V2Protocol.c
index f7e5e81..096551d 100644 (file)
@@ -42,7 +42,13 @@ uint32_t CurrentAddress;
 /** Flag to indicate that the next read/write operation must update the device's current address */\r
 bool MustSetAddress;\r
 \r
-bool CommandTimedOut;\r
+\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
 /** Initializes the hardware and software associated with the V2 protocol command handling. */\r
 void V2Protocol_Init(void)\r
@@ -57,7 +63,7 @@ void V2Protocol_Init(void)
        /* Millisecond timer initialization for managing the command timeout counter */\r
        OCR0A  = ((F_CPU / 64) / 1000);\r
        TCCR0A = (1 << WGM01);\r
-       TCCR0B = ((1 << CS01) | (1 << CS00));\r
+       TIMSK0 = (1 << OCIE0A);\r
        \r
        V2Params_LoadNonVolatileParamValues();\r
 }\r
@@ -70,7 +76,9 @@ void V2Protocol_ProcessCommand(void)
 {\r
        uint8_t V2Command = Endpoint_Read_Byte();\r
        \r
-       CommandTimedOut = false;\r
+       /* Start the timeout management timer */\r
+       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+       TCCR0B = ((1 << CS01) | (1 << CS00));\r
        \r
        switch (V2Command)\r
        {\r
@@ -131,8 +139,12 @@ void V2Protocol_ProcessCommand(void)
                        V2Protocol_UnknownCommand(V2Command);\r
                        break;\r
        }\r
+       \r
+       /* Disable the timeout management timer */\r
+       TCCR0B = 0;\r
 \r
        Endpoint_WaitUntilReady();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_OUT_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_OUT);\r
 }\r
 \r
@@ -151,6 +163,7 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
        }\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
 \r
        Endpoint_Write_Byte(V2Command);\r
@@ -162,6 +175,7 @@ static void V2Protocol_UnknownCommand(const uint8_t V2Command)
 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
@@ -177,6 +191,7 @@ static void V2Protocol_SignOn(void)
 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
@@ -199,6 +214,7 @@ static void V2Protocol_GetSetParam(const uint8_t V2Command)
          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
@@ -232,6 +248,7 @@ static void V2Protocol_LoadAddress(void)
        Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress), NO_STREAM_CALLBACK);\r
 \r
        Endpoint_ClearOUT();\r
+       Endpoint_SelectEndpoint(AVRISP_DATA_IN_EPNUM);\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
        \r
        MustSetAddress = true;\r