Fix endianness of LOAD_ADDRESS command data, add correct PROGRAM_FLASH_ISP/PROGRAM_EE...
authorDean Camera <dean@fourwalledcubicle.com>
Mon, 24 Aug 2009 13:39:47 +0000 (13:39 +0000)
committerDean Camera <dean@fourwalledcubicle.com>
Mon, 24 Aug 2009 13:39:47 +0000 (13:39 +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 2c29669..8d14802 100644 (file)
@@ -158,7 +158,7 @@ static void V2Protocol_Command_GetSetParam(uint8_t V2Command)
 \r
 static void V2Protocol_Command_LoadAddress(void)\r
 {\r
-       Endpoint_Read_Stream_LE(&CurrentAddress, sizeof(CurrentAddress));\r
+       Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress));\r
 \r
        Endpoint_ClearOUT();\r
        Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
@@ -282,14 +282,16 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
        \r
        if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)\r
        {\r
+               uint16_t StartAddress = (CurrentAddress & 0xFFFF);\r
+       \r
                /* Paged 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
+                       if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+                         Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK;\r
                          \r
                        SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);\r
                        SPI_SendByte(CurrentAddress >> 8);\r
@@ -298,11 +300,18 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                        \r
                        if (!(PollAddress) && (ByteToWrite != PollValue))\r
                        {\r
-                               if (V2Command == CMD_PROGRAM_FLASH_ISP)\r
-                                 PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte);\r
-                               else\r
-                                 PollAddress = (CurrentAddress & 0xFFFF);                              \r
+                               if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+                                 Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;\r
+                                 \r
+                               PollAddress = (CurrentAddress & 0xFFFF);                                \r
                        }\r
+                               \r
+                       /* Check if the endpoint bank is currently empty */\r
+                       if (!(Endpoint_IsReadWriteAllowed()))\r
+                       {\r
+                               Endpoint_ClearOUT();\r
+                               Endpoint_WaitUntilReady();\r
+                       }                       \r
 \r
                        if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
                          CurrentAddress++;\r
@@ -312,8 +321,8 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK)\r
                {\r
                        SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]);\r
-                       SPI_SendByte(CurrentAddress >> 8);\r
-                       SPI_SendByte(CurrentAddress & 0xFF);\r
+                       SPI_SendByte(StartAddress >> 8);\r
+                       SPI_SendByte(StartAddress & 0xFF);\r
                        SPI_SendByte(0x00);\r
                        \r
                        /* Check if polling is possible, if not switch to timed delay mode */\r
@@ -322,11 +331,10 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                                Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK;\r
                                Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_TIMEDELAY_MASK;                         \r
                        }\r
+\r
+                       ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,\r
+                                                                                                                          Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);\r
                }\r
-               \r
-               ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,\r
-                                                                      Write_Memory_Params.DelayMS, (V2Command == CMD_READ_FLASH_ISP),\r
-                                                                      Write_Memory_Params.ProgrammingCommands[2]);\r
        }\r
        else\r
        {\r
@@ -336,8 +344,8 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                        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
+                       if (IsOddByte && (V2Command == CMD_READ_FLASH_ISP))\r
+                         Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK;\r
                          \r
                        SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);\r
                        SPI_SendByte(CurrentAddress >> 8);\r
@@ -346,18 +354,24 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
                        \r
                        if (ByteToWrite != PollValue)\r
                        {\r
-                               if (V2Command == CMD_PROGRAM_FLASH_ISP)\r
-                                 PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte);\r
-                               else\r
-                                 PollAddress = (CurrentAddress & 0xFFFF);\r
+                               if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+                                 Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;\r
+                                 \r
+                               PollAddress = (CurrentAddress & 0xFFFF);\r
                        }\r
                        \r
+                       /* Check if the endpoint bank is currently empty */\r
+                       if (!(Endpoint_IsReadWriteAllowed()))\r
+                       {\r
+                               Endpoint_ClearOUT();\r
+                               Endpoint_WaitUntilReady();\r
+                       }       \r
+\r
                        if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
                          CurrentAddress++;\r
                        \r
                        ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,\r
-                                                                          Write_Memory_Params.DelayMS, (V2Command == CMD_READ_FLASH_ISP),\r
-                                                                          Write_Memory_Params.ProgrammingCommands[2]);\r
+                                                                          Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);\r
                          \r
                        if (ProgrammingStatus != STATUS_CMD_OK)\r
                          break;\r
@@ -393,7 +407,7 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
        for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)\r
        {\r
                if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))\r
-                 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_ODD_BYTE_MASK;\r
+                 Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;\r
 \r
                SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);\r
                SPI_SendByte(CurrentAddress >> 8);\r
index 55c1c78..ce7c1ee 100644 (file)
@@ -47,7 +47,7 @@
        /* Macros: */
                #define PROGRAMMER_ID                   "AVRISP_MK2"\r
                \r
-               #define READ_WRITE_ODD_BYTE_MASK        (1 << 3)\r
+               #define READ_WRITE_HIGH_BYTE_MASK       (1 << 3)\r
 \r
                #define PROG_MODE_PAGED_WRITES_MASK     (1 << 0)\r
                #define PROG_MODE_WORD_TIMEDELAY_MASK   (1 << 1)\r
index f9b4427..d259917 100644 (file)
@@ -83,7 +83,7 @@ void V2Protocol_DelayMS(uint8_t MS)
 }\r
 \r
 uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAddress, uint8_t PollValue,\r
-                                       uint8_t DelayMS, bool IsFlashMemory, uint8_t ReadMemCommand)\r
+                                       uint8_t DelayMS, uint8_t ReadMemCommand)\r
 {\r
        uint8_t ProgrammingStatus = STATUS_CMD_OK;\r
 \r
@@ -96,12 +96,6 @@ uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAdd
                        break;\r
                case PROG_MODE_WORD_VALUE_MASK:\r
                case PROG_MODE_PAGED_VALUE_MASK:\r
-                       if (IsFlashMemory && (PollAddress & 0x01))\r
-                       {\r
-                               ReadMemCommand |= READ_WRITE_ODD_BYTE_MASK;\r
-                               PollAddress >>= 1;\r
-                       }\r
-\r
                        TCNT0  = 0;\r
 \r
                        do\r
@@ -119,6 +113,7 @@ uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAdd
                case PROG_MODE_WORD_READYBUSY_MASK:\r
                case PROG_MODE_PAGED_READYBUSY_MASK:\r
                        ProgrammingStatus = V2Protocol_WaitWhileTargetBusy();\r
+                       break;\r
        }\r
 \r
        return ProgrammingStatus;\r
@@ -126,8 +121,6 @@ uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAdd
 \r
 uint8_t V2Protocol_WaitWhileTargetBusy(void)\r
 {\r
-       uint8_t ResponseByte;\r
-       \r
        TCNT0  = 0;\r
        \r
        do\r
@@ -136,9 +129,8 @@ uint8_t V2Protocol_WaitWhileTargetBusy(void)
                SPI_SendByte(0x00);\r
 \r
                SPI_SendByte(0x00);\r
-               ResponseByte = SPI_ReceiveByte();\r
        }\r
-       while ((ResponseByte & 0x01) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS));\r
+       while ((SPI_ReceiveByte() & 0x01) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS));\r
 \r
        if (TCNT0 >= TARGET_BUSY_TIMEOUT_MS)\r
          return STATUS_RDY_BSY_TOUT;\r
index d67f847..2156225 100644 (file)
@@ -48,7 +48,7 @@
                #include "V2ProtocolParams.h"\r
 \r
        /* Macros: */\r
-               #define TARGET_BUSY_TIMEOUT_MS    200\r
+               #define TARGET_BUSY_TIMEOUT_MS    150\r
        \r
        /* External Variables: */\r
                extern uint32_t CurrentAddress;\r
@@ -58,7 +58,7 @@
                        void    V2Protocol_ChangeTargetResetLine(bool ResetTarget);\r
                        void    V2Protocol_DelayMS(uint8_t MS);\r
                        uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAddress, uint8_t PollValue,\r
-                                                   uint8_t DelayMS, bool IsFlashMemory, uint8_t ReadMemCommand);\r
+                                                   uint8_t DelayMS, uint8_t ReadMemCommand);\r
                        uint8_t V2Protocol_WaitWhileTargetBusy(void);\r
                        void    V2Protocol_LoadExtendedAddress(void);\r
 \r