optimize: reorder pageerase to gain some bytes
authorStephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Tue, 31 Jul 2012 17:00:13 +0000 (19:00 +0200)
committerStephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
Tue, 31 Jul 2012 18:16:55 +0000 (18:16 +0000)
Signed-off-by: Stephan Baerwolf <stephan.baerwolf@tu-ilmenau.de>
firmware/main.c

index 97d7bd7..7e92970 100644 (file)
@@ -292,17 +292,6 @@ uchar   isLast;
 #endif
            }
 #endif
-#if !HAVE_CHIP_ERASE
-            if((currentAddress.w[0] & (SPM_PAGESIZE - 1)) == 0){    /* if page start: erase */
-                DBG1(0x33, 0, 0);
-#   ifndef NO_FLASH_WRITE
-                cli();
-                boot_page_erase(CURRENT_ADDRESS);   /* erase page */
-                sei();
-                boot_spm_busy_wait();               /* wait until page is erased */
-#   endif
-            }
-#endif
             i += 2;
             DBG1(0x32, 0, 0);
             cli();
@@ -312,6 +301,15 @@ uchar   isLast;
             data += 2;
             /* write page when we cross page boundary or we have the last partial page */
             if((currentAddress.w[0] & (SPM_PAGESIZE - 1)) == 0 || (isLast && i >= len && isLastPage)){
+#if !HAVE_CHIP_ERASE
+                DBG1(0x33, 0, 0);
+#   ifndef NO_FLASH_WRITE
+                cli();
+                boot_page_erase((currentAddress.w[0]-2) & (SPM_PAGESIZE - 1));   /* erase page */
+                sei();
+                boot_spm_busy_wait();               /* wait until page is erased */
+#   endif
+#endif
                 DBG1(0x34, 0, 0);
 #ifndef NO_FLASH_WRITE
                 cli();