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 const char store_data[] = "ESP-IDF Partition Memory Map Example";
31
32 // Prepare data to be read later using the mapped address
33 ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->size));
34 ESP_ERROR_CHECK(esp_partition_write(partition, 0, store_data, sizeof(store_data)));
35 ESP_LOGI(TAG, "Written sample data to partition: %s", store_data);
36
37 const void *map_ptr;
38 spi_flash_mmap_handle_t map_handle;
39
40 // Map the partition to data memory
41 ESP_ERROR_CHECK(esp_partition_mmap(partition, 0, partition->size, SPI_FLASH_MMAP_DATA, &map_ptr, &map_handle));
42 ESP_LOGI(TAG, "Mapped partition to data memory address %p", map_ptr);
43
44 // Read back the written verification data using the mapped memory pointer
45 char read_data[sizeof(store_data)];
46 memcpy(read_data, map_ptr, sizeof(read_data));
47 ESP_LOGI(TAG, "Read sample data from partition using mapped memory: %s", (char*) read_data);
48
49 assert(strcmp(store_data, read_data) == 0);
50 ESP_LOGI(TAG, "Data matches");
51
52 // Unmap mapped memory
53 spi_flash_munmap(map_handle);
54 ESP_LOGI(TAG, "Unmapped partition from data memory");
55
56 ESP_LOGI(TAG, "Example end");
57 }
58