LittleFS is not bundled with STM32CubeIDE in the way it is with the Arduino cores for Espressif chips, yet it is one of the most popular ways to add a filesystem to STM32 firmware. The reason is simple: the library asks for only four functions and a configuration structure, and STM32 parts expose everything those functions need through the HAL. This article walks through a port from start to finish, for both internal flash and an external SPI NOR chip, and explains the geometry choices that make or break it.
LittleFS behaviour described here comes from the upstream README, DESIGN.md and the configuration comments in lfs.h, checked on 23 November 2026. STM32 families differ in their flash organisation, so always confirm sector sizes and programming granularity in the reference manual for your exact part.
What a LittleFS port consists of
LittleFS talks to storage exclusively through a block device you provide. That block device is just four callbacks stored in struct lfs_config:
int (*read)(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size);
int (*prog)(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size);
int (*erase)(const struct lfs_config *c, lfs_block_t block);
int (*sync)(const struct lfs_config *c);
LittleFS guarantees that reads are multiples of read_size, programs are multiples of prog_size, and erases cover exactly one block of block_size bytes. It also promises never to program a region twice without erasing it first. Your job is to translate those requests into flash operations and return 0, or a negative error code if the hardware fails.
LittleFS does the rest: allocation, metadata, wear leveling and recovery. That clean division is why ports are usually a few dozen lines.
Choosing where the filesystem lives
LittleFS on STM32 typically uses one of two kinds of storage, and the choice shapes your configuration.
Internal flash
LittleFS can use spare pages or sectors of the microcontroller's own flash. This needs no extra hardware, but has drawbacks. Many STM32 families erase in large sectors on some parts (for example 16 KB to 128 KB on the STM32F4) and small pages on others (often 2 KB on low-power and mainstream families). Programming granularity also varies; some families program a half-word or word at a time, while others require a 64-bit double word or a 256-bit flash word. Erasing a bank can also stall code execution from the same bank.
LittleFS copes with large blocks, but every non-inlined file occupies at least one block, so 128 KB sectors waste a great deal of space. Internal flash suits a handful of configuration files on parts with small pages.
External SPI or QSPI NOR flash
LittleFS feels most at home on an external NOR chip, such as the common 25-series parts. These typically erase 4 KB sectors, program pages of up to 256 bytes and read at any byte offset. Because the chip is separate from program memory, erasing it never stalls your code. For data logging and larger datasets, this is the recommended option.
Mapping geometry to lfs_config
LittleFS geometry must follow the rules in lfs.h. The block size must be a multiple of the read and program sizes; the cache size must be a multiple of both and a factor of the block size.
| Field | Internal flash | External SPI NOR |
|---|---|---|
read_size |
Your programming unit, e.g. 8 | 1 or 16 |
prog_size |
Programming unit, e.g. 8 for double-word parts | 1 to 256 (page size or less) |
block_size |
Page or sector size, e.g. 2048 | 4096 (sector) |
block_count |
Reserved pages | Chip size ÷ 4096 |
cache_size |
64–256 | 256 |
lookahead_size |
≥ block_count ÷ 8 | ≥ block_count ÷ 8 |
block_cycles |
100–1000 | 500 |
LittleFS values in the table are starting points, not rules. The configuration calculator on our home page checks the divisibility requirements as you type.
An internal flash block device
LittleFS callbacks for internal flash map onto HAL_FLASH_Program() and HAL_FLASHEx_Erase(). The sketch below assumes a family with 2 KB pages and 64-bit programming, with the filesystem starting at a fixed address that your linker script keeps free.
#define LFS_BASE 0x08060000u /* reserved in the linker script */
#define PAGE_SIZE 2048u
static int bd_read(const struct lfs_config *c, lfs_block_t b,
lfs_off_t off, void *buf, lfs_size_t size) {
memcpy(buf, (const void *)(LFS_BASE + b * c->block_size + off), size);
return 0;
}
static int bd_prog(const struct lfs_config *c, lfs_block_t b,
lfs_off_t off, const void *buf, lfs_size_t size) {
uint32_t addr = LFS_BASE + b * c->block_size + off;
const uint8_t *p = buf;
HAL_FLASH_Unlock();
for (lfs_size_t i = 0; i < size; i += 8) {
uint64_t dw; memcpy(&dw, p + i, 8);
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr + i, dw) != HAL_OK) {
HAL_FLASH_Lock(); return LFS_ERR_IO;
}
}
HAL_FLASH_Lock();
return 0;
}
LittleFS erase and sync complete the set. Erase fills in a FLASH_EraseInitTypeDef for one page and calls HAL_FLASHEx_Erase(); sync can simply return 0, because internal flash writes are not cached. Remember to clear any pending flash error flags before programming, as a stale error bit makes the HAL refuse the next operation.
An external SPI NOR block device
LittleFS callbacks for a typical SPI NOR chip use three standard commands: read data, page program and sector erase, with a write-enable before every program or erase and a status poll afterwards.
static int bd_prog(const struct lfs_config *c, lfs_block_t b,
lfs_off_t off, const void *buf, lfs_size_t size) {
uint32_t addr = b * c->block_size + off;
/* split into page-sized chunks so no write crosses a 256-byte page */
while (size) {
lfs_size_t n = 256 - (addr % 256);
if (n > size) n = size;
nor_write_enable();
nor_page_program(addr, buf, n); /* uses HAL_SPI_Transmit */
if (nor_wait_busy() != 0) return LFS_ERR_IO;
addr += n; buf = (const uint8_t *)buf + n; size -= n;
}
return 0;
}
LittleFS will happily issue programs of prog_size or larger, so splitting at page boundaries in the driver is essential; a program that crosses a page boundary wraps around inside most NOR chips and corrupts data.
Concurrency and memory
LittleFS is not thread-safe by default. If several RTOS tasks share a filesystem, build with LFS_THREADSAFE and supply lock and unlock callbacks backed by a mutex, or route all file access through one task. For deterministic memory, define LFS_NO_MALLOC and pass static read_buffer, prog_buffer and lookahead_buffer arrays, each sized exactly as the configuration specifies.
Testing the port properly
LittleFS includes an emulated block device and a large test suite that runs on a Linux host, which is useful for checking configuration ideas. On the target, run the classic boot-count test from the README, then pull the power repeatedly while it writes. A correct port never loses the filesystem, only at most the write that was in flight.
LittleFS also expects erased flash to read back consistently. If your driver returns stale cached data after a program, mounts can fail with LFS_ERR_CORRUPT; the README warns that sync must flush any caching so the next read sees the real memory contents.
Troubleshooting an STM32 port
| Symptom | Likely cause | Fix |
|---|---|---|
LFS_ERR_INVAL at mount |
Geometry breaks the lfs.h rules | Make block_size a multiple of read/prog sizes |
| Corruption after a few writes | Programs crossing NOR page boundaries | Split writes at 256-byte pages |
| HardFault during erase | Erasing the bank you execute from | Use another bank or an external chip |
| HAL program returns error | Stale flash error flags | Clear flags before programming |
| Mount fails after flashing firmware | Linker placed code in the LittleFS region | Reserve the region in the linker script |
Planning memory on small parts
LittleFS memory needs on an STM32 are easy to predict. Each mounted filesystem needs a read cache and a program cache of cache_size bytes, a lookahead buffer of lookahead_size bytes, and one further cache for every file open at the same time, plus the lfs_t and lfs_file_t structures themselves. On a part with 20 KB of RAM, a 256-byte cache, a 32-byte lookahead and two open files cost roughly a kilobyte of buffers, which is usually acceptable. If RAM is very tight, shrink the cache first, accepting more flash accesses in exchange.
Bringing the port to production
LittleFS ports that pass bench tests still deserve a production checklist. Confirm the linker script reserves the filesystem region in every build configuration, including debug builds that may be larger. Decide whether a failed mount should format automatically or report an error, since formatting silently after a hardware fault destroys evidence. Add a factory test that mounts, writes and reads a file, and log the LittleFS version compiled into each firmware release so that field reports can be matched to the code.
Verifying the reserved flash region
Before integrating the filesystem, inspect the memory map used by the exact STM32 device and linker configuration. Reserve whole erase units for storage and ensure that firmware sections, bootloader data and update slots cannot overlap that region. Record its start address and size in one shared configuration so the linker, programmer and block driver do not silently use different boundaries.
Test the read, program and erase callbacks independently before mounting LittleFS. Confirm alignment, check every hardware return status and verify that an erase affects only the intended unit. On devices where executing code and programming flash interact, review the board's execution and timing constraints for the selected memory bank. The correct approach depends on the particular device and its flash controller.
Use a dedicated test board to fill the filesystem, delete files and repeat writes across several blocks. Include a restart after a completed update and controlled interruptions during writes. Check that the application recognises a mount or I/O error and does not automatically erase valuable data. Keep a known backup before experimenting with a new flash layout or firmware update process.
Frequently Asked Questions
Does STMicroelectronics provide an official LittleFS driver?
LittleFS is not part of the standard STM32Cube packages, so most projects write the small block device shown above. RTOS environments such as Zephyr and Mbed OS include ready-made integrations for many STM32 boards.
Should I use internal flash or an external chip?
LittleFS works with both, but external SPI NOR is usually better for data, thanks to small 4 KB erase blocks and no execution stalls. Internal flash suits small configuration data on parts with small pages.
What block_size should I use?
LittleFS block_size should normally equal the erase unit: the sector or page size of your flash. It may be larger than the physical erase size, but never smaller.
How do I make LittleFS thread-safe on FreeRTOS?
LittleFS gains thread safety when compiled with LFS_THREADSAFE and given lock and unlock callbacks, typically wrapping a FreeRTOS mutex.
Can I use LittleFS on an SD card with STM32?
LittleFS can run on an SD card through a block device that reads and writes 512-byte sectors, though FAT may be preferable if a PC must read the card directly.
How much RAM does LittleFS need on STM32?
LittleFS needs two caches, a lookahead buffer and one cache per open file, plus its state structures; with small caches this is around one kilobyte.
Conclusion
LittleFS on STM32 comes down to four honest callbacks and geometry that respects the rules in lfs.h. Choose external NOR flash for anything beyond a few settings, split programs at page boundaries, reserve the flash region in your linker script, and test with real power cuts. Once those foundations are right, the filesystem's own guarantees take over. If you later move to an RTOS, our LittleFS Zephyr article shows how the same configuration appears in devicetree.