bool TINYNVM_WaitWhileNVMBusBusy(void)\r
{\r
/* Poll the STATUS register to check to see if NVM access has been enabled */\r
+ uint8_t TimeoutMSRemaining = 100;\r
while (TimeoutMSRemaining)\r
{\r
/* Send the SLDCS command to read the TPI STATUS register to see the NVM bus is active */\r
XPROGTarget_SendByte(TPI_CMD_SLDCS | TPI_STATUS_REG);\r
if (XPROGTarget_ReceiveByte() & TPI_STATUS_NVM)\r
return true;\r
+\r
+ /* Manage software timeout */\r
+ if (TIFR0 & (1 << OCF0A))\r
+ {\r
+ TIFR0 |= (1 << OCF0A);\r
+ TimeoutMSRemaining--;\r
+ }\r
}\r
- \r
+\r
return false;\r
}\r
\r
bool TINYNVM_WaitWhileNVMControllerBusy(void)\r
{\r
/* Poll the STATUS register to check to see if NVM access has been enabled */\r
+ uint8_t TimeoutMSRemaining = 100;\r
while (TimeoutMSRemaining)\r
{\r
/* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */\r
/* Check to see if the BUSY flag is still set */\r
if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
return true;\r
+\r
+ /* Manage software timeout */\r
+ if (TIFR0 & (1 << OCF0A))\r
+ {\r
+ TIFR0 |= (1 << OCF0A);\r
+ TimeoutMSRemaining--;\r
+ }\r
}\r
- \r
+\r
return false;\r
}\r
\r
\r
while (WriteLength)\r
{\r
+ /* Wait until the NVM controller is no longer busy */\r
+ if (!(TINYNVM_WaitWhileNVMControllerBusy()))\r
+ return false;\r
+\r
/* Write the low byte of data to the target */\r
XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);\r
XPROGTarget_SendByte(*(WriteBuffer++));\r
XPROGTarget_SendByte(TPI_CMD_SST | TPI_POINTER_INDIRECT_PI);\r
XPROGTarget_SendByte(*(WriteBuffer++));\r
\r
- /* Wait until the NVM controller is no longer busy */\r
- if (!(TINYNVM_WaitWhileNVMControllerBusy()))\r
- return false;\r
-\r
/* Need to decrement the write length twice, since we read out a whole word */\r
WriteLength -= 2;\r
}\r