*\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
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
+ uint8_t ParityData = Byte;\r
while (ParityData)\r
{\r
- EvenParityBit ^= true;\r
- ParityData &= (ParityData - 1);\r
+ NewUSARTData ^= (1 << 9);\r
+ ParityData &= (ParityData - 1);\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
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
}\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
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
- 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
+ if (TIFR0 & (1 << OCF1A))\r
+ {\r
+ TIFR0 = (1 << OCF1A);\r
+ TimeoutMS--;\r
+ }\r
}\r
\r
return false;\r