LittleFS for ESP32-WROOM

kan.deng@qq.com
Posts: 1
Joined: Wed Jan 08, 2025 3:56 am

LittleFS for ESP32-WROOM

Postby kan.deng@qq.com » Wed Jan 08, 2025 5:47 am

How to configure and program LittleFS in platoformio to run on a ESP32-WROOM chip?

I did the following, but failed.

Can anyone give me a hint how to solve the problem?

Step 1. configure platformio.ini

Notice that: board_build.filesytem = littlefs

Code: Select all

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
board_build.filesytem = 
board_build.partitions = default_4MB.csv
;board_build.flash_mode = dio
;board_build.f_flash = 80000000L
Step 2. create a partition table, default_4MB.csv

Notice that we created a partition called "littlefs",

Code: Select all

# 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 "data" file folder

Notice that in "data" folder, there is a file "index.html", and a subdirectory "subdir" which contains another file "test.txt"

Code: Select all

project_home/data/
                      index.html
                      subdir/test.txt
Step 4. Build the filesystem image, and upload the filesystem image to ESP32-WROOM, using Platformio tools.

Notice that in the log, platformio create a filesystem image called "" and uploaded it successfully.

Code: Select all

Building FS image from 'data' directory to .pio/build/esp32dev/spiffs.bin
/index.html
/subdir/test.txt
Looking for upload port...
...
Auto-detected: /dev/ttyUSB0
Uploading .pio/build/esp32dev/spiffs.bin
Step 5. We wrote a C++ program to use LittleFS

Code: Select all

#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 SPIFSS 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. Compile, upload the programs and run them on ESP32-WROOM

Following is the execution result, which contains bugs.

Code: Select all

[INFO] Successfully mounts LittleFS at '/littlefs'. 

[INFO] Listing directory: '/'
-- Notice that SPIFSS 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. Change begin()

We changed LittleFS.begin(true, "/littlefs", 10U, "littlefs") to LittleFS.begin(true, "/littlefs", 10U, "spiffs"),

More severe bugs are thrown, as following,

Code: Select all

 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 anyone give us a hint how to fix the bug? Many thanks.

lbernstone
Posts: 886
Joined: Mon Jul 22, 2019 3:20 pm

Re: LittleFS for ESP32-WROOM

Postby lbernstone » Wed Jan 08, 2025 8:10 pm

TL;DR. Make sure all your variables are valid at the time you instantiate the class, and you don't have anything local that needs to be persistent.
If you don't need Arduino FS compatibility, I'd recommend just using the POSIX vfs layer.

Who is online

Users browsing this forum: sazanof and 71 guests