case CMD_LOAD_ADDRESS:\r
V2Protocol_Command_LoadAddress();\r
break;\r
+ case CMD_RESET_PROTECTION:\r
+ V2Protocol_Command_ResetProtection();\r
+ break;\r
case CMD_ENTER_PROGMODE_ISP:\r
V2Protocol_Command_EnterISPMode();\r
break;\r
case CMD_LEAVE_PROGMODE_ISP:\r
V2Protocol_Command_LeaveISPMode();\r
break;\r
+ case CMD_PROGRAM_FLASH_ISP:\r
+ case CMD_PROGRAM_EEPROM_ISP:\r
+ V2Protocol_Command_ProgramMemory(V2Command); \r
+ break;\r
+ case CMD_READ_FLASH_ISP:\r
+ case CMD_READ_EEPROM_ISP:\r
+ V2Protocol_Command_ReadMemory(V2Command);\r
+ break;\r
case CMD_CHIP_ERASE_ISP:\r
V2Protocol_Command_ChipErase();\r
break;\r
\r
static void V2Protocol_Command_LoadAddress(void)\r
{\r
- Endpoint_Read_Stream_LE(&CurrentAddress, sizeof(CurrentAddress));\r
+ Endpoint_Read_Stream_BE(&CurrentAddress, sizeof(CurrentAddress));\r
\r
Endpoint_ClearOUT();\r
Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
Endpoint_ClearIN();\r
}\r
\r
+static void V2Protocol_Command_ResetProtection(void)\r
+{\r
+ Endpoint_ClearOUT();\r
+ Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+ \r
+ Endpoint_Write_Byte(CMD_RESET_PROTECTION);\r
+ Endpoint_Write_Byte(STATUS_CMD_OK);\r
+ Endpoint_ClearIN(); \r
+}\r
+\r
static void V2Protocol_Command_EnterISPMode(void)\r
{\r
struct\r
\r
V2Protocol_DelayMS(Enter_ISP_Params.ExecutionDelayMS); \r
SPI_Init(V2Protocol_GetSPIPrescalerMask() | SPI_SCK_LEAD_RISING | SPI_SAMPLE_LEADING | SPI_MODE_MASTER);\r
- V2Protocol_ChangeTargetResetLine(true);\r
- V2Protocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);\r
\r
while (Enter_ISP_Params.SynchLoops-- && (ResponseStatus == STATUS_CMD_FAILED))\r
{\r
uint8_t ResponseBytes[4];\r
\r
+ V2Protocol_ChangeTargetResetLine(true);\r
+ V2Protocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);\r
+\r
for (uint8_t RByte = 0; RByte < sizeof(ResponseBytes); RByte++)\r
{\r
V2Protocol_DelayMS(Enter_ISP_Params.ByteDelay);\r
}\r
\r
/* Check if polling disabled, or if the polled value matches the expected value */\r
- if (!Enter_ISP_Params.PollIndex || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue))\r
- ResponseStatus = STATUS_CMD_OK;\r
+ if (!(Enter_ISP_Params.PollIndex) || (ResponseBytes[Enter_ISP_Params.PollIndex - 1] == Enter_ISP_Params.PollValue))\r
+ {\r
+ ResponseStatus = STATUS_CMD_OK;\r
+ }\r
+ else\r
+ {\r
+ V2Protocol_ChangeTargetResetLine(false);\r
+ V2Protocol_DelayMS(Enter_ISP_Params.PinStabDelayMS);\r
+ }\r
}\r
\r
Endpoint_Write_Byte(CMD_ENTER_PROGMODE_ISP);\r
Endpoint_ClearIN();\r
}\r
\r
+static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)\r
+{\r
+ struct\r
+ {\r
+ uint16_t BytesToWrite;\r
+ uint8_t ProgrammingMode;\r
+ uint8_t DelayMS;\r
+ uint8_t ProgrammingCommands[3];\r
+ uint8_t PollValue1;\r
+ uint8_t PollValue2;\r
+ } Write_Memory_Params;\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
+ uint8_t PollValue = (V2Command == CMD_PROGRAM_FLASH_ISP) ? Write_Memory_Params.PollValue1 :\r
+ Write_Memory_Params.PollValue2;\r
+ \r
+ if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)\r
+ {\r
+ uint16_t StartAddress = (CurrentAddress & 0xFFFF);\r
+ \r
+ /* Paged 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 (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+ Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_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 (!(PollAddress) && (ByteToWrite != PollValue))\r
+ {\r
+ if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+ Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;\r
+ \r
+ PollAddress = (CurrentAddress & 0xFFFF); \r
+ }\r
+ \r
+ /* Check if the endpoint bank is currently empty */\r
+ if (!(Endpoint_IsReadWriteAllowed()))\r
+ {\r
+ Endpoint_ClearOUT();\r
+ Endpoint_WaitUntilReady();\r
+ } \r
+\r
+ if (IsOddByte || (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(StartAddress >> 8);\r
+ SPI_SendByte(StartAddress & 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
+ ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,\r
+ Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);\r
+ }\r
+ }\r
+ else\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 (IsOddByte && (V2Command == CMD_READ_FLASH_ISP))\r
+ Write_Memory_Params.ProgrammingCommands[0] ^= READ_WRITE_HIGH_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 != PollValue)\r
+ {\r
+ if (IsOddByte && (V2Command == CMD_PROGRAM_FLASH_ISP))\r
+ Write_Memory_Params.ProgrammingCommands[2] |= READ_WRITE_HIGH_BYTE_MASK;\r
+ \r
+ PollAddress = (CurrentAddress & 0xFFFF);\r
+ }\r
+ \r
+ /* Check if the endpoint bank is currently empty */\r
+ if (!(Endpoint_IsReadWriteAllowed()))\r
+ {\r
+ Endpoint_ClearOUT();\r
+ Endpoint_WaitUntilReady();\r
+ } \r
+\r
+ if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))\r
+ CurrentAddress++;\r
+ \r
+ ProgrammingStatus = V2Protocol_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,\r
+ Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);\r
+ \r
+ if (ProgrammingStatus != STATUS_CMD_OK)\r
+ break;\r
+ }\r
+ }\r
+ \r
+ Endpoint_ClearOUT();\r
+ Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+\r
+ Endpoint_Write_Byte(V2Command);\r
+ Endpoint_Write_Byte(ProgrammingStatus);\r
+ \r
+ Endpoint_ClearIN();\r
+}\r
+\r
+static void V2Protocol_Command_ReadMemory(uint8_t V2Command)\r
+{\r
+ struct\r
+ {\r
+ uint16_t BytesToRead;\r
+ uint8_t ReadMemoryCommand;\r
+ } Read_Memory_Params;\r
+ \r
+ Endpoint_Read_Stream_LE(&Read_Memory_Params, sizeof(Read_Memory_Params));\r
+ Read_Memory_Params.BytesToRead = SwapEndian_16(Read_Memory_Params.BytesToRead);\r
+ \r
+ Endpoint_ClearOUT();\r
+ Endpoint_SetEndpointDirection(ENDPOINT_DIR_IN);\r
+ \r
+ Endpoint_Write_Byte(V2Command);\r
+ Endpoint_Write_Byte(STATUS_CMD_OK);\r
+ \r
+ for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)\r
+ {\r
+ if ((V2Command == CMD_READ_FLASH_ISP) && (CurrentByte & 0x01))\r
+ Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;\r
+\r
+ SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);\r
+ SPI_SendByte(CurrentAddress >> 8);\r
+ SPI_SendByte(CurrentAddress & 0xFF);\r
+ Endpoint_Write_Byte(SPI_ReceiveByte());\r
+ \r
+ /* Check if the endpoint bank is currently full */\r
+ if (!(Endpoint_IsReadWriteAllowed()))\r
+ {\r
+ Endpoint_ClearIN();\r
+ Endpoint_WaitUntilReady();\r
+ }\r
+ \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 IsEndpointFull = !(Endpoint_IsReadWriteAllowed());\r
+ Endpoint_ClearIN();\r
+ \r
+ /* Ensure last packet is a short packet to terminate the transfer */\r
+ if (IsEndpointFull)\r
+ {\r
+ Endpoint_WaitUntilReady(); \r
+ Endpoint_ClearIN();\r
+ Endpoint_WaitUntilReady(); \r
+ }\r
+}\r
+\r
static void V2Protocol_Command_ChipErase(void)\r
{\r
struct\r
for (uint8_t SByte = 0; SByte < sizeof(Erase_Chip_Params.EraseCommandBytes); SByte++)\r
SPI_SendByte(Erase_Chip_Params.EraseCommandBytes[SByte]);\r
\r
- if (Erase_Chip_Params.PollMethod == 0)\r
+ if (!(Erase_Chip_Params.PollMethod))\r
V2Protocol_DelayMS(Erase_Chip_Params.EraseDelayMS);\r
else\r
ResponseStatus = V2Protocol_WaitWhileTargetBusy();\r
\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