main.c 2.3 KB

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