Make sure that the NVM bus/controller busy waits in the AVRISP MKII clone project...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 30 Apr 2010 08:27:21 +0000 (08:27 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 30 Apr 2010 08:27:21 +0000 (08:27 +0000)
Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c
Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c
Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.c

index c0f04c6..7b9ff09 100644 (file)
@@ -81,7 +81,15 @@ bool TINYNVM_WaitWhileNVMBusBusy(void)
        {\r
                /* Send the SLDCS command to read the TPI STATUS register to see the NVM bus is active */\r
                XPROGTarget_SendByte(TPI_CMD_SLDCS | TPI_STATUS_REG);\r
-               if (XPROGTarget_ReceiveByte() & TPI_STATUS_NVM)\r
+\r
+               uint8_t StatusRegister = XPROGTarget_ReceiveByte();\r
+\r
+               /* We might have timed out waiting for the status register read response, check here */\r
+               if (!(TimeoutMSRemaining))\r
+                 return false;\r
+\r
+               /* Check the status register read response to see if the NVM bus is enabled */\r
+               if (StatusRegister & TPI_STATUS_NVM)\r
                {\r
                        TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
                        return true;\r
@@ -111,8 +119,14 @@ bool TINYNVM_WaitWhileNVMControllerBusy(void)
                /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */\r
                TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr);\r
 \r
+               uint8_t StatusRegister = XPROGTarget_ReceiveByte();\r
+\r
+               /* We might have timed out waiting for the status register read response, check here */\r
+               if (!(TimeoutMSRemaining))\r
+                 return false;\r
+\r
                /* Check to see if the BUSY flag is still set */\r
-               if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
+               if (!(StatusRegister & (1 << 7)))\r
                {\r
                        TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
                        return true;\r
index f63f13a..7ca0c5e 100644 (file)
@@ -76,7 +76,15 @@ bool XMEGANVM_WaitWhileNVMBusBusy(void)
        {\r
                /* Send the LDCS command to read the PDI STATUS register to see the NVM bus is active */\r
                XPROGTarget_SendByte(PDI_CMD_LDCS | PDI_STATUS_REG);\r
-               if (XPROGTarget_ReceiveByte() & PDI_STATUS_NVM)\r
+               \r
+               uint8_t StatusRegister = XPROGTarget_ReceiveByte();\r
+               \r
+               /* We might have timed out waiting for the status register read response, check here */\r
+               if (!(TimeoutMSRemaining))\r
+                 return false;\r
+               \r
+               /* Check the status register read response to see if the NVM bus is enabled */\r
+               if (StatusRegister & PDI_STATUS_NVM)\r
                {\r
                        TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
                        return true;\r
@@ -107,8 +115,14 @@ bool XMEGANVM_WaitWhileNVMControllerBusy(void)
                XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));\r
                XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS);\r
                \r
+               uint8_t StatusRegister = XPROGTarget_ReceiveByte();\r
+\r
+               /* We might have timed out waiting for the status register read response, check here */\r
+               if (!(TimeoutMSRemaining))\r
+                 return false;\r
+\r
                /* Check to see if the BUSY flag is still set */\r
-               if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
+               if (!(StatusRegister & (1 << 7)))\r
                {\r
                        TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
                        return true;\r
index bd269ed..6f0d87a 100644 (file)
@@ -453,22 +453,33 @@ static void XPROGTarget_SetRxMode(void)
        {\r
                BITBANG_PDIDATA_DDR  &= ~BITBANG_PDIDATA_MASK;\r
                BITBANG_PDIDATA_PORT &= ~BITBANG_PDIDATA_MASK;\r
+\r
+               /* Wait until DATA line has been pulled up to idle by the target */\r
+               while (!(BITBANG_PDIDATA_PIN & BITBANG_PDIDATA_MASK) && TimeoutMSRemaining)\r
+               {\r
+                       /* Manage software timeout */\r
+                       if (TIFR0 & (1 << OCF0A))\r
+                       {\r
+                               TIFR0 |= (1 << OCF0A);\r
+                               TimeoutMSRemaining--;\r
+                       }\r
+               }\r
        }\r
        else\r
        {\r
                BITBANG_TPIDATA_DDR  &= ~BITBANG_TPIDATA_MASK;\r
-               BITBANG_TPIDATA_PORT &= ~BITBANG_TPIDATA_MASK;  \r
-       }\r
-       \r
-       /* Wait until DATA line has been pulled up to idle by the target */\r
-       while (!(BITBANG_PDIDATA_PIN & BITBANG_PDIDATA_MASK) && TimeoutMSRemaining)\r
-       {\r
-               /* Manage software timeout */\r
-               if (TIFR0 & (1 << OCF0A))\r
+               BITBANG_TPIDATA_PORT &= ~BITBANG_TPIDATA_MASK;\r
+\r
+               /* Wait until DATA line has been pulled up to idle by the target */\r
+               while (!(BITBANG_TPIDATA_PIN & BITBANG_TPIDATA_MASK) && TimeoutMSRemaining)\r
                {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
-               }\r
+                       /* Manage software timeout */\r
+                       if (TIFR0 & (1 << OCF0A))\r
+                       {\r
+                               TIFR0 |= (1 << OCF0A);\r
+                               TimeoutMSRemaining--;\r
+                       }\r
+               }       \r
        }       \r
 #endif\r
 \r