Skip to content

Commit 80ed209

Browse files
committed
Merge branch 'feat/nvs_iteration_statistics_examples' into 'master'
feat(examples/storage): add nvs statistics and iteration examples Closes DOC-12742 See merge request espressif/esp-idf!42327
2 parents 18eee1d + f13c16e commit 80ed209

File tree

20 files changed

+756
-0
lines changed

20 files changed

+756
-0
lines changed

docs/en/api-guides/file-system-considerations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ Points to keep in mind when developing NVS related code:
190190

191191
- :example:`storage/nvs/nvs_rw_value` demonstrates how to use NVS to write and read a single integer value.
192192
- :example:`storage/nvs/nvs_rw_blob` demonstrates how to use NVS to write and read a blob.
193+
- :example:`storage/nvs/nvs_statistics` demonstrates how to obtain and interpret NVS usage statistics: free/used/available/total number of entries and number of namespaces in given NVS partition.
194+
- :example:`storage/nvs/nvs_iteration` demonstrates how to iterate over entries of specific (or any) NVS data type and how to obtain info about such entries.
193195
- :example:`security/nvs_encryption_hmac` demonstrates NVS encryption using the HMAC peripheral, where the encryption keys are derived from the HMAC key burnt in eFuse.
194196
- :example:`security/flash_encryption` demonstrates the flash encryption workflow including NVS partition creation and usage.
195197

docs/en/api-reference/storage/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ Examples
5353
- Shows the use of the C-style API to read and write integer data types in NVS flash.
5454
* - :example:`nvs_rw_value_cxx <storage/nvs/nvs_rw_value_cxx>`
5555
- Shows the use of the C++-style API to read and write integer data types in NVS flash.
56+
* - :example:`nvs_statistics <storage/nvs/nvs_statistics>`
57+
- Shows the use of the C-style API to obtain NVS usage statistics: free/used/available/total number of entries and number of namespaces in given NVS partition.
58+
* - :example:`nvs_iteration <storage/nvs/nvs_iteration>`
59+
- Shows the use of the C-style API to iterate over entries of specific (or any) NVS data type and how to obtain info about such entries.
5660
* - :example:`nvs_bootloader <storage/nvs/nvs_bootloader>`
5761
- Shows the use of the API available to the bootloader code to read NVS data.
5862
* - :example:`nvsgen <storage/nvs/nvsgen>`

docs/en/api-reference/storage/nvs_flash.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ You can find code examples in the :example:`storage/nvs` directory of ESP-IDF ex
177177

178178
This example does exactly the same as :example:`storage/nvs/nvs_rw_value`, except that it uses the C++ NVS handle class.
179179

180+
:example:`storage/nvs/nvs_statistics`
181+
182+
This example demonstrates how to obtain and interpret NVS usage statistics: free/used/available/total number of entries and number of namespaces in given NVS partition.
183+
184+
Default NVS partition is erased for a clean run of this example. Then mock data string values are written.
185+
186+
Usage statistics are obtained prior to and post writing, with the differences being compared to expected values of newly used entries.
187+
188+
:example:`storage/nvs/nvs_iteration`
189+
190+
This example demonstrates how to iterate over entries of specific (or any) NVS data type and how to obtain info about such entries.
191+
192+
Default NVS partition is erased for a clean run of this example. Then mock data consisting of different NVS integer data types are written.
193+
194+
After that, the example iterates over each individual data type as well as the generic ``NVS_TYPE_ANY`` type, and logs the information obtained from each iteration.
195+
180196
Internals
181197
---------
182198

docs/zh_CN/api-guides/file-system-considerations.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ NVS 具有如下特性:
190190

191191
- :example:`storage/nvs/nvs_rw_value` 演示了如何写入和读取一个整数值。
192192
- :example:`storage/nvs/nvs_rw_blob` 演示如何写入和读取一个 blob。
193+
- :example:`storage/nvs/nvs_statistics` 演示了如何获取并解读 NVS 使用情况统计信息:包括指定 NVS 分区中的空闲、已用、可用、总条目数、以及命名空间数量。
194+
- :example:`storage/nvs/nvs_iteration` 演示了如何遍历特定(或任意)NVS 数据类型的条目,以及如何获取这些条目的相关信息。
193195
- :example:`security/nvs_encryption_hmac` 演示了如何用 HMAC 外设进行 NVS 加密,并通过 efuse 中的 HMAC 密钥生成加密密钥。
194196
- :example:`security/flash_encryption` 演示了如何进行 flash 加密,包括创建和使用 NVS 分区。
195197

docs/zh_CN/api-reference/storage/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
- 演示了如何在 NVS flash 中使用 C 语言 API 读写整数数据类型。
5454
* - :example:`nvs_rw_value <storage/nvs/nvs_rw_value_cxx>`
5555
- 演示了如何在 NVS flash 中使用 C++ 语言 API 读写整数数据类型。
56+
* - :example:`nvs_statistics <storage/nvs/nvs_statistics>`
57+
- 演示了如何使用 C 风格 API 获取 NVS 使用情况统计信息,包括指定 NVS 分区中的空闲、已用、可用、总条目数、以及命名空间数量。
58+
* - :example:`nvs_iteration <storage/nvs/nvs_iteration>`
59+
- 演示了如何使用 C 风格 API 遍历特定(或任意)NVS 数据类型的条目,以及如何获取这些条目的相关信息。
5660
* - :example:`nvs_bootloader <storage/nvs/nvs_bootloader>`
5761
- 演示了如何使用引导加载程序代码中可用的 API 来读取 NVS 数据。
5862
* - :example:`nvsgen <storage/nvs/nvsgen>`

docs/zh_CN/api-reference/storage/nvs_flash.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ ESP-IDF :example:`storage/nvs` 目录下提供了数个代码示例:
177177

178178
这个例子与 :example:`storage/nvs/nvs_rw_value` 完全一样,只是使用了 C++ 的 NVS 句柄类。
179179

180+
:example:`storage/nvs/nvs_statistics`
181+
182+
该示例演示了如何获取并解读 NVS 使用情况统计信息:包括指定 NVS 分区中的空闲、已用、可用、总条目数、以及命名空间数量。
183+
184+
默认的 NVS 分区会在运行本示例前被擦除,以确保干净的运行环境。随后,会写入模拟的字符串类型数据。
185+
186+
在写入数据前后分别获取使用情况统计信息,并将两者的差异与新占用条目的预期值进行比较。
187+
188+
:example:`storage/nvs/nvs_iteration`
189+
190+
该示例演示了如何遍历特定(或任意)NVS 数据类型的条目,以及如何获取这些条目的相关信息。
191+
192+
默认的 NVS 分区会在运行本示例前被擦除,以确保干净的运行环境。随后,会写入包含不同 NVS 整型数据类型的模拟数据。
193+
194+
之后,本示例会遍历各个数据类型以及通用的 ``NVS_TYPE_ANY`` 类型,并记录在每次遍历过程中获取到的信息。
195+
180196
内部实现
181197
---------
182198

examples/storage/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ The examples are grouped into sub-directories by category. Each category directo
1818
* `nvs_rw_value` example demonstrates how to read and write a single integer value using NVS.
1919
* `nvs_rw_value_cxx` example demonstrates how to read and write a single integer value using NVS (it uses the C++ NVS handle API).
2020
* `nvs_console` example demonstrates how to use NVS through an interactive console interface.
21+
* `nvs_statistics` example demonstrates how to obtain and interpret stats about used/available NVS storage entries in given NVS partition.
22+
* `nvs_iteration` example demonstrates iterating over entries of specific (or any) data type in given namespace, and the info to be obtained about the entries while doing so.
2123
* `partition_api` examples demonstrate how to use different partition APIs.
2224
* `parttool` example demonstrates common operations the partitions tool allows the user to perform.
2325
* `sd_card` examples demonstrate how to use an SD card with an ESP device.

examples/storage/nvs/.build-test-rules.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ examples/storage/nvs/nvs_console:
1717
disable_test:
1818
- if: IDF_TARGET not in ["esp32", "esp32c3"]
1919
reason: only one target per arch needed
20+
examples/storage/nvs/nvs_iteration:
21+
depends_components:
22+
- nvs_flash
23+
disable_test:
24+
- if: IDF_TARGET not in ["esp32", "esp32c3"]
25+
reason: only one target per arch needed
2026
examples/storage/nvs/nvs_rw_blob:
2127
depends_components:
2228
- nvs_flash
@@ -38,6 +44,13 @@ examples/storage/nvs/nvs_rw_value_cxx:
3844
- if: IDF_TARGET not in ["esp32", "esp32c3"]
3945
reason: only one target per arch needed
4046

47+
examples/storage/nvs/nvs_statistics:
48+
depends_components:
49+
- nvs_flash
50+
disable_test:
51+
- if: IDF_TARGET not in ["esp32", "esp32c3"]
52+
reason: only one target per arch needed
53+
4154
examples/storage/nvs/nvsgen:
4255
depends_components:
4356
- nvs_flash
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.22)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
7+
idf_build_set_property(MINIMAL_BUILD ON)
8+
project(nvs_iteration)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C5 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-H21 | ESP32-H4 | ESP32-P4 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | --------- | -------- | --------- | -------- | -------- | -------- | -------- |
3+
4+
# Non-Volatile Storage (NVS) Iteration Example
5+
6+
This example showcases how to iterate and obtain info about NVS entries of a specific (or any) NVS datatype.
7+
8+
Default "nvs" partition is first erased to allow for clean example run, followed by writing 2 sets of key value pairs of different types to NVS storage.
9+
After that, iteration is performed over the individual data types, as well as the generic `NVS_TYPE_ANY`, and relevant entry info gained from iteration is verbosely logged.
10+
11+
Iterator creation in this example is performed by [nvs_entry_find()](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/nvs_flash.html#_CPPv414nvs_entry_findPKcPKc10nvs_type_tP14nvs_iterator_t) and accessing entry info by [nvs_entry_info()](https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/storage/nvs_flash.html#_CPPv414nvs_entry_infoK14nvs_iterator_tP16nvs_entry_info_t).
12+
13+
14+
Detailed functional description of NVS and API is provided in [documentation](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/storage/nvs_flash.html).
15+
16+
## How to use example
17+
18+
### Hardware required
19+
20+
This example does not require any special hardware, and can be run on any common development board.
21+
22+
### Build and flash
23+
24+
Build the project and flash it to the board, then run monitor tool to view serial output:
25+
26+
```
27+
idf.py -p PORT flash monitor
28+
```
29+
30+
(To exit the serial monitor, type ``Ctrl-]``.)
31+
32+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
33+
34+
## Example Output
35+
36+
```
37+
...
38+
I (265) nvs_iteration_example: Erasing the contents of the default NVS partition...
39+
I (595) nvs_iteration_example: Opening Non-Volatile Storage (NVS) handle for namespace '_mock_data'...
40+
I (605) nvs_iteration_example: Writing u32 mock data key-value pairs to NVS namespace '_mock_data'...
41+
I (605) nvs_iteration_example: Wrote 7 u32 mock data key-value pairs to NVS namespace '_mock_data'.
42+
I (615) nvs_iteration_example: Writing i8 mock data key-value pairs to NVS namespace '_mock_data'...
43+
I (625) nvs_iteration_example: Wrote 7 i8 mock data key-value pairs to NVS namespace '_mock_data'.
44+
I (625) nvs_iteration_example: Committing data in NVS namespace '_mock_data'...
45+
I (635) nvs_iteration_example: NVS handle for namespace '_mock_data' closed.
46+
I (645) nvs_iteration_example: Iterating over NVS_TYPE_U32 data in namespace '_mock_data'...
47+
I (655) nvs_iteration_example: Entry info: key 'system_startup' for data type type NVS_TYPE_U32 in namespace '_mock_data'
48+
I (665) nvs_iteration_example: Entry info: key 'user_login' for data type type NVS_TYPE_U32 in namespace '_mock_data'
49+
I (675) nvs_iteration_example: Entry info: key 'data_backup' for data type type NVS_TYPE_U32 in namespace '_mock_data'
50+
I (685) nvs_iteration_example: Entry info: key 'sched_update' for data type type NVS_TYPE_U32 in namespace '_mock_data'
51+
I (695) nvs_iteration_example: Entry info: key 'db_cleanup' for data type type NVS_TYPE_U32 in namespace '_mock_data'
52+
I (705) nvs_iteration_example: Entry info: key 'security_scan' for data type type NVS_TYPE_U32 in namespace '_mock_data'
53+
I (715) nvs_iteration_example: Entry info: key 'system_shutdown' for data type type NVS_TYPE_U32 in namespace '_mock_data'
54+
I (725) nvs_iteration_example: Iterated over 7 entries.
55+
I (725) nvs_iteration_example: Iterating over NVS_TYPE_I8 data in namespace '_mock_data'...
56+
I (735) nvs_iteration_example: Entry info: key 'temperature' for data type type NVS_TYPE_I8 in namespace '_mock_data'
57+
I (745) nvs_iteration_example: Entry info: key 'humidity' for data type type NVS_TYPE_I8 in namespace '_mock_data'
58+
I (755) nvs_iteration_example: Entry info: key 'light_level' for data type type NVS_TYPE_I8 in namespace '_mock_data'
59+
I (765) nvs_iteration_example: Entry info: key 'motion_detected' for data type type NVS_TYPE_I8 in namespace '_mock_data'
60+
I (775) nvs_iteration_example: Entry info: key 'battery_level' for data type type NVS_TYPE_I8 in namespace '_mock_data'
61+
I (785) nvs_iteration_example: Entry info: key 'signal_strength' for data type type NVS_TYPE_I8 in namespace '_mock_data'
62+
I (795) nvs_iteration_example: Entry info: key 'error_code' for data type type NVS_TYPE_I8 in namespace '_mock_data'
63+
I (805) nvs_iteration_example: Iterated over 7 entries.
64+
I (815) nvs_iteration_example: Iterating over NVS_TYPE_ANY data in namespace '_mock_data'...
65+
I (825) nvs_iteration_example: Iterated over 14 entries.
66+
I (825) nvs_iteration_example: Returning from app_main().
67+
I (835) main_task: Returned from app_main()
68+
...
69+
```

0 commit comments

Comments
 (0)