Device mode class driver callbacks are now fired before the control request status...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XMEGANVM.c
index 6ef59db..1671d29 100644 (file)
@@ -72,19 +72,22 @@ static void XMEGANVM_SendNVMRegAddress(const uint8_t Register)
 bool XMEGANVM_WaitWhileNVMBusBusy(void)\r
 {\r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\r
 bool XMEGANVM_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 LDCS command to read the PDI STATUS register to see the NVM bus is active */\r
                XPROGTarget_SendByte(PDI_CMD_LDCS | PDI_STATUS_REG);\r
        while (TimeoutMSRemaining)\r
        {\r
                /* Send the LDCS command to read the PDI STATUS register to see the NVM bus is active */\r
                XPROGTarget_SendByte(PDI_CMD_LDCS | PDI_STATUS_REG);\r
-               if (XPROGTarget_ReceiveByte() & PDI_STATUS_NVM)\r
-                 return true;\r
-\r
-               /* Manage software timeout */\r
-               if (TIFR0 & (1 << OCF0A))\r
+               \r
+               uint8_t StatusRegister = XPROGTarget_ReceiveByte();\r
+               \r
+               /* We might have timed out waiting for the status register read response, check here */\r
+               if (!(TimeoutMSRemaining))\r
+                 return false;\r
+               \r
+               /* Check the status register read response to see if the NVM bus is enabled */\r
+               if (StatusRegister & PDI_STATUS_NVM)\r
                {\r
                {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
                }\r
        }\r
        \r
                }\r
        }\r
        \r
@@ -99,22 +102,23 @@ bool XMEGANVM_WaitWhileNVMBusBusy(void)
 bool XMEGANVM_WaitWhileNVMControllerBusy(void)\r
 {\r
        /* Poll the NVM STATUS register while the NVM controller is busy */\r
 bool XMEGANVM_WaitWhileNVMControllerBusy(void)\r
 {\r
        /* Poll the NVM STATUS register while the NVM controller is busy */\r
-       uint8_t TimeoutMSRemaining = 100;\r
        while (TimeoutMSRemaining)\r
        {\r
                /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */\r
                XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));\r
                XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS);\r
                \r
        while (TimeoutMSRemaining)\r
        {\r
                /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */\r
                XPROGTarget_SendByte(PDI_CMD_LDS | (PDI_DATSIZE_4BYTES << 2));\r
                XMEGANVM_SendNVMRegAddress(XMEGA_NVM_REG_STATUS);\r
                \r
-               /* Check to see if the BUSY flag is still set */\r
-               if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
-                 return true;\r
+               uint8_t StatusRegister = XPROGTarget_ReceiveByte();\r
 \r
 \r
-               /* Manage software timeout */\r
-               if (TIFR0 & (1 << OCF0A))\r
+               /* We might have timed out waiting for the status register read response, check here */\r
+               if (!(TimeoutMSRemaining))\r
+                 return false;\r
+\r
+               /* Check to see if the BUSY flag is still set */\r
+               if (!(StatusRegister & (1 << 7)))\r
                {\r
                {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
                }\r
        }\r
        \r
                }\r
        }\r
        \r
@@ -165,7 +169,7 @@ bool XMEGANVM_GetMemoryCRC(const uint8_t CRCCommand, uint32_t* const CRCDest)
        for (uint8_t i = 0; i < XMEGA_CRC_LENGTH; i++)\r
          ((uint8_t*)CRCDest)[i] = XPROGTarget_ReceiveByte();\r
        \r
        for (uint8_t i = 0; i < XMEGA_CRC_LENGTH; i++)\r
          ((uint8_t*)CRCDest)[i] = XPROGTarget_ReceiveByte();\r
        \r
-       return true;\r
+       return (TimeoutMSRemaining != 0);\r
 }\r
 \r
 /** Reads memory from the target's memory spaces.\r
 }\r
 \r
 /** Reads memory from the target's memory spaces.\r
@@ -197,10 +201,10 @@ bool XMEGANVM_ReadMemory(const uint32_t ReadAddress, uint8_t* ReadBuffer, uint16
                \r
        /* Send a LD command with indirect access and postincrement to read out the bytes */\r
        XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);\r
                \r
        /* Send a LD command with indirect access and postincrement to read out the bytes */\r
        XPROGTarget_SendByte(PDI_CMD_LD | (PDI_POINTER_INDIRECT_PI << 2) | PDI_DATSIZE_1BYTE);\r
-       while (ReadSize--)\r
+       while (ReadSize-- && TimeoutMSRemaining)\r
          *(ReadBuffer++) = XPROGTarget_ReceiveByte();\r
        \r
          *(ReadBuffer++) = XPROGTarget_ReceiveByte();\r
        \r
-       return true;\r
+       return (TimeoutMSRemaining != 0);\r
 }\r
 \r
 /** Writes byte addressed memory to the target's memory spaces.\r
 }\r
 \r
 /** Writes byte addressed memory to the target's memory spaces.\r