Skip to content

Commit

Permalink
target: optimize flash erase using the mass erase routine
Browse files Browse the repository at this point in the history
When an erase is requested that would result in a complete erase of a flash
and the mass erase routine is provided, use it as a speed optimization
  • Loading branch information
perigoso authored and rg-silva committed Nov 7, 2024
1 parent 110a1aa commit 971b340
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/target/target_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,29 @@ bool target_flash_erase(target_s *target, target_addr_t addr, size_t len)
active_flash = flash;
}

/* Align the start address to the erase block size */
const target_addr_t local_start_addr = addr & ~(flash->blocksize - 1U);
const target_addr_t local_end_addr = local_start_addr + flash->blocksize;

if (!flash_prepare(flash, FLASH_OPERATION_ERASE))
/* Check if we can use mass erase */
const bool can_use_mass_erase = flash->mass_erase != NULL && local_start_addr == flash->start &&
(addr + len) >= (flash->start + flash->length);

/* Calculate the address at the end of the erase block */
const target_addr_t local_end_addr =
can_use_mass_erase ? flash->start + flash->length : local_start_addr + flash->blocksize;

if (!flash_prepare(flash, can_use_mass_erase ? FLASH_OPERATION_MASS_ERASE : FLASH_OPERATION_ERASE))
return false;

result &= flash->erase(flash, local_start_addr, flash->blocksize);
/* Erase flash, either a single aligned block size or a full mass erase */
result &= can_use_mass_erase ? flash->mass_erase(flash, NULL) :
flash->erase(flash, local_start_addr, flash->blocksize);
if (!result) {
DEBUG_ERROR("Erase failed at %" PRIx32 "\n", local_start_addr);
break;
}

/* Update the remaining length and address, taking into account the alignment */
len -= MIN(local_end_addr - addr, len);
addr = local_end_addr;
}
Expand Down

0 comments on commit 971b340

Please sign in to comment.