This example demonstrates how to use the Mbed OS file system.
You can find more information about the Mbed OS file system and other related pieces of the Mbed OS storage stack in the storage overview.
This example uses a block device as storage. This can be one of:
This example uses an instance of the LittleFileSystem API (LittleFS) on external SPI flash. The section changing the filesystem describes how to switch between the LittleFileSystem and the FatFileSystem.
Starting with version 6.5, Mbed OS uses Mbed CLI 2. It uses Ninja as a build system, and CMake to generate the build environment and manage the build process in a compiler-independent manner. If you are working with Mbed OS version prior to 6.5 then check the section Mbed CLI 1.
mbed-tools import mbed-os-example-filesystem
mbed import mbed-os-example-filesystem
Run the following command to build the example project, program the microcontroller flash memory and open a serial monitor:
$ mbed-tools compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm
$ mbed compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm
Your PC may take a few minutes to compile your code.
--- Mbed OS filesystem example ---
Mounting the filesystem... Fail :(
No filesystem found, formatting... OK
Opening "/fs/numbers.txt"... Fail :(
No file found, creating a new file... OK
Writing numbers (10/10)... OK
Seeking file... OK
Incrementing numbers (10/10)... OK
Closing "/fs/numbers.txt"... OK
Opening the root directory... OK
root directory:
.
..
numbers.txt
Closing the root directory... OK
Opening "/fs/numbers.txt"...OK
numbers:
1
2
3
4
5
6
7
8
9
10
Closing "/fs/numbers.txt"... OK
Unmounting... OK
Mbed OS filesystem example done!
You can also reset the board to see the data persist across boots. Each boot increments the numbers stored on disk:
--- Mbed OS filesystem example ---
Mounting the filesystem... OK
Opening "/fs/numbers.txt"... OK
Incrementing numbers (10/10)... OK
Closing "/fs/numbers.txt"... OK
Opening the root directory... OK
root directory:
.
..
numbers.txt
Closing the root directory... OK
Opening "/fs/numbers.txt"...OK
numbers:
2
3
4
5
6
7
8
9
10
11
Closing "/fs/numbers.txt"... OK
Unmounting... OK
Mbed OS filesystem example done!
If you find yourself with a corrupted file system, you can reset the storage by pressing BUTTON1:
Initializing the block device... OK
Erasing the block device... OK
Deinitializing the block device... OK
Note that if you press the reset button at the wrong time, you may corrupt a file system that is not power resilient!
If you have problems, you can review the documentation for suggestions on what could be wrong and how to fix it.
In Mbed OS, C++ classes that inherit from the FileSystem
interface are used to implement a file system instance. Mbed OS currently supports the LittleFileSystem, and FATFileSystem. In this example, you can determine which is used, by modifying the declaration of the object fs
in main.cpp.
For instance, to use the FATFileSystem, declare fs
as an instance of the class FATFileSystem:
- LittleFileSystem fs("fs");
+ FATFileSystem fs("fs");
Note: Different file systems require different minimum numbers of storage
blocks to function. For the FATFileSystem
, this example requires a minimum of
256 blocks, and for the LittleFileSystem
, this example requires a minimum of 6
blocks. You can find the number of blocks on a block device by dividing the
block device's size by its erase size.
Mbed-OS supports a variety of block devices. You can find information on configuring, and changing the block device used in this example here.
The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info.
This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.