Add Lock/Fuse byte programming support to the AVRISP PDI programming protocol code.
[pub/USBasp.git] / Projects / AVRISP / Lib / NVMTarget.c
index c2ed92d..d84ae6b 100644 (file)
@@ -114,6 +114,10 @@ bool NVMTarget_GetMemoryCRC(uint8_t CRCCommand, uint32_t* CRCDest)
        NVMTarget_SendNVMRegAddress(NVM_REG_CTRLA);\r
        PDITarget_SendByte(1 << 0);\r
 \r
+       /* Wait until the NVM bus is ready again */\r
+       if (!(PDITarget_WaitWhileNVMBusBusy()))\r
+         return false;\r
+\r
        /* Wait until the NVM controller is no longer busy */\r
        if (!(NVMTarget_WaitWhileNVMControllerBusy()))\r
          return false;\r
@@ -180,6 +184,37 @@ bool NVMTarget_ReadMemory(uint32_t ReadAddress, uint8_t* ReadBuffer, uint16_t Re
        return true;\r
 }\r
 \r
+/** Writes byte addressed memory to the target's memory spaces.\r
+ *\r
+ *  \param[in]  WriteCommand      Command to send to the device to write each memory page\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]  WriteSize         Number of bytes to write\r
+ *\r
+ *  \return Boolean true if the command sequence complete sucessfully\r
+ */\r
+bool NVMTarget_WriteByteMemory(uint8_t WriteCommand, uint32_t WriteAddress, uint8_t* WriteBuffer, uint16_t WriteSize)\r
+{\r
+       for (uint8_t i = 0; i < WriteSize; i++)\r
+       {\r
+               /* Wait until the NVM controller is no longer busy */\r
+               if (!(NVMTarget_WaitWhileNVMControllerBusy()))\r
+                 return false;\r
+\r
+               /* Send the memory write command to the target */\r
+               PDITarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));\r
+               NVMTarget_SendNVMRegAddress(NVM_REG_CMD);\r
+               PDITarget_SendByte(WriteCommand);\r
+       \r
+               /* Send each new memory byte to the memory to the target */\r
+               PDITarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));\r
+               NVMTarget_SendAddress(WriteAddress++);\r
+               PDITarget_SendByte(*(WriteBuffer++));\r
+       }\r
+       \r
+       return true;\r
+}\r
+\r
 /** Erases a specific memory space of the target.\r
  *\r
  *  \param[in] EraseCommand  NVM erase command to send to the device\r