Before we show how to mount LittleFS on Linux, let’s have a quick overview of its benefits and features.
LittleFS solution is a small, fast, simple and open-source solution. Perhaps the most significant LittleFS feature, which is very important when using file system with internal MCU flash, is wear leveling mechanism, while one of the major LittleFS advantage is PC Linux support tools. This tool allows users to mount, create and access files, created on MCU, on PC Linux machine. Also, this tool implements vice versa process (files created on PC side is accessible and editable on MCU side). More about LittleFS can be found here.
LittleFS PC mounting tool
One possible approach when using LittleFS would be to create file system on MCU using available LittleFS function but this would not be the most efficient approach. This is especially true if you want to access and explore file system data on MCU which had previously been created on the PC side. LittleFS has specifically useful tool which enables users to create, mount and explore file system content.
Check out the steps how to use this tool below:
How to get and build the mounting tool
For successful use of LittleFS mounting tool, FUSE version 2.6 or higher is necessary. If you do not have this version on your Linux machine you can download it with:
2. sudo apt-get install libfuse-dev
Now we need to create the directory, for example LittleFSImageTool, and to download littlefs-fuse tool in that directory using the following command:
3. git clone https://github.com/ARMmbed/littlefs-fuse.git
After we download source code from git repository, enter in littlefs-fuse directory and run the following command:
After this command is executed, lfs application is available at the same directory where we call make command. Now, we have everything we need to successfully build our first file system image.
Building and mounting LittleFS image
Finally, it’s time to create our first file system image. The first step is to create image file:
5. dd if=/dev/zero bs=2048 count=256 | tr '\000' '\377' > FSImage
With this command we create FSImage image file which has 256 blocks where each block size is 2kB. This image represents our MCU embedded flash memory part which will be used with the file system. If we dump FSImage file content, we will see that this image file is empty, only fill with “1”, and it is not yet ready to be used on MCU.
In order to make properly formatted image file and mount that image file on Linux PC file system, we will use some of the available Linux loop device. Available loop device can be viewed with the following command:
6. sudo losetup -f
After running this command, the first available device will be printed and that device will be used as our file system loop device. In our case, the first available loopback device is /dev/loop17. Now, we need to make this device user accessible with the following command:
7. sudo chmod a+rw /dev/loop17
Next, we will assign our FSImage file to the /dev/loop17 loop device with the following command:
8. sudo losetup /dev/loop17 FSImage
After executing this command, we need to format out loop device to be sure that it really acts as our MCU embedded flash memory. You can do this with the following command:
9. ./lfs --block_size=2048 --block_count=256 --block_cycles=100 --read_size=8 --prog_size=8 --cache_size=8 --lookahead_size=8 --format /dev/loop17
10. sudo dd if=/dev/loop17 of=FSImageOut.bin bs=2048 count=256 skip=0 | xxd -g1
However, before we explain how to program MCU with this file system, we should first look at how to mount your file system to Linux machine.
First, we will create directory with the following command:
11. mkdir mount
Next, we want to make this directory our file system entry point on Linux PC. This is done using the following command:
12. ./lfs --block_size=2048 --block_count=256 --block_cycles=100 --read_size=8 --prog_size=8 --cache_size=8 --lookahead_size=8 /dev/loop17 mount
After this command is executed, we can use directory as any other Linux directory. For example, let’s create some content in mount directory using these commands:
13. cd mount/ mkdir TestDir1 mkdir TestDir2 mkdir TestDir3 echo "TestContent1">TestFile.txt echo "TestContent2">TestFile1.txt cd TestDir1
When the directory content is created, loopback device image is ready to be written to output binary image file using this command:
14. sudo dd if=/dev/loop17 of=FSImageOut.bin bs=2048 count=256 skip=0 | xxd -g1
Now, using STM32Cube Programmer, or some other tool used for downloading flash content to MCU, we will program flash memory with our file system. This process is shown in the picture below:
How to check that mounting is completed
If the whole image create process is done successfully, we can restart our MCU and the content shown in the picture below should be displayed.
As can be seen in the next image, the file system on MCU is now ready to use. For example, if we want to see root directory content, just enter ls command in CLI and content created on PC will be now available on MCU command line. The image below illustrates execution of some of the available commands from the command list. Users can see available commands at any time by entering “-h” command in Command Line Interface.