Fix PDI code - must send NVM enable key least significant byte first, need to make...
authorDean Camera <dean@fourwalledcubicle.com>
Fri, 11 Dec 2009 04:04:34 +0000 (04:04 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Fri, 11 Dec 2009 04:04:34 +0000 (04:04 +0000)
Projects/AVRISP/Lib/PDIProtocol.c
Projects/AVRISP/Lib/PDITarget.c
Projects/AVRISP/makefile

index 28bd540..32a74c5 100644 (file)
@@ -111,18 +111,20 @@ static void PDIProtocol_EnterXPROGMode(void)
 \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
index d012a1b..22a4d01 100644 (file)
@@ -38,8 +38,9 @@
 #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
@@ -192,31 +193,60 @@ void PDITarget_DisableTargetPDI(void)
 \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
index 37c0e72..003eaa5 100644 (file)
@@ -66,7 +66,7 @@ MCU = at90usb1287
 # Target board (see library "Board Types" documentation, USER or blank for projects not requiring\r
 # LUFA board drivers). If USER is selected, put custom board drivers in a directory called \r
 # "Board" inside the application directory.\r
-BOARD  = XPLAIN\r
+BOARD = XPLAIN\r
 \r
 \r
 # Processor frequency.\r