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 defd7ff..6ef59db 100644 (file)
@@ -72,12 +72,20 @@ 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
+       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
                  return true;\r
+\r
+               /* Manage software timeout */\r
+               if (TIFR0 & (1 << OCF0A))\r
+               {\r
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
+               }\r
        }\r
        \r
        return false;\r
@@ -91,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
+       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
@@ -100,6 +109,13 @@ bool XMEGANVM_WaitWhileNVMControllerBusy(void)
                /* 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
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
+               }\r
        }\r
        \r
        return false;\r
@@ -322,7 +338,7 @@ bool XMEGANVM_EraseMemory(const uint8_t EraseCommand, const uint32_t Address)
        {\r
                /* Other erase modes just need us to address a byte within the target memory space */\r
                XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));\r
-               XMEGANVM_SendAddress(Address);  \r
+               XMEGANVM_SendAddress(Address);\r
                XPROGTarget_SendByte(0x00);\r
        }\r
        \r