]> granicus.if.org Git - esp-idf/commitdiff
nvs: initialize using layout from partition table
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 15 Nov 2016 10:23:29 +0000 (18:23 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Fri, 18 Nov 2016 12:11:16 +0000 (20:11 +0800)
components/nvs_flash/include/nvs_flash.h
components/nvs_flash/src/nvs_api.cpp

index d6e199025030133cad4bb0181e8d6c8d59e68241..eca7f99eddd6d15a3347dd2ab9b7239e04393d9d 100644 (file)
 extern "C" {
 #endif
 
-/** Initialise NVS flash storage with default flash sector layout
-
-    Temporarily, this region is hardcoded as a 12KB (0x3000 byte)
-    region starting at 36KB (0x9000 byte) offset in flash.
-
-    @return ESP_OK if flash was successfully initialised.
-*/
+/**
+ * @brief Initialize NVS flash storage with layout given in the partition table.
+ *
+ * @return ESP_OK if storage was successfully initialized.
+ */
 esp_err_t nvs_flash_init(void);
 
-/** Initialise NVS flash storage with custom flash sector layout
-
-    @param baseSector Flash sector (units of 4096 bytes) offset to start NVS.
-    @param sectorCount Length (in flash sectors) of NVS region.
-
-    @return ESP_OK if flash was successfully initialised.
-
-    @note Use this parameter if you're not using the options in menuconfig for
-          configuring flash layout & partition table.
-*/
+/**
+ * @brief Initialize NVS flash storage with custom flash sector layout
+ * @note  Make sure specified sectors don't fall within ranges occupied
+ *        by other partitions.
+ * @param baseSector Flash sector (units of 4096 bytes) offset to start NVS
+ * @param sectorCount Length (in flash sectors) of NVS region
+ * @return ESP_OK if flash was successfully initialized
+ */
 esp_err_t nvs_flash_init_custom(uint32_t baseSector, uint32_t sectorCount);
 
 
index f6c6c588aaab90e548e2f3e372bd60f3b357cb64..b2cb5e7ad48eede824195bc8ea521754ef5f8f8d 100644 (file)
@@ -16,6 +16,7 @@
 #include "nvs_storage.hpp"
 #include "intrusive_list.h"
 #include "nvs_platform.hpp"
+#include "esp_partition.h"
 #include "sdkconfig.h"
 
 #ifdef ESP_PLATFORM
@@ -61,10 +62,19 @@ extern "C" void nvs_dump()
     s_nvs_storage.debugDump();
 }
 
+#ifdef ESP_PLATFORM
 extern "C" esp_err_t nvs_flash_init(void)
 {
-    return nvs_flash_init_custom(9, 3);
+    const esp_partition_t* partition = esp_partition_find_first(
+            ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
+    if (partition == NULL) {
+        return ESP_ERR_NOT_FOUND;
+    }
+
+    return nvs_flash_init_custom(partition->address / SPI_FLASH_SEC_SIZE,
+            partition->size / SPI_FLASH_SEC_SIZE);
 }
+#endif
 
 extern "C" esp_err_t nvs_flash_init_custom(uint32_t baseSector, uint32_t sectorCount)
 {