Added PROGRAM_FLASH_ISP and PROGRAM_EEPROM_ISP handler code to the V2 Protocol handle...
authorDean Camera <dean@fourwalledcubicle.com>
Mon, 24 Aug 2009 11:04:05 +0000 (11:04 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Mon, 24 Aug 2009 11:04:05 +0000 (11:04 +0000)
Projects/Incomplete/AVRISP/Lib/V2Protocol.c
Projects/Incomplete/AVRISP/Lib/V2Protocol.h
Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c
Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h

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
index 39406e4..cd96482 100644 (file)
                #define PROGRAMMER_ID                   "AVRISP_MK2"\r
                \r
                #define READ_WRITE_ODD_BYTE_MASK        (1 << 3)\r
+\r
                #define PROG_MODE_PAGED_WRITES_MASK     (1 << 0)\r
+               #define PROG_MODE_WORD_TIMEDELAY_MASK   (1 << 1)\r
+               #define PROG_MODE_WORD_VALUE_MASK       (1 << 2)\r
+               #define PROG_MODE_WORD_READYBUSY_MASK   (1 << 3)\r
+               #define PROG_MODE_PAGED_TIMEDELAY_MASK  (1 << 4)\r
+               #define PROG_MODE_PAGED_VALUE_MASK      (1 << 5)\r
+               #define PROG_MODE_PAGED_READYBUSY_MASK  (1 << 6)\r
+\r
                #define PROG_MODE_COMMIT_PAGE_MASK      (1 << 7)\r
 
        /* Function Prototypes: */\r
index caa7010..b2064fc 100644 (file)
@@ -82,6 +82,13 @@ void V2Protocol_DelayMS(uint8_t MS)
          _delay_ms(1);\r
 }\r
 \r
+uint8_t V2Protocol_WaitForProgrammingComplete(uint16_t PollAddress, uint8_t ProgrammingMode)\r
+{\r
+       // TODO\r
+\r
+       return STATUS_CMD_OK;\r
+}\r
+\r
 uint8_t V2Protocol_WaitWhileTargetBusy(void)\r
 {\r
        uint8_t TimeoutMS = TARGET_BUSY_TIMEOUT_MS;\r
index b7b593c..847770c 100644 (file)
@@ -57,6 +57,7 @@
                        uint8_t V2Protocol_GetSPIPrescalerMask(void);\r
                        void    V2Protocol_ChangeTargetResetLine(bool ResetTarget);\r
                        void    V2Protocol_DelayMS(uint8_t MS);\r
+                       uint8_t V2Protocol_WaitForProgrammingComplete(uint16_t PollAddress, uint8_t ProgrammingMode);\r
                        uint8_t V2Protocol_WaitWhileTargetBusy(void);\r
                        void    V2Protocol_LoadExtendedAddress(void);\r
 \r