CRC error when read-write values from/to flash

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

CRC error when read-write values from/to flash

Postby Palonso » Thu Feb 04, 2021 8:32 pm

Hi,

I have been saving values to an ESP-WROVER-B module and sometimes, when I want to read back, I have the next error:

Code: Select all

"CRC failed"
Which only happens when the next condition is met:

Code: Select all

esp_partition_read(partition, index * sizeof(my_data), (void*)data_to_read, sizeof(my_data));

uint8_t crc = crc8_be(0, (const uint8_t*)data_to_read, sizeof(my_data) - 1);

if (data_to_read->crc != crc)
{
	ESP_LOGE(TAG, "CRC failed");
	return ESP_FAIL;
}

return ESP_OK;
where my_data is:

Code: Select all

struct
{
	uint32_t value;
	uint8_t crc;
}my_data;
I'm pretty sure everything is all right since I create data this way:

Code: Select all

my_data new_value;
new_value.value = its_value;
new_value.crc = crc8_be(0, (const uint8_t*)&new_value, sizeof(my_data) - 1);
and saving it with the example partition_write function.

My actual problem is that these pieces of code work fine, but sometimes I have the "CRC failed" message in the log, what could have happened?

Best regards,
P

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

Re: CRC error when read-write values from/to flash

Postby WiFive » Fri Feb 05, 2021 3:54 am

You should print the two crc values and see if you notice any pattern and also consider if you should be using wear leveling.

Palonso
Posts: 95
Joined: Tue Sep 24, 2019 8:43 pm

Re: CRC error when read-write values from/to flash

Postby Palonso » Fri Feb 05, 2021 4:19 am

Hi WiFive , can you explain to me a bit more about wear leveling? I didn't get what you are trying to say.

Best regards,
P

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

Re: CRC error when read-write values from/to flash

Postby ESP_Sprite » Fri Feb 05, 2021 8:06 am

Wrt WiFives wear leveling remark: Are you absolutely sure that the sector you're writing to is erased? Flash, without erasing a sector, can only ever turn 1 bits into 0 bits. If this is all you want to do, you may be better off using the NVS api, which handles the crc, erasing and wear-leveling for you.

Who is online

Users browsing this forum: No registered users and 263 guests