Can't open a file using LittleFS while in the loop()

Description:

Hi all,
I am using ESP32 Dev Module and I am trying to open a file using LittleFS library in the loop() method, but my ESP32 board always restarts and throws Guru Meditation Error.
If I am opening the same file the same way in setup() method it opens the file without any problem and I can read it and write to it.
Would someone kindly tell to me what is going on or what I am doing wrong? How can I achieve this in the loop() method?
The module actually crashes when executing this line of code:

File file = LITTLEFS.open(filename);

Sketch:


#include <Arduino.h>
#include <ArduinoJson.h>
#include <LITTLEFS.h>

// Create Task handle objects
TaskHandle_t Core0_Task0;
TaskHandle_t Core1_Task0;

void listAllFiles() {
  // List all available files (if any) in the SPI Flash File System
  File root = LITTLEFS.open("/");
  File file = root.openNextFile();
  while (file) {
    Serial.print("FILE: ");
    Serial.println(file.name());
    file = root.openNextFile();
  }
  root.close();
  file.close();  
}

void readDataFromFile(const char* filename) {
  // Read JSON data from a file
  File file = LITTLEFS.open(filename);
  if (file) {
    // Deserialize the JSON data
    DynamicJsonDocument doc(5120);
    DeserializationError error = deserializeJson(doc, file);
    if (!error) {
      int data_str = doc["ports"][1];
      Serial.println(data_str);
    } else {
      log_v("deserializing JSON", error);
    }
  } else {
    Serial.print("Could not open file: ");
    Serial.println(filename);
  }
  file.close();  
}

void writeDataToFile(const char* filename) {
  File outfile = LITTLEFS.open(filename, FILE_WRITE);
  
  DynamicJsonDocument doc(5120);
  doc["ports"][0] = "80";
  doc["ports"][1] = 443;

  serializeJson(doc, Serial);
  if (!serializeJson(doc, outfile)) {
    Serial.println("Failed to write to LITTLEFS file.");
  } else {
    Serial.println("Success!");
  }
  outfile.close();  
}

void createTasks() {
  xTaskCreatePinnedToCore(
    primaryTask,      // Function to implement the task
    "PrimaryTask",    // Name of the task
    2000,             // Stack size in words
    NULL,             // Task input parameter
    0,                // Priority of the task
    &Core1_Task0,     // Task handle.
    1                 // Core where the task should run
  );

  xTaskCreatePinnedToCore(
    secondaryTask,    // Function to implement the task
    "SecondaryTask",  // Name of the task
    2000,             // Stack size in words
    NULL,             // Task input parameter
    0,                // Priority of the task
    &Core0_Task0,     // Task handle.
    0                 // Core where the task should run
  );
}

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  
  /* if (!LITTLEFS.begin(true)) {
    Serial.println("LITTLEFS Mount Failed.");
  } else {
    Serial.println("LITTLEFS Mount SUCCESSFUL.");
  }

  const char* filename = "/data.json";
  writeDataToFile(filename);
  listAllFiles();  
  readDataFromFile(filename);

  LITTLEFS.end(); */
  createTasks();
}

void loop() {
  vTaskDelete(NULL);
}

void primaryTask(void * parameter) {

  bool flag = true;
  for (;;) {
   if (flag) {
      flag = false;
      if (!LITTLEFS.begin(true)) {
        Serial.println("LITTLEFS Mount Failed.");
      } else {
        Serial.println("LITTLEFS Mount SUCCESSFUL.");
        const char* filename = "/data.json";
        writeDataToFile(filename);
        listAllFiles();  
        readDataFromFile(filename);
      }
      
      LITTLEFS.end();
    }
  }
}

void secondaryTask(void * parameter) {  
  for (;;) {
    // do nothing
  }
}

Debug Messages:

This is out of my Serial monitor:

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception)
Debug exception reason: Stack canary watchpoint triggered (PrimaryTask) 
Core 1 register dump:
PC      : 0x40083780  PS      : 0x00060236  A0      : 0x3ffb8510  A1      : 0x3ffb8450  
A2      : 0x3ff000e0  A3      : 0x00000001  A4      : 0x3ffbde5c  A5      : 0x00000001  
A6      : 0x00060223  A7      : 0x00000000  A8      : 0x80082b8e  A9      : 0x3ffb84f0  
A10     : 0x00000000  A11     : 0x3ffb8c2c  A12     : 0x00000001  A13     : 0x00000001  
A14     : 0x00060220  A15     : 0x00000000  SAR     : 0x00000018  EXCCAUSE: 0x00000001  
EXCVADDR: 0x00000000  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x40083780:0x3ffb8450 0x3ffb850d:0x3ffb8530 0x40086109:0x3ffb8550 0x400ea4ca:0x3ffb8590 0x400ea592:0x3ffb85b0 0x40083b49:0x3ffb85d0 0x4008403d:0x3ffb85f0 0x400ee046:0x3ffb8660 0x400d6eec:0x3ffb8690 0x400d3be5:0x3ffb86c0 0x400d4037:0x3ffb86f0 0x400d45d5:0x3ffb8790 0x400d6a35:0x3ffb87f0 0x400d2d51:0x3ffb8840 0x400e94ad:0x3ffb8970 0x4000bcc5:0x3ffb8990 0x400dee19:0x3ffb89b0 0x400d7727:0x3ffb89d0 0x400d7c13:0x3ffb8a00 0x400d7ed9:0x3ffb8a40 0x400d73c7:0x3ffb8ac0 0x400d2509:0x3ffb8af0 0x400d2626:0x3ffb8b70 0x400862c1:0x3ffb8b90

This is the output of the Exception decoder:

PC: 0x40083780
EXCVADDR: 0x00000000

Decoding stack results
0x40086109: xQueueGenericReceive at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/queue.c line 1592
0x400ea4ca: esp_ipc_call_and_wait at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/ipc.c line 116
0x400ea592: esp_ipc_call at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/ipc.c line 123
0x40083b49: spi_flash_disable_interrupts_caches_and_other_cpu at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/cache_utils.c line 122
0x4008403d: spi_flash_read at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_ops.c line 165
0x400ee046: esp_partition_read at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/partition.c line 242
0x400d6eec: littlefs_api_read at D:\Projects\Arduino\libraries\LittleFS_esp32\src\littlefs_api.c line 22
0x400d3be5: lfs_bd_read at D:\Projects\Arduino\libraries\LittleFS_esp32\src\lfs.c line 104
0x400d4037: lfs_dir_fetchmatch at D:\Projects\Arduino\libraries\LittleFS_esp32\src\lfs.c line 861
0x400d45d5: lfs_dir_find at D:\Projects\Arduino\libraries\LittleFS_esp32\src\lfs.c line 1245
0x400d6a35: lfs_stat at D:\Projects\Arduino\libraries\LittleFS_esp32\src\lfs.c line 3189
0x400d2d51: vfs_littlefs_stat at D:\Projects\Arduino\libraries\LittleFS_esp32\src\esp_littlefs.c line 1196
0x400e94ad: esp_vfs_stat at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/vfs/vfs.c line 492
0x400dee19: stat at ../../../.././newlib/libc/syscalls/sysstat.c line 12
0x400d7727: VFSFileImpl::VFSFileImpl(VFSImpl*, char const*, char const*) at C:\Users\franc\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\FS\src\vfs_api.cpp line 237
0x400d7c13: std::__shared_ptr ::__shared_ptr  , VFSImpl*&, char const*, char const*&>(std::_Sp_make_shared_tag, std::allocator  const&, VFSImpl*&, char const*&&, char const*&) at c:\users\franc\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-97-gc752ad5-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\ext/new_allocator.h line 120
0x400d7ed9: VFSImpl::open(char const*, char const*) at c:\users\franc\appdata\local\arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-97-gc752ad5-5.2.0\xtensa-esp32-elf\include\c++\5.2.0\bits/shared_ptr.h line 319
0x400d73c7: fs::FS::open(char const*, char const*) at C:\Users\franc\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\libraries\FS\src\FS.cpp line 191
0x400d2509: writeDataToFile(char const*) at D:\Projects\Arduino\sketch_jul15b/sketch_jul15b.ino line 43
0x400d2626: primaryTask(void*) at D:\Projects\Arduino\sketch_jul15b/sketch_jul15b.ino line 114
0x400862c1: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

Why is all that stuff commented out in setup()?

Just to show the an example that works in the setup()

I have found the solution to my problem by increasing the stack size of the task when pinned to core. Thanks anyway.

Regards

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.