Switch back to C based timeout ISR in the AVRISP project - assembly version was faili...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / XPROG / XPROGTarget.c
index e75adbe..6adb24f 100644 (file)
@@ -165,6 +165,10 @@ void XPROGTarget_EnableTargetPDI(void)
        BITBANG_PDIDATA_DDR  |= BITBANG_PDIDATA_MASK;\r
        BITBANG_PDICLOCK_DDR |= BITBANG_PDICLOCK_MASK;\r
        \r
+       /* Set DATA line low for at least 90ns to ensure that the device is ready for PDI mode to be entered */\r
+       BITBANG_PDIDATA_PORT &= ~BITBANG_PDIDATA_MASK;\r
+       _delay_us(1);\r
+\r
        /* Set DATA line high for at least 90ns to disable /RESET functionality */\r
        BITBANG_PDIDATA_PORT |= BITBANG_PDIDATA_MASK;\r
        _delay_us(1);\r
@@ -314,6 +318,9 @@ void XPROGTarget_SendByte(const uint8_t Byte)
        SoftUSART_Data     = NewUSARTData;\r
        SoftUSART_BitCount = BITS_IN_USART_FRAME;\r
 #endif\r
+\r
+    if (TimeoutMSRemaining)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
 }\r
 \r
 /** Receives a byte via the software USART, blocking until data is received.\r
@@ -328,15 +335,7 @@ uint8_t XPROGTarget_ReceiveByte(void)
 \r
 #if defined(XPROG_VIA_HARDWARE_USART)\r
        /* Wait until a byte has been received before reading */\r
-       while (!(UCSR1A & (1 << RXC1)) && TimeoutMSRemaining)\r
-       {\r
-               /* Manage software timeout */\r
-               if (TIFR0 & (1 << OCF0A))\r
-               {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
-               }       \r
-       }\r
+       while (!(UCSR1A & (1 << RXC1)) && TimeoutMSRemaining);\r
        \r
        if (TimeoutMSRemaining)\r
          TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
@@ -345,15 +344,7 @@ uint8_t XPROGTarget_ReceiveByte(void)
 #else\r
        /* Wait until a byte has been received before reading */\r
        SoftUSART_BitCount = BITS_IN_USART_FRAME;\r
-       while (SoftUSART_BitCount && TimeoutMSRemaining)\r
-       {\r
-               /* Manage software timeout */\r
-               if (TIFR0 & (1 << OCF0A))\r
-               {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
-               }\r
-       }\r
+       while (SoftUSART_BitCount && TimeoutMSRemaining);\r
 \r
        if (TimeoutMSRemaining)\r
          TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
@@ -385,6 +376,9 @@ void XPROGTarget_SendBreak(void)
        SoftUSART_Data     = 0x0FFF;\r
        SoftUSART_BitCount = BITS_IN_USART_FRAME;\r
 #endif\r
+\r
+    if (TimeoutMSRemaining)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
 }\r
 \r
 static void XPROGTarget_SetTxMode(void)\r
@@ -402,14 +396,7 @@ static void XPROGTarget_SetTxMode(void)
                \r
        IsSending = true;\r
 #else\r
-       while (SoftUSART_BitCount && TimeoutMSRemaining)\r
-       {       \r
-               if (TIFR0 & (1 << OCF0A))\r
-               {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
-               }\r
-       }\r
+       while (SoftUSART_BitCount && TimeoutMSRemaining);\r
        \r
        /* Wait for a full cycle of the clock */\r
        SoftUSART_Data     = 0x0001;\r
@@ -428,6 +415,9 @@ static void XPROGTarget_SetTxMode(void)
        }\r
 #endif\r
 \r
+    if (TimeoutMSRemaining)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+\r
        IsSending = true;\r
 }\r
 \r
@@ -443,14 +433,7 @@ static void XPROGTarget_SetRxMode(void)
        DDRD   &= ~(1 << 3);\r
        PORTD  &= ~(1 << 3);\r
 #else\r
-       while (SoftUSART_BitCount && TimeoutMSRemaining)\r
-       {       \r
-               if (TIFR0 & (1 << OCF0A))\r
-               {\r
-                       TIFR0 |= (1 << OCF0A);\r
-                       TimeoutMSRemaining--;\r
-               }\r
-       }\r
+       while (SoftUSART_BitCount && TimeoutMSRemaining);\r
 \r
        if (XPROG_SelectedProtocol == XPRG_PROTOCOL_PDI)\r
        {\r
@@ -458,15 +441,7 @@ static void XPROGTarget_SetRxMode(void)
                BITBANG_PDIDATA_PORT &= ~BITBANG_PDIDATA_MASK;\r
 \r
                /* Wait until DATA line has been pulled up to idle by the target */\r
-               while (!(BITBANG_PDIDATA_PIN & BITBANG_PDIDATA_MASK) && TimeoutMSRemaining)\r
-               {\r
-                       /* Manage software timeout */\r
-                       if (TIFR0 & (1 << OCF0A))\r
-                       {\r
-                               TIFR0 |= (1 << OCF0A);\r
-                               TimeoutMSRemaining--;\r
-                       }\r
-               }\r
+               while (!(BITBANG_PDIDATA_PIN & BITBANG_PDIDATA_MASK) && TimeoutMSRemaining);\r
        }\r
        else\r
        {\r
@@ -474,15 +449,7 @@ static void XPROGTarget_SetRxMode(void)
                BITBANG_TPIDATA_PORT &= ~BITBANG_TPIDATA_MASK;\r
 \r
                /* Wait until DATA line has been pulled up to idle by the target */\r
-               while (!(BITBANG_TPIDATA_PIN & BITBANG_TPIDATA_MASK) && TimeoutMSRemaining)\r
-               {\r
-                       /* Manage software timeout */\r
-                       if (TIFR0 & (1 << OCF0A))\r
-                       {\r
-                               TIFR0 |= (1 << OCF0A);\r
-                               TimeoutMSRemaining--;\r
-                       }\r
-               }       \r
+               while (!(BITBANG_TPIDATA_PIN & BITBANG_TPIDATA_MASK) && TimeoutMSRemaining);\r
        }       \r
 #endif\r
 \r