1 /* Finding Partitions Example
2 This example code is in the Public Domain (or CC0 licensed, at your option.)
3
4 Unless required by applicable law or agreed to in writing, this
5 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6 CONDITIONS OF ANY KIND, either express or implied.
7 */
8 #include <string.h>
9 #include <assert.h>
10 #include "esp_partition.h"
11 #include "esp_log.h"
12
13 static const char *TAG = "example";
14
app_main(void)15 void app_main(void)
16 {
17 /*
18 * This example uses the partition table from ../partitions_example.csv. For reference, its contents are as follows:
19 *
20 * nvs, data, nvs, 0x9000, 0x6000,
21 * phy_init, data, phy, 0xf000, 0x1000,
22 * factory, app, factory, 0x10000, 1M,
23 * storage, data, , , 0x40000,
24 */
25
26 // Find the partition map in the partition table
27 const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, "storage");
28 assert(partition != NULL);
29
30 static char store_data[] = "ESP-IDF Partition Operations Example (Read, Erase, Write)";
31 static char read_data[sizeof(store_data)];
32
33 // Erase entire partition
34 memset(read_data, 0xFF, sizeof(read_data));
35 ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
36
37 // Write the data, starting from the beginning of the partition
38 ESP_ERROR_CHECK(esp_partition_write(partition, 0, store_data, sizeof(store_data)));
39 ESP_LOGI(TAG, "Written data: %s", store_data);
40
41 // Read back the data, checking that read data and written data match
42 ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, sizeof(read_data)));
43 assert(memcmp(store_data, read_data, sizeof(read_data)) == 0);
44 ESP_LOGI(TAG, "Read data: %s", read_data);
45
46 // Erase the area where the data was written. Erase size shoud be a multiple of SPI_FLASH_SEC_SIZE
47 // and also be SPI_FLASH_SEC_SIZE aligned
48 ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, SPI_FLASH_SEC_SIZE));
49
50 // Read back the data (should all now be 0xFF's)
51 memset(store_data, 0xFF, sizeof(read_data));
52 ESP_ERROR_CHECK(esp_partition_read(partition, 0, read_data, sizeof(read_data)));
53 assert(memcmp(store_data, read_data, sizeof(read_data)) == 0);
54
55 ESP_LOGI(TAG, "Erased data");
56
57 ESP_LOGI(TAG, "Example end");
58 }
59