AVRISP programmer project now has a more robust timeout system, allowing for a doubli...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XMEGANVM.c
index fc98cfc..6ef59db 100644 (file)
@@ -72,14 +72,19 @@ static void XMEGANVM_SendNVMRegAddress(const uint8_t Register)
 bool XMEGANVM_WaitWhileNVMBusBusy(void)\r
 {\r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\r
 bool XMEGANVM_WaitWhileNVMBusBusy(void)\r
 {\r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\r
+       uint8_t TimeoutMSRemaining = 100;\r
        while (TimeoutMSRemaining)\r
        {\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
        while (TimeoutMSRemaining)\r
        {\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
+                 return true;\r
+\r
+               /* Manage software timeout */\r
+               if (TIFR0 & (1 << OCF0A))\r
                {\r
                {\r
-                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
-                       return true;\r
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
                }\r
        }\r
        \r
                }\r
        }\r
        \r
@@ -94,6 +99,7 @@ bool XMEGANVM_WaitWhileNVMBusBusy(void)
 bool XMEGANVM_WaitWhileNVMControllerBusy(void)\r
 {\r
        /* Poll the NVM STATUS register while the NVM controller is busy */\r
 bool XMEGANVM_WaitWhileNVMControllerBusy(void)\r
 {\r
        /* Poll the NVM STATUS register while the NVM controller is busy */\r
+       uint8_t TimeoutMSRemaining = 100;\r
        while (TimeoutMSRemaining)\r
        {\r
                /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */\r
        while (TimeoutMSRemaining)\r
        {\r
                /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */\r
@@ -102,9 +108,13 @@ bool XMEGANVM_WaitWhileNVMControllerBusy(void)
                \r
                /* Check to see if the BUSY flag is still set */\r
                if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
                \r
                /* Check to see if the BUSY flag is still set */\r
                if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
+                 return true;\r
+\r
+               /* Manage software timeout */\r
+               if (TIFR0 & (1 << OCF0A))\r
                {\r
                {\r
-                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
-                       return true;\r
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
                }\r
        }\r
        \r
                }\r
        }\r
        \r