Be doubly-certain that the incomming CDC class driver's endpoint/pipe is flushed...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XMEGANVM.c
index c83ae01..defd7ff 100644 (file)
@@ -1,21 +1,21 @@
 /*\r
              LUFA Library\r
 /*\r
              LUFA Library\r
-     Copyright (C) Dean Camera, 2009.\r
+     Copyright (C) Dean Camera, 2010.\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
               \r
   dean [at] fourwalledcubicle [dot] com\r
       www.fourwalledcubicle.com\r
 */\r
 \r
 /*\r
-  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
-\r
-  Permission to use, copy, modify, and distribute this software\r
-  and its documentation for any purpose and without fee is hereby\r
-  granted, provided that the above copyright notice appear in all\r
-  copies and that both that the copyright notice and this\r
-  permission notice and warranty disclaimer appear in supporting\r
-  documentation, and that the name of the author not be used in\r
-  advertising or publicity pertaining to distribution of the\r
+  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)\r
+\r
+  Permission to use, copy, modify, distribute, and sell this \r
+  software and its documentation for any purpose is hereby granted\r
+  without fee, provided that the above copyright notice appear in \r
+  all copies and that both that the copyright notice and this\r
+  permission notice and warranty disclaimer appear in supporting \r
+  documentation, and that the name of the author not be used in \r
+  advertising or publicity pertaining to distribution of the \r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
   software without specific, written prior permission.\r
 \r
   The author disclaim all warranties with regard to this\r
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
 \r
 \r
 #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)\r
 \r
-/** Sends the given NVM register address to the target.\r
- *\r
- *  \param[in] Register  NVM register whose absolute address is to be sent\r
- */\r
-void XMEGANVM_SendNVMRegAddress(const uint8_t Register)\r
-{\r
-       /* Determine the absolute register address from the NVM base memory address and the NVM register address */\r
-       uint32_t Address = XPROG_Param_NVMBase | Register;\r
-\r
-       /* Send the calculated 32-bit address to the target, LSB first */\r
-       XMEGANVM_SendAddress(Address);\r
-}\r
-\r
 /** Sends the given 32-bit absolute address to the target.\r
  *\r
  *  \param[in] AbsoluteAddress  Absolute address to send to the target\r
  */\r
 /** Sends the given 32-bit absolute address to the target.\r
  *\r
  *  \param[in] AbsoluteAddress  Absolute address to send to the target\r
  */\r
-void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)\r
+static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)\r
 {\r
        /* Send the given 32-bit address to the target, LSB first */\r
        XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[0]);\r
 {\r
        /* Send the given 32-bit address to the target, LSB first */\r
        XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[0]);\r
@@ -64,6 +51,19 @@ void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress)
        XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[3]);\r
 }\r
 \r
        XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[3]);\r
 }\r
 \r
+/** Sends the given NVM register address to the target.\r
+ *\r
+ *  \param[in] Register  NVM register whose absolute address is to be sent\r
+ */\r
+static void XMEGANVM_SendNVMRegAddress(const uint8_t Register)\r
+{\r
+       /* Determine the absolute register address from the NVM base memory address and the NVM register address */\r
+       uint32_t Address = XPROG_Param_NVMBase | Register;\r
+\r
+       /* Send the calculated 32-bit address to the target, LSB first */\r
+       XMEGANVM_SendAddress(Address);\r
+}\r
+\r
 /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read or CRC\r
  *  calculation.\r
  *\r
 /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read or CRC\r
  *  calculation.\r
  *\r
@@ -136,24 +136,18 @@ bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand, uint32_t* const CRCDest)
        if (!(XMEGANVM_WaitWhileNVMControllerBusy()))\r
          return false;\r
        \r
        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
        XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0);\r
-       MemoryCRC  = XPROGTarget_ReceiveByte();\r
 \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
        \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
        \r
        return true;\r
 }\r
@@ -196,12 +190,12 @@ bool XMEGANVM_ReadMemory(const uint32_t ReadAddress, uint8_t* ReadBuffer, uint16
 /** 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 byte\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 byte\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]  WriteAddress  Address to write to within the target's address space\r
+ *  \param[in]  Byte          Byte to write to the target\r
  *\r
  *  \return Boolean true if the command sequence complete successfully\r
  */\r
  *\r
  *  \return Boolean true if the command sequence complete successfully\r
  */\r
-bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand, const uint32_t WriteAddress, const uint8_t* WriteBuffer)\r
+bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand, const uint32_t WriteAddress, const uint8_t Byte)\r
 {\r
        /* Wait until the NVM controller is no longer busy */\r
        if (!(XMEGANVM_WaitWhileNVMControllerBusy()))\r
 {\r
        /* Wait until the NVM controller is no longer busy */\r
        if (!(XMEGANVM_WaitWhileNVMControllerBusy()))\r
@@ -215,7 +209,7 @@ bool XMEGANVM_WriteByteMemory(const uint8_t WriteCommand, const uint32_t WriteAd
        /* Send new memory byte to the memory to the target */\r
        XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));\r
        XMEGANVM_SendAddress(WriteAddress);\r
        /* Send new memory byte to the memory to the target */\r
        XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2));\r
        XMEGANVM_SendAddress(WriteAddress);\r
-       XPROGTarget_SendByte(*(WriteBuffer++));\r
+       XPROGTarget_SendByte(Byte);\r
        \r
        return true;\r
 }\r
        \r
        return true;\r
 }\r