Keep data

SD card storage

Field logs for the customer. Design the slot for hot-plug.

Keep data SD card ESP32 family

Official path: examples/storage/sd_card

01 Overview

SD cards are the go-to removable storage for field devices: non-volatile, high capacity, and readable by any PC. This example shows how to read and write SD cards from an ESP32 using SDMMC or SPI mode, covering the full path from hardware wiring to software mounting.

Use it for long-term logging of runtime data or sensor readings, or for exporting configuration and firmware backups to a customer. The FAT filesystem is natively recognized by Windows/macOS, so no special tools are needed.

SDMMC mode uses a dedicated peripheral with a 4-bit parallel data bus, offering high throughput for large data volumes. SPI mode reuses the common SPI bus with fewer pins, but is slower—suitable for small data or pin-constrained designs.

If you only need to store a few configuration bytes, skip the SD card—use the NVS partition instead. It's simpler, more reliable, and saves power. SD cards are for 'big files' and 'removable' scenarios.

Hot-plugging is a hidden pitfall in SD design: mechanical contacts, power sequencing, and filesystem state all need attention. This example highlights these risks and gives engineering recommendations.

02 Hardware

  • Slot: Standard microSD slot, with optional card-detect (CD) pin. Ensure mechanical strength to avoid contact issues from vibration.
  • Power: 3.3V supply, capable of at least 500mA (card peak current). For hot-plug, the rail must be well-behaved; add TVS and decoupling caps.
  • Data lines: SDMMC uses 6 lines (CLK, CMD, D0-D3); SPI uses 4 (CS, CLK, MOSI, MISO). Pull-ups as per the chip datasheet.
  • Level matching: ESP32 IOs are 3.3V, and the card is 3.3V, so direct connection is fine. Mind the pin order on the slot.
  • Card detect: If using CD, pull it up and use it to detect insertion/removal for software prompts.
  • Layout: Keep data lines short and equal length to avoid signal distortion. Place the slot near the board edge for easy access.
  • Protection: Add ESD protection on power and data lines; field environments are static-prone.

03 Software flow

1. Configure the SDMMC or SPI peripheral and initialize the driver. Use sdmmc_host_t for SDMMC, spi_bus_config_t for SPI.

2. Mount the FAT filesystem: call esp_vfs_fat_sdmmc_mount or esp_vfs_fat_spiflash_mount (note the difference). Mount point like "/sdcard".

3. Write files: use standard C library fopen/fwrite, but remember to fflush and fsync to prevent data loss on power failure.

4. Read files: fopen/fread, for export or reading configuration.

5. Before removal: ensure all files are closed, call fflush and fsync, then unmount. The product must warn the user when a write is in flight.

6. Error handling: handle card missing, mount failure, write errors with clear messages, and retry or degrade gracefully.

7. Hot-plug detection: if using CD, poll or interrupt to detect card removal and immediately stop writes and unmount.

04 Core points

  • Card quality varies; fixture-test lots. Compatibility differs across brands/batches; validate with a test fixture before mass production.
  • Hot-plug is critical: Power must be well-behaved, and software must handle sudden removal to avoid filesystem corruption.
  • SDMMC and SPI are not interchangeable: Pins, drivers, and speeds differ; decide early.
  • FAT has a 4GB per-file limit; for long logs, split files or use circular overwriting.
  • Custom work: log format, slot layout, export flow.
  • Don't copy SD drivers from other chips: ESP32's SDMMC peripheral and SPI implementation differ; refer to the official example.

中文

掉电还在

SD 卡存储

现场日志、导出给客户。卡槽和电源要按热插拔想。

掉电还在 SD 卡 ESP32 family

官方路径: examples/storage/sd_card

01 项目概述

SD 卡是现场设备最常用的可移动存储:断电不丢、容量大、电脑直接读。本示例演示 ESP32 通过 SDMMC 或 SPI 模式读写 SD 卡,覆盖从硬件连接到软件挂载的完整流程。

适合长时间记录运行日志、传感器数据,或把配置、固件备份导出给客户。文件系统用 FAT,Windows/macOS 直接识别,省去专用工具。

SDMMC 模式走专用外设,4 线数据并行,速度快,适合大量数据;SPI 模式复用通用 SPI 总线,引脚少,但速度慢,适合小数据量或引脚紧张的设计。

如果只是存几个配置字节,别用 SD 卡——NVS 分区更简单可靠,还省电。SD 卡是给“大文件”和“可移动”场景的。

热插拔是 SD 卡设计的隐形坑:卡槽机械触点、电源上电顺序、文件系统状态都要考虑。本示例会指出这些风险,并给出工程建议。

02 项目硬件描述

  • 卡槽: 标准 microSD 卡槽,带卡检测(CD)引脚可选。注意卡槽机械强度,固定好,避免振动导致接触不良。
  • 电源: 3.3V 供电,电流能力至少 500mA(卡峰值电流)。热插拔时电源要防抖动,建议加 TVS 和去耦电容。
  • 数据线: SDMMC 模式用 6 线(CLK、CMD、D0-D3),SPI 模式用 4 线(CS、CLK、MOSI、MISO)。上拉电阻按芯片手册要求。
  • 电平匹配: ESP32 是 3.3V IO,卡也是 3.3V,直接连。但注意卡槽引脚顺序,别接反。
  • 卡检测: 如果使用 CD 引脚,接上拉,检测卡插入/拔出,软件可据此提示。
  • 布局: 数据线等长、短走线,避免高速信号失真。卡槽靠近板边,方便插拔。
  • 保护: 建议在电源和数据线上加 ESD 保护,现场环境静电多。

03 项目软件流程描述

1. 配置 SDMMC 或 SPI 外设,初始化驱动。SDMMC 用 sdmmc_host_t,SPI 用 spi_bus_config_t。

2. 挂载 FAT 文件系统:调用 esp_vfs_fat_sdmmc_mount 或 esp_vfs_fat_spiflash_mount(注意区分)。挂载点如 "/sdcard"。

3. 写文件:用标准 C 库 fopen/fwrite,注意及时 fflush 和 fsync,防止掉电丢数据。

4. 读文件:fopen/fread,用于导出或读取配置。

5. 卸载前:确保所有文件关闭,调用 fflush 和 fsync,然后 unmount。产品要提示用户“正在写,勿拔卡”。

6. 错误处理:卡未插、挂载失败、写入错误都要有提示,并重试或降级。

7. 热插拔检测:如果用了 CD 引脚,轮询或中断检测卡拔出,立即停止写入并卸载。

04 项目核心点

  • 卡质量参差,工装要抽测。 不同品牌/批次兼容性差,批量前用测试工装验证。
  • 热插拔是重点: 电源要防抖动,软件要处理卡突然拔出,否则文件系统损坏。
  • SDMMC 和 SPI 不能混用: 引脚、驱动、速度都不同,选型时定好。
  • FAT 文件系统有单文件 4GB 限制, 长时间日志要分文件或循环覆盖。
  • 可定制: 日志格式、卡槽布局、导出流程。
  • 不要照搬其他芯片的 SD 驱动: ESP32 的 SDMMC 外设和 SPI 实现有差异,参考官方示例。