\r
/* Enable access to the XPROG NVM bus by sending the documented NVM access key to the device */\r
PDITarget_SendByte(PDI_CMD_KEY); \r
- for (uint8_t i = 0; i < sizeof(PDI_NVMENABLE_KEY); i++)\r
- PDITarget_SendByte(PDI_NVMENABLE_KEY[i]);\r
+ for (uint8_t i = sizeof(PDI_NVMENABLE_KEY); i > 0; i--)\r
+ PDITarget_SendByte(PDI_NVMENABLE_KEY[i - 1]);\r
\r
/* Poll the STATUS register to check to see if NVM access has been enabled */\r
- uint8_t NVMAttemptsRemaining = 200;\r
- while (NVMAttemptsRemaining--)\r
+ uint8_t NVMAttemptsRemaining = 150;\r
+ while (NVMAttemptsRemaining)\r
{\r
_delay_ms(1);\r
- PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG);\r
\r
+ PDITarget_SendByte(PDI_CMD_LDCS | PD_STATUS_REG);\r
if (PDITarget_ReceiveByte() & PDI_STATUS_NVM)\r
break;\r
+\r
+ NVMAttemptsRemaining--;\r
}\r
\r
Endpoint_Write_Byte(CMD_XPROG);\r
#define INCLUDE_FROM_PDITARGET_C\r
#include "PDITarget.h"\r
\r
-#if !defined(PDI_VIA_HARDWARE_USART)\r
volatile bool IsSending;\r
+\r
+#if !defined(PDI_VIA_HARDWARE_USART)\r
volatile uint16_t DataBits;\r
volatile uint8_t BitCount;\r
\r
\r
void PDITarget_SendByte(uint8_t Byte)\r
{\r
- UCSR1B &= ~(1 << RXEN1);\r
- UCSR1B |= (1 << TXEN1);\r
+ /* Switch to Tx mode if currently in Rx mode */\r
+ if (!(IsSending))\r
+ {\r
+ PORTD |= (1 << 3);\r
+ DDRD |= (1 << 3);\r
\r
+ UCSR1B &= ~(1 << RXEN1);\r
+ UCSR1B |= (1 << TXEN1);\r
+ \r
+ IsSending = true;\r
+ }\r
+ \r
+ /* Wait until there is space in the hardware Tx buffer before writing */\r
+ while (!(UCSR1A & (1 << UDRE1)));\r
UDR1 = Byte;\r
-\r
- while (!(UCSR1A & (1 << TXC1)));\r
- UCSR1A |= (1 << TXC1);\r
}\r
\r
uint8_t PDITarget_ReceiveByte(void)\r
{\r
- UCSR1B &= ~(1 << TXEN1);\r
- UCSR1B |= (1 << RXEN1);\r
+ /* Switch to Rx mode if currently in Tx mode */\r
+ if (IsSending)\r
+ {\r
+ while (!(UCSR1A & (1 << TXC1)));\r
+ UCSR1A |= (1 << TXC1);\r
\r
+ UCSR1B &= ~(1 << TXEN1);\r
+ UCSR1B |= (1 << RXEN1);\r
+\r
+ DDRD &= ~(1 << 3);\r
+ PORTD &= ~(1 << 3);\r
+ \r
+ IsSending = false;\r
+ }\r
+\r
+ /* Wait until a byte has been received before reading */\r
while (!(UCSR1A & (1 << RXC1)));\r
- UCSR1A |= (1 << RXC1);\r
- \r
return UDR1;\r
}\r
\r
void PDITarget_SendBreak(void)\r
{\r
- UCSR1B &= ~(1 << RXEN1);\r
- UCSR1B |= (1 << TXEN1);\r
+ /* Switch to Tx mode if currently in Rx mode */\r
+ if (!(IsSending))\r
+ {\r
+ PORTD |= (1 << 3);\r
+ DDRD |= (1 << 3);\r
+\r
+ UCSR1B &= ~(1 << RXEN1);\r
+ UCSR1B |= (1 << TXEN1);\r
+ \r
+ IsSending = true;\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
{\r
/* Wait for rising edge of clock */\r