From: Ivan Grokhotkov Date: Tue, 15 Nov 2016 10:23:29 +0000 (+0800) Subject: nvs: initialize using layout from partition table X-Git-Tag: v1.0~44^2~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=51021b06f8c7e49e937f8c295df95807bf505eb3;p=esp-idf nvs: initialize using layout from partition table --- diff --git a/components/nvs_flash/include/nvs_flash.h b/components/nvs_flash/include/nvs_flash.h index d6e1990250..eca7f99edd 100644 --- a/components/nvs_flash/include/nvs_flash.h +++ b/components/nvs_flash/include/nvs_flash.h @@ -18,25 +18,21 @@ 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); diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index f6c6c588aa..b2cb5e7ad4 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -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) {