@ -158,7 +158,7 @@ static void V2Protocol_Command_GetSetParam(uint8_t V2Command)
static void V2Protocol_Command_LoadAddress ( void )
static void V2Protocol_Command_LoadAddress ( void )
{
{
Endpoint_Read_Stream_ L E( & CurrentAddress , sizeof ( CurrentAddress ) ) ;
Endpoint_Read_Stream_ B E( & CurrentAddress , sizeof ( CurrentAddress ) ) ;
Endpoint_ClearOUT ( ) ;
Endpoint_ClearOUT ( ) ;
Endpoint_SetEndpointDirection ( ENDPOINT_DIR_IN ) ;
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 )
if ( Write_Memory_Params . ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK )
{
{
uint16_t StartAddress = ( CurrentAddress & 0xFFFF ) ;
/* Paged mode memory programming */
/* Paged mode memory programming */
for ( uint16_t CurrentByte = 0 ; CurrentByte < Write_Memory_Params . BytesToWrite ; CurrentByte + + )
for ( uint16_t CurrentByte = 0 ; CurrentByte < Write_Memory_Params . BytesToWrite ; CurrentByte + + )
{
{
bool IsOddByte = ( CurrentByte & 0x01 ) ;
bool IsOddByte = ( CurrentByte & 0x01 ) ;
uint8_t ByteToWrite = Endpoint_Read_Byte ( ) ;
uint8_t ByteToWrite = Endpoint_Read_Byte ( ) ;
if ( ( V2Command = = CMD_ READ_FLASH_ISP) & & IsOddByte )
if ( IsOddByte & & ( V2Command = = CMD_ PROGRAM_FLASH_ISP) )
Write_Memory_Params . ProgrammingCommands [ 0 ] ^ = READ_WRITE_ ODD _BYTE_MASK;
Write_Memory_Params . ProgrammingCommands [ 0 ] ^ = READ_WRITE_ HIGH _BYTE_MASK;
SPI_SendByte ( Write_Memory_Params . ProgrammingCommands [ 0 ] ) ;
SPI_SendByte ( Write_Memory_Params . ProgrammingCommands [ 0 ] ) ;
SPI_SendByte ( CurrentAddress > > 8 ) ;
SPI_SendByte ( CurrentAddress > > 8 ) ;
@ -298,12 +300,19 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
if ( ! ( PollAddress ) & & ( ByteToWrite ! = PollValue ) )
if ( ! ( PollAddress ) & & ( ByteToWrite ! = PollValue ) )
{
{
if ( V2Command = = CMD_PROGRAM_FLASH_ISP )
if ( IsOddByte & & ( V2Command = = CMD_PROGRAM_FLASH_ISP ) )
PollAddress = ( ( ( CurrentAddress & 0xFFFF ) < < 1 ) | IsOddByte ) ;
Write_Memory_Params. ProgrammingCommands [ 2 ] | = READ_WRITE_HIGH_BYTE_MASK ;
else
PollAddress = ( CurrentAddress & 0xFFFF ) ;
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 ) )
if ( IsOddByte | | ( V2Command = = CMD_PROGRAM_EEPROM_ISP ) )
CurrentAddress + + ;
CurrentAddress + + ;
}
}
@ -312,8 +321,8 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
if ( Write_Memory_Params . ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK )
if ( Write_Memory_Params . ProgrammingMode & PROG_MODE_COMMIT_PAGE_MASK )
{
{
SPI_SendByte ( Write_Memory_Params . ProgrammingCommands [ 1 ] ) ;
SPI_SendByte ( Write_Memory_Params . ProgrammingCommands [ 1 ] ) ;
SPI_SendByte ( Curren tAddress > > 8 ) ;
SPI_SendByte ( Star tAddress > > 8 ) ;
SPI_SendByte ( Curren tAddress & 0xFF ) ;
SPI_SendByte ( Star tAddress & 0xFF ) ;
SPI_SendByte ( 0x00 ) ;
SPI_SendByte ( 0x00 ) ;
/* Check if polling is possible, if not switch to timed delay mode */
/* 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_VALUE_MASK ;
Write_Memory_Params . ProgrammingMode & = ~ PROG_MODE_PAGED_TIMEDELAY_MASK ;
Write_Memory_Params . ProgrammingMode & = ~ PROG_MODE_PAGED_TIMEDELAY_MASK ;
}
}
}
ProgrammingStatus = V2Protocol_WaitForProgComplete ( Write_Memory_Params . ProgrammingMode , PollAddress , PollValue ,
ProgrammingStatus = V2Protocol_WaitForProgComplete ( Write_Memory_Params . ProgrammingMode , PollAddress , PollValue ,
Write_Memory_Params . DelayMS , ( V2Command = = CMD_READ_FLASH_ISP ) ,
Write_Memory_Params . DelayMS , Write_Memory_Params . ProgrammingCommands [ 2 ] ) ;
Write_Memory_Params . ProgrammingCommands [ 2 ] ) ;
}
}
}
else
else
{
{
@ -336,8 +344,8 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
bool IsOddByte = ( CurrentByte & 0x01 ) ;
bool IsOddByte = ( CurrentByte & 0x01 ) ;
uint8_t ByteToWrite = Endpoint_Read_Byte ( ) ;
uint8_t ByteToWrite = Endpoint_Read_Byte ( ) ;
if ( ( V2Command = = CMD_READ_FLASH_ISP ) & & IsOddByte )
if ( IsOddByte & & ( V2Command = = CMD_READ_FLASH_ISP ) )
Write_Memory_Params . ProgrammingCommands [ 0 ] ^ = READ_WRITE_ ODD _BYTE_MASK;
Write_Memory_Params . ProgrammingCommands [ 0 ] ^ = READ_WRITE_ HIGH _BYTE_MASK;
SPI_SendByte ( Write_Memory_Params . ProgrammingCommands [ 0 ] ) ;
SPI_SendByte ( Write_Memory_Params . ProgrammingCommands [ 0 ] ) ;
SPI_SendByte ( CurrentAddress > > 8 ) ;
SPI_SendByte ( CurrentAddress > > 8 ) ;
@ -346,18 +354,24 @@ static void V2Protocol_Command_ProgramMemory(uint8_t V2Command)
if ( ByteToWrite ! = PollValue )
if ( ByteToWrite ! = PollValue )
{
{
if ( V2Command = = CMD_PROGRAM_FLASH_ISP )
if ( IsOddByte & & ( V2Command = = CMD_PROGRAM_FLASH_ISP ) )
PollAddress = ( ( ( CurrentAddress & 0xFFFF ) < < 1 ) | IsOddByte ) ;
Write_Memory_Params. ProgrammingCommands [ 2 ] | = READ_WRITE_HIGH_BYTE_MASK ;
else
PollAddress = ( CurrentAddress & 0xFFFF ) ;
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 ) )
if ( IsOddByte | | ( V2Command = = CMD_PROGRAM_EEPROM_ISP ) )
CurrentAddress + + ;
CurrentAddress + + ;
ProgrammingStatus = V2Protocol_WaitForProgComplete ( Write_Memory_Params . ProgrammingMode , PollAddress , PollValue ,
ProgrammingStatus = V2Protocol_WaitForProgComplete ( Write_Memory_Params . ProgrammingMode , PollAddress , PollValue ,
Write_Memory_Params . DelayMS , ( V2Command = = CMD_READ_FLASH_ISP ) ,
Write_Memory_Params . DelayMS , Write_Memory_Params . ProgrammingCommands [ 2 ] ) ;
Write_Memory_Params . ProgrammingCommands [ 2 ] ) ;
if ( ProgrammingStatus ! = STATUS_CMD_OK )
if ( ProgrammingStatus ! = STATUS_CMD_OK )
break ;
break ;
@ -393,7 +407,7 @@ static void V2Protocol_Command_ReadMemory(uint8_t V2Command)
for ( uint16_t CurrentByte = 0 ; CurrentByte < Read_Memory_Params . BytesToRead ; CurrentByte + + )
for ( uint16_t CurrentByte = 0 ; CurrentByte < Read_Memory_Params . BytesToRead ; CurrentByte + + )
{
{
if ( ( V2Command = = CMD_READ_FLASH_ISP ) & & ( CurrentByte & 0x01 ) )
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 ( Read_Memory_Params . ReadMemoryCommand ) ;
SPI_SendByte ( CurrentAddress > > 8 ) ;
SPI_SendByte ( CurrentAddress > > 8 ) ;