Fix TeensyHID bootloader for the largest AVRs - only the ATMEGA128x needs the alterna...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / TINYNVM.c
index 91ed775..817f223 100644 (file)
@@ -65,7 +65,7 @@ static void TINYNVM_SendReadNVMRegister(const uint8_t Address)
  */\r
 static void TINYNVM_SendWriteNVMRegister(const uint8_t Address)\r
 {\r
-       /* The TPI command for writing to the I/O space uses weird addressing, where the I/O address's upper\r
+       /* The TPI command for reading from the I/O space uses strange addressing, where the I/O address's upper\r
         * two bits of the 6-bit address are shifted left once */\r
        XPROGTarget_SendByte(TPI_CMD_SOUT | ((Address & 0x30) << 1) | (Address & 0x0F));\r
 }\r
@@ -81,10 +81,21 @@ 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
-                 return true;\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
+               }\r
        }\r
-       \r
+\r
        return false;\r
 }\r
 \r
@@ -101,11 +112,20 @@ 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
-                 return true;\r
+               if (!(StatusRegister & (1 << 7)))\r
+               {\r
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
+               }\r
        }\r
-       \r
+\r
        return false;\r
 }\r
 \r
@@ -130,14 +150,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
@@ -167,6 +187,10 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint
        \r
        while (WriteLength)\r
        {\r
+               /* Wait until the NVM controller is no longer busy */\r
+               if (!(TINYNVM_WaitWhileNVMControllerBusy()))\r
+                 return false;\r
+\r
                /* Write the low byte of data to the target */\r
                XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);\r
                XPROGTarget_SendByte(*(WriteBuffer++));\r
@@ -175,10 +199,6 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint
                XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);\r
                XPROGTarget_SendByte(*(WriteBuffer++));\r
 \r
-               /* Wait until the NVM controller is no longer busy */\r
-               if (!(TINYNVM_WaitWhileNVMControllerBusy()))\r
-                 return false;\r
-\r
                /* Need to decrement the write length twice, since we read out a whole word */\r
                WriteLength -= 2;\r
        }\r