Added support for the JM-DB-U2 board hardware.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPTarget.c
index 29cb18e..94f1c3d 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
@@ -53,14 +53,14 @@ uint8_t ISPTarget_GetSPIPrescalerMask(void)
                SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   1MHz SPI, Actual =   1MHz SPI\r
                SPI_SPEED_FCPU_DIV_16,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI\r
                SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI\r
                SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   1MHz SPI, Actual =   1MHz SPI\r
                SPI_SPEED_FCPU_DIV_16,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI\r
                SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI\r
-               SPI_SPEED_FCPU_DIV_64    // AVRStudio = 125KHz SPI, Actual = 125KHz SPI \r
+               SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI\r
        #elif (F_CPU == 16000000)\r
                SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   8MHz SPI\r
                SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI\r
                SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI\r
                SPI_SPEED_FCPU_DIV_16,   // AVRStudio =   1MHz SPI, Actual =   1MHz SPI\r
                SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI\r
        #elif (F_CPU == 16000000)\r
                SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   8MHz SPI\r
                SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI\r
                SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI\r
                SPI_SPEED_FCPU_DIV_16,   // AVRStudio =   1MHz SPI, Actual =   1MHz SPI\r
                SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI\r
-               SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI         \r
+               SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI\r
                SPI_SPEED_FCPU_DIV_128   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI\r
        #else\r
                #error No SPI prescaler masks for chosen F_CPU speed.\r
                SPI_SPEED_FCPU_DIV_128   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI\r
        #else\r
                #error No SPI prescaler masks for chosen F_CPU speed.\r
@@ -84,15 +84,15 @@ void ISPTarget_ChangeTargetResetLine(const bool ResetTarget)
 {\r
        if (ResetTarget)\r
        {\r
 {\r
        if (ResetTarget)\r
        {\r
-               RESET_LINE_DDR |= RESET_LINE_MASK;\r
+               AUX_LINE_DDR |= AUX_LINE_MASK;\r
                \r
                if (!(V2Params_GetParameterValue(PARAM_RESET_POLARITY)))\r
                \r
                if (!(V2Params_GetParameterValue(PARAM_RESET_POLARITY)))\r
-                 RESET_LINE_PORT |= RESET_LINE_MASK;\r
+                 AUX_LINE_PORT |= AUX_LINE_MASK;\r
        }\r
        else\r
        {\r
        }\r
        else\r
        {\r
-               RESET_LINE_DDR  &= ~RESET_LINE_MASK;\r
-               RESET_LINE_PORT &= ~RESET_LINE_MASK;\r
+               AUX_LINE_DDR  &= ~AUX_LINE_MASK;\r
+               AUX_LINE_PORT &= ~AUX_LINE_MASK;\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -111,7 +111,7 @@ void ISPTarget_ChangeTargetResetLine(const bool ResetTarget)
 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint16_t PollAddress, const uint8_t PollValue,\r
                                       const uint8_t DelayMS, const uint8_t ReadMemCommand)\r
 {\r
 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint16_t PollAddress, const uint8_t PollValue,\r
                                       const uint8_t DelayMS, const uint8_t ReadMemCommand)\r
 {\r
-       uint8_t ProgrammingStatus = STATUS_CMD_OK;\r
+       uint8_t ProgrammingStatus  = STATUS_CMD_OK;\r
 \r
        /* Determine method of Programming Complete check */\r
        switch (ProgrammingMode & ~(PROG_MODE_PAGED_WRITES_MASK | PROG_MODE_COMMIT_PAGE_MASK))\r
 \r
        /* Determine method of Programming Complete check */\r
        switch (ProgrammingMode & ~(PROG_MODE_PAGED_WRITES_MASK | PROG_MODE_COMMIT_PAGE_MASK))\r
@@ -124,11 +124,18 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                case PROG_MODE_PAGED_VALUE_MASK:\r
                        do\r
                        {\r
                case PROG_MODE_PAGED_VALUE_MASK:\r
                        do\r
                        {\r
+                               /* Manage software timeout */\r
+                               if (TIFR0 & (1 << OCF0A))\r
+                               {\r
+                                       TIFR0 |= (1 << OCF0A);\r
+                                       TimeoutMSRemaining--;\r
+                               }\r
+\r
                                SPI_SendByte(ReadMemCommand);\r
                                SPI_SendByte(PollAddress >> 8);\r
                                SPI_SendByte(PollAddress & 0xFF);\r
                        }\r
                                SPI_SendByte(ReadMemCommand);\r
                                SPI_SendByte(PollAddress >> 8);\r
                                SPI_SendByte(PollAddress & 0xFF);\r
                        }\r
-                       while ((SPI_TransferByte(0x00) != PollValue) && TimeoutMSRemaining);\r
+                       while ((SPI_TransferByte(0x00) == PollValue) && TimeoutMSRemaining);\r
 \r
                        if (!(TimeoutMSRemaining))\r
                         ProgrammingStatus = STATUS_CMD_TOUT;\r
 \r
                        if (!(TimeoutMSRemaining))\r
                         ProgrammingStatus = STATUS_CMD_TOUT;\r
@@ -140,11 +147,14 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                        break;\r
        }\r
 \r
                        break;\r
        }\r
 \r
+       if (ProgrammingStatus == STATUS_CMD_OK)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+\r
        return ProgrammingStatus;\r
 }\r
 \r
 /** Waits until the target has completed the last operation, by continuously polling the device's\r
        return ProgrammingStatus;\r
 }\r
 \r
 /** Waits until the target has completed the last operation, by continuously polling the device's\r
- *  BUSY flag until it is cleared, or until the \ref TARGET_BUSY_TIMEOUT_MS timeout period has expired.\r
+ *  BUSY flag until it is cleared, or until the command timeout period has expired.\r
  *\r
  *  \return V2 Protocol status \ref STATUS_CMD_OK if the no timeout occurred, \ref STATUS_RDY_BSY_TOUT otherwise\r
  */\r
  *\r
  *  \return V2 Protocol status \ref STATUS_CMD_OK if the no timeout occurred, \ref STATUS_RDY_BSY_TOUT otherwise\r
  */\r
@@ -152,17 +162,28 @@ uint8_t ISPTarget_WaitWhileTargetBusy(void)
 {\r
        do\r
        {\r
 {\r
        do\r
        {\r
+               /* Manage software timeout */\r
+               if (TIFR0 & (1 << OCF0A))\r
+               {\r
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
+               }       \r
+\r
                SPI_SendByte(0xF0);\r
                SPI_SendByte(0x00);\r
                SPI_SendByte(0xF0);\r
                SPI_SendByte(0x00);\r
-\r
                SPI_SendByte(0x00);\r
        }\r
        while ((SPI_ReceiveByte() & 0x01) && TimeoutMSRemaining);\r
 \r
                SPI_SendByte(0x00);\r
        }\r
        while ((SPI_ReceiveByte() & 0x01) && TimeoutMSRemaining);\r
 \r
-       if (!(TimeoutMSRemaining))\r
-         return STATUS_RDY_BSY_TOUT;\r
+       if (TimeoutMSRemaining)\r
+       {\r
+               TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+               return STATUS_CMD_OK;\r
+       }\r
        else\r
        else\r
-         return STATUS_CMD_OK;\r
+       {\r
+               return STATUS_RDY_BSY_TOUT;\r
+       }\r
 }\r
 \r
 /** Sends a low-level LOAD EXTENDED ADDRESS command to the target, for addressing of memory beyond the\r
 }\r
 \r
 /** Sends a low-level LOAD EXTENDED ADDRESS command to the target, for addressing of memory beyond the\r
@@ -171,7 +192,7 @@ uint8_t ISPTarget_WaitWhileTargetBusy(void)
  */\r
 void ISPTarget_LoadExtendedAddress(void)\r
 {\r
  */\r
 void ISPTarget_LoadExtendedAddress(void)\r
 {\r
-       SPI_SendByte(0x4D);\r
+       SPI_SendByte(LOAD_EXTENDED_ADDRESS_CMD);\r
        SPI_SendByte(0x00);\r
        SPI_SendByte((CurrentAddress & 0x00FF0000) >> 16);\r
        SPI_SendByte(0x00);     \r
        SPI_SendByte(0x00);\r
        SPI_SendByte((CurrentAddress & 0x00FF0000) >> 16);\r
        SPI_SendByte(0x00);     \r