On this page

Storage Technologies

Where and how to store data in MCU projects


Storage Pyramid

      Capacity     Speed       Price/GB
SRAM   KB~MB      Fastest (ns) Most expensive  ← CPU Cache/Registers
DRAM   MB~GB      Fast (10ns)  Medium          ← Memory
NOR    MB~GB      Medium (μs)  Medium-High     ← Code Storage
NAND   GB~TB      Slow (ms)    Low             ← Data Storage

Embedded systems care about non-volatile storage (data retention after power loss):
  - Where to store code → NOR Flash (XIP - Execute In Place)
  - Where to store data → EEPROM / NOR / NAND / SD

Built-in vs. External

MCU Built-in Flash

STM32F407: 1MB built-in NOR Flash
ESP32:     4~16MB external SPI NOR Flash
RP2040:    No built-in, all external QSPI NOR Flash
nRF52840:  1MB built-in

Code is stored in Flash and runs directly upon power-up (XIP).
Erase/Write endurance: Typically 10k~100k cycles.

Built-in EEPROM (Emulated)

Most MCUs do not have true EEPROM; they emulate it using Flash:
  STM32: Allocates a block at the end of Flash, using wear leveling algorithms.
  ESP32: NVS (Non-Volatile Storage), based on Flash.

The lifespan of emulated EEPROM is limited by the Flash page erase count.
Practical usage: Writing 10 times per day with 100k endurance = 27 years.

Flash Type Comparison

FeatureNOR FlashNAND Flash
ReadRandom access (XIP capable)Page read
WriteSlow (requires erase first)Slow (requires erase first)
EraseBlock erase (~100ms)Block erase (~ms)
CapacityMB~1GBGB~TB
InterfaceSPI/QSPI/OSPIParallel/ONFI
Endurance10k~100k1k~10k (requires wear leveling!)
Bad BlocksRarePresent at factory, requires management
PriceExpensive ($/MB)Cheap ($/GB)
Embedded MCUs use NOR Flash 99% of the time (via SPI/QSPI).
NAND is used in eMMC/SD cards and internal SSDs.

EEPROM vs. FRAM

FeatureEEPROMFRAM
Write SpeedSlow (5ms/page)Extremely fast (ns level)
Erase before write?No (byte-addressable)No
Endurance100k~1M cycles10¹²~10¹⁴ cycles!
Power ConsumptionLowLower
Capacity1Kb~1Mb4Kb~4Mb
InterfaceI2C/SPII2C/SPI
PriceCheap2-3x more expensive
EEPROM: Store configuration/calibration data (infrequent writes).
FRAM:  Frequent recording (data logging, power-fail protection).
        Virtually unlimited lifespan → no worry about wear out.

Typical Chips:
  EEPROM: 24LC256 (256Kb, I2C), AT25SF041 (4Mb, SPI)
  FRAM:   MB85RC256V (256Kb, I2C), FM25V02A (2Mb, SPI)

SD Cards

Two Modes

SPI Mode:
  4 wires (CS/CLK/MOSI/MISO)
  Slow (25~50 Mbps)
  Compatible with all MCUs
  Library: FatFs + SPI driver

SDIO Mode:
  4-bit parallel
  Fast (up to 100Mbps+)
  Requires hardware SDIO controller (STM32F4+ or ESP32 support)

Regardless of the mode, a file system is required → FatFs (most universal).

Wiring

SD Card      MCU (SPI Mode)
──────────────────────────
CS    ←→  GPIO (Chip Select)
MOSI  ←→  MOSI
MISO  ←→  MISO
SCK   ←→  SCK
VDD   ←→  3.3V
GND   ←→  GND

Must add:
  Pull-up resistors (10k~100k) on all signal lines to 3.3V.
  Bypass capacitors (0.1μF + 10μF) on VDD.

Note: SD cards are 3.3V! 5V will burn them.

File Systems

Using SD cards with bare MCUs:

FatFs:  Most universal, supports FAT32/exFAT.
        Small code footprint, suitable for MCUs.
        http://elm-chan.org/fsw/ff/

LittleFS: File system designed for Flash.
          Power-fail safe, wear leveling.
          Suitable for internal/external NOR Flash on MCUs.

SPIFFS: Old file system for ESP32 (replaced by LittleFS).

Selection Guide:
  SD Card → FatFs (PC compatible)
  Internal Flash → LittleFS (power-fail safe)

Quick Reference: Common Storage Solutions

Scenario                          Recommendation
─────────────────────────────────────────────
Store a few config values (WiFi password, etc.)  MCU Internal Flash / Emulated EEPROM
Store hundreds of bytes of config      I2C EEPROM (24LCxx)
Frequent data recording (multiple times/sec)  FRAM or SPI NOR + LittleFS
Code exceeds internal Flash capacity   External QSPI NOR Flash (e.g., W25Q64)
Store logs/data, need export to PC     SD Card + FatFs
Product-grade data storage             eMMC (large capacity, reliable, but BGA package)
Non-volatile data cache                FRAM (virtually unlimited lifespan)

Practical Pitfalls

1. Flash must be erased before writing.
   → To write 1 byte, you must erase the entire Page/Block first.
   → Updating one config value = Read whole page → Modify → Erase → Write back.

2. NAND bad block management.
   → Bad blocks exist at the factory!
   → Requires ECC (Error Correction Code).
   → Running NAND directly on an MCU is painful; use eMMC (with built-in controller).

3. Flash lifespan.
   → Writing once every 10 seconds with 100k endurance → Only lasts 11 days!
   → Solution: Wear Leveling.
   → Use LittleFS or SPIFFS; do not write directly to Flash.

4. SD card compatibility.
   → Not all cards support SPI mode!
   → Some cards require extra dummy clocks after power-up.
   → Initialization differs for high-capacity cards (SDHC/SDXC).

5. Writing Flash during power loss.
   → Power loss mid-write → Data corruption.
   → Use dual-bank backup: Write new data to Bank B, mark as valid after verification.
   → Or add sufficient capacitors to sustain power until write completes (but this is unreliable).

Keywords: NOR Flash, NAND Flash, EEPROM, FRAM, SD Card, FatFs, LittleFS, Wear Leveling, QSPI