Ooops - TeensyHID bootloader makefile should use a 16MHz FCPU, not 8MHz.
[pub/USBasp.git] / Projects / AVRISP / Lib / PDITarget.c
index e21942a..29bb339 100644 (file)
@@ -167,7 +167,7 @@ void PDITarget_DisableTargetPDI(void)
  *\r
  *  \param[in] Byte  Byte to send through the USART\r
  */\r
  *\r
  *  \param[in] Byte  Byte to send through the USART\r
  */\r
-void PDITarget_SendByte(uint8_t Byte)\r
+void PDITarget_SendByte(const uint8_t Byte)\r
 {\r
 #if defined(PDI_VIA_HARDWARE_USART)\r
        /* Switch to Tx mode if currently in Rx mode */\r
 {\r
 #if defined(PDI_VIA_HARDWARE_USART)\r
        /* Switch to Tx mode if currently in Rx mode */\r
@@ -196,19 +196,18 @@ void PDITarget_SendByte(uint8_t Byte)
                IsSending = true;\r
        }\r
 \r
                IsSending = true;\r
        }\r
 \r
-       bool    EvenParityBit = false;\r
-       uint8_t ParityData    = Byte;\r
+       /* Calculate the new USART frame data here while while we wait for a previous byte (if any) to finish sending */\r
+       uint16_t NewUSARTData = ((1 << 11) | (1 << 10) | (0 << 9) | ((uint16_t)Byte << 1) | (0 << 0));\r
 \r
        /* Compute Even parity - while a bit is still set, chop off lowest bit and toggle parity bit */\r
 \r
        /* Compute Even parity - while a bit is still set, chop off lowest bit and toggle parity bit */\r
+       uint8_t ParityData    = Byte;\r
        while (ParityData)\r
        {\r
        while (ParityData)\r
        {\r
-               EvenParityBit ^= true;\r
-               ParityData    &= (ParityData - 1);\r
+               NewUSARTData ^= (1 << 9);\r
+               ParityData   &= (ParityData - 1);\r
        }\r
 \r
        }\r
 \r
-       /* Calculate the new USART frame data here while while we wait for a previous byte (if any) to finish sending */\r
-       uint16_t NewUSARTData = ((1 << 11) | (1 << 10) | ((uint16_t)EvenParityBit << 9) | ((uint16_t)Byte << 1) | (0 << 0));\r
-\r
+       /* Wait until transmitter is idle before writing new data */\r
        while (SoftUSART_BitCount);\r
 \r
        /* Data shifted out LSB first, START DATA PARITY STOP STOP */\r
        while (SoftUSART_BitCount);\r
 \r
        /* Data shifted out LSB first, START DATA PARITY STOP STOP */\r
@@ -258,7 +257,7 @@ uint8_t PDITarget_ReceiveByte(void)
        SoftUSART_BitCount = BITS_IN_FRAME;\r
        while (SoftUSART_BitCount);\r
        \r
        SoftUSART_BitCount = BITS_IN_FRAME;\r
        while (SoftUSART_BitCount);\r
        \r
-       /* Throw away the start, parity and stop bits to leave only the data */\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
 }\r
        return (uint8_t)SoftUSART_Data;\r
 #endif\r
 }\r
@@ -280,7 +279,7 @@ void PDITarget_SendBreak(void)
        }\r
 \r
        /* Need to do nothing for a full frame to send a BREAK */\r
        }\r
 \r
        /* Need to do nothing for a full frame to send a BREAK */\r
-       for (uint8_t i = 0; i <= BITS_IN_FRAME; i++)\r
+       for (uint8_t i = 0; i < BITS_IN_FRAME; i++)\r
        {\r
                /* Wait for a full cycle of the clock */\r
                while (PIND & (1 << 5));\r
        {\r
                /* Wait for a full cycle of the clock */\r
                while (PIND & (1 << 5));\r
@@ -312,14 +311,23 @@ void PDITarget_SendBreak(void)
 bool PDITarget_WaitWhileNVMBusBusy(void)\r
 {\r
        TCNT0 = 0;\r
 bool PDITarget_WaitWhileNVMBusBusy(void)\r
 {\r
        TCNT0 = 0;\r
-\r
+       TIFR0 = (1 << OCF1A);\r
+                       \r
+       uint8_t TimeoutMS = PDI_NVM_TIMEOUT_MS;\r
+       \r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\r
        /* Poll the STATUS register to check to see if NVM access has been enabled */\r
-       while (TCNT0 < PDI_NVM_TIMEOUT_MS)\r
+       while (TimeoutMS)\r
        {\r
                /* Send the LDCS command to read the PDI STATUS register to see the NVM bus is active */\r
                PDITarget_SendByte(PDI_CMD_LDCS | PDI_STATUS_REG);\r
                if (PDITarget_ReceiveByte() & PDI_STATUS_NVM)\r
                  return true;\r
        {\r
                /* Send the LDCS command to read the PDI STATUS register to see the NVM bus is active */\r
                PDITarget_SendByte(PDI_CMD_LDCS | PDI_STATUS_REG);\r
                if (PDITarget_ReceiveByte() & PDI_STATUS_NVM)\r
                  return true;\r
+\r
+               if (TIFR0 & (1 << OCF1A))\r
+               {\r
+                       TIFR0 = (1 << OCF1A);\r
+                       TimeoutMS--;\r
+               }\r
        }\r
        \r
        return false;\r
        }\r
        \r
        return false;\r