Page 1 of 1

SPI Flash Functions + unaligned access

Posted: Mon Dec 05, 2016 7:03 pm
by imtiaz
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

Re: SPI Flash Functions + unaligned access

Posted: Mon Dec 05, 2016 7:44 pm
by kolban
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?

Re: SPI Flash Functions + unaligned access

Posted: Mon Dec 05, 2016 8:07 pm
by imtiaz
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.

Re: SPI Flash Functions + unaligned access

Posted: Tue Dec 06, 2016 8:50 pm
by ESP_Angus
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?

Re: SPI Flash Functions + unaligned access

Posted: Thu Dec 08, 2016 9:57 pm
by imtiaz
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);
}
}

Re: SPI Flash Functions + unaligned access

Posted: Fri Dec 09, 2016 3:27 am
by ESP_Angus
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

Re: SPI Flash Functions + unaligned access

Posted: Fri Dec 09, 2016 4:36 am
by WiFive
Can both 32kb and 64kb block erase be supported?

Re: SPI Flash Functions + unaligned access

Posted: Fri Dec 09, 2016 5:22 am
by ESP_Angus
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?

Re: SPI Flash Functions + unaligned access

Posted: Fri Dec 09, 2016 6:25 am
by WiFive
Just the opportunity to reduce erase time for sections that span 32k but not 64k blocks.

Re: SPI Flash Functions + unaligned access

Posted: Mon Dec 12, 2016 8:06 pm
by imtiaz
Hi Angus,

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

Thanks
imtiaz