I am following this example to implement my own partition and store some statistical data in my project: https://github.com/espressif/esp-idf/bl ... ain/main.c
It's simple enough and it works just fine but I have a doubt.
The documentation says that you must first call esp_partition_erase_range() before esp_partition_write()
How do I know if the partition has already been erased? Since I am gonna use this partition to save statistical data from my project, I want to check if this is the first time I am gonna save data and only that time erase the entire partition. Otherwise just save the new data.
Should I store another variable like "partionErased" in the NVS? I cannot find how to check this in the documentation.
Also I modified that example and added another data partition. From this:
Code: Select all
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, , , 0x40000,
Code: Select all
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, 0x9000, 0x6000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
storage, data, , , 0x40000,
storage2, data, , , 0x40000,
I just added this code at the end:
Code: Select all
const esp_partition_t *partition2 = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage2");
assert(partition2 != NULL);
ESP_LOGI(TAG, "partition2 found");
static char store_data2[] = "PARTITION 2 DATA";
static char read_data2[sizeof(store_data2)];
esp_err_t err = esp_partition_write(partition2, 0, store_data2, sizeof(store_data2));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Error (%s) al escribir!\n", esp_err_to_name(err));
}
else
{
ESP_LOGI(TAG, "Written data to partition2: %s", store_data2);
}
ESP_ERROR_CHECK(esp_partition_read(partition2, 0, read_data2, sizeof(read_data2)));
assert(memcmp(store_data2, read_data2, sizeof(read_data2)) == 0);
ESP_LOGI(TAG, "Read data2: %s", read_data2);
Why is this working?
Thank you.