Use static inline intermediatary functions in Endpoint.h to handle the masking for...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPTarget.c
index d7c6882..2df8710 100644 (file)
 
 #if defined(ENABLE_ISP_PROTOCOL) || defined(__DOXYGEN__)
 
+/** List of SPI prescaler masks for possible AVRStudio ISP programming speeds. */
+static uint8_t SPIMaskFromSCKDuration[] PROGMEM =
+{
+#if (F_CPU == 8000000)
+       SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   4MHz SPI
+       SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
+       SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
+       SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
+       SPI_SPEED_FCPU_DIV_16,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
+       SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
+       SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
+#elif (F_CPU == 16000000)
+       SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   8MHz SPI
+       SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
+       SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
+       SPI_SPEED_FCPU_DIV_16,   // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
+       SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
+       SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
+       SPI_SPEED_FCPU_DIV_128   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
+#else
+       #error No SPI prescaler masks for chosen F_CPU speed.
+#endif
+};
+
 /** Converts the given AVR Studio SCK duration parameter (set by a SET PARAM command from the host) to the nearest
  *  possible SPI clock prescaler mask for passing to the SPI_Init() routine.
  *
  */
 uint8_t ISPTarget_GetSPIPrescalerMask(void)
 {
-       static const uint8_t SPIMaskFromSCKDuration[] =
-       {
-       #if (F_CPU == 8000000)
-               SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   4MHz SPI
-               SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
-               SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
-               SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
-               SPI_SPEED_FCPU_DIV_16,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
-               SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
-               SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
-       #elif (F_CPU == 16000000)
-               SPI_SPEED_FCPU_DIV_2,    // AVRStudio =   8MHz SPI, Actual =   8MHz SPI
-               SPI_SPEED_FCPU_DIV_4,    // AVRStudio =   4MHz SPI, Actual =   4MHz SPI
-               SPI_SPEED_FCPU_DIV_8,    // AVRStudio =   2MHz SPI, Actual =   2MHz SPI
-               SPI_SPEED_FCPU_DIV_16,   // AVRStudio =   1MHz SPI, Actual =   1MHz SPI
-               SPI_SPEED_FCPU_DIV_32,   // AVRStudio = 500KHz SPI, Actual = 500KHz SPI
-               SPI_SPEED_FCPU_DIV_64,   // AVRStudio = 250KHz SPI, Actual = 250KHz SPI
-               SPI_SPEED_FCPU_DIV_128   // AVRStudio = 125KHz SPI, Actual = 125KHz SPI
-       #else
-               #error No SPI prescaler masks for chosen F_CPU speed.
-       #endif
-       };
-
        uint8_t SCKDuration = V2Params_GetParameterValue(PARAM_SCK_DURATION);
 
        if (SCKDuration >= sizeof(SPIMaskFromSCKDuration))
          SCKDuration = (sizeof(SPIMaskFromSCKDuration) - 1);
          
-       return SPIMaskFromSCKDuration[SCKDuration];
+       return pgm_read_byte(&SPIMaskFromSCKDuration[SCKDuration]);
 }
 
 /** Asserts or deasserts the target's reset line, using the correct polarity as set by the host using a SET PARAM command.
@@ -108,8 +109,11 @@ void ISPTarget_ChangeTargetResetLine(const bool ResetTarget)
  *  \return V2 Protocol status \ref STATUS_CMD_OK if the no timeout occurred, \ref STATUS_RDY_BSY_TOUT or
  *          \ref STATUS_CMD_TOUT otherwise
  */
-uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint16_t PollAddress, const uint8_t PollValue,
-                                      const uint8_t DelayMS, const uint8_t ReadMemCommand)
+uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode,
+                                      const uint16_t PollAddress,
+                                      const uint8_t PollValue,
+                                      const uint8_t DelayMS,
+                                      const uint8_t ReadMemCommand)
 {
        uint8_t ProgrammingStatus  = STATUS_CMD_OK;
 
@@ -128,9 +132,9 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                                SPI_SendByte(PollAddress >> 8);
                                SPI_SendByte(PollAddress & 0xFF);
                        }
-                       while ((SPI_TransferByte(0x00) == PollValue) && TimeoutMSRemaining);
+                       while ((SPI_TransferByte(0x00) == PollValue) && TimeoutTicksRemaining);
 
-                       if (!(TimeoutMSRemaining))
+                       if (!(TimeoutTicksRemaining))
                         ProgrammingStatus = STATUS_CMD_TOUT;
                        
                        break;          
@@ -140,9 +144,6 @@ uint8_t ISPTarget_WaitForProgComplete(const uint8_t ProgrammingMode, const uint1
                        break;
        }
 
-       if (ProgrammingStatus == STATUS_CMD_OK)
-         TimeoutMSRemaining = COMMAND_TIMEOUT_MS;
-
        return ProgrammingStatus;
 }
 
@@ -159,17 +160,9 @@ uint8_t ISPTarget_WaitWhileTargetBusy(void)
                SPI_SendByte(0x00);
                SPI_SendByte(0x00);
        }
-       while ((SPI_ReceiveByte() & 0x01) && TimeoutMSRemaining);
+       while ((SPI_ReceiveByte() & 0x01) && TimeoutTicksRemaining);
 
-       if (TimeoutMSRemaining)
-       {
-               TimeoutMSRemaining = COMMAND_TIMEOUT_MS;
-               return STATUS_CMD_OK;
-       }
-       else
-       {
-               return STATUS_RDY_BSY_TOUT;
-       }
+       return TimeoutTicksRemaining ? STATUS_CMD_OK : STATUS_RDY_BSY_TOUT;
 }
 
 /** Sends a low-level LOAD EXTENDED ADDRESS command to the target, for addressing of memory beyond the