X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/7f9f97c792dee6875fbca9806422bdd7d6c5a657..c58c53dba90fdc19d38f5e5d6957f2ede2a740f3:/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c diff --git a/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c b/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c index c23b9d75b..f63f13a97 100644 --- a/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c +++ b/Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c @@ -38,24 +38,11 @@ #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__) -/** Sends the given NVM register address to the target. - * - * \param[in] Register NVM register whose absolute address is to be sent - */ -void XMEGANVM_SendNVMRegAddress(const uint8_t Register) -{ - /* Determine the absolute register address from the NVM base memory address and the NVM register address */ - uint32_t Address = XPROG_Param_NVMBase | Register; - - /* Send the calculated 32-bit address to the target, LSB first */ - XMEGANVM_SendAddress(Address); -} - /** Sends the given 32-bit absolute address to the target. * * \param[in] AbsoluteAddress Absolute address to send to the target */ -void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress) +static void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress) { /* Send the given 32-bit address to the target, LSB first */ XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[0]); @@ -64,6 +51,19 @@ void XMEGANVM_SendAddress(const uint32_t AbsoluteAddress) XPROGTarget_SendByte(((uint8_t*)&AbsoluteAddress)[3]); } +/** Sends the given NVM register address to the target. + * + * \param[in] Register NVM register whose absolute address is to be sent + */ +static void XMEGANVM_SendNVMRegAddress(const uint8_t Register) +{ + /* Determine the absolute register address from the NVM base memory address and the NVM register address */ + uint32_t Address = XPROG_Param_NVMBase | Register; + + /* Send the calculated 32-bit address to the target, LSB first */ + XMEGANVM_SendAddress(Address); +} + /** Busy-waits while the NVM controller is busy performing a NVM operation, such as a FLASH page read or CRC * calculation. * @@ -77,7 +77,17 @@ bool XMEGANVM_WaitWhileNVMBusBusy(void) /* Send the LDCS command to read the PDI STATUS register to see the NVM bus is active */ XPROGTarget_SendByte(PDI_CMD_LDCS | PDI_STATUS_REG); if (XPROGTarget_ReceiveByte() & PDI_STATUS_NVM) - return true; + { + TimeoutMSRemaining = COMMAND_TIMEOUT_MS; + return true; + } + + /* Manage software timeout */ + if (TIFR0 & (1 << OCF0A)) + { + TIFR0 |= (1 << OCF0A); + TimeoutMSRemaining--; + } } return false; @@ -99,7 +109,17 @@ bool XMEGANVM_WaitWhileNVMControllerBusy(void) /* Check to see if the BUSY flag is still set */ if (!(XPROGTarget_ReceiveByte() & (1 << 7))) - return true; + { + TimeoutMSRemaining = COMMAND_TIMEOUT_MS; + return true; + } + + /* Manage software timeout */ + if (TIFR0 & (1 << OCF0A)) + { + TIFR0 |= (1 << OCF0A); + TimeoutMSRemaining--; + } } return false; @@ -136,24 +156,18 @@ bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand, uint32_t* const CRCDest) if (!(XMEGANVM_WaitWhileNVMControllerBusy())) return false; - uint32_t MemoryCRC = 0; - - /* Read the first generated CRC byte value */ - XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2)); + /* Load the PDI pointer register with the DAT0 register start address */ + XPROGTarget_SendByte(PDI_CMD_ST | (PDI_POINTER_DIRECT << 2) | PDI_DATSIZE_4BYTES); XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT0); - MemoryCRC = XPROGTarget_ReceiveByte(); - /* Read the second generated CRC byte value */ - XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2)); - XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT1); - MemoryCRC |= ((uint16_t)XPROGTarget_ReceiveByte() << 8); - - /* Read the third generated CRC byte value */ - XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2)); - XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_DAT2); - MemoryCRC |= ((uint32_t)XPROGTarget_ReceiveByte() << 16); + /* Send the REPEAT command to grab the CRC bytes */ + XPROGTarget_SendByte(PDI_CMD_REPEAT | PDI_DATSIZE_1BYTE); + XPROGTarget_SendByte(XMEGA_CRC_LENGTH - 1); - *CRCDest = MemoryCRC; + /* Read in the CRC bytes from the target */ + XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE); + for (uint8_t i = 0; i < XMEGA_CRC_LENGTH; i++) + ((uint8_t*)CRCDest)[i] = XPROGTarget_ReceiveByte(); return true; } @@ -328,7 +342,7 @@ bool XMEGANVM_EraseMemory(const uint8_t EraseCommand, const uint32_t Address) { /* Other erase modes just need us to address a byte within the target memory space */ XPROGTarget_SendByte(PDI_CMD_STS | (PDI_DATSIZE_4BYTES << 2)); - XMEGANVM_SendAddress(Address); + XMEGANVM_SendAddress(Address); XPROGTarget_SendByte(0x00); }