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
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
#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