Fixed AVRISP project not sending a full erase-and-write EEPROM command to XMEGA targe...
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
index a16504d..2ce11a8 100644 (file)
@@ -186,22 +186,18 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                                                                            Write_Memory_Params.PollValue2;
        uint8_t* NextWriteByte = Write_Memory_Params.ProgData;
 
                                                                            Write_Memory_Params.PollValue2;
        uint8_t* NextWriteByte = Write_Memory_Params.ProgData;
 
-       /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
-        * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
-        * FLASH barrier) */
-       if (MustSetAddress)
-       {
-               if (CurrentAddress & (1UL << 31))
-                 ISPTarget_LoadExtendedAddress();
-
-               MustSetAddress = false;
-       }
-
        /* Check the programming mode desired by the host, either Paged or Word memory writes */
        if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)
        {
                uint16_t StartAddress = (CurrentAddress & 0xFFFF);
        
        /* Check the programming mode desired by the host, either Paged or Word memory writes */
        if (Write_Memory_Params.ProgrammingMode & PROG_MODE_PAGED_WRITES_MASK)
        {
                uint16_t StartAddress = (CurrentAddress & 0xFFFF);
        
+               /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
+               if (MustLoadExtendedAddress)
+               {
+                       ISPTarget_LoadExtendedAddress();
+                       MustLoadExtendedAddress = false;
+               }
+
                /* Paged mode memory programming */
                for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
                {
                /* Paged mode memory programming */
                for (uint16_t CurrentByte = 0; CurrentByte < Write_Memory_Params.BytesToWrite; CurrentByte++)
                {
@@ -227,6 +223,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                                PollAddress = (CurrentAddress & 0xFFFF);                                
                        }               
 
                                PollAddress = (CurrentAddress & 0xFFFF);                                
                        }               
 
+                       /* EEPROM increments the address on each byte, flash needs to increment on each word */
                        if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
                          CurrentAddress++;
                }
                        if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
                          CurrentAddress++;
                }
@@ -248,6 +245,10 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
 
                        ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
                                                                          Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);
 
                        ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
                                                                          Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);
+
+                       /* Check to see if the FLASH address has crossed the extended address boundary */
+                       if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
+                         MustLoadExtendedAddress = true;                       
                }
        }
        else
                }
        }
        else
@@ -258,6 +259,13 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                        bool    IsOddByte   = (CurrentByte & 0x01);
                        uint8_t ByteToWrite = *(NextWriteByte++);
                          
                        bool    IsOddByte   = (CurrentByte & 0x01);
                        uint8_t ByteToWrite = *(NextWriteByte++);
                          
+                       /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
+                       if (MustLoadExtendedAddress)
+                       {
+                               ISPTarget_LoadExtendedAddress();
+                               MustLoadExtendedAddress = false;
+                       }
+
                        SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
                        SPI_SendByte(CurrentAddress >> 8);
                        SPI_SendByte(CurrentAddress & 0xFF);
                        SPI_SendByte(Write_Memory_Params.ProgrammingCommands[0]);
                        SPI_SendByte(CurrentAddress >> 8);
                        SPI_SendByte(CurrentAddress & 0xFF);
@@ -275,15 +283,24 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                                  
                                PollAddress = (CurrentAddress & 0xFFFF);
                        }
                                  
                                PollAddress = (CurrentAddress & 0xFFFF);
                        }
-
-                       if (IsOddByte || (V2Command == CMD_PROGRAM_EEPROM_ISP))
-                         CurrentAddress++;
                        
                        ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
                                                                          Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);
                          
                        
                        ProgrammingStatus = ISPTarget_WaitForProgComplete(Write_Memory_Params.ProgrammingMode, PollAddress, PollValue,
                                                                          Write_Memory_Params.DelayMS, Write_Memory_Params.ProgrammingCommands[2]);
                          
+                       /* Abort the programming loop early if the byte/word programming failed */
                        if (ProgrammingStatus != STATUS_CMD_OK)
                          break;
                        if (ProgrammingStatus != STATUS_CMD_OK)
                          break;
+
+                       /* EEPROM just increments the address each byte, flash needs to increment on each word and
+                        * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
+                        * address boundary has been crossed */
+                       if ((CurrentByte & 0x01) || (V2Command == CMD_PROGRAM_EEPROM_ISP))
+                       {
+                               CurrentAddress++;
+                       
+                               if ((V2Command != CMD_PROGRAM_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
+                                 MustLoadExtendedAddress = true;                       
+                       }
                }
        }
 
                }
        }
 
@@ -314,21 +331,17 @@ void ISPProtocol_ReadMemory(uint8_t V2Command)
        
        Endpoint_Write_Byte(V2Command);
        Endpoint_Write_Byte(STATUS_CMD_OK);
        
        Endpoint_Write_Byte(V2Command);
        Endpoint_Write_Byte(STATUS_CMD_OK);
-       
-       /* Check to see if the host has issued a SET ADDRESS command and we haven't sent a
-        * LOAD EXTENDED ADDRESS command (if needed, used when operating beyond the 128KB
-        * FLASH barrier) */
-       if (MustSetAddress)
-       {
-               if (CurrentAddress & (1UL << 31))
-                 ISPTarget_LoadExtendedAddress();
-
-               MustSetAddress = false;
-       }
 
        /* Read each byte from the device and write them to the packet for the host */
        for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
        {
 
        /* Read each byte from the device and write them to the packet for the host */
        for (uint16_t CurrentByte = 0; CurrentByte < Read_Memory_Params.BytesToRead; CurrentByte++)
        {
+               /* Check to see if we need to send a LOAD EXTENDED ADDRESS command to the target */
+               if (MustLoadExtendedAddress)
+               {
+                       ISPTarget_LoadExtendedAddress();
+                       MustLoadExtendedAddress = false;
+               }
+
                /* Read the next byte from the desired memory space in the device */
                SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);
                SPI_SendByte(CurrentAddress >> 8);
                /* Read the next byte from the desired memory space in the device */
                SPI_SendByte(Read_Memory_Params.ReadMemoryCommand);
                SPI_SendByte(CurrentAddress >> 8);
@@ -346,11 +359,17 @@ void ISPProtocol_ReadMemory(uint8_t V2Command)
                 * or low byte at the current word address */
                if (V2Command == CMD_READ_FLASH_ISP)
                  Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;
                 * or low byte at the current word address */
                if (V2Command == CMD_READ_FLASH_ISP)
                  Read_Memory_Params.ReadMemoryCommand ^= READ_WRITE_HIGH_BYTE_MASK;
-
-               /* Only increment the current address if we have read both bytes in the current word when in FLASH
-                * read mode, or for each byte when in EEPROM read mode */               
-               if (((CurrentByte & 0x01) && (V2Command == CMD_READ_FLASH_ISP)) || (V2Command == CMD_READ_EEPROM_ISP))
-                 CurrentAddress++;
+                
+               /* EEPROM just increments the address each byte, flash needs to increment on each word and
+                * also check to ensure that a LOAD EXTENDED ADDRESS command is issued each time the extended
+                * address boundary has been crossed */
+               if ((CurrentByte & 0x01) || (V2Command == CMD_READ_EEPROM_ISP))
+               {
+                       CurrentAddress++;
+               
+                       if ((V2Command != CMD_READ_EEPROM_ISP) && !(CurrentAddress & 0xFFFF))
+                         MustLoadExtendedAddress = true;                       
+               }
        }
 
        Endpoint_Write_Byte(STATUS_CMD_OK);
        }
 
        Endpoint_Write_Byte(STATUS_CMD_OK);