Follow+Star Public Account, don’t miss out on exciting content

Author | strongerHuang
WeChat Public Account | strongerHuang
For many embedded electronic devices, having a small and “flexible” file system is crucial.
There are not many file systems that run on MCUs, and most people should know the FatFS file system. Today, I will discuss FatFS and LittleFS, as well as some differences between them.
File System FatFS
FatFs is a generic file system (FAT/exFAT) module used to implement the FAT file system in small embedded systems.
Website:
http://elm-chan.org/fsw/ff/00index_e.html
The FatFs component is written in ANSI C (C89), completely separated from the disk I/O layer, thus it does not depend on the hardware platform. It can be embedded into resource-limited microcontrollers such as 8051, PIC, AVR, ARM, Z80, RX, etc., without any modifications.
— From Baidu Encyclopedia
Features
a. DOS/Windows compatible FAT/exFAT file system.
b. Platform-independent and easy to port.
c. Very small footprint for program code and workspace.
d. Supports various configuration options:
- Long file names in ANSI/OEM or Unicode.
- exFAT file system, 64-bit LBA, and GPT can store large amounts of data.
- Thread safety for RTOS.
- Multiple volumes (physical drives and partitions).
- Variable sector sizes.
- Multiple code pages, including DBCS.
- Read-only, optional API, I/O buffers, etc…


If you know how to use STM32CubeMX, using FatFS is very easy, and you can turn STM32 into a USB flash drive in just a few steps.
File System LittleFS
Fewer people know about the LittleFS file system, but most people who have used the Mbed OS system should be aware of it.
Mbed OS is a free, open-source embedded operating system developed by Arm for Cortex-M series processors, specifically designed for IoT development.

LittleFS is just part of Mbed, as shown in the block diagram below:

Source code address:
https://github.com/armmbed/mbed-littlefs
LittleFS Features:
-
Low resource usage: IoT devices are limited by ROM and RAM.
-
Power failure recovery capability: The file system must remain consistent and refresh data to the underlying storage.
-
Average wear leveling: Generally, storage supports a limited number of erasures per block, making it crucial to use the entire storage device for reliability.

The usage is also quite simple; refer to the official examples:
#include "LittleFileSystem2.h"
#include "SPIFBlockDevice.h"
// Physical block device, can be any device that supports the BlockDevice API
SPIFBlockDevice bd(PTE2, PTE4, PTE1, PTE5);
// Storage for the littlefs
LittleFileSystem2 fs("fs");
// Entry point
int main() {
// Mount the filesystem
int err = fs.mount(&bd);
if (err) {
// Reformat if we can't mount the filesystem,
// this should only happen on the first boot
LittleFileSystem2::format(&bd);
fs.mount(&bd);
}
// Read the boot count
uint32_t boot_count = 0;
FILE *f = fopen("/fs/boot_count", "r+");
if (!f) {
// Create the file if it doesn't exist
f = fopen("/fs/boot_count", "w+");
}
fread(&boot_count, sizeof(boot_count), 1, f);
// Update the boot count
boot_count += 1;
rewind(f);
fwrite(&boot_count, sizeof(boot_count), 1, f);
// Remember that storage may not be updated until the file
// is closed successfully
fclose(f);
// Release any resources we were using
fs.unmount();
// Print the boot count
printf("boot_count: %ld\n", boot_count);
}
Comparison and Differences Between the Two
Every product has its value, and file systems are no exception, each with its own advantages and disadvantages. Below are some of the differences between them.
1. Resource RAM / ROM Size
LittleFS is a high-integrity embedded file system in Mbed OS, optimized for use with RAM and ROM-limited MCUs.

LittleFS uses 13K ROM and less than 4K RAM, significantly less than FAT.
2. Power Failure Recovery Capability
LittleFS has a robust copy-on-write guarantee, ensuring that storage on disk is always kept in a valid state, making it suitable for systems that may experience random power failures.
3. Wear Leveling
Most storage chips used in embedded devices support a limited number of erasures per sector, and without leveling, the lifespan of embedded devices may be affected.
———— END ————Follow the public account and reply “Add Group” to join the technical exchange group according to the rules, reply “1024” to see more content.


Click “Read Original” to see more shares.