X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/5687ac7316335009160ccd7f56fce5e7746c5889..2a2de8fea94d2a524e988725585b17746cd30dba:/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c?ds=sidebyside diff --git a/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c b/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c index ca5def27e..817f22347 100644 --- a/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c +++ b/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c @@ -37,7 +37,6 @@ #include "TINYNVM.h" #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__) -#warning TPI Protocol support is currently incomplete and is not suitable for general use. /** Sends the given pointer address to the target's TPI pointer register */ static void TINYNVM_SendPointerAddress(const uint16_t AbsoluteAddress) @@ -66,7 +65,7 @@ static void TINYNVM_SendReadNVMRegister(const uint8_t Address) */ static void TINYNVM_SendWriteNVMRegister(const uint8_t Address) { - /* The TPI command for writing to the I/O space uses wierd addressing, where the I/O address's upper + /* The TPI command for reading from the I/O space uses strange addressing, where the I/O address's upper * two bits of the 6-bit address are shifted left once */ XPROGTarget_SendByte(TPI_CMD_SOUT | ((Address & 0x30) << 1) | (Address & 0x0F)); } @@ -82,10 +81,21 @@ bool TINYNVM_WaitWhileNVMBusBusy(void) { /* Send the SLDCS command to read the TPI STATUS register to see the NVM bus is active */ XPROGTarget_SendByte(TPI_CMD_SLDCS | TPI_STATUS_REG); - if (XPROGTarget_ReceiveByte() & TPI_STATUS_NVM) - return true; + + uint8_t StatusRegister = XPROGTarget_ReceiveByte(); + + /* We might have timed out waiting for the status register read response, check here */ + if (!(TimeoutMSRemaining)) + return false; + + /* Check the status register read response to see if the NVM bus is enabled */ + if (StatusRegister & TPI_STATUS_NVM) + { + TimeoutMSRemaining = COMMAND_TIMEOUT_MS; + return true; + } } - + return false; } @@ -102,11 +112,20 @@ bool TINYNVM_WaitWhileNVMControllerBusy(void) /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */ TINYNVM_SendReadNVMRegister(XPROG_Param_NVMCSRRegAddr); + uint8_t StatusRegister = XPROGTarget_ReceiveByte(); + + /* We might have timed out waiting for the status register read response, check here */ + if (!(TimeoutMSRemaining)) + return false; + /* Check to see if the BUSY flag is still set */ - if (!(XPROGTarget_ReceiveByte() & (1 << 7))) - return true; + if (!(StatusRegister & (1 << 7))) + { + TimeoutMSRemaining = COMMAND_TIMEOUT_MS; + return true; + } } - + return false; } @@ -131,21 +150,21 @@ bool TINYNVM_ReadMemory(const uint16_t ReadAddress, uint8_t* ReadBuffer, uint16_ /* Send the address of the location to read from */ TINYNVM_SendPointerAddress(ReadAddress); - while (ReadSize--) + while (ReadSize-- && TimeoutMSRemaining) { /* Read the byte of data from the target */ XPROGTarget_SendByte(TPI_CMD_SLD | TPI_POINTER_INDIRECT_PI); *(ReadBuffer++) = XPROGTarget_ReceiveByte(); } - return true; + return (TimeoutMSRemaining != 0); } /** Writes word addressed memory to the target's memory spaces. * - * \param[in] WriteAddress Start address to write to within the target's address space - * \param[in] WriteBuffer Buffer to source data from - * \param[in] WriteLength Total number of bytes to write to the device (must be an integer multiple of 2) + * \param[in] WriteAddress Start address to write to within the target's address space + * \param[in] WriteBuffer Buffer to source data from + * \param[in] WriteLength Total number of bytes to write to the device (must be an integer multiple of 2) * * \return Boolean true if the command sequence complete successfully */ @@ -189,7 +208,8 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint /** Erases the target's memory space. * - * \param[in] Address Address inside the memory space to erase + * \param[in] EraseCommand NVM erase command to send to the device + * \param[in] Address Address inside the memory space to erase * * \return Boolean true if the command sequence complete successfully */ @@ -203,13 +223,13 @@ bool TINYNVM_EraseMemory(const uint8_t EraseCommand, const uint16_t Address) TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr); XPROGTarget_SendByte(EraseCommand); - /* Write to a location within the target address space to start the erase process */ - TINYNVM_SendPointerAddress(Address); + /* Write to a high byte location within the target address space to start the erase process */ + TINYNVM_SendPointerAddress(Address | 0x0001); XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT); XPROGTarget_SendByte(0x00); - /* Wait until the NVM bus is ready again */ - if (!(TINYNVM_WaitWhileNVMBusBusy())) + /* Wait until the NVM controller is no longer busy */ + if (!(TINYNVM_WaitWhileNVMControllerBusy())) return false; return true;