Make sure that long reads are aborted early if the connection times out while waiting...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / TINYNVM.c
index c0f04c6..acd6b65 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
@@ -150,14 +164,14 @@ bool TINYNVM_ReadMemory(const uint16_t ReadAddress, uint8_t* ReadBuffer, uint16_
        /* Send the address of the location to read from */\r
        TINYNVM_SendPointerAddress(ReadAddress);\r
        \r
-       while (ReadSize--)\r
+       while (ReadSize-- && TimeoutMSRemaining)\r
        {\r
                /* Read the byte of data from the target */\r
                XPROGTarget_SendByte(TPI_CMD_SLD | TPI_POINTER_INDIRECT_PI);\r
                *(ReadBuffer++) = XPROGTarget_ReceiveByte();\r
        }\r
        \r
-       return true;\r
+       return (TimeoutMSRemaining != 0);\r
 }\r
 \r
 /** Writes word addressed memory to the target's memory spaces.\r