Make TPI programming protocol program in words, not bytes to satisfy the datasheet...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / TINYNVM.c
index 4ed3da7..39cdf2e 100644 (file)
@@ -141,11 +141,11 @@ bool TINYNVM_ReadMemory(const uint16_t ReadAddress, uint8_t* ReadBuffer, uint16_
        return true;\r
 }\r
 \r
-/** Writes byte addressed memory to the target's memory spaces.\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\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
@@ -154,6 +154,10 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, const uint8_t* WriteBuffer
        /* Wait until the NVM controller is no longer busy */\r
        if (!(TINYNVM_WaitWhileNVMControllerBusy()))\r
          return false;\r
+         \r
+       /* Must have an integer number of words to write - if extra bytes, abort programming */\r
+       if (WriteLength & 0x01)\r
+         return false;\r
 \r
        /* Set the NVM control register to the WORD WRITE command for memory reading */\r
        TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr);\r
@@ -162,11 +166,22 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, const uint8_t* WriteBuffer
        /* Send the address of the location to write to */\r
        TINYNVM_SendPointerAddress(WriteAddress);\r
        \r
-       while (WriteLength--)\r
+       while (WriteLength)\r
        {\r
-               /* Write the byte of data to the target */\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
+               \r
+               /* Write the high byte of data to the target */\r
+               XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);\r
+               XPROGTarget_SendByte(*(WriteBuffer++));\r
+\r
+               /* Need to decrement the write length twice, since we read out a whole word */\r
+               WriteLength -= 2;\r
        }\r
        \r
        return true;\r