Problem with SPIFFS with ESP32 on Arduino IDE

Description

I’m trying to implement the SPIFFS inside LittlevGL, but i’m meeting some problems. I can manipulate my files with pure SPIFFS Arduino library, but not with the LittlevGL callback functions.

What MCU/Processor/Board and compiler are you using?

ESP32

Code to reproduce

This is only the insterested part:

spiffs_drv_init();

  lv_fs_file_t f;
  lv_fs_res_t res;
  res = lv_fs_open(&f, "S:/prova.txt", LV_FS_MODE_RD);
  uint32_t read_num;
  uint8_t buf[8];
  res = lv_fs_read(&f, buf, 8, &read_num);
  Serial.print("File read: ");
  Serial.println((char*)buf);

  lv_fs_close(&f);

and

void spiffs_drv_init(){
  lv_fs_drv_init(&drv);                     /*Basic initialization*/

  drv.letter = 'S';                         /*An uppercase letter to identify the drive */
  drv.file_size = sizeof(File);   /*Size required to store a file object*/
  //drv.rddir_size = sizeof(my_dir_object);   /*Size required to store a directory object (used by dir_open/close/read)*/
  //drv.ready_cb = my_ready_cb;               /*Callback to tell if the drive is ready to use */
  drv.open_cb = my_open_cb;                 /*Callback to open a file */
  drv.close_cb = my_close_cb;               /*Callback to close a file */
  drv.read_cb = my_read_cb;                 /*Callback to read a file */
  drv.write_cb = my_write_cb;               /*Callback to write a file */
  drv.seek_cb = my_seek_cb;                 /*Callback to seek in a file (Move cursor) */
  drv.tell_cb = my_tell_cb;                 /*Callback to tell the cursor position  */
  drv.trunc_cb = my_trunc_cb;               /*Callback to delete a file */
  //drv.size_cb = my_size_cb;                 /*Callback to tell a file's size */
  //drv.rename_cb = my_size_cb;               /*Callback to rename a file */

  //drv.dir_open_cb = my_dir_open_cb;         /*Callback to open directory to read its content */
  //drv.dir_read_cb = my_dir_read_cb;         /*Callback to read a directory's content */
  //drv.dir_close_cb = my_dir_close_cb;       /*Callback to close a directory */

  //drv.free_space_cb = my_size_cb;           /*Callback to tell free space on the drive */

  //drv.user_data = my_user_data;             /*Any custom data if required*/

  lv_fs_drv_register(&drv);                 /*Finally register the drive*/
}

lv_fs_res_t my_open_cb(lv_fs_drv_t *drv, void *file_p, const char *fn, lv_fs_mode_t mode){
  (void) drv; /*Unused*/

  if (f) {
    return LV_FS_RES_OK;
  }

  Serial.print("PATH: ");
  Serial.println(fn);

  f = SPIFFS.open("/prova.txt", FILE_READ);

  if(!f || f.isDirectory()){
    Serial.println("ERROR!");
    return LV_FS_RES_UNKNOWN;
  } else{
    Serial.println("It's OK");
    return LV_FS_RES_OK;
  }
}

lv_fs_res_t my_close_cb(lv_fs_drv_t *drv, void *file_p){
  (void) drv; /*Unused*/

  f.close();
  return LV_FS_RES_OK;
}

lv_fs_res_t my_read_cb(lv_fs_drv_t *drv, void *file_p, 
                        void *buf, uint32_t btr, uint32_t *br){
  (void) drv; /*Unused*/

  *br = f.read((uint8_t*)buf, btr);
  return LV_FS_RES_OK;
}

My Serial gives the Error message which i insterted in open callback function, so the problem is first in that part.
I precise that actualy i put a static path file in the open function to compare it with the pure SPIFFS (which works):

    f = SPIFFS.open("/prova.txt", FILE_READ);

    if(!f || f.isDirectory()){
      Serial.println("ERROR!");
    } else{
      Serial.println("It's OK");
    }
    
    uint8_t buf[16];
    uint32_t *p;
    f.read(buf, sizeof(buf));
    
    //my_read_cb(buf, sizeof(buf), p);
    Serial.print("Provaaaa: ");
    Serial.println((char*)buf);

Solved! I forgot the SPIFFS.begin(true) command.

1 Like

Hello! Would you happen to still have the code to get SPIFFS working together with all the other callback functions? I would very much like to try it out in my project.

Thank you!