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:
- A built-in SPI flash, such as on the FRDM-K82F.
- An external block device (one of SPI flash, DataFlash or an SD card).
- Simulated on a heap block device on boards with enough RAM.
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.
- Install Mbed CLI 2.
- From the command-line, import the example:
mbed-tools import mbed-os-example-filesystem
- Change the current directory to where the project was imported.
- Install Mbed CLI 1.
- From the command-line, import the example:
mbed import mbed-os-example-filesystem
- Change the current directory to where the project was imported.
-
Connect a USB cable between the USB port on the board and the host computer.
-
Run the following command to build the example project, program the microcontroller flash memory and open a serial monitor:
- Mbed CLI 2
$ mbed-tools compile -m <TARGET> -t <TOOLCHAIN> --flash --sterm
- Mbed CLI 1
$ 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.
- K64F + Heap + LittleFS
- K64F + Heap + FATFS
- K64F + SD + LittleFS
- K64F + SD + FATFS
- K64F + SPIF (requires shield) + LittleFS
- K64F + SPIF (requires shield) + FATFS
- K64F + DataFlash (requires shield) + LittleFS
- K64F + DataFlash (requires shield) + FATFS
- NUCLEO_F429ZI + Heap + LittleFS
- NUCLEO_F429ZI + Heap + FATFS
- NUCLEO_F429ZI + SD (requires shield) + LittleFS
- NUCLEO_F429ZI + SD (requires shield) + FATFS
- NUCLEO_F429ZI + SPIF (requires shield) + LittleFS
- NUCLEO_F429ZI + SPIF (requires shield) + FATFS
- NUCLEO_F429ZI + DataFlash (requires shield) + LittleFS
- NUCLEO_F429ZI + DataFlash (requires shield) + FATFS
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.