Clean up and add more comments to the AVRISP-MKII project. Make sure the SPI_MULTI...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XMEGANVM.c
index c23b9d7..573f8fd 100644 (file)
@@ -136,24 +136,18 @@ bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand, uint32_t* const CRCDest)
        if (!(XMEGANVM_WaitWhileNVMControllerBusy()))\r
          return false;\r
        \r
-       uint32_t MemoryCRC = 0;\r
-       \r
-       /* Read the first generated CRC byte value */\r
-       XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));\r
+       /* Load the PDI pointer register with the DAT0 register start address */\r
+       XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES);\r
        XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0);\r
-       MemoryCRC  = XPROGTarget_ReceiveByte();\r
-\r
-       /* Read the second generated CRC byte value */\r
-       XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));\r
-       XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT1);\r
-       MemoryCRC |= ((uint16_t)XPROGTarget_ReceiveByte() << 8);\r
 \r
-       /* Read the third generated CRC byte value */\r
-       XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));\r
-       XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT2);\r
-       MemoryCRC |= ((uint32_t)XPROGTarget_ReceiveByte() << 16);\r
+       /* Send the REPEAT command to grab the CRC bytes */\r
+       XPROGTarget_SendByte(PDI_CMD_REPEAT | PDI_DATSIZE_1BYTE);\r
+       XPROGTarget_SendByte(XMEGA_CRC_LENGTH - 1);\r
        \r
-       *CRCDest = MemoryCRC;\r
+       /* Read in the CRC bytes from the target */\r
+       XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);\r
+       for (uint8_t i = 0; i < XMEGA_CRC_LENGTH; i++)\r
+         ((uint8_t*)CRCDest)[i] = XPROGTarget_ReceiveByte();\r
        \r
        return true;\r
 }\r