Added PROGRAM_FLASH_ISP and PROGRAM_EEPROM_ISP handler code to the V2 Protocol handle...
[pub/lufa.git] / Projects / Incomplete / AVRISP / Lib / V2Protocol.c
index 7bb8f71..16a1a07 100644 (file)
@@ -259,26 +259,37 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                uint8_t  PollValue2;\r
        } Write_Memory_Params;\r
        \r
-       uint8_t ProgrammingStatus = STATUS_CMD_OK;\r
-       \r
        Endpoint_Read_Stream_LE(&Write_Memory_Params, sizeof(Write_Memory_Params));\r
        Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);\r
        \r
+       uint8_t  ProgrammingStatus = STATUS_CMD_OK;     \r
+       uint16_t PollAddress       = 0;\r
+       \r
        if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)\r
        {\r
+               /* Paged mode memory programming */\r
                for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)\r
                {\r
-                       if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))\r
+                       bool    IsOddByte   = (CurrentByte & 0x01);\r
+                       uint8_t ByteToWrite = Endpoint_Read_Byte();\r
+               \r
+                       if ((V2Command == CMD_READ_FLASH_ISP) && IsOddByte)\r
                          Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK;\r
                          \r
                        SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);\r
                        SPI_SendByte(CurrentAddress >> 8);\r
                        SPI_SendByte(CurrentAddress & 0xFF);\r
-                       SPI_SendByte(Endpoint_Read_Byte());\r
+                       SPI_SendByte(ByteToWrite);\r
                        \r
-                       // TODO - Correct Polling\r
-\r
-                       if (((V2Command == CMD_PROGRAM_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
+                       if (!(PollAddress))\r
+                       {\r
+                               if ((ByteToWrite != Write_Memory_Params.PollValue1) && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+                                 PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte);\r
+                               else if ((ByteToWrite != Write_Memory_Params.PollValue2) && (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
+                                 PollAddress = (CurrentAddress & 0xFFFF);                              \r
+                       }\r
+\r
+                       if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
                          CurrentAddress++;\r
                }\r
                \r
@@ -289,11 +300,46 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                        SPI_SendByte(CurrentAddress >> 8);\r
                        SPI_SendByte(CurrentAddress & 0xFF);\r
                        SPI_SendByte(0x00);\r
+                       \r
+                       /* Check if polling is possible, if not switch to timed delay mode */\r
+                       if (!(PollAddress))\r
+                       {\r
+                               Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK;\r
+                               Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_TIMEDELAY_MASK;                         \r
+                       }\r
                }\r
+               \r
+               ProgrammingStatus = V2Protocol_WaitForProgrammingComplete(PollAddress, Write_Memory_Params.ProgrammingMode);\r
        }\r
        else\r
-       {                       \r
-               // TODO - Read in programming data, write to device             \r
+       {\r
+               /* Word/byte mode memory programming */\r
+               for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)\r
+               {\r
+                       bool    IsOddByte   = (CurrentByte & 0x01);\r
+                       uint8_t ByteToWrite = Endpoint_Read_Byte();\r
+               \r
+                       if ((V2Command == CMD_READ_FLASH_ISP) && IsOddByte)\r
+                         Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK;\r
+                         \r
+                       SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);\r
+                       SPI_SendByte(CurrentAddress >> 8);\r
+                       SPI_SendByte(CurrentAddress & 0xFF);\r
+                       SPI_SendByte(ByteToWrite);\r
+                       \r
+                       if ((ByteToWrite != Write_Memory_Params.PollValue1) && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+                         PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte);\r
+                       else if ((ByteToWrite != Write_Memory_Params.PollValue2) && (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
+                         PollAddress = (CurrentAddress & 0xFFFF);\r
+\r
+                       ProgrammingStatus = V2Protocol_WaitForProgrammingComplete(PollAddress, Write_Memory_Params.ProgrammingMode);\r
+\r
+                       if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
+                         CurrentAddress++;\r
+                         \r
+                       if (ProgrammingStatus != STATUS_CMD_OK)\r
+                         break;\r
+               }\r
        }\r
        \r
        Endpoint_ClearOUT();\r