From 92adb00f8eebab41853f95bbc0de8a2006c6df3d Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Mon, 24 Aug 2009 11:04:05 +0000 Subject: [PATCH] Added PROGRAM_FLASH_ISP and PROGRAM_EEPROM_ISP handler code to the V2 Protocol handler in the AVRISP project, programming complete checks not yet complete. --- Projects/Incomplete/AVRISP/Lib/V2Protocol.c | 64 ++++++++++++++++--- Projects/Incomplete/AVRISP/Lib/V2Protocol.h | 8 +++ .../Incomplete/AVRISP/Lib/V2ProtocolTarget.c | 7 ++ .../Incomplete/AVRISP/Lib/V2ProtocolTarget.h | 1 + 4 files changed, 71 insertions(+), 9 deletions(-) diff --git a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c index 7bb8f71ca3..16a1a07086 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c +++ b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c @@ -259,26 +259,37 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) uint8_t PollValue2; } Write_Memory_Params; - uint8_t ProgrammingStatus = STATUS_CMD_OK; - Endpoint_Read_Stream_LE(&Write_Memory_Params, sizeof(Write_Memory_Params)); Write_Memory_Params.BytesToWrite = SwapEndian_16(Write_Memory_Params.BytesToWrite); + uint8_t ProgrammingStatus = STATUS_CMD_OK; + uint16_t PollAddress = 0; + if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK) { + /* Paged mode memory programming */ for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++) { - if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01)) + bool IsOddByte = (CurrentByte & 0x01); + uint8_t ByteToWrite = Endpoint_Read_Byte(); + + if ((V2Command == CMD_READ_FLASH_ISP) && IsOddByte) Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK; SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]); SPI_SendByte(CurrentAddress >> 8); SPI_SendByte(CurrentAddress & 0xFF); - SPI_SendByte(Endpoint_Read_Byte()); + SPI_SendByte(ByteToWrite); - // TODO - Correct Polling - - if (((V2Command == CMD_PROGRAM_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_PROGRAM_EEPROM_ISP)) + if (!(PollAddress)) + { + if ((ByteToWrite != Write_Memory_Params.PollValue1) && (V2Command == CMD_PROGRAM_FLASH_ISP)) + PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte); + else if ((ByteToWrite != Write_Memory_Params.PollValue2) && (V2Command == CMD_PROGRAM_EEPROM_ISP)) + PollAddress = (CurrentAddress & 0xFFFF); + } + + if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP)) CurrentAddress++; } @@ -289,11 +300,46 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) SPI_SendByte(CurrentAddress >> 8); SPI_SendByte(CurrentAddress & 0xFF); SPI_SendByte(0x00); + + /* Check if polling is possible, if not switch to timed delay mode */ + if (!(PollAddress)) + { + Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK; + Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_TIMEDELAY_MASK; + } } + + ProgrammingStatus = V2Protocol_WaitForProgrammingComplete(PollAddress, Write_Memory_Params.ProgrammingMode); } else - { - // TODO - Read in programming data, write to device + { + /* Word/byte mode memory programming */ + for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++) + { + bool IsOddByte = (CurrentByte & 0x01); + uint8_t ByteToWrite = Endpoint_Read_Byte(); + + if ((V2Command == CMD_READ_FLASH_ISP) && IsOddByte) + Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_ODD_BYTE_MASK; + + SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]); + SPI_SendByte(CurrentAddress >> 8); + SPI_SendByte(CurrentAddress & 0xFF); + SPI_SendByte(ByteToWrite); + + if ((ByteToWrite != Write_Memory_Params.PollValue1) && (V2Command == CMD_PROGRAM_FLASH_ISP)) + PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte); + else if ((ByteToWrite != Write_Memory_Params.PollValue2) && (V2Command == CMD_PROGRAM_EEPROM_ISP)) + PollAddress = (CurrentAddress & 0xFFFF); + + ProgrammingStatus = V2Protocol_WaitForProgrammingComplete(PollAddress, Write_Memory_Params.ProgrammingMode); + + if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP)) + CurrentAddress++; + + if (ProgrammingStatus != STATUS_CMD_OK) + break; + } } Endpoint_ClearOUT(); diff --git a/Projects/Incomplete/AVRISP/Lib/V2Protocol.h b/Projects/Incomplete/AVRISP/Lib/V2Protocol.h index 39406e465b..cd9648253f 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2Protocol.h +++ b/Projects/Incomplete/AVRISP/Lib/V2Protocol.h @@ -48,7 +48,15 @@ #define PROGRAMMER_ID "AVRISP_MK2" #define READ_WRITE_ODD_BYTE_MASK (1 << 3) + #define PROG_MODE_PAGED_WRITES_MASK (1 << 0) + #define PROG_MODE_WORD_TIMEDELAY_MASK (1 << 1) + #define PROG_MODE_WORD_VALUE_MASK (1 << 2) + #define PROG_MODE_WORD_READYBUSY_MASK (1 << 3) + #define PROG_MODE_PAGED_TIMEDELAY_MASK (1 << 4) + #define PROG_MODE_PAGED_VALUE_MASK (1 << 5) + #define PROG_MODE_PAGED_READYBUSY_MASK (1 << 6) + #define PROG_MODE_COMMIT_PAGE_MASK (1 << 7) /* Function Prototypes: */ diff --git a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c index caa7010ba1..b2064fc26c 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c +++ b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c @@ -82,6 +82,13 @@ void V2Protocol_DelayMS(uint8_t MS) _delay_ms(1); } +uint8_t V2Protocol_WaitForProgrammingComplete(uint16_t PollAddress, uint8_t ProgrammingMode) +{ + // TODO + + return STATUS_CMD_OK; +} + uint8_t V2Protocol_WaitWhileTargetBusy(void) { uint8_t TimeoutMS = TARGET_BUSY_TIMEOUT_MS; diff --git a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h index b7b593cdb9..847770c7b8 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h +++ b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h @@ -57,6 +57,7 @@ uint8_t V2Protocol_GetSPIPrescalerMask(void); void V2Protocol_ChangeTargetResetLine(bool ResetTarget); void V2Protocol_DelayMS(uint8_t MS); + uint8_t V2Protocol_WaitForProgrammingComplete(uint16_t PollAddress, uint8_t ProgrammingMode); uint8_t V2Protocol_WaitWhileTargetBusy(void); void V2Protocol_LoadExtendedAddress(void);