X-Git-Url: http://git.linex4red.de/pub/lufa.git/blobdiff_plain/dbb5f249bd057c7b292645463c52067c1da58efa..6d1adf7339b71952d1ba8616af9422fbc7333eb1:/Projects/Incomplete/AVRISP/Lib/V2Protocol.c?ds=inline diff --git a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c index 85e3a382d..8d1480269 100644 --- a/Projects/Incomplete/AVRISP/Lib/V2Protocol.c +++ b/Projects/Incomplete/AVRISP/Lib/V2Protocol.c @@ -52,6 +52,9 @@ void V2Protocol_ProcessCommand(void) case CMD_LOAD_ADDRESS: V2Protocol_Command_LoadAddress(); break; + case CMD_RESET_PROTECTION: + V2Protocol_Command_ResetProtection(); + break; case CMD_ENTER_PROGMODE_ISP: V2Protocol_Command_EnterISPMode(); break; @@ -155,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); @@ -168,6 +171,16 @@ static void V2Protocol_Command_LoadAddress(void) Endpoint_ClearIN(); } +static void V2Protocol_Command_ResetProtection(void) +{ + Endpoint_ClearOUT(); + Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN); + + Endpoint_Write_Byte(CMD_RESET_PROTECTION); + Endpoint_Write_Byte(STATUS_CMD_OK); + Endpoint_ClearIN(); +} + static void V2Protocol_Command_EnterISPMode(void) { struct @@ -208,7 +221,7 @@ static void V2Protocol_Command_EnterISPMode(void) } /* Check if polling disabled, or if the polled value matches the expected value */ - if (!Enter_ISP_Params.PollIndex || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue)) + if (!(Enter_ISP_Params.PollIndex) || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue)) { ResponseStatus = STATUS_CMD_OK; } @@ -259,14 +272,110 @@ 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); - for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++) + uint8_t ProgrammingStatus = STATUS_CMD_OK; + uint16_t PollAddress = 0; + uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 : + Write_Memory_Params.PollValue2; + + 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 (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); + SPI_SendByte(CurrentAddress & 0xFF); + SPI_SendByte(ByteToWrite); + + if (!(PollAddress) && (ByteToWrite != PollValue)) + { + 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++; + } + + /* If the current page must be committed, send the PROGRAM PAGE command to the target */ + if (Write_Memory_Params.ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK) + { + SPI_SendByte(Write_Memory_Params.ProgrammingCommands[1]); + SPI_SendByte(StartAddress >> 8); + SPI_SendByte(StartAddress & 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_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue, + Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]); + } + } + 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 (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); + SPI_SendByte(CurrentAddress & 0xFF); + SPI_SendByte(ByteToWrite); + + if (ByteToWrite != PollValue) + { + 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, Write_Memory_Params.ProgrammingCommands[2]); + + if (ProgrammingStatus != STATUS_CMD_OK) + break; + } } Endpoint_ClearOUT(); @@ -294,11 +403,11 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command) Endpoint_Write_Byte(V2Command); Endpoint_Write_Byte(STATUS_CMD_OK); - + 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); @@ -312,19 +421,21 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command) Endpoint_WaitUntilReady(); } - CurrentAddress++; + if (((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01)) || (V2Command == CMD_READ_EEPROM_ISP)) + CurrentAddress++; } Endpoint_Write_Byte(STATUS_CMD_OK); - - bool EndpointBankFull = Endpoint_IsReadWriteAllowed(); + + bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed()); Endpoint_ClearIN(); - /* Ensure data transfer is terminated by a short packet if the last sent bank was completely full */ - if (EndpointBankFull) + /* Ensure last packet is a short packet to terminate the transfer */ + if (IsEndpointFull) { - Endpoint_WaitUntilReady(); + Endpoint_WaitUntilReady(); Endpoint_ClearIN(); + Endpoint_WaitUntilReady(); } } @@ -347,7 +458,7 @@ static void V2Protocol_Command_ChipErase(void) for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++) SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]); - if (Erase_Chip_Params.PollMethod == 0) + if (!(Erase_Chip_Params.PollMethod)) V2Protocol_DelayMS(Erase_Chip_Params.EraseDelayMS); else ResponseStatus = V2Protocol_WaitWhileTargetBusy(); @@ -377,7 +488,7 @@ static void V2Protocol_Command_ReadFuseLockSigOSCCAL(uint8_t V2Command) Endpoint_Write_Byte(V2Command); Endpoint_Write_Byte(STATUS_CMD_OK); - Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte]); + Endpoint_Write_Byte(ResponseBytes[Read_FuseLockSigOSCCAL_Params.RetByte - 1]); Endpoint_Write_Byte(STATUS_CMD_OK); Endpoint_ClearIN(); }