SPI Flash Functions + unaligned access

imtiaz
Posts: 106
Joined: Wed Oct 26, 2016 1:34 am

SPI Flash Functions + unaligned access

Postby imtiaz » Mon Dec 05, 2016 7:03 pm

Hi ESP32 developers,

Question 1:

I am using the Flash functions in flash_ops.c. It seems that the flash erase function esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size) is limited to erasing 32768 bytes at a time - is this True?

The erase function does not return any error - however I have a function that I am using to test the SPI flash by writing and reading back data. It only works if I erase smaller chunks at a time.

Just need confirmation.

Question 2:
Do these functons support unalligned access yet?


Thanks
Imtiaz

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: SPI Flash Functions + unaligned access

Postby kolban » Mon Dec 05, 2016 7:44 pm

If I examine the docs for that API call here:

http://esp-idf.readthedocs.io/en/latest ... e_t6size_t

I seem to see that a sector is 4K bytes and that the size of an erase must be multiples of this value. Are you seeing elsewhere a mention of data units being 32K?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

imtiaz
Posts: 106
Joined: Wed Oct 26, 2016 1:34 am

Re: SPI Flash Functions + unaligned access

Postby imtiaz » Mon Dec 05, 2016 8:07 pm

Yes correct sector size is 4096 and erase must be a multiple of this. I just reduced my erase to a maximum of 32768 and then everything started working. The hint was this line of code ..... #define BLOCK_ERASE_SIZE 32768 .....

However the problem may be unrelated to this which is why I want the confirmation.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: SPI Flash Functions + unaligned access

Postby ESP_Angus » Tue Dec 06, 2016 8:50 pm

The spi_flash_erase_range() function should erase any size, as long as it's offset/size are a multiple of 4096 bytes and the region doesn't overflow the configured flash size.

If the erase covers any 32KB aligned blocks, these are erased using the "block erase" function as this is faster than erasing 8x 4KB sectors individually.

When the erase didn't seem to be happening, was spi_flash_erase_range() returning ESP_OK (0) or a different result?

imtiaz
Posts: 106
Joined: Wed Oct 26, 2016 1:34 am

Re: SPI Flash Functions + unaligned access

Postby imtiaz » Thu Dec 08, 2016 9:57 pm

HI,

It was returning ESP_OK - I was erasing 250 sectors (1 MByte). Then if I wrote the flash and read back the data some data would be corrupted. I just changes the erase to erase one sector at a time as below and then all my verification passes every time. for(uint16_t i = 0 ; i < 250 ; i++) //erase 250 sectors
{
if( (SpiErr = spi_flash_erase_range(MY_FLASH_START + (i*SPI_FLASH_SEC_SIZE),SPI_FLASH_SEC_SIZE)) == ESP_OK)
{
TRACE_D("4096 bytes of flash erased starting at %lu \n",(unsigned long)Address);
}
}

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: SPI Flash Functions + unaligned access

Postby ESP_Angus » Fri Dec 09, 2016 3:27 am

Hi imtiaz,

My apologies, I somehow documented the erase block size as 32KB but the hardware actually performs 64KB block erases (different SPI command). There will be a fix in esp-idf master branch soon, but if you change the BLOCK_ERASE_SIZE macro value to 65536 then you should find this begins working correctly for larger erase sizes.

Angus

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: SPI Flash Functions + unaligned access

Postby WiFive » Fri Dec 09, 2016 4:36 am

Can both 32kb and 64kb block erase be supported?

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: SPI Flash Functions + unaligned access

Postby ESP_Angus » Fri Dec 09, 2016 5:22 am

WiFive wrote:Can both 32kb and 64kb block erase be supported?
It's technically possible. The SPI hardware controller can only issue one block erase command, but it supports "USR" commands which can be anything - so a 32KB erase could be added.

Is there a particular use case you are thinking of?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: SPI Flash Functions + unaligned access

Postby WiFive » Fri Dec 09, 2016 6:25 am

Just the opportunity to reduce erase time for sections that span 32k but not 64k blocks.

imtiaz
Posts: 106
Joined: Wed Oct 26, 2016 1:34 am

Re: SPI Flash Functions + unaligned access

Postby imtiaz » Mon Dec 12, 2016 8:06 pm

Hi Angus,

Tested with the new block erase size - works like a charm :)

Thanks
imtiaz

Who is online

Users browsing this forum: VinayDand and 427 guests