Note
The version corresponding to the current document is ESP-IDF v5.5.0
NVS rw value example
Note
This document is automatically translated using AI. Please excuse any detailed errors. The official English version is still in progress.
Example Description
This example demonstrates how to initialize and configure NVS on ESP32, perform read and write operations on a single integer value or string value, and check whether the operation is successful. Through this example, developers can learn the initialization process of NVS, basic read and write methods, and result verification methods.
Running Method
The complete code of the example can be found at nvs_rw_value example. Detailed project build and burn steps before running can be found in the README.md file in the example directory.
Header File Description
The header files used in this example cover standard libraries, FreeRTOS, system control and logging, and NVS storage and other functional modules, building the core functions of task scheduling, system operation management, debug information output, and non-volatile data read and write, providing complete support for application operation and persistent storage.
The header files are classified by function as follows:
Standard library functions: Provide basic input and output functions and integer type support, used for printing debug information and ensuring integer precision across platforms.
#include <stdio.h>
#include <inttypes.h>
System and logging: Provide system-level API and log output functions, used for chip information acquisition, system control, and runtime debugging.
#include "esp_system.h"
#include "esp_log.h"
FreeRTOS: Provide the core definitions and task management interfaces of the real-time operating system, used for multi-task creation, scheduling, and control.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Non-volatile storage: Provide initialization and access interfaces for non-volatile storage, used for storing and reading persistent data.
#include "nvs_flash.h"
#include "nvs.h"
Structure/Global Constant Description
Structure type definition: A structure type_str_pair_t
is defined to establish a mapping relationship between NVS data types and string representations, including two members:
type
is used to store the NVS data type enumeration value.
str
is used to store the string description of the corresponding type.
Global constants: Used to simplify the processing logic of type and string conversion, and ensure the safety and readability during traversal.
Define a read-only static array
type_str_pair[]
, the content is various NVS data types and their corresponding strings. Commonly used for printing, debugging, or selecting different processing logic based on type.Define
TYPE_STR_PAIR_SIZE
, used to store the number of elements in thetype_str_pair[]
array, to avoid manual calculation of length, commonly used for traversing arrays.
Task Function Description
This example defines a utility function type_to_str()
, which is used to convert NVS data type enumeration values (nvs_type_t
) into corresponding string representations, so that data types can be displayed more intuitively when printing logs or debugging.
When calling this function, you need to pass in the enumeration value to be converted, and its execution logic is:
Traverse the global constant array
type_str_pair[]
.Compare the current element’s
type
with the passed-in parameter.If a match is found, return the corresponding string
str
of that element.If no match is found after traversing, return
"Unknown"
, indicating that the type is undefined or not in the mapping table.
Main Function Description
This function serves as the program entry point, demonstrating the complete process of initialization, reading, writing, querying, and deleting operations using NVS (Non-Volatile Storage) on ESP32. Through this example, developers can learn about NVS initialization, data operations, and error handling methods.
The following are the main implementation steps of the function. Considering that most of the process has been detailed in General Steps, you can refer to the corresponding titles in the NVS General Steps section of the document. For repetitive content, it will not be repeated here, only the key implementation and differences in this example will be highlighted.
NVS Initialization
Open NVS Handle
The namespace is
"storage"
.
Integer Read and Write
After reading the integer value, determine the reading status through the return value and print the corresponding log.
String Read and Write
After reading the string value, determine the reading status through the return value and print the corresponding log.
Query NVS Key Value
After obtaining the key-value pair information, call the utility function
type_to_str()
to convert the type of the storage item into a readable string, and print the log together with the key name, which is convenient for debugging and viewing the key-value information in NVS.
Delete NVS Key Value
Commit Changes