X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/b736a5ef2b72f888acdc8fe825f8f8151ec09680..ce8d0424b1a59bb2b0bd3ab8f69f4e4cf8c9930b:/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c diff --git a/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c b/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c index 39cdf2e9c..428469df7 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 writing to the I/O space uses weird 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)); } @@ -78,14 +77,22 @@ static void TINYNVM_SendWriteNVMRegister(const uint8_t Address) bool TINYNVM_WaitWhileNVMBusBusy(void) { /* Poll the STATUS register to check to see if NVM access has been enabled */ + uint8_t TimeoutMSRemaining = 100; while (TimeoutMSRemaining) { /* 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; + + /* Manage software timeout */ + if (TIFR0 & (1 << OCF0A)) + { + TIFR0 |= (1 << OCF0A); + TimeoutMSRemaining--; + } } - + return false; } @@ -97,6 +104,7 @@ bool TINYNVM_WaitWhileNVMBusBusy(void) bool TINYNVM_WaitWhileNVMControllerBusy(void) { /* Poll the STATUS register to check to see if NVM access has been enabled */ + uint8_t TimeoutMSRemaining = 100; while (TimeoutMSRemaining) { /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */ @@ -105,8 +113,15 @@ bool TINYNVM_WaitWhileNVMControllerBusy(void) /* Check to see if the BUSY flag is still set */ if (!(XPROGTarget_ReceiveByte() & (1 << 7))) return true; + + /* Manage software timeout */ + if (TIFR0 & (1 << OCF0A)) + { + TIFR0 |= (1 << OCF0A); + TimeoutMSRemaining--; + } } - + return false; } @@ -143,21 +158,21 @@ bool TINYNVM_ReadMemory(const uint16_t ReadAddress, uint8_t* ReadBuffer, uint16_ /** 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 */ -bool TINYNVM_WriteMemory(const uint16_t WriteAddress, const uint8_t* WriteBuffer, uint16_t WriteLength) +bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint16_t WriteLength) { /* Wait until the NVM controller is no longer busy */ if (!(TINYNVM_WaitWhileNVMControllerBusy())) return false; - /* Must have an integer number of words to write - if extra bytes, abort programming */ + /* Must have an integer number of words to write - if extra byte, word-align via a dummy high byte */ if (WriteLength & 0x01) - return false; + WriteBuffer[WriteLength++] = 0xFF; /* Set the NVM control register to the WORD WRITE command for memory reading */ TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr); @@ -189,20 +204,28 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, const uint8_t* WriteBuffer /** Erases the target's memory space. * + * \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 */ -bool TINYNVM_EraseMemory(void) +bool TINYNVM_EraseMemory(const uint8_t EraseCommand, const uint16_t Address) { /* Wait until the NVM controller is no longer busy */ if (!(TINYNVM_WaitWhileNVMControllerBusy())) return false; - /* Set the NVM control register to the CHIP ERASE command to erase the target */ + /* Set the NVM control register to the target memory erase command */ TINYNVM_SendWriteNVMRegister(XPROG_Param_NVMCMDRegAddr); - XPROGTarget_SendByte(TINY_NVM_CMD_CHIPERASE); + XPROGTarget_SendByte(EraseCommand); + + /* 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;