How to mount LittleFS to ESP32 WROOM chip using Platformio?
We took following steps to upload our files from Platformio runningon Ubuntu 22.04, to a board with ESP32-WROOM chip, but failed.
Can anyone give us a hint how to fix the problem?
Step 1. Configure platformio.ini
Notice that, board_build.filesytem = littlefs
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
board_build.filesytem = littlefs
board_build.partitions = default_4MB.csv
;board_build.flash_mode = dio
;board_build.f_flash = 80000000L
Step 2. Create a partition table
The partition table’s name is default_4MB.csv
,
Notice that we created a partition, littlefs, data, littlefs, 0x290000,0x160000,
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x5000,
otadata, data, ota, 0xe000, 0x2000,
app0, app, ota_0, 0x10000, 0x140000,
app1, app, ota_1, 0x150000,0x140000,
littlefs, data, littlefs, 0x290000,0x160000,
coredump, data, coredump,0x3F0000,0x10000,
Step 3. Create a /data directory to contain our files
In our platformio project, we created a /data
directory to contain our files and subdirectory,
+-- data
+- index.html
+- /subdir
+- test.txt
Step 4. Make a filesystem image and upload it
Using platformio IDE, we made an image for our data
filesystem, and uploaded the image to ESP32. Following log snippet was extracted from the platformio IDE.
Notice that the name of the filesystem image is spiffs.bin
, not littlefs.bin
.
Building FS image from 'data' directory to .pio/build/esp32dev/spiffs.bin
/index.html
/subdir/test.txt
Looking for upload port...
Warning! Please install `99-platformio-udev.rules`.
More details: https://docs.platformio.org/en/latest/core/installation/udev-rules.html
Auto-detected: /dev/ttyUSB0
Uploading .pio/build/esp32dev/spiffs.bin
esptool.py v4.8.5
Serial port /dev/ttyUSB0
Connecting.....
Step 5. Write our C++ programs
We wrote C++ program to use LittleFS, a part of it was quoted as following,
Notice that we used LittleFS.begin(true, "/littlefs", 10U, "littlefs")
to mount the LittleFS.
#include <LittleFS.h>
void EmbeddedFS::setup_embeddedfs() {
// LittleFS.begin(bool formatOnFail, const char *basePath, uint8_t maxOpenFiles, const char *partitionLabel)
if (!LittleFS.begin(true, "/littlefs", 10U, "littlefs")) {
// if (!SPIFFS.begin(true, "/spiffs", 10U, "spiffs")) {
// if (!SPIFFS.begin(true)) {
Serial.printf("\n[WARN] LittleFS mount failed. \n");
return;
}
else {
Serial.printf("\n[INFO] Successfully mounts LittleFS at '%s'. \n",
LittleFS.mountpoint()
);
}
// Verify that the LittleFS file system works well.
// List file directory with 3 levels.
list_dir("/", 3);
}
void EmbeddedFS::list_dir(String dir_name, int levels) {
const char *_dir_name = dir_name.c_str();
Serial.printf("\n[INFO] Listing directory: '%s'\n", dir_name);
Serial.printf("-- Notice that LittleFS doesn't recognize directories, but it can find the files inside subdirs. --\n\n");
// File root = SPIFFS.open(_dir_name);
File root = LittleFS.open(_dir_name);
if (!root) {
Serial.printf("\n[WARN] Failed to open directory: %s.\n", _dir_name);
return;
}
if (!root.isDirectory()) {
Serial.printf("\n[WARN] '%s' not a directory. \n", _dir_name);
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
Serial.println(file.name());
if (levels) {
list_dir(file.path(), levels - 1);
}
} else {
Serial.printf(" FILE: '%s', SIZE: %d \n", file.name(), file.size());
}
file = root.openNextFile();
}
}
Step 6. Compiled, uploaded and ran our C++ program
When running our C++ programs, it throwed the following log with bugs in the Platformio serial monitor,
[INFO] Successfully mounts LittleFS at '/littlefs'.
[INFO] Listing directory: '/'
-- Notice that LittleFS doesn't recognize directories, but it can find the files inside subdirs. --
[ 64][E][vfs_api.cpp:23] open(): File system is not mounted
[WARN] Failed to open file '/subdir/test.txt' for writing.
[ 74][E][vfs_api.cpp:23] open(): File system is not mounted
[WARN] Failed to open file '/subdir/test.txt' for reading.
Step 7. Tried to change the parameters of mounting, still failed
We changed the mount function
from LittleFS.begin(true, "/littlefs", 10U, "littlefs")
to LittleFS.begin(true, "/littlefs", 10U, "spiffs")
,
it still failed, and with more severe bugs as following,
E (20) esp_littlefs: partition "spiffs" could not be found
E (20) esp_littlefs: Failed to initialize LittleFS
[ 51][E][LittleFS.cpp:79] begin(): Mounting LittleFS failed! Error: 261
Can you please help us find what went wrong, and how to fix the bugs?
Many thanks,
Kan