X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/35a0fe93848cc6e1a79d2e606a2e3f6a84ba5755..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 91ed775bb..428469df7 100644 --- a/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c +++ b/Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c @@ -77,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; } @@ -96,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 */ @@ -104,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; } @@ -167,6 +183,10 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint while (WriteLength) { + /* Wait until the NVM controller is no longer busy */ + if (!(TINYNVM_WaitWhileNVMControllerBusy())) + return false; + /* Write the low byte of data to the target */ XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI); XPROGTarget_SendByte(*(WriteBuffer++)); @@ -175,10 +195,6 @@ bool TINYNVM_WriteMemory(const uint16_t WriteAddress, uint8_t* WriteBuffer, uint XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI); XPROGTarget_SendByte(*(WriteBuffer++)); - /* Wait until the NVM controller is no longer busy */ - if (!(TINYNVM_WaitWhileNVMControllerBusy())) - return false; - /* Need to decrement the write length twice, since we read out a whole word */ WriteLength -= 2; }