Keep data

SPIFFS small filesystem

Mount SPIFFS on internal flash for static web pages, small configs, and certs; keep critical config in NVS.

Keep data Flash filesspiffsvfspartition ESP32 family

Official path: examples/storage/spiffs

01 Overview

SPIFFS is the built-in flash filesystem in ESP-IDF, designed for small files and low-capacity scenarios. It formats a partition of internal flash and exposes it through the standard VFS interface (fopen/fwrite/readdir).

It solves the problem of managing static resources (web pages, icons, certificates, small logs) as files, instead of stuffing data into NVS or compiling it into firmware. SPIFFS allows runtime read/write and supports pre-seeding files at manufacturing time.

Typical uses: local web server pages and CSS/JS, TLS client certificates, JSON backups of device config, low-frequency sensor logs.

It is not a general-purpose hard disk: capacity is small (typically hundreds of KB to a few MB), write endurance is limited, and power loss can corrupt files. For high-rate logs, use an SD card or external storage.

Division of labor with NVS: NVS is for small key-value pairs (Wi-Fi config, calibration values), SPIFFS is for file-type data. Keep critical config in NVS to avoid device lockout due to file corruption.

This example demonstrates how to mount SPIFFS, read/write files, and pre-seed resources in a factory image.

02 Hardware

  • Flash partition: SPIFFS uses a partition of internal flash; define it in the partition table (partitions.csv), e.g., `spiffs, data, spiffs, , 0x100000`. Partition size determines available space.
  • No extra hardware: No external storage chip; all operations happen on the ESP32's internal flash.
  • Flash endurance: Internal flash has limited erase cycles (~100k), SPIFFS has wear leveling, but frequent writes still shorten lifespan.
  • Power-loss protection: Hardware cannot guarantee atomic writes during power loss; software measures are needed (e.g., write temp file then rename).
  • Wiring: No external wiring; just ensure stable power to avoid drops during writes.
  • Layout note: Partition table must be configured at build time; ensure spiffs partition does not overlap others.

03 Software flow

1. Configure partition table: Add a spiffs partition in `partitions.csv` with appropriate size (e.g., 1MB).

2. Initialize SPIFFS: Call `esp_vfs_spiffs_register` with partition label, max files, etc.

3. Check mount: Use `esp_spiffs_check` to verify filesystem consistency; format if necessary.

4. Read/write files: Use standard C functions `fopen`/`fwrite`/`fread` or POSIX APIs.

5. Pre-seed assets: Use `spiffs_create_partition_image` in CMake to pack files from `spiffs_image` directory into firmware; mounted on first boot.

6. Error handling: Check return values of mount and file operations; handle insufficient space, corruption, etc.

7. Power-loss safety: For important files, write to a temp file then `rename` to reduce corruption risk.

8. Log rotation: For log files, implement size limits and rotation to avoid filling the partition.

04 Core points

  • Power-loss corruption: Power loss during write can corrupt the filesystem; do not store critical config only in SPIFFS, use NVS.
  • Capacity limits: SPIFFS is not for large files or many small files; plan partition size based on actual assets.
  • Wear leveling: Although SPIFFS has wear leveling, frequent writes still wear flash; avoid high-rate logging.
  • Difference from NVS: Do not use SPIFFS for small key-value pairs; NVS is more efficient and reliable.
  • Customizable: Partition size, pre-seeded assets, and filesystem parameters (e.g., logical block size) can be adjusted.
  • Cross-chip caution: Different ESP32 models have different flash sizes; partition tables must be adapted, not copied directly.

中文

掉电还在

SPIFFS 小文件系统

在内部 Flash 上挂载 SPIFFS,用于静态网页、小配置和证书文件;关键配置仍用 NVS。

掉电还在 Flash 文件spiffsvfspartition ESP32 family

官方路径: examples/storage/spiffs

01 项目概述

SPIFFS 是 ESP-IDF 内置的 Flash 文件系统,面向小文件、低容量场景。它把内部 Flash 的一块分区格式化为可挂载的文件系统,通过标准 VFS 接口(fopen/fwrite/readdir)访问。

它解决的问题:需要以文件形式管理静态资源(网页、图标、证书、小日志),而不是把数据塞进 NVS 或编译进固件。SPIFFS 允许运行时读写,也支持量产时预置文件。

典型用途:本地 Web 服务器的页面和 CSS/JS、TLS 客户端证书、设备配置的 JSON 备份、低频传感器日志。

它不是通用硬盘:容量小(通常几百 KB 到几 MB)、写寿命有限、掉电可能损坏文件。高频率大日志请用 SD 卡或外部存储。

与 NVS 的分工:NVS 适合小键值对(Wi-Fi 配置、校准值),SPIFFS 适合文件型数据。关键配置优先 NVS,避免文件损坏导致设备失联。

本示例演示如何挂载 SPIFFS、读写文件,以及如何在量产镜像中预置资源。

02 项目硬件描述

  • Flash 分区: SPIFFS 使用内部 Flash 的一个分区,需在分区表(partitions.csv)中定义,例如 `spiffs, data, spiffs, , 0x100000`。分区大小决定可用空间。
  • 无额外硬件: 不需要外部存储芯片,所有操作在 ESP32 内部 Flash 上完成。
  • Flash 寿命: 内部 Flash 有擦写次数限制(约 10 万次),SPIFFS 有磨损均衡,但频繁写入仍会缩短寿命。
  • 掉电保护: 硬件上无法保证掉电时写操作原子完成,需要软件层面处理(如写临时文件再改名)。
  • 接线: 无外部接线,仅需保证电源稳定,避免写入时掉电。
  • 布局注意: 分区表需在编译前配置,确保 spiffs 分区不与其他分区重叠。

03 项目软件流程描述

1. 配置分区表: 在 `partitions.csv` 中添加 spiffs 分区,并设置合适的大小(例如 1MB)。

2. 初始化 SPIFFS: 调用 `esp_vfs_spiffs_register`,指定分区标签、最大文件数等参数。

3. 挂载检查: 使用 `esp_spiffs_check` 检查文件系统一致性,必要时格式化。

4. 读写文件: 通过标准 C 库函数 `fopen`/`fwrite`/`fread` 操作文件,或使用 POSIX API。

5. 预置资源: 在构建时使用 `spiffs_create_partition_image` 将 `spiffs_image` 目录下的文件打包进固件,首次启动自动挂载。

6. 错误处理: 检查挂载和文件操作返回值,处理空间不足、文件损坏等情况。

7. 掉电安全: 写重要文件时先写临时文件,再 `rename` 覆盖,减少损坏风险。

8. 日志轮转: 对于日志文件,实现大小限制和轮转,避免写满分区。

04 项目核心点

  • 掉电损坏: 写入过程中断电可能损坏文件系统,关键配置不要只存在 SPIFFS,应放 NVS。
  • 容量限制: SPIFFS 不适合大文件或大量小文件,分区大小需根据实际资源规划。
  • 磨损均衡: 虽然 SPIFFS 有磨损均衡,但频繁写入仍会损耗 Flash,避免高频率日志。
  • 与 NVS 区别: 不要用 SPIFFS 存小键值,NVS 更高效且更可靠。
  • 可定制: 分区大小、预置资源内容、文件系统参数(如逻辑块大小)均可调整。
  • 跨芯片注意: 不同 ESP32 型号 Flash 大小不同,分区表需适配,不能直接复制。