Add bluetooth channel connection callback to the incomplete BluetoothHost demo.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPTarget.c
index 19b0aa0..94f1c3d 100644 (file)
@@ -111,7 +111,7 @@ void ISPTarget_ChangeTargetResetLine(const bool ResetTarget)
 uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint16_t PollAddress, const uint8_t PollValue,\r
                                       const uint8_t DelayMS, const uint8_t ReadMemCommand)\r
 {\r
-       uint8_t ProgrammingStatus = STATUS_CMD_OK;\r
+       uint8_t ProgrammingStatus  = STATUS_CMD_OK;\r
 \r
        /* Determine method of Programming Complete check */\r
        switch (ProgrammingMode & ~(PROG_MODE_PAGED_WRITES_MASK | PROG_MODE_COMMIT_PAGE_MASK))\r
@@ -124,11 +124,18 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                case PROG_MODE_PAGED_VALUE_MASK:\r
                        do\r
                        {\r
+                               /* Manage software timeout */\r
+                               if (TIFR0 & (1 << OCF0A))\r
+                               {\r
+                                       TIFR0 |= (1 << OCF0A);\r
+                                       TimeoutMSRemaining--;\r
+                               }\r
+\r
                                SPI_SendByte(ReadMemCommand);\r
                                SPI_SendByte(PollAddress >> 8);\r
                                SPI_SendByte(PollAddress & 0xFF);\r
                        }\r
-                       while ((SPI_TransferByte(0x00) != PollValue) && TimeoutMSRemaining);\r
+                       while ((SPI_TransferByte(0x00) == PollValue) && TimeoutMSRemaining);\r
 \r
                        if (!(TimeoutMSRemaining))\r
                         ProgrammingStatus = STATUS_CMD_TOUT;\r
@@ -140,6 +147,9 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                        break;\r
        }\r
 \r
+       if (ProgrammingStatus == STATUS_CMD_OK)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+\r
        return ProgrammingStatus;\r
 }\r
 \r
@@ -152,17 +162,28 @@ uint8_t ISPTarget_WaitWhileTargetBusy(void)
 {\r
        do\r
        {\r
+               /* Manage software timeout */\r
+               if (TIFR0 & (1 << OCF0A))\r
+               {\r
+                       TIFR0 |= (1 << OCF0A);\r
+                       TimeoutMSRemaining--;\r
+               }       \r
+\r
                SPI_SendByte(0xF0);\r
                SPI_SendByte(0x00);\r
-\r
                SPI_SendByte(0x00);\r
        }\r
        while ((SPI_ReceiveByte() & 0x01) && TimeoutMSRemaining);\r
 \r
-       if (!(TimeoutMSRemaining))\r
-         return STATUS_RDY_BSY_TOUT;\r
+       if (TimeoutMSRemaining)\r
+       {\r
+               TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+               return STATUS_CMD_OK;\r
+       }\r
        else\r
-         return STATUS_CMD_OK;\r
+       {\r
+               return STATUS_RDY_BSY_TOUT;\r
+       }\r
 }\r
 \r
 /** Sends a low-level LOAD EXTENDED ADDRESS command to the target, for addressing of memory beyond the\r