Commit for 100513 re-release due to critical device mode bug.
[pub/USBasp.git] / Projects / AVRISP-MKII / Lib / ISP / ISPProtocol.c
index a16504d..e49f590 100644 (file)
@@ -227,6 +227,7 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                                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++;
                }
@@ -248,6 +249,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]);
+
+                       /* Check to see if the FLASH address has crossed the extended address boundary */
+                       if ((V2Command == CMD_PROGRAM_FLASH_ISP) && !(CurrentAddress & 0xFFFF))
+                         ISPTarget_LoadExtendedAddress();                      
                }
        }
        else
@@ -275,15 +280,26 @@ void ISPProtocol_ProgramMemory(uint8_t V2Command)
                                  
                                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]);
                          
+                       /* Abort the programming loop early if the byte/word programming failed */
                        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 (V2Command == CMD_PROGRAM_EEPROM_ISP)
+                       {
+                               CurrentAddress++;
+                       }
+                       else if (IsOddByte)
+                       {
+                               if (!(++CurrentAddress & 0xFFFF))
+                                 ISPTarget_LoadExtendedAddress();                      
+                       }
                }
        }
 
@@ -346,11 +362,19 @@ 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;
-
-               /* 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 (V2Command == CMD_READ_EEPROM_ISP)
+               {
+                       CurrentAddress++;
+               }
+               else if (CurrentByte & 0x01)
+               {
+                       if (!(++CurrentAddress & 0xFFFF))
+                         ISPTarget_LoadExtendedAddress();                      
+               }
        }
 
        Endpoint_Write_Byte(STATUS_CMD_OK);