ESP-IDF SPIFFS open file issues

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

ESP-IDF SPIFFS open file issues

Postby mikemoy » Wed Sep 26, 2018 1:25 pm

I have been pulling my hair out trying to figure out how to upload files, and open them for reading.

I created a simple test for this, when the app first runs it says .
W (302) SPIFFS: mount failed, -10025. formatting...
which is what I would expect. After its done formatting I reboot, and it says:
file: "/spiffs/test.txt"
Error opening file (2) No such file or directory
Which is also what I would expect since I have not uploaded any files.

I then create a "test.txt" file with the contents "Hello World!" in it. I then run:
./mkspiffs.exe -c C:/z -b 4096 -p 256 -s 0x100000 spiffs.bin
Then I upload it to the ESP32 using:
python $IDF_PATH/components/esptool_py/esptool/esptool.py --chip esp32 --port COM8 --baud 921600 write_flash -z 0x210000 spiffs.bin
When I reboot my board I get this:
file: "/spiffs/test.txt"
Error reading from file



Can anyone shed some light on what might be the issue here?

Here is my code & .csv file.

Code: Select all

#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <errno.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include "esp_vfs.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "esp_vfs_fat.h"


static const char tag[] = "[SPIFFS example]";


//-------------------------------
static void readTest(char *fname)
{
    printf("  file: \"%s\"\n", fname);

    int res;
    char *buf;
    buf = calloc(1024, 1);
    if (buf == NULL) {
        printf("  Error allocating read buffer\n");
        printf("\n");
        return;
    }

    FILE *fd = fopen(fname, "rb");
    if (fd == NULL) 
    {
        printf("  Error opening file (%d) %s\n", errno, strerror(errno));
        free(buf);
        printf("\n");
        return;
    }

    res = fread(buf, 1, 10, fd);
    if (res <= 0) 
    {
        printf("  Error reading from file\n");
    }
    else 
    {
        printf("  %d bytes read [\n", res);
        buf[res] = '\0';
        printf("%s\n]\n", buf);
    }
    free(buf);

    res = fclose(fd);
    if (res) 
    {
        printf("  Error closing file\n");
    }
    printf("\n");
}






int app_main(void)
{
    printf("\r\n\n");
    ESP_LOGI(tag, "==== STARTING SPIFFS TEST ====\n");

    esp_vfs_spiffs_conf_t conf = {
      .base_path = "/spiffs",
      .partition_label = NULL,
      .max_files = 10,
      .format_if_mount_failed = true
    };
    

    // Use settings defined above to initialize and mount SPIFFS filesystem.
    // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
    esp_err_t ret = esp_vfs_spiffs_register(&conf);

    if (ret != ESP_OK) 
    {
        if (ret == ESP_FAIL) 
        {
            ESP_LOGE(tag, "Failed to mount or format filesystem");
        } 
        else if (ret == ESP_ERR_NOT_FOUND) 
        {
            ESP_LOGE(tag, "Failed to find SPIFFS partition");
        } 
        else 
        {
            ESP_LOGE(tag, "Failed to initialize SPIFFS (%d)", ret);
        }
        return 0;
    }


    readTest("/spiffs/test.txt");

    return 0;

}

Code: Select all

# Name,   Type, SubType, Offset,  Size, Flags
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 1M,
storage,  data, spiffs, 0x210000, 1M, 

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP-IDF SPIFFS open file issues

Postby ESP_igrr » Wed Sep 26, 2018 2:36 pm

Please check that mkspiffs config matches your sdkconfig:

mkspiffs --version

cat sdkconfig | grep SPIFFS

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS open file issues

Postby mikemoy » Wed Sep 26, 2018 2:41 pm

Thanks, but i can't do that as I am on Windows using mingw32 terminal

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP-IDF SPIFFS open file issues

Postby ESP_igrr » Wed Sep 26, 2018 3:14 pm

Can you run mkspiffs --version from the same terminal where you have run mkspiffs before? It should work.

Second command simply finds all SPIFFS related settings in sdkconfig. If it doesn't work for some reason, you can also attach the entire sdkconfig file.

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS open file issues

Postby mikemoy » Wed Sep 26, 2018 3:37 pm

Ah.. my problem before was I didn't use "./"
it does not report a version.
Mike@DESKTOP-J24VA57 MINGW32 ~/mkspiffs-0.2.0
$ ./mkspiffs.exe --version

./mkspiffs version:


Mike@DESKTOP-J24VA57 MINGW32 ~/mkspiffs-0.2.0
$

ESP_igrr
Posts: 2071
Joined: Tue Dec 01, 2015 8:37 am

Re: ESP-IDF SPIFFS open file issues

Postby ESP_igrr » Wed Sep 26, 2018 3:43 pm

Given the file name, this seems to be 0.2.0. This version is quite old and is not compatible with the current IDF. Please download the latest one (at the moment this is mkspiffs-0.2.3-esp-idf-win32.zip)
from the releases page: https://github.com/igrr/mkspiffs/releases.

This version will work with default sdkconfig of IDF (metadata length =4 bytes, timestamps enabled, page size 256 bytes)

mikemoy
Posts: 626
Joined: Fri Jan 12, 2018 9:10 pm

Re: ESP-IDF SPIFFS open file issues

Postby mikemoy » Wed Sep 26, 2018 4:00 pm

Thank you so much that took care of it!

Who is online

Users browsing this forum: No registered users and 134 guests