/* Erases the flash memory. Note that this function also checks that no data is erased outside the flash memory region, so the bootloader can never be erased.  */
blt_bool FlashErase(blt_addr addr, blt_int32u len)
/* Erases the flash memory. Note that this function also checks that no data is erased outside the flash memory region, so the bootloader can never be erased.  */
blt_bool FlashErase(blt_addr addr, blt_int32u len)
{
  blt_bool  result = BLT_TRUE;
  blt_int8u first_sector_idx = FLASH_INVALID_SECTOR_IDX;
  blt_int8u last_sector_idx = FLASH_INVALID_SECTOR_IDX;
  if ((len - 1) > (FLASH_END_ADDRESS - addr))
  {
    result = BLT_FALSE;
  }
  if (result == BLT_TRUE)
  {
    first_sector_idx = FlashGetSectorIdx(addr);
    last_sector_idx  = FlashGetSectorIdx(addr+len-1);
    if ((first_sector_idx == FLASH_INVALID_SECTOR_IDX) ||
        (last_sector_idx == FLASH_INVALID_SECTOR_IDX))
    {
      result = BLT_FALSE;
    }
  }
  if (result == BLT_TRUE)
  {
    result = FlashEraseSectors(first_sector_idx, last_sector_idx);
  }
  return result;
}