be quicker about determining erased byte pattern when flashing, more readable code
authortexane <texane@gmail.com>
Sat, 27 Feb 2016 18:17:19 +0000 (19:17 +0100)
committertexane <texane@gmail.com>
Sat, 27 Feb 2016 18:17:19 +0000 (19:17 +0100)
src/stlink-common.c

index 917a1e0e3c63c68e4ffc7002e7fd107963212bdc..51efae43fcf99282371b13bd4a2657b6b9a24c04 100644 (file)
@@ -1966,7 +1966,7 @@ int stlink_write_flash(stlink_t *sl, stm32_addr_t addr, uint8_t* base, uint32_t
 int stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr) {
     /* write the file in flash at addr */
     int err;
-    unsigned int num_empty = 0, index;
+    unsigned int num_empty, index;
     unsigned char erased_pattern = (sl->chip_id == STM32_CHIPID_L1_MEDIUM || sl->chip_id == STM32_CHIPID_L1_CAT2
             || sl->chip_id == STM32_CHIPID_L1_MEDIUM_PLUS || sl->chip_id == STM32_CHIPID_L1_HIGH
             || sl->chip_id == STM32_CHIPID_L152_RE) ? 0:0xff;
@@ -1975,11 +1975,11 @@ int stlink_fwrite_flash(stlink_t *sl, const char* path, stm32_addr_t addr) {
         ELOG("map_file() == -1\n");
         return -1;
     }
-    for(index = mf.len - 1; num_empty < mf.len; index --) {
-        if (mf.base[index] != erased_pattern) {
+    index = mf.len;
+    for(num_empty = 0; num_empty != mf.len; ++num_empty) {
+        if (mf.base[--index] != erased_pattern) {
             break;
         }
-        num_empty ++;
     }
     /* Round down to words */
     num_empty -= (num_empty & 3);