Storage & SD Card
The ESP32-P4-WIFI6-DEV-KIT provides two storage options beyond the main firmware flash: a high-speed microSD card slot and 16 MB of onboard NOR flash. This guide covers setting up both for file I/O in your applications.
Storage overview
Section titled “Storage overview”| Storage | Interface | Capacity | Speed | Use case |
|---|---|---|---|---|
| microSD card | SDIO 3.0 (4-bit) | Up to 128 GB+ | Up to ~50 MB/s | Large files, logs, media, OTA images |
| Onboard NOR flash | QSPI | 16 MB | ~40 MB/s read | Configuration, small assets, SPIFFS/LittleFS |
microSD card
Section titled “microSD card”Supported cards
Section titled “Supported cards”The SDIO 3.0 interface supports:
- SDHC: 4 GB — 32 GB (FAT32)
- SDXC: 64 GB — 2 TB (exFAT, with appropriate filesystem support)
- Speed class: Class 10 or UHS-I recommended for sustained write performance
Hardware setup
Section titled “Hardware setup”-
Power off the board.
-
Insert a microSD card into the TF card slot on the board. The slot uses a push-push mechanism — press to insert, press again to eject.
-
Power the board back on.
SDMMC setup (ESP-IDF)
Section titled “SDMMC setup (ESP-IDF)”-
Include the SDMMC and VFS headers in your application:
#include "esp_vfs_fat.h"#include "sdmmc_cmd.h"#include "driver/sdmmc_host.h" -
Mount the SD card using the SDMMC host driver:
#define MOUNT_POINT "/sdcard"esp_vfs_fat_sdmmc_mount_config_t mount_config = {.format_if_mount_failed = false,.max_files = 5,.allocation_unit_size = 16 * 1024,};sdmmc_card_t *card;sdmmc_host_t host = SDMMC_HOST_DEFAULT();host.max_freq_khz = SDMMC_FREQ_HIGHSPEED; // 40 MHzsdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();slot_config.width = 4; // 4-bit SDIO modeesp_err_t ret = esp_vfs_fat_sdmmc_mount(MOUNT_POINT, &host, &slot_config, &mount_config, &card);if (ret == ESP_OK) {sdmmc_card_print_info(stdout, card);} -
Once mounted, use standard C file I/O:
FILE *f = fopen("/sdcard/data.txt", "w");if (f) {fprintf(f, "Sensor reading: %d\n", sensor_value);fclose(f);}f = fopen("/sdcard/data.txt", "r");if (f) {char line[64];fgets(line, sizeof(line), f);printf("Read: %s", line);fclose(f);} -
Unmount when done (important before power-off to prevent corruption):
esp_vfs_fat_sdcard_unmount(MOUNT_POINT, card);
Onboard NOR flash (16 MB QSPI)
Section titled “Onboard NOR flash (16 MB QSPI)”The board includes 16 MB of NOR flash connected via QSPI. Part of this flash is used for firmware storage. The remaining space can be partitioned for application data.
Flash partition layout
Section titled “Flash partition layout”ESP-IDF uses a partition table to divide the flash into regions. A typical layout:
| Partition | Type | Size | Purpose |
|---|---|---|---|
nvs | data | 24 KB | Non-volatile storage (WiFi credentials, settings) |
phy_init | data | 4 KB | PHY calibration data |
factory | app | 4 MB | Main application firmware |
storage | data | ~10 MB | Application data (SPIFFS, LittleFS, or FATFS) |
ota_0 / ota_1 | app | variable | OTA update slots (if OTA is enabled) |
Using SPIFFS for flash storage
Section titled “Using SPIFFS for flash storage”SPIFFS (SPI Flash File System) is a lightweight filesystem designed for NOR flash:
#include "esp_spiffs.h"
esp_vfs_spiffs_conf_t conf = { .base_path = "/storage", .partition_label = "storage", .max_files = 5, .format_if_mount_failed = true,};
esp_err_t ret = esp_vfs_spiffs_register(&conf);if (ret == ESP_OK) { FILE *f = fopen("/storage/config.json", "w"); fprintf(f, "{\"brightness\": 80}"); fclose(f);}Using LittleFS (alternative)
Section titled “Using LittleFS (alternative)”LittleFS handles power-loss resilience better than SPIFFS and is recommended for applications where data integrity is critical:
idf.py add-dependency "joltwallet/littlefs"#include "esp_littlefs.h"
esp_vfs_littlefs_conf_t conf = { .base_path = "/storage", .partition_label = "storage", .format_if_mount_failed = true,};
esp_vfs_littlefs_register(&conf);Choosing between SD card and flash
Section titled “Choosing between SD card and flash”| Factor | microSD card | Onboard NOR flash |
|---|---|---|
| Capacity | 4 GB — 128+ GB | ~10 MB usable |
| Removable | Yes | No |
| Speed (write) | ~10—50 MB/s | ~1—3 MB/s |
| Wear leveling | Handled by card controller | Must use wear-aware FS (LittleFS) |
| Power-loss safety | Risk of corruption without unmount | LittleFS handles gracefully |
| Best for | Logs, media, large datasets | Configuration, small assets, OTA |
Troubleshooting
Section titled “Troubleshooting”| Symptom | Cause | Fix |
|---|---|---|
| SD card mount fails | Card not formatted as FAT32 | Reformat the card (use SD Card Formatter tool) |
| “No card detected” | Card not fully inserted | Reseat the card in the slot |
| Slow write speeds | Using 1-bit SDIO mode | Set slot_config.width = 4 for 4-bit mode |
| Flash partition not found | Partition label mismatch | Verify the label matches your partitions.csv |
| SPIFFS corruption after power loss | SPIFFS limitation | Migrate to LittleFS for better resilience |