Skip to content

Commit d69378c

Browse files
committed
Merge branch 'update/nvs_example' into 'master'
refactor(nvs_examples): refactor nvs storage examples and add nvs_console example See merge request espressif/esp-idf!37978
2 parents 241afda + b077cae commit d69378c

File tree

71 files changed

+1022
-495
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1022
-495
lines changed

docs/en/api-guides/bootloader.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,6 @@ The current bootloader implementation allows a project to extend it or modify it
186186

187187
In the bootloader space, you cannot use the drivers and functions from other components unless they explicitly support run in bootloader. If necessary, then the required functionality should be placed in the project's `bootloader_components` directory (note that this will increase its size). Examples of components that can be used in the bootloader are:
188188

189-
* :example:`storage/nvs_bootloader`
189+
* :example:`storage/nvs/nvs_bootloader`
190190

191191
If the bootloader grows too large then it can collide with the partition table, which is flashed at offset 0x8000 by default. Increase the :ref:`partition table offset <CONFIG_PARTITION_TABLE_OFFSET>` value to place the partition table later in the flash. This increases the space available for the bootloader.

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

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

182182
**Examples:**
183183

184-
- :example:`storage/nvs_rw_value` demonstrates how to use NVS to write and read a single integer value.
185-
- :example:`storage/nvs_rw_blob` demonstrates how to use NVS to write and read a blob.
184+
- :example:`storage/nvs/nvs_rw_value` demonstrates how to use NVS to write and read a single integer value.
185+
- :example:`storage/nvs/nvs_rw_blob` demonstrates how to use NVS to write and read a blob.
186186
- :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.
187187
- :example:`security/flash_encryption` demonstrates the flash encryption workflow including NVS partition creation and usage.
188188

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ Examples
4747

4848
* - **Code Example**
4949
- **Description**
50-
* - :example:`nvs_rw_blob <storage/nvs_rw_blob>`
50+
* - :example:`nvs_rw_blob <storage/nvs/nvs_rw_blob>`
5151
- Shows the use of the C-style API to read and write blob data types in NVS flash.
52-
* - :example:`nvs_rw_value <storage/nvs_rw_value>`
52+
* - :example:`nvs_rw_value <storage/nvs/nvs_rw_value>`
5353
- Shows the use of the C-style API to read and write integer data types in NVS flash.
54-
* - :example:`nvs_rw_value_cxx <storage/nvs_rw_value_cxx>`
54+
* - :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_bootloader <storage/nvs_bootloader>`
56+
* - :example:`nvs_bootloader <storage/nvs/nvs_bootloader>`
5757
- Shows the use of the API available to the bootloader code to read NVS data.
58-
* - :example:`nvsgen <storage/nvsgen>`
58+
* - :example:`nvsgen <storage/nvs/nvsgen>`
5959
- Demonstrates how to use the Python-based NVS image generation tool to create an NVS partition image from the contents of a CSV file.
60+
* - :example:`nvs_console <storage/nvs/nvs_console>`
61+
- Demonstrates how to use NVS through an interactive console interface.
6062

6163
.. list-table:: Common Filesystem API
6264
:widths: 25 75

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Applications are expected to follow the steps below in order to enable decryptio
3737
Application Example
3838
-------------------
3939

40-
You can find code examples in :example:`storage/nvs_bootloader` (in the :example:`storage` directory of ESP-IDF examples).
40+
You can find code examples in :example:`storage/nvs/nvs_bootloader` (in the :example:`storage/nvs` directory of ESP-IDF examples).
4141

4242
This section demonstrates how to prepare data in the input-output structure for various data types, namespaces, and keys. It includes an example of reading string data from NVS.
4343

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ If ``FLASH_IN_PROJECT`` is not specified, the image will still be generated, but
154154
Application Example
155155
-------------------
156156

157-
You can find code examples in the :example:`storage` directory of ESP-IDF examples:
157+
You can find code examples in the :example:`storage/nvs` directory of ESP-IDF examples:
158158

159-
:example:`storage/nvs_rw_value`
159+
:example:`storage/nvs/nvs_rw_value`
160160

161161
Demonstrates how to read a single integer value from, and write it to NVS.
162162

163163
The value checked in this example holds the number of the {IDF_TARGET_NAME} module restarts. The value's function as a counter is only possible due to its storing in NVS.
164164

165165
The example also shows how to check if a read/write operation was successful, or if a certain value has not been initialized in NVS. The diagnostic procedure is provided in plain text to help you track the program flow and capture any issues on the way.
166166

167-
:example:`storage/nvs_rw_blob`
167+
:example:`storage/nvs/nvs_rw_blob`
168168

169169
Demonstrates how to read a single integer value and a blob (binary large object), and write them to NVS to preserve this value between {IDF_TARGET_NAME} module restarts.
170170

@@ -173,9 +173,9 @@ You can find code examples in the :example:`storage` directory of ESP-IDF exampl
173173

174174
The example also shows how to implement the diagnostic procedure to check if the read/write operation was successful.
175175

176-
:example:`storage/nvs_rw_value_cxx`
176+
:example:`storage/nvs/nvs_rw_value_cxx`
177177

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

180180
Internals
181181
---------

docs/zh_CN/api-guides/bootloader.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,6 @@ ESP-IDF 二级引导加载程序位于 flash 的 {IDF_TARGET_CONFIG_BOOTLOADER_O
186186

187187
在引导加载程序的代码中,不能使用其他组件提供的驱动和函数,除非某个驱动或函数明确声明支持在引导加载程序中运行。如果确实需要,请将所需功能放在项目的 `bootloader_components` 目录中(注意,这会增加引导加载程序的大小)。以下是可以在引导加载程序中使用的组件示例:
188188

189-
* :example:`storage/nvs_bootloader`
189+
* :example:`storage/nvs/nvs_bootloader`
190190

191191
如果引导加载程序过大,则可能与内存中的分区表重叠,分区表默认烧录在偏移量 0x8000 处。增加 :ref:`分区表偏移量 <CONFIG_PARTITION_TABLE_OFFSET>` ,将分区表放在 flash 中靠后的区域,这样可以增加引导加载程序的可用空间。

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

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

182182
**示例:**
183183

184-
- :example:`storage/nvs_rw_value` 演示了如何写入和读取一个整数值。
185-
- :example:`storage/nvs_rw_blob` 演示如何写入和读取一个 blob。
184+
- :example:`storage/nvs/nvs_rw_value` 演示了如何写入和读取一个整数值。
185+
- :example:`storage/nvs/nvs_rw_blob` 演示如何写入和读取一个 blob。
186186
- :example:`security/nvs_encryption_hmac` 演示了如何用 HMAC 外设进行 NVS 加密,并通过 efuse 中的 HMAC 密钥生成加密密钥。
187187
- :example:`security/flash_encryption` 演示了如何进行 flash 加密,包括创建和使用 NVS 分区。
188188

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@
4747

4848
* - **例程**
4949
- **描述**
50-
* - :example:`nvs_rw_blob <storage/nvs_rw_blob>`
50+
* - :example:`nvs_rw_blob <storage/nvs/nvs_rw_blob>`
5151
- 演示了如何在 NVS flash 中使用 C 语言 API 读写 blob 数据类型。
52-
* - :example:`nvs_rw_value <storage/nvs_rw_value>`
52+
* - :example:`nvs_rw_value <storage/nvs/nvs_rw_value>`
5353
- 演示了如何在 NVS flash 中使用 C 语言 API 读写整数数据类型。
54-
* - :example:`nvs_rw_value <storage/nvs_rw_value>`
54+
* - :example:`nvs_rw_value <storage/nvs/nvs_rw_value_cxx>`
5555
- 演示了如何在 NVS flash 中使用 C++ 语言 API 读写整数数据类型。
56-
* - :example:`nvs_bootloader <storage/nvs_bootloader>`
56+
* - :example:`nvs_bootloader <storage/nvs/nvs_bootloader>`
5757
- 演示了如何使用引导加载程序代码中可用的 API 来读取 NVS 数据。
58-
* - :example:`nvsgen <storage/nvsgen>`
58+
* - :example:`nvsgen <storage/nvs/nvsgen>`
5959
- 演示了如何使用基于 Python 的 NVS 镜像生成工具,根据 CSV 文件内容创建 NVS 分区镜像。
60+
* - :example:`nvs_console <storage/nvs/nvs_console>`
61+
- 演示了如何通过交互式控制台界面使用 NVS。
6062

6163
.. list-table:: 常用文件系统 API
6264
:widths: 25 75

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
应用示例
3838
-----------
3939

40-
代码示例请参阅 ESP-IDF 示例 :example:`storage` 目录下的 :example:`storage/nvs_bootloader`。
40+
代码示例请参阅 ESP-IDF 示例 :example:`storage/nvs` 目录下的 :example:`storage/nvs/nvs_bootloader`。
4141

4242
本节演示了如何在输入/输出结构中准备数据,以支持不同的数据类型、命名空间和键。此外,还包含从 NVS 读取字符串数据的示例。
4343

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,17 @@ NVS 分区生成程序帮助生成 NVS 分区二进制文件,可使用烧录
154154
应用示例
155155
-------------------
156156

157-
ESP-IDF :example:`storage` 目录下提供了数个代码示例:
157+
ESP-IDF :example:`storage/nvs` 目录下提供了数个代码示例:
158158

159-
:example:`storage/nvs_rw_value`
159+
:example:`storage/nvs/nvs_rw_value`
160160

161161
演示如何读取及写入 NVS 单个整数值。
162162

163163
此示例中的值表示 {IDF_TARGET_NAME} 模组重启次数。NVS 中数据不会因为模组重启而丢失,因此只有将这一值存储于 NVS 中,才能起到重启次数计数器的作用。
164164

165165
该示例也演示了如何检测读取/写入操作是否成功,以及某个特定值是否在 NVS 中尚未初始化。诊断程序以纯文本形式提供,有助于追踪程序流程,及时发现问题。
166166

167-
:example:`storage/nvs_rw_blob`
167+
:example:`storage/nvs/nvs_rw_blob`
168168

169169
演示如何读取及写入 NVS 单个整数值和 BLOB(二进制大对象),并在 NVS 中存储这一数值,即便 {IDF_TARGET_NAME} 模组重启也不会消失。
170170

@@ -173,9 +173,9 @@ ESP-IDF :example:`storage` 目录下提供了数个代码示例:
173173

174174
该示例也演示了如何执行诊断程序以检测读取/写入操作是否成功。
175175

176-
:example:`storage/nvs_rw_value_cxx`
176+
:example:`storage/nvs/nvs_rw_value_cxx`
177177

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

180180
内部实现
181181
---------

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

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,6 @@ examples/storage/emmc:
1616
- if: IDF_TARGET == "esp32s3"
1717
reason: only support on esp32s3
1818

19-
examples/storage/nvs_bootloader:
20-
depends_components:
21-
- nvs_flash
22-
- nvs_sec_provider
23-
disable:
24-
- if: CONFIG_NAME == "nvs_enc_flash_enc" and (SOC_AES_SUPPORTED != 1 and ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB != 1)
25-
- if: CONFIG_NAME == "nvs_enc_hmac" and (SOC_HMAC_SUPPORTED != 1 or (SOC_HMAC_SUPPORTED == 1 and (SOC_AES_SUPPORTED != 1 and ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB != 1)))
26-
reason: As of now in such cases, we do not have any way to perform AES operations in the bootloader build
27-
28-
examples/storage/nvs_rw_blob:
29-
depends_components:
30-
- nvs_flash
31-
- driver
32-
disable_test:
33-
- if: IDF_TARGET not in ["esp32", "esp32c3"]
34-
reason: only one target per arch needed
35-
36-
examples/storage/nvs_rw_value:
37-
depends_components:
38-
- nvs_flash
39-
disable_test:
40-
- if: IDF_TARGET not in ["esp32", "esp32c3"]
41-
reason: only one target per arch needed
42-
43-
examples/storage/nvs_rw_value_cxx:
44-
depends_components:
45-
- nvs_flash
46-
disable_test:
47-
- if: IDF_TARGET not in ["esp32", "esp32c3"]
48-
reason: only one target per arch needed
49-
50-
examples/storage/nvsgen:
51-
depends_components:
52-
- nvs_flash
53-
disable_test:
54-
- if: IDF_TARGET != "esp32"
55-
reason: only one target needed
56-
5719
examples/storage/partition_api/partition_find:
5820
depends_components:
5921
- esp_partition

examples/storage/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The examples are grouped into sub-directories by category. Each category directo
1717
* `nvs_rw_blob` example demonstrates how to read and write a single integer value and a blob (binary large object) using NVS to preserve them between ESP module restarts.
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).
20+
* `nvs_console` example demonstrates how to use NVS through an interactive console interface.
2021
* `partition_api` examples demonstrate how to use different partition APIs.
2122
* `parttool` example demonstrates common operations the partitions tool allows the user to perform.
2223
* `sd_card` examples demonstrate how to use an SD card with an ESP device.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps
2+
3+
examples/storage/nvs/nvs_bootloader:
4+
depends_components:
5+
- nvs_flash
6+
- nvs_sec_provider
7+
disable:
8+
- if: CONFIG_NAME == "nvs_enc_flash_enc" and (SOC_AES_SUPPORTED != 1 and ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB != 1)
9+
- if: CONFIG_NAME == "nvs_enc_hmac" and (SOC_HMAC_SUPPORTED != 1 or (SOC_HMAC_SUPPORTED == 1 and (SOC_AES_SUPPORTED != 1 and ESP_ROM_HAS_MBEDTLS_CRYPTO_LIB != 1)))
10+
reason: As of now in such cases, we do not have any way to perform AES operations in the bootloader build
11+
12+
examples/storage/nvs/nvs_console:
13+
depends_components:
14+
- nvs_flash
15+
- console
16+
- vfs
17+
disable_test:
18+
- if: IDF_TARGET not in ["esp32", "esp32c3"]
19+
reason: only one target per arch needed
20+
examples/storage/nvs/nvs_rw_blob:
21+
depends_components:
22+
- nvs_flash
23+
- driver
24+
disable_test:
25+
- if: IDF_TARGET not in ["esp32", "esp32c3"]
26+
reason: only one target per arch needed
27+
28+
examples/storage/nvs/nvs_rw_value:
29+
depends_components:
30+
- nvs_flash
31+
disable_test:
32+
- if: IDF_TARGET not in ["esp32", "esp32c3"]
33+
reason: only one target per arch needed
34+
35+
examples/storage/nvs/nvs_rw_value_cxx:
36+
depends_components:
37+
- nvs_flash
38+
disable_test:
39+
- if: IDF_TARGET not in ["esp32", "esp32c3"]
40+
reason: only one target per arch needed
41+
42+
examples/storage/nvs/nvsgen:
43+
depends_components:
44+
- nvs_flash
45+
disable_test:
46+
- if: IDF_TARGET != "esp32"
47+
reason: only one target needed

examples/storage/nvs_bootloader/README.md renamed to examples/storage/nvs/nvs_bootloader/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Enable NVS encryption using your preferred scheme. Please find more details rega
137137
(Note: In case you select the `HMAC based NVS encryption scheme`, make sure that you burn the below mentioned [HMAC key](./main/nvs_enc_hmac_key.bin) in the efuses.)
138138

139139
For generating the encrypted NVS partitions, we shall use [NVS partition generator](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/storage/nvs_partition_gen.html#nvs-partition-generator-utility).
140-
We shall use the [nvs_partition_gen.py](../../../components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py) script for the operations.
140+
We shall use the [nvs_partition_gen.py](../../../../components/nvs_flash/nvs_partition_generator/nvs_partition_gen.py) script for the operations.
141141

142142
Along with the above mentioned file structure, the project folder also contains pre-generated encrypted partitions and the partition corresponding to the selected NVS encryption scheme is flashed along with the build artefacts using the `main/CMakeLists.txt`.
143143

@@ -146,13 +146,13 @@ In case the data in `nvs_data.csv` is modified, these encrypted NVS partitions c
146146
1. NVS Encryption using the flash encryption scheme
147147

148148
```
149-
python nvs_partition_gen.py encrypt $IDF_PATH/examples/storage/nvs_bootloader/nvs_data.csv $IDF_PATH/examples/storage/nvs_bootloader/main/nvs_encrypted.bin 0x6000 --inputkey $IDF_PATH/examples/storage/nvs_bootloader/main/encryption_keys.bin
149+
python nvs_partition_gen.py encrypt $IDF_PATH/examples/storage/nvs/nvs_bootloader/nvs_data.csv $IDF_PATH/examples/storage/nvs/nvs_bootloader/main/nvs_encrypted.bin 0x6000 --inputkey $IDF_PATH/examples/storage/nvs/nvs_bootloader/main/encryption_keys.bin
150150
```
151151

152152
2. NVS Encryption using the HMAC scheme
153153

154154
```
155-
python nvs_partition_gen.py encrypt $IDF_PATH/examples/storage/nvs_bootloader/nvs_data.csv $IDF_PATH/examples/storage/nvs_bootloader/main/nvs_encrypted_hmac.bin 0x6000 --keygen --key_protect_hmac --kp_hmac_inputkey $IDF_PATH/examples/storage/nvs_bootloader/main/nvs_enc_hmac_key.bin
155+
python nvs_partition_gen.py encrypt $IDF_PATH/examples/storage/nvs/nvs_bootloader/nvs_data.csv $IDF_PATH/examples/storage/nvs/nvs_bootloader/main/nvs_encrypted_hmac.bin 0x6000 --keygen --key_protect_hmac --kp_hmac_inputkey $IDF_PATH/examples/storage/nvs/nvs_bootloader/main/nvs_enc_hmac_key.bin
156156
```
157157

158158
Build the application using configurations corresponding to the NVS encryption scheme that you have selected:
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.16)
4+
5+
list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/examples/system/console/advanced/components")
6+
7+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
8+
project(nvs_console_example)

0 commit comments

Comments
 (0)