Note
The version corresponding to the current document is ESP-IDF v5.5.0
Spiffs 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 results are 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 spiffs 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, debugging 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 common input and output, string and memory operation basic functions, used for file I/O and data processing.
#include <stdio.h>
#include <string.h>
Error Handling and Logging: Used for error code definition and runtime log output, convenient for debugging and problem positioning.
#include "esp_err.h"
#include "esp_log.h"
POSIX/System Calls: Provide support for Unix/Linux-like files and system calls, such as file reading and writing, file status query, directory operations, etc.
#include <sys/unistd.h>
#include <sys/stat.h>
Spiffs File System: Provide the interface of Spiffs file system, used to mount and manage file system on ESP32’s Flash.
#include "esp_spiffs.h"
Main Function Description
This function serves as the program entry point, demonstrating the complete process of initialization, reading and writing, renaming and deleting operations using Spiffs on ESP32. Through this example, developers can learn the initialization and file operation methods of Spiffs.
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 title in the Spiffs General Steps section of the document. For repeated content, this place will not be repeated, only the key implementation and differences in this example are highlighted.
Mounting and Initialization
The example configuration mounts Spiffs to the
/spiffs
path, uses the default partition, opens up to 5 files at the same time, and automatically formats the partition when mounting fails.After completing initialization and mounting, judge the mounting status through the return value and print the corresponding log.
Verification and Partition Information Check
The namespace is
"storage"
.
Create a file and write to it
Check the file and rename it
Read the file
Process and print the read content
Unmount the file system