How to mount LittleFS to ESP32 WROOM chip using Platformio?

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

This should be “littlefs.bin”!

Change the platform to
platform = espressif32 @ 6.9.0
to use the latest version.

Perform a full clean.
Rebuild and upload the project and filesystem.

Yes, we noticed that the FS image was wrong. We expected to have littlefs.bin, rather than spiffs.bin.

We tried “Full Clean” in platformio before, but it didn’t work through.


We will change platformio.ini with platform = espressif32 @ 6.9.0, and build and upload the FS image again.

Many thanks for help!

Cheers,
Kan

Please do this in the following order:

  1. change the platform
  2. perform the full clean
  3. rebuild firmware and filesystem + upload

It is important that you first change the platform

Gotcha :grinning:

Many thanks again!

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.....

Strictly following this order,

  1. change the platform to platform = espressif32 @ 6.9.0

  2. perform the full clean

  3. rebuild firmware and filesystem + upload

But still build and upload the FS image, with name spiffs.bin :frowning_face:

In addition, when changing platformio.ini to platform = espressif32 @ 6.9.0,

it could not compile our C++ programs, throwing the following error.

fatal error: esp_adc/adc_cali.h: No such file or directory. 
fatal error: esp_adc/adc_cali.h: No such file or directory. 

In platformio.ini,

  1. When platform = espressif32 @ 6.9.0,

    fatal error: esp_adc/adc_cali.h, No such file or directory.
    fatal error: 'class fs::LittleFSFS' has no member named 'mountpoint'
        LittleFS.mountpoint()
    
  2. When platform = espressif32,

    compilation succeeded, no error.

Looks like espressif32 @ 6.9.0 is not stable?

Many thanks again.

Kan

Espressif32 @ 6.9.0 is fully stable.
What’s your Espressif32 platform version ?

A dumb question, how to find my Espressif32 platform version ?

Click on the PIO Icon → PIO Home → Platforms
Scroll to Espressif32 and check the version on the right side:

There are multiple versions of ESP32 installed in my platformio,

On the top, is “ESP 6.9.0”,

Following the top, there are “6.4.0”, “6.7.0”, “6.9.0”, “51.3.4”, “53.3.10” installed, too.

I didn’t know you where using pioarduino.

Now this makes a bit more sense.
If no version is specified the highest version will be used.
In this case it is pioarduino. which is Espressif32 Arduino 3.x
That’s why you had some errors when changing to 6.9.0.

I tought you where below 6.9.0, that’s why i asked you to add this verison number.

Let me check again with pioarduino…

Hm, even pioarduino works without issues and creates a littlefs.bin

 *  Executing task: C:\Users\boris\.platformio\penv\Scripts\platformio.exe run --target buildfs --environment esp32dev 

Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
----------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (53.3.10) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (cmsis-dap) External (cmsis-dap, esp-bridge, esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
 - framework-arduinoespressif32 @ 3.1.0
 - framework-arduinoespressif32-libs @ 5.3.0+sha.083aad99cf
 - tool-esptoolpy @ 4.8.5
 - tool-mklittlefs @ 3.2.0
 - tool-riscv32-esp-elf-gdb @ 14.2.0+20240403
 - tool-xtensa-esp-elf-gdb @ 14.2.0+20240403
 - toolchain-xtensa-esp-elf @ 13.2.0+20240530
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 41 compatible libraries
Scanning dependencies...
Dependency Graph
|-- LittleFS @ 3.1.0
Building in release mode
Building FS image from 'data' directory to .pio\build\esp32dev\littlefs.bin
/hello.txt
============== [SUCCESS] Took 1.65 seconds ==============
 *  Terminal will be reused by tasks, press any key to close it. 

I think your platform or package is broken.
Change the platform to

platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10/platform-espressif32.zip

Close VS Code

Then delete the content of the platforms and packages folders.
These are located in your home directory in a subfolder .platformio
On Windows this is C:\users\<username>\.platformio

Restart VS Code and wait until the platform is reinstalled completely

Wow, awesome!

Shall I uninstall my “ESP32 @ 6.9.0 + sha.ad5d77a” ?

Will that resolve the Espressif32 Arduino 3.x issue?

This will happen automatically if you delete the content of the folders named above. (I would call this a “manual uninstall” :wink: )

What issue?

platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10/platform-espressif32.zip

When platformio downloading this zip, the connection timed out. I tried several times, always timed out.

But when I typed in the URL in my chrome browser, I could download this zip to my computer.

A question is that, how can I manually install the zip file in platformio / VSCode?

That’s strange!

How about this?

platform = https://github.com/pioarduino/platform-espressif32/releases/download/stable/platform-espressif32.zip

Sounds like an antivirus program is blocking vs code from downloading the packages?

I don’t know one. There are also some ohter parts downloaded later.
So i don’t think a “manual” way will work at all.

Tried back and forth, but no clue yet what happen.

The strange thing is that when we “Build Filesystem Image”, it always built “.pio/build/esp32dev/spiffs.bin”, rather than “.pio/build/esp32dev/littlefs.bin” that we expected.

In order to repeat the bug, we created a simple project,

Notice that we used “stable/platform-espressif32”, that is 6.9.0.

A lot of thanks for your great help!

Finally found the issue. There is a typo in your platformio.ini:

board_build.filesytem = LittleFS
                  ^--- missing "s"

Correct:

board_build.filesystem = LittleFS