Working on an ESP32 platform, SPIFFS is not workable for our needs and we want to use LittleFS as the virtual file system for our internal Flash memory. I can’t find information how I would have to define a LittleFS partition in PlatformIO, though I presume it would simply be some custom type.
Also, are there any good tools to create BIN images for LittleFS that PlatformIO can use and how would I tell PIO to use that tool and then flash the respective BIN file to the device?
I can’t seem to find any documentation that explains the path to set up custom partitions, their image creation and upload in PIO, so any suggestions or pointers would be really appreciated.
So, I got a custom Python script to build my LittleFS partition image and I can upload it to the device using esptool, after all.
I’m still trying to figure out how to get PlatformIO to call the respective custom scripts, though, to build my image instead of a SPIFFS image when I select “Upload File System Image” and flash it to the device in the respective partition. Thoughts, anyone?
For all those interested, I solved the problem. I am essentially creating a SPIFFS partition and load my own LittleFS image into it instead, fooling PlatformIO into thinking, it’s actually dealing with a SPIFFS image. I am doing this by overloading the MKSPIFFS command.
I created a Python script that I call from platformio.ini
extra_scripts = LittleFSBuilder.py
In this file I replace the MKSPIFFS tool command with my own like this
My own tool now uses the command line arguments, builds the LittleFS bin image accordingly and saves it to where PlatformIO tells it to (the last argument in the mkspiffs call is the destination file name).
PlatformIO now believes it has actually created a SPIFFS.BIN file and proceeds to upload it to the device. Done!
Nice! I’ll add an issue to the github mentioning this, as LittleFS is missing from PIO at the moment as an FS option, for both ESP8266 and ESP32. Would you be willing to share your mklittlefs.py script also?
Edit: Er… now I’m confused… the ESP8266 core has LittleFS support as of 2.6.0, but not the ESP32 yet? Are you using a third party like esp32_littleflash or something?
As for the mklittlefs.py script, you can find it below. The file copying is a bit awkward but shutil did not work for some reason and locked up, so I decided to really read each file into a buffer instead and save it out from there.
#
# This is a small tool to read files from the PlatformIO data directory and write
# them to a flashable BIN image in the LittleFS file format
#
# This script requires the LittleFS-Python mopackage
# https://pypi.org/project/littlefs-python/
#
#
# Author: Guido Henkel
# Date: 1/2/2020
#
import getopt, sys
from littlefs import LittleFS
from pathlib import Path
print( "Building LittleFS image…" )
argList = sys.argv[1:]
arxx = { argList[i]: argList[i+1] for i in range(0, len(argList)-1, 2) }
print( arxx )
dataPath = arxx["-c"] # Grab the source path parameter
fileList = [pth for pth in Path(dataPath).iterdir() if pth.stem[0] != '.'] # Create a list of all the files in the directory
fs = LittleFS(block_size=4096, block_count=3072) # Open LittleFS, create a 12MB partition
for curFile in fileList:
print( "Adding " + curFile.name )
with open( curFile, 'rb' ) as f: # Read the file into a buffer
data = f.read()
with fs.open( curFile.name, 'w') as fh: # Write the file to the LittleFS partition
fh.write( data )
outName = argList[-1] # The last argument is the destination file name
with open(outName, 'wb') as fh: # Dump the filesystem content to a file
fh.write(fs.context.buffer)
Anywhere it can be accessed! Keep in mind you need to know where it goes when you add the extra_scripts line in your platformio… so you probably want it in the root of the project folder, or in a extras subfolder… etc.
the script will invoke python3, not python… Though I don’t know why the double quotes are exactly this way, you might have to delete them so that it results in python3 <project_dir>/mklittefs.py (or put the whole path to the script in quotes in case the path contains spaces)
I tried the proposed option, but the resulting image (I guess) is not compatible with LittleFSv2 in Arduino.
What worked for me is a similar substitution:
I changed python3 to python in the script as you suggested, and now it seems to be running OK, but I get an error message saying that there is not enough space…:
I tried your proposal and it finally went through without a problem. However, none of the web pages (in the data folder) get displayed. This leads me to believe that I haven’t made sufficient changes in my code (for the ESP).
As I read that LittleFS is “drop-in compatible” with SPIFFS, so far I only changed all occurrences of SPIFFS to LittleFS, like this: fs::File f = LittleFS.open("/pageheader.html", "r");
Is there anything else I need to be aware of?
Worth noting, that any existing code that creates/modifies/reads files on the fly work. The problem is only with the file system I upload.
Do you know what block size and count should I use for the ESP12-F?
I reduced the block_count to 250 to make the image the same size as the other method creates, it uploads fine, but the pages are not loadable… (I don’t get any error messages, simply nothing is served to the client). Again, the file system seems to be OK, because my software can read and write a new file.
I’m not an expert on how littlefs-python · PyPI creates the LittleFS exactly and what kind of debugging should be undertaken to find out what’s going on behind the curtain. As a general debugging step I’d recommend trying to unpack / list the contents of the generated spiffs.bin (actually LittelFS) with the tools given above, like mklittlefs. You should also try to mount and enumerate the LittleFS filesystem (there’s got to be some functions which tells you how big the mounted LittleFS partition is and a directory/file crawler enumerating all files).
For reproduction I’d also recommend upload your project (e.g. to github) so that other people can reproduce your project and problem 1:1.