Page 1 of 1

LittleFS behaves differently on Arduino for ESP8266 and ESP32

Posted: Tue Sep 19, 2023 7:33 pm
by trullock
I'm porting some code from ESP8266 to ESP32 and I've found a behavioural difference between the two which I need to solve.

I'm writing log data to a LittleFS file on the ESP8266, calling flush after each write. On the ESP8266 if there is a power failure before `file.close()` is called, the data is synced to flash and can be read back. On the ESP32 it is not, the file has 0 Bytes.

I loosely understand why, its to do with how LFS stages the content and when `sync()` gets called. This thread is relevant https://github.com/littlefs-project/littlefs/issues/344

My question is, whats (or rather where) is different between the ESP32 and ESP8266? What code or configuration settings are different between the two?

LFS looks to be precompiled in the arduino-esp32 source which isn't helpful, whereas the ESP8266 uses sources from https://github.com/ARMmbed/littlefs.git

What is the ESP32 doing, why is the behaviour different and how to I get it to behave like the ESP8266?

In the absence of an answer to the above, at least, what sources are used to compile LFS for the ESP32 Arduino project?

Thanks

Code below that repro's the issue

Code: Select all

#include <LittleFS.h>
File currentLog;

#define DELAY 200
uint64_t lastMillis = 0;
uint8_t loopBuffer[6] = {0};
uint16_t counter = 0;

void setup() {
  Serial.begin(115200);

  if (!LittleFS.begin())
  {
    Serial.println("LittleFS mount failed");
    return;
  }


  currentLog = LittleFS.open("test", "r");
  auto size = currentLog.size();
  auto rows = size > 0 ? (size - 65) / 6 : 0;

  Serial.print("Opened previous log of size: ");
  Serial.print(size);
  Serial.print(" bytes, found ");
  Serial.print(rows);
  Serial.println(" rows");
  currentLog.close();

  Serial.println("Waiting 5 secs...");
  delay(5000);

  currentLog = LittleFS.open("test", "w");
  
  lastMillis = millis();
}

void loop() {
  uint64_t now = millis();
  if(now < lastMillis + DELAY)
    return;

  lastMillis = now;

  currentLog.write(loopBuffer, 6);
  currentLog.flush();
  
  Serial.print("Looped: ");
  Serial.println(counter++);

}

Re: LittleFS behaves differently on Arduino for ESP8266 and ESP32

Posted: Wed Sep 20, 2023 5:07 am
by lbernstone
LittleFS has an esp-idf component which gets compiled down into a library, sourced from https://github.com/joltwallet/esp_littlefs. That mounts a LittleFS partition into the vfs. It also has a C++ component at https://github.com/espressif/arduino-es ... s/LittleFS, which mounts it into the Arduino FS, and provides the basic methods in that header file.

Re: LittleFS behaves differently on Arduino for ESP8266 and ESP32

Posted: Wed Sep 20, 2023 10:43 am
by trullock
Thanks I'll follow that lead.

If anyone else can shed any light I'd be very grateful. I'll report back if I ever solve this.

Re: LittleFS behaves differently on Arduino for ESP8266 and ESP32

Posted: Wed Sep 20, 2023 6:15 pm
by lbernstone
Files on esp32 are mounted into a vfs. You should use a path ("/test").