Fixed V2 Protocol READ_FLASH_ISP and READ_EEPROM_ISP handler code in the AVRISP project.
[pub/USBasp.git] / Projects / Incomplete / AVRISP / Lib / V2Protocol.c
index 85e3a38..7bb8f71 100644 (file)
@@ -264,9 +264,36 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
        Endpoint_Read_Stream_LE(&Write_Memory_Params, sizeof(Write_Memory_Params));\r
        Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite);\r
        \r
-       for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)\r
+       if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)\r
        {\r
-               // TODO - Read in programming data, write to device\r
+               for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)\r
+               {\r
+                       if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))\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
+                       \r
+                       // TODO - Correct Polling\r
+\r
+                       if (((V2Command == CMD_PROGRAM_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
+                         CurrentAddress++;\r
+               }\r
+               \r
+               /* If the current page must be committed, send the PROGRAM PAGE command to the target */\r
+               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(0x00);\r
+               }\r
+       }\r
+       else\r
+       {                       \r
+               // TODO - Read in programming data, write to device             \r
        }\r
        \r
        Endpoint_ClearOUT();\r
@@ -294,7 +321,7 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
        \r
        Endpoint_Write_Byte(V2Command);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
-\r
+       \r
        for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)\r
        {\r
                if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))\r
@@ -312,20 +339,12 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
                        Endpoint_WaitUntilReady();\r
                }\r
                \r
-               CurrentAddress++;\r
+               if (((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_READ_EEPROM_ISP))\r
+                 CurrentAddress++;\r
        }\r
 \r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
-       \r
-       bool EndpointBankFull = Endpoint_IsReadWriteAllowed();\r
        Endpoint_ClearIN();\r
-       \r
-       /* Ensure data transfer is terminated by a short packet if the last sent bank was completely full */\r
-       if (EndpointBankFull)\r
-       {\r
-               Endpoint_WaitUntilReady();\r
-               Endpoint_ClearIN();\r
-       }\r
 }\r
 \r
 static void V2Protocol_Command_ChipErase(void)\r
@@ -377,7 +396,7 @@ static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command)
                \r
        Endpoint_Write_Byte(V2Command);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
-       Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte]);\r
+       Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]);\r
        Endpoint_Write_Byte(STATUS_CMD_OK);\r
        Endpoint_ClearIN();\r
 }\r