]> granicus.if.org Git - esp-idf/commitdiff
nvs: allow nvs_flash_init to be called more than once
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 15 Nov 2016 10:24:56 +0000 (18:24 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Fri, 18 Nov 2016 12:11:16 +0000 (20:11 +0800)
Also don’t assert in nvs_* functions if nvs_flash_init wasn’t called,
and make nvs_flash_init_custom an internal API for unit tests.

components/nvs_flash/include/nvs_flash.h
components/nvs_flash/src/nvs_api.cpp
components/nvs_flash/src/nvs_platform.hpp
components/nvs_flash/src/nvs_storage.cpp
components/nvs_flash/src/nvs_storage.hpp
components/nvs_flash/src/nvs_test_api.h [new file with mode: 0644]
components/nvs_flash/test/test_nvs.cpp

index eca7f99eddd6d15a3347dd2ab9b7239e04393d9d..0162a8f8ac33e383ccbca4869bee7427f4bf2601 100644 (file)
@@ -25,16 +25,6 @@ extern "C" {
  */
 esp_err_t nvs_flash_init(void);
 
-/**
- * @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);
-
 
 #ifdef __cplusplus
 }
index b2cb5e7ad48eede824195bc8ea521754ef5f8f8d..751542ee1202ffdceadecaa005aa6ebc55c970f4 100644 (file)
@@ -65,6 +65,11 @@ extern "C" void nvs_dump()
 #ifdef ESP_PLATFORM
 extern "C" esp_err_t nvs_flash_init(void)
 {
+    Lock::init();
+    Lock lock;
+    if (s_nvs_storage.isValid()) {
+        return ESP_OK;
+    }
     const esp_partition_t* partition = esp_partition_find_first(
             ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS, NULL);
     if (partition == NULL) {
@@ -78,9 +83,7 @@ extern "C" esp_err_t nvs_flash_init(void)
 
 extern "C" esp_err_t nvs_flash_init_custom(uint32_t baseSector, uint32_t sectorCount)
 {
-    Lock::init();
-    Lock lock;
-    ESP_LOGD(TAG, "init start=%d count=%d", baseSector, sectorCount);
+    ESP_LOGD(TAG, "nvs_flash_init_custom start=%d count=%d", baseSector, sectorCount);
     s_nvs_handles.clear();
     return s_nvs_storage.init(baseSector, sectorCount);
 }
index 374dbca6cc2b6b2af0f810d4787f964c70c9bcf4..0973c4875c38f5eaca4d6ae48ae88ce9d6db4e68 100644 (file)
@@ -16,9 +16,6 @@
 
 
 #ifdef ESP_PLATFORM
-#define NVS_DEBUGV(...) ets_printf(__VA_ARGS__)
-
-#include "rom/ets_sys.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/semphr.h"
 
@@ -30,19 +27,23 @@ class Lock
 public:
     Lock()
     {
-        assert(mSemaphore);
-        xSemaphoreTake(mSemaphore, portMAX_DELAY);
+        if (mSemaphore) {
+            xSemaphoreTake(mSemaphore, portMAX_DELAY);
+        }
     }
 
     ~Lock()
     {
-        assert(mSemaphore);
-        xSemaphoreGive(mSemaphore);
+        if (mSemaphore) {
+            xSemaphoreGive(mSemaphore);
+        }
     }
 
     static esp_err_t init()
     {
-        assert(mSemaphore == nullptr);
+        if (mSemaphore) {
+            return ESP_OK;
+        }
         mSemaphore = xSemaphoreCreateMutex();
         if (!mSemaphore) {
             return ESP_ERR_NO_MEM;
@@ -52,7 +53,9 @@ public:
 
     static void uninit()
     {
-        vSemaphoreDelete(mSemaphore);
+        if (mSemaphore) {
+            vSemaphoreDelete(mSemaphore);
+        }
         mSemaphore = nullptr;
     }
 
index cacfbd402268f12adcfd80c8121b4f59a64686fc..f8da28fa242b34c99927f46cacba67168799a0a3 100644 (file)
@@ -69,6 +69,11 @@ esp_err_t Storage::init(uint32_t baseSector, uint32_t sectorCount)
     return ESP_OK;
 }
 
+bool Storage::isValid() const
+{
+    return mState == StorageState::ACTIVE;
+}
+
 esp_err_t Storage::findItem(uint8_t nsIndex, ItemType datatype, const char* key, Page* &page, Item& item)
 {
     for (auto it = std::begin(mPageManager); it != std::end(mPageManager); ++it) {
index f8cee9f2a96809abf45f8ec543b0dedd8eee8bc7..ecf2651ae5ffecc22e095316831c843c15876cfb 100644 (file)
@@ -47,6 +47,8 @@ public:
 
     esp_err_t init(uint32_t baseSector, uint32_t sectorCount);
 
+    bool isValid() const;
+
     esp_err_t createOrOpenNamespace(const char* nsName, bool canCreate, uint8_t& nsIndex);
 
     esp_err_t writeItem(uint8_t nsIndex, ItemType datatype, const char* key, const void* data, size_t dataSize);
diff --git a/components/nvs_flash/src/nvs_test_api.h b/components/nvs_flash/src/nvs_test_api.h
new file mode 100644 (file)
index 0000000..9794009
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+    
+#include "nvs_flash.h"
+
+/**
+ * @brief Initialize NVS flash storage with custom flash sector layout
+ *
+ * @note  This API is intended to be used in unit tests.
+ * 
+ * @param baseSector Flash sector (units of 4096 bytes) offset to start NVS
+ * @param sectorCount Length (in flash sectors) of NVS region. 
+                                         NVS partition must be at least 3 sectors long.
+ * @return ESP_OK if flash was successfully initialized
+ */
+esp_err_t nvs_flash_init_custom(uint32_t baseSector, uint32_t sectorCount);
+
+
+/**
+ * @brief Dump contents of NVS storage to stdout
+ *
+ * This function may be used for debugging purposes to inspect the state
+ * of NVS pages. For each page, list of entries is also dumped.
+ */
+void nvs_dump(void);
+
+
+#ifdef __cplusplus
+}
+#endif
index 81bf7fd2169d3de7c06b45d7e6c82560ab0cd4fd..282d4de48ebbe9b60f95efc90bb69cca468d28b0 100644 (file)
@@ -13,7 +13,7 @@
 // limitations under the License.
 #include "catch.hpp"
 #include "nvs.hpp"
-#include "nvs_flash.h"
+#include "nvs_test_api.h"
 #include "spi_flash_emulation.h"
 #include <sstream>
 #include <iostream>