flash_erase() only erasing partial memory

Go To Last Post
2 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Short form:

 

In a system where the page size is reported as 512, I would expect the command

ret = flash_erase(&FLASH_0, 0, 128);

to erase from 0 to 0x10000.  Instead, it's only erasing from 0 to x02000.

 

Details:

 

Processor is ATSAME54.  IDE is IAR EWARM.  Full code is:

  uint32_t n_bytes = 0x10000;
  uint32_t page_size = flash_get_page_size(&FLASH_0);  // debugger reports page_size = 512
  uint32_t n_pages = n_bytes / page_size;              // debugger reports n_pages = 128
  if (n_pages * page_size != n_bytes) {                // assure that n_bytes is a multiple of page_size
    ERROR("n_bytes (%lx) is not a multiple of page size (%lx)\n", n_bytes, page_size);
  }
  ret = flash_erase(&FLASH_0, 0, n_pages);

flash_erase() returns with an error code of 0, which indicates success. 

flash_erase() should have erased 128 * 512 bytes = 0x10000, but appears to have erased only up to 0x2000:

Ideas?  (Perhaps is page_size really 64 bytes?)

 

Update:

 

I see WHAT the problem is, but I don't know WHY:

 

Inside of _flash_erase() is this loop:

	while (page_nums >= NVMCTRL_BLOCK_PAGES) {
		_flash_erase_block(device->hw, block_start_addr);
		block_start_addr += NVMCTRL_PAGE_SIZE;
		page_nums -= NVMCTRL_BLOCK_PAGES;
	}

Where page_nums is the number of pages to be erased (128 in my case).  Each time through the loop, it erases one page of flash, but decrements page_nums by 16.  So it exits the loop after 8 iterations and erasing 512 * 8 bytes = 0x2000, which is what I'm observing.

 

Am I misinterpreting the "page_nums" argument to flash_erase()?

This topic has a solution.
Last Edited: Thu. Jul 22, 2021 - 09:29 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Update 2:

 

I looked at a project more recently generated using Atmel START.  The same loop in _flash_erase() now reads:

	while (page_nums >= NVMCTRL_BLOCK_PAGES) {
		_flash_erase_block(device->hw, block_start_addr);
		block_start_addr += NVMCTRL_BLOCK_SIZE;
		page_nums -= NVMCTRL_BLOCK_PAGES;
	}

... which makes a lot more sense. But even after making that change, I still don't see memory about 0x2000 getting erased.  So I'm more confused as ever.

 

Update 3:

 

Solved.  Even though I changed the source code, it was compiled as a library as a separate project.  (#facepalm!)  After compiling the library and re-building the main program, everything works as expected.

 

Last Edited: Thu. Jul 22, 2021 - 09:29 PM