From f070902bdb96795c497bce283288affaa9a4fc5b Mon Sep 17 00:00:00 2001 From: Dean Camera Date: Mon, 24 Aug 2009 13:39:47 +0000 Subject: Fix endianness of LOAD_ADDRESS command data, add correct PROGRAM_FLASH_ISP/PROGRAM_EEPROM_ISP polling and endpoint bank clearing, add hardware delays/timeouts to the V2 Protocol handler in the AVRISP project. --- Projects/Incomplete/AVRISP/Lib/V2Protocol.c | 58 ++++++++++++++--------- Projects/Incomplete/AVRISP/Lib/V2Protocol.h | 2 +- Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c | 14 ++---- Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h | 4 +- 4 files changed, 42 insertions(+), 36 deletions(-) (limited to 'Projects/Incomplete') diff --git a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c index 2c2966906..8d1480269 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c +++ b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c @@ -158,7 +158,7 @@ static void V2Protocol_Command_GetSetParam(uint8_t V2Command) static void V2Protocol_Command_LoadAddress(void) { - Endpoint_Read_Stream_LE(&CurrentAddress, sizeof(CurrentAddress)); + Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress)); Endpoint_ClearOUT(); Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); @@ -282,14 +282,16 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK) { + uint16_t StartAddress = (CurrentAddress & 0xFFFF); + /* Paged 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; + if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP)) + Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK; SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]); SPI_SendByte(CurrentAddress >> 8); @@ -298,11 +300,18 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) if (!(PollAddress) && (ByteToWrite != PollValue)) { - if (V2Command == CMD_PROGRAM_FLASH_ISP) - PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte); - else - PollAddress = (CurrentAddress & 0xFFFF); + if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP)) + Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK; + + PollAddress = (CurrentAddress & 0xFFFF); } + + /* Check if the endpoint bank is currently empty */ + if (!(Endpoint_IsReadWriteAllowed())) + { + Endpoint_ClearOUT(); + Endpoint_WaitUntilReady(); + } if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP)) CurrentAddress++; @@ -312,8 +321,8 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK) { SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]); - SPI_SendByte(CurrentAddress >> 8); - SPI_SendByte(CurrentAddress & 0xFF); + SPI_SendByte(StartAddress >> 8); + SPI_SendByte(StartAddress & 0xFF); SPI_SendByte(0x00); /* Check if polling is possible, if not switch to timed delay mode */ @@ -322,11 +331,10 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_VALUE_MASK; Write_Memory_Params.ProgrammingMode &= ~PROG_MODE_PAGED_TIMEDELAY_MASK; } + + ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue, + Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]); } - - ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue, - Write_Memory_Params.DelayMS, (V2Command == CMD_READ_FLASH_ISP), - Write_Memory_Params.ProgrammingCommands[2]); } else { @@ -336,8 +344,8 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) 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; + if (IsOddByte && (V2Command == CMD_READ_FLASH_ISP)) + Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_BYTE_MASK; SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]); SPI_SendByte(CurrentAddress >> 8); @@ -346,18 +354,24 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command) if (ByteToWrite != PollValue) { - if (V2Command == CMD_PROGRAM_FLASH_ISP) - PollAddress = (((CurrentAddress & 0xFFFF) << 1) | IsOddByte); - else - PollAddress = (CurrentAddress & 0xFFFF); + if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP)) + Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK; + + PollAddress = (CurrentAddress & 0xFFFF); } + /* Check if the endpoint bank is currently empty */ + if (!(Endpoint_IsReadWriteAllowed())) + { + Endpoint_ClearOUT(); + Endpoint_WaitUntilReady(); + } + if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP)) CurrentAddress++; ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue, - Write_Memory_Params.DelayMS, (V2Command == CMD_READ_FLASH_ISP), - Write_Memory_Params.ProgrammingCommands[2]); + Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]); if (ProgrammingStatus != STATUS_CMD_OK) break; @@ -393,7 +407,7 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command) for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++) { if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01)) - Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_ODD_BYTE_MASK; + Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK; SPI_SendByte(Read_Memory_Params.ReadMemoryCommand); SPI_SendByte(CurrentAddress >> 8); diff --git a/Projects/Incomplete/AVRISP/Lib/V2Protocol.h b/Projects/Incomplete/AVRISP/Lib/V2Protocol.h index 55c1c78c2..ce7c1ee4c 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2Protocol.h +++ b/Projects/Incomplete/AVRISP/Lib/V2Protocol.h @@ -47,7 +47,7 @@ /* Macros: */ #define PROGRAMMER_ID "AVRISP_MK2" - #define READ_WRITE_ODD_BYTE_MASK (1 << 3) + #define READ_WRITE_HIGH_BYTE_MASK (1 << 3) #define PROG_MODE_PAGED_WRITES_MASK (1 << 0) #define PROG_MODE_WORD_TIMEDELAY_MASK (1 << 1) diff --git a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c index f9b4427ce..d259917a6 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c +++ b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.c @@ -83,7 +83,7 @@ void V2Protocol_DelayMS(uint8_t MS) } uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAddress, uint8_t PollValue, - uint8_t DelayMS, bool IsFlashMemory, uint8_t ReadMemCommand) + uint8_t DelayMS, uint8_t ReadMemCommand) { uint8_t ProgrammingStatus = STATUS_CMD_OK; @@ -96,12 +96,6 @@ uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAdd break; case PROG_MODE_WORD_VALUE_MASK: case PROG_MODE_PAGED_VALUE_MASK: - if (IsFlashMemory && (PollAddress & 0x01)) - { - ReadMemCommand |= READ_WRITE_ODD_BYTE_MASK; - PollAddress >>= 1; - } - TCNT0 = 0; do @@ -119,6 +113,7 @@ uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAdd case PROG_MODE_WORD_READYBUSY_MASK: case PROG_MODE_PAGED_READYBUSY_MASK: ProgrammingStatus = V2Protocol_WaitWhileTargetBusy(); + break; } return ProgrammingStatus; @@ -126,8 +121,6 @@ uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAdd uint8_t V2Protocol_WaitWhileTargetBusy(void) { - uint8_t ResponseByte; - TCNT0 = 0; do @@ -136,9 +129,8 @@ uint8_t V2Protocol_WaitWhileTargetBusy(void) SPI_SendByte(0x00); SPI_SendByte(0x00); - ResponseByte = SPI_ReceiveByte(); } - while ((ResponseByte & 0x01) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS)); + while ((SPI_ReceiveByte() & 0x01) && (TCNT0 < TARGET_BUSY_TIMEOUT_MS)); if (TCNT0 >= TARGET_BUSY_TIMEOUT_MS) return STATUS_RDY_BSY_TOUT; diff --git a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h index d67f84708..2156225a5 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h +++ b/Projects/Incomplete/AVRISP/Lib/V2ProtocolTarget.h @@ -48,7 +48,7 @@ #include "V2ProtocolParams.h" /* Macros: */ - #define TARGET_BUSY_TIMEOUT_MS 200 + #define TARGET_BUSY_TIMEOUT_MS 150 /* External Variables: */ extern uint32_t CurrentAddress; @@ -58,7 +58,7 @@ void V2Protocol_ChangeTargetResetLine(bool ResetTarget); void V2Protocol_DelayMS(uint8_t MS); uint8_t V2Protocol_WaitForProgComplete(uint8_t ProgrammingMode, uint16_t PollAddress, uint8_t PollValue, - uint8_t DelayMS, bool IsFlashMemory, uint8_t ReadMemCommand); + uint8_t DelayMS, uint8_t ReadMemCommand); uint8_t V2Protocol_WaitWhileTargetBusy(void); void V2Protocol_LoadExtendedAddress(void); -- cgit v1.2.3