ESP-IDF 4.2 esp_partition_write() fail with strange error

nonagon
Posts: 17
Joined: Thu May 06, 2021 12:56 pm

ESP-IDF 4.2 esp_partition_write() fail with strange error

Postby nonagon » Thu May 06, 2021 1:40 pm

Hello,
I'm developing a product that uses a ESP32-WROVER-E(M213EH6464PH3Q0) Module (ESP32-D0WD-V3, 3.3V 64Mbit PSRAM, 8 MB SPI flash).
I can't figure why when I write on a custom partition in flash, when I read back the data it appear to be corrupted.

this is my partition table:

Code: Select all

# Name,   Type, SubType,  Offset,   Size,  Flags
nvs,      data, nvs,      0x9000,   32K
otadata,  data, ota,      ,         8K
phy_init, data, phy,      ,         2K
factory,  app,  factory,  ,         1536K
ota_0,    app,  ota_0,    ,         1536K
ota_1,    app,  ota_1,    ,         1536K
myappdata,  data, 0x87,     ,         1536K
I use esp_partition_find_first() to search myappdata partition, then i perform the write

Code: Select all

	
int fs_write(const void *data, void *addr, u32 size)
{
	const esp_partition_t* part;
	part = esp_partition_find_first(
		ESP_PARTITION_TYPE_DATA,
		0x87,
		NULL);

	if (part != NULL)
	{
		if (esp_partition_write(part, (size_t)addr, data, (size_t)size) == ESP_OK)
		{
		
		...
just after I check with:

Code: Select all

	
if(esp_partition_read(part,(size_t)addr + c,(void*)tmp,toread) == ESP_OK)
				{
					if(memcmp((const void*)&dataptr[c], tmp, toread) != 0)
					{
					..
I changed the spi_flash to legacy version < IDF 4.0 (but the result is the same) to see if somethings changes and I get this log:

Code: Select all

spi_flash: Write at offset 0x4b4014 requests 0x48d766fe but will write 0x40850000 -> 0x40850000␛[0m

or similar:
spi_flash: Write at offset 0x4b4000 requests 0x00000001 but will write 0x00000000 -> 0x00000000␛[0m
spi_flash: Write at offset 0x4b4004 requests 0x89abcdef but will write 0x00000000 -> 0x00000000␛[0m
spi_flash: Write at offset 0x4b4008 requests 0x01234567 but will write 0x00000000 -> 0x00000000␛[0m
So, basically i try to write 0x48d766fe but the driver write 0x40850000 (?!).
And yes, when I call esp_partition_read I will read 0x40850000.
the write offset (0x4b4014) seems good because myappdata starts at 0x493800 as partition table
Why this happens?!
Can someone help me with this issue?
Have a nice day,
Thank you in advice

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

Re: ESP-IDF 4.2 esp_partition_write() fail with strange error

Postby WiFive » Thu May 06, 2021 9:08 pm

You have to erase before you write, erase size minimum is sector size. That's why layers like filesystems, wear leveling and nvs exist above raw flash apis.

nonagon
Posts: 17
Joined: Thu May 06, 2021 12:56 pm

Re: ESP-IDF 4.2 esp_partition_write() fail with strange error

Postby nonagon » Fri May 07, 2021 9:33 am

Thank you, it was the problem.
Then I have to write a driver to read, modify, erase, write working with chunks of 4kb, aligned at 4kb address.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP-IDF 4.2 esp_partition_write() fail with strange error

Postby ESP_Sprite » Fri May 07, 2021 9:40 am

Also take into account that dependent on the amount of writes you expect, you may also want to do wear levelling as flash sectors can only be erased a limited (but fairly large, generally 10,000-100,000) times. Or look into nvs/spiffs/fatfs in esp-idf as they already implement that for you.

nonagon
Posts: 17
Joined: Thu May 06, 2021 12:56 pm

Re: ESP-IDF 4.2 esp_partition_write() fail with strange error

Postby nonagon » Fri May 07, 2021 11:53 am

Yes, I need to store data with a write access time not frequent, fairly far from the limit of 10k total sector writes.
Now with addressing writing with 4kb page and 4 kb align it works fine.
Have a nice day.
Thank you for the replies.

Who is online

Users browsing this forum: No registered users and 155 guests