More fixes to the AVRISP command timeout system so that it should no longer lock...
authorDean Camera <dean@fourwalledcubicle.com>
Sun, 21 Feb 2010 06:26:33 +0000 (06:26 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Sun, 21 Feb 2010 06:26:33 +0000 (06:26 +0000)
Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.c
Projects/AVRISP-MKII/Lib/ISP/ISPProtocol.h
Projects/AVRISP-MKII/Lib/ISP/ISPTarget.c
Projects/AVRISP-MKII/Lib/V2Protocol.c
Projects/AVRISP-MKII/Lib/V2Protocol.h
Projects/AVRISP-MKII/Lib/XPROG/TINYNVM.c
Projects/AVRISP-MKII/Lib/XPROG/XMEGANVM.c
Projects/AVRISP-MKII/Lib/XPROG/XPROGTarget.c

index e0eb441..85b31fb 100644 (file)
@@ -75,7 +75,7 @@ void ISPProtocol_EnterISPMode(void)
 \r
        /* Continuously attempt to synchronize with the target until either the number of attempts specified\r
         * by the host has exceeded, or the the device sends back the expected response values */\r
 \r
        /* Continuously attempt to synchronize with the target until either the number of attempts specified\r
         * by the host has exceeded, or the the device sends back the expected response values */\r
-       while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED))\r
+       while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED) && TimeoutMSRemaining)\r
        {\r
                uint8_t ResponseBytes[4];\r
 \r
        {\r
                uint8_t ResponseBytes[4];\r
 \r
@@ -518,4 +518,19 @@ void ISPProtocol_SPIMulti(void)
        }\r
 }\r
 \r
        }\r
 }\r
 \r
+/** Blocking delay for a given number of milliseconds.\r
+ *\r
+ *  \param[in] DelayMS  Number of milliseconds to delay for\r
+ */\r
+void ISPProtocol_DelayMS(uint8_t DelayMS)\r
+{\r
+       while (DelayMS-- && TimeoutMSRemaining)\r
+       {\r
+               if (TimeoutMSRemaining)\r
+                 TimeoutMSRemaining--;\r
+                 \r
+               _delay_ms(1);\r
+       }\r
+}\r
+\r
 #endif
\ No newline at end of file
 #endif
\ No newline at end of file
index 88e3534..a9114e1 100644 (file)
                #define PROG_MODE_PAGED_READYBUSY_MASK  (1 << 6)\r
                #define PROG_MODE_COMMIT_PAGE_MASK      (1 << 7)\r
 \r
                #define PROG_MODE_PAGED_READYBUSY_MASK  (1 << 6)\r
                #define PROG_MODE_COMMIT_PAGE_MASK      (1 << 7)\r
 \r
-       /* Inline Functions: */\r
-               /** Blocking delay for a given number of milliseconds.\r
-                *\r
-                *  \param[in] DelayMS  Number of milliseconds to delay for\r
-                */\r
-               static inline void ISPProtocol_DelayMS(uint8_t DelayMS)\r
-               {\r
-                       while (DelayMS--)\r
-                         _delay_ms(1);\r
-               }\r
-\r
        /* Function Prototypes: */\r
                void ISPProtocol_EnterISPMode(void);\r
                void ISPProtocol_LeaveISPMode(void);\r
        /* Function Prototypes: */\r
                void ISPProtocol_EnterISPMode(void);\r
                void ISPProtocol_LeaveISPMode(void);\r
@@ -86,5 +75,5 @@
                void ISPProtocol_ReadFuseLockSigOSCCAL(const uint8_t V2Command);\r
                void ISPProtocol_WriteFuseLock(const uint8_t V2Command);\r
                void ISPProtocol_SPIMulti(void);\r
                void ISPProtocol_ReadFuseLockSigOSCCAL(const uint8_t V2Command);\r
                void ISPProtocol_WriteFuseLock(const uint8_t V2Command);\r
                void ISPProtocol_SPIMulti(void);\r
-\r
+               void ISPProtocol_DelayMS(uint8_t DelayMS);\r
 #endif\r
 #endif\r
index 51ba1ec..94f1c3d 100644 (file)
@@ -112,7 +112,6 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                                       const uint8_t DelayMS, const uint8_t ReadMemCommand)\r
 {\r
        uint8_t ProgrammingStatus  = STATUS_CMD_OK;\r
                                       const uint8_t DelayMS, const uint8_t ReadMemCommand)\r
 {\r
        uint8_t ProgrammingStatus  = STATUS_CMD_OK;\r
-       uint8_t TimeoutMSRemaining = 100;\r
 \r
        /* Determine method of Programming Complete check */\r
        switch (ProgrammingMode & ~(PROG_MODE_PAGED_WRITES_MASK | PROG_MODE_COMMIT_PAGE_MASK))\r
 \r
        /* Determine method of Programming Complete check */\r
        switch (ProgrammingMode & ~(PROG_MODE_PAGED_WRITES_MASK | PROG_MODE_COMMIT_PAGE_MASK))\r
@@ -148,6 +147,9 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                        break;\r
        }\r
 \r
                        break;\r
        }\r
 \r
+       if (ProgrammingStatus == STATUS_CMD_OK)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+\r
        return ProgrammingStatus;\r
 }\r
 \r
        return ProgrammingStatus;\r
 }\r
 \r
@@ -158,8 +160,6 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
  */\r
 uint8_t ISPTarget_WaitWhileTargetBusy(void)\r
 {\r
  */\r
 uint8_t ISPTarget_WaitWhileTargetBusy(void)\r
 {\r
-       uint8_t TimeoutMSRemaining = 100;\r
-\r
        do\r
        {\r
                /* Manage software timeout */\r
        do\r
        {\r
                /* Manage software timeout */\r
@@ -175,7 +175,15 @@ uint8_t ISPTarget_WaitWhileTargetBusy(void)
        }\r
        while ((SPI_ReceiveByte() & 0x01) && TimeoutMSRemaining);\r
 \r
        }\r
        while ((SPI_ReceiveByte() & 0x01) && TimeoutMSRemaining);\r
 \r
-       return ((TimeoutMSRemaining) ? STATUS_CMD_OK : STATUS_RDY_BSY_TOUT);\r
+       if (TimeoutMSRemaining)\r
+       {\r
+               TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+               return STATUS_CMD_OK;\r
+       }\r
+       else\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
 }\r
 \r
 /** Sends a low-level LOAD EXTENDED ADDRESS command to the target, for addressing of memory beyond the\r
index f7e5e81..3df8045 100644 (file)
@@ -42,8 +42,6 @@ uint32_t CurrentAddress;
 /** Flag to indicate that the next read/write operation must update the device's current address */\r
 bool MustSetAddress;\r
 \r
 /** Flag to indicate that the next read/write operation must update the device's current address */\r
 bool MustSetAddress;\r
 \r
-bool CommandTimedOut;\r
-\r
 /** Initializes the hardware and software associated with the V2 protocol command handling. */\r
 void V2Protocol_Init(void)\r
 {\r
 /** Initializes the hardware and software associated with the V2 protocol command handling. */\r
 void V2Protocol_Init(void)\r
 {\r
@@ -70,7 +68,7 @@ void V2Protocol_ProcessCommand(void)
 {\r
        uint8_t V2Command = Endpoint_Read_Byte();\r
        \r
 {\r
        uint8_t V2Command = Endpoint_Read_Byte();\r
        \r
-       CommandTimedOut = false;\r
+       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
        \r
        switch (V2Command)\r
        {\r
        \r
        switch (V2Command)\r
        {\r
index 97fea47..26c42ab 100644 (file)
                /** Programmer ID string, returned to the host during the CMD_SIGN_ON command processing */\r
                #define PROGRAMMER_ID              "AVRISP_MK2"\r
                \r
                /** Programmer ID string, returned to the host during the CMD_SIGN_ON command processing */\r
                #define PROGRAMMER_ID              "AVRISP_MK2"\r
                \r
+               /** Timeout period for each issued command from the host before it is aborted */\r
+               #define COMMAND_TIMEOUT_MS         200\r
+               \r
+               /** Command timeout counter register, GPIOR for speed */\r
+               #define TimeoutMSRemaining         GPIOR0\r
+\r
                /** MUX mask for the VTARGET ADC channel number */\r
                #define VTARGET_ADC_CHANNEL_MASK   _GETADCMUXMASK(ADC_CHANNEL, VTARGET_ADC_CHANNEL)\r
 \r
                /** MUX mask for the VTARGET ADC channel number */\r
                #define VTARGET_ADC_CHANNEL_MASK   _GETADCMUXMASK(ADC_CHANNEL, VTARGET_ADC_CHANNEL)\r
 \r
index 428469d..a894793 100644 (file)
@@ -77,13 +77,15 @@ static void TINYNVM_SendWriteNVMRegister(const uint8_t Address)
 bool TINYNVM_WaitWhileNVMBusBusy(void)\r
 {\r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\r
 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
        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
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
+               }\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
@@ -104,7 +106,6 @@ bool TINYNVM_WaitWhileNVMBusBusy(void)
 bool TINYNVM_WaitWhileNVMControllerBusy(void)\r
 {\r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\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
        while (TimeoutMSRemaining)\r
        {\r
                /* Send the SIN command to read the TPI STATUS register to see the NVM bus is busy */\r
@@ -112,7 +113,10 @@ bool TINYNVM_WaitWhileNVMControllerBusy(void)
 \r
                /* Check to see if the BUSY flag is still set */\r
                if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
 \r
                /* Check to see if the BUSY flag is still set */\r
                if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
-                 return true;\r
+               {\r
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
+               }\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
index 6ef59db..f63f13a 100644 (file)
@@ -72,13 +72,15 @@ 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
                if (XPROGTarget_ReceiveByte() & PDI_STATUS_NVM)\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
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
+               }\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
@@ -99,7 +101,6 @@ 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
        while (TimeoutMSRemaining)\r
        {\r
                /* Send a LDS command to read the NVM STATUS register to check the BUSY flag */\r
@@ -108,7 +109,10 @@ bool XMEGANVM_WaitWhileNVMControllerBusy(void)
                \r
                /* Check to see if the BUSY flag is still set */\r
                if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
                \r
                /* Check to see if the BUSY flag is still set */\r
                if (!(XPROGTarget_ReceiveByte() & (1 << 7)))\r
-                 return true;\r
+               {\r
+                       TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+                       return true;\r
+               }\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
 \r
                /* Manage software timeout */\r
                if (TIFR0 & (1 << OCF0A))\r
index 45c1d22..7bef25e 100644 (file)
@@ -350,7 +350,6 @@ uint8_t XPROGTarget_ReceiveByte(void)
 \r
 #if defined(XPROG_VIA_HARDWARE_USART)\r
        /* Wait until a byte has been received before reading */\r
 \r
 #if defined(XPROG_VIA_HARDWARE_USART)\r
        /* Wait until a byte has been received before reading */\r
-       uint8_t TimeoutMSRemaining = 100;\r
        while (!(UCSR1A & (1 << RXC1)) && TimeoutMSRemaining)\r
        {\r
                /* Manage software timeout */\r
        while (!(UCSR1A & (1 << RXC1)) && TimeoutMSRemaining)\r
        {\r
                /* Manage software timeout */\r
@@ -365,7 +364,6 @@ uint8_t XPROGTarget_ReceiveByte(void)
 #else\r
        /* Wait until a byte has been received before reading */\r
        SoftUSART_BitCount = BITS_IN_USART_FRAME;\r
 #else\r
        /* Wait until a byte has been received before reading */\r
        SoftUSART_BitCount = BITS_IN_USART_FRAME;\r
-       uint8_t TimeoutMSRemaining = 100;\r
        while (SoftUSART_BitCount && TimeoutMSRemaining)\r
        {\r
                /* Manage software timeout */\r
        while (SoftUSART_BitCount && TimeoutMSRemaining)\r
        {\r
                /* Manage software timeout */\r
@@ -376,6 +374,9 @@ uint8_t XPROGTarget_ReceiveByte(void)
                }\r
        }\r
 \r
                }\r
        }\r
 \r
+       if (TimeoutMSRemaining)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+\r
        /* Throw away the parity and stop bits to leave only the data (start bit is already discarded) */\r
        return (uint8_t)SoftUSART_Data;\r
 #endif\r
        /* Throw away the parity and stop bits to leave only the data (start bit is already discarded) */\r
        return (uint8_t)SoftUSART_Data;\r
 #endif\r
@@ -468,7 +469,6 @@ static void XPROGTarget_SetRxMode(void)
        }\r
        \r
        /* Wait until DATA line has been pulled up to idle by the target */\r
        }\r
        \r
        /* Wait until DATA line has been pulled up to idle by the target */\r
-       uint8_t TimeoutMSRemaining = 100;\r
        while (!(BITBANG_PDIDATA_PIN & BITBANG_PDIDATA_MASK) && TimeoutMSRemaining)\r
        {\r
                /* Manage software timeout */\r
        while (!(BITBANG_PDIDATA_PIN & BITBANG_PDIDATA_MASK) && TimeoutMSRemaining)\r
        {\r
                /* Manage software timeout */\r
@@ -480,6 +480,9 @@ static void XPROGTarget_SetRxMode(void)
        }       \r
 #endif\r
 \r
        }       \r
 #endif\r
 \r
+    if (TimeoutMSRemaining)\r
+         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;\r
+\r
        IsSending = false;\r
 }\r
 \r
        IsSending = false;\r
 }\r
 \r