AVRISP programmer project now has a more robust timeout system, allowing for a doubli...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / TINYNVM.c
index ca5def2..428469d 100644 (file)
@@ -37,7 +37,6 @@
 #include "TINYNVM.h"\r
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
-#warning TPI Protocol support is currently incomplete and is not suitable for general use.\r
 \r
 /** Sends the given pointer address to the target's TPI pointer register */\r
 static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress)\r
@@ -66,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 wierd addressing, where the I/O address's upper\r
+       /* The TPI command for writing to the I/O space uses weird 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
@@ -78,14 +77,22 @@ static void TINYNVM_SendWriteNVMRegister(const uint8_t Address)
 bool TINYNVM_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 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
+               /* Manage software timeout */\r
+               if (TIFR0 & (1 << OCF0A))\r
+               {\r
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
+               }\r
        }\r
-       \r
+\r
        return false;\r
 }\r
 \r
@@ -97,6 +104,7 @@ bool TINYNVM_WaitWhileNVMBusBusy(void)
 bool TINYNVM_WaitWhileNVMControllerBusy(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 SIN command to read the TPI STATUS register to see the NVM bus is busy */\r
@@ -105,8 +113,15 @@ bool TINYNVM_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
+\r
        return false;\r
 }\r
 \r
@@ -143,9 +158,9 @@ bool TINYNVM_ReadMemory(const uint16_t ReadAddress, uint8_t* ReadBuffer, uint16_
 \r
 /** Writes word addressed memory to the target's memory spaces.\r
  *\r
- *  \param[in]  WriteAddress  Start address to write to within the target's address space\r
- *  \param[in]  WriteBuffer   Buffer to source data from\r
- *  \param[in]  WriteLength   Total number of bytes to write to the device (must be an integer multiple of 2)\r
+ *  \param[in] WriteAddress  Start address to write to within the target's address space\r
+ *  \param[in] WriteBuffer   Buffer to source data from\r
+ *  \param[in] WriteLength   Total number of bytes to write to the device (must be an integer multiple of 2)\r
  *\r
  *  \return Boolean true if the command sequence complete successfully\r
  */\r
@@ -189,7 +204,8 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint
 \r
 /** Erases the target's memory space.\r
  *\r
- *  \param[in] Address  Address inside the memory space to erase\r
+ *  \param[in] EraseCommand  NVM erase command to send to the device\r
+ *  \param[in] Address       Address inside the memory space to erase\r
  *\r
  *  \return Boolean true if the command sequence complete successfully\r
  */\r
@@ -203,13 +219,13 @@ bool TINYNVM_EraseMemory(const uint8_t EraseCommand, const uint16_t Address)
        TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);\r
        XPROGTarget_SendByte(EraseCommand);\r
 \r
-       /* Write to a location within the target address space to start the erase process */\r
-       TINYNVM_SendPointerAddress(Address);\r
+       /* Write to a high byte location within the target address space to start the erase process */\r
+       TINYNVM_SendPointerAddress(Address | 0x0001);\r
        XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT);\r
        XPROGTarget_SendByte(0x00);\r
 \r
-       /* Wait until the NVM bus is ready again */\r
-       if (!(TINYNVM_WaitWhileNVMBusBusy()))\r
+       /* Wait until the NVM controller is no longer busy */\r
+       if (!(TINYNVM_WaitWhileNVMControllerBusy()))\r
          return false;\r
        \r
        return true;\r