ESP-IDF SPIFFS open file issues
Posted: 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 .
I then create a "test.txt" file with the contents "Hello World!" in it. I then run:
Can anyone shed some light on what might be the issue here?
Here is my code & .csv file.
I created a simple test for this, when the app first runs it says .
which is what I would expect. After its done formatting I reboot, and it says:W (302) SPIFFS: mount failed, -10025. formatting...
Which is also what I would expect since I have not uploaded any files.file: "/spiffs/test.txt"
Error opening file (2) No such file or directory
I then create a "test.txt" file with the contents "Hello World!" in it. I then run:
Then I upload it to the ESP32 using:./mkspiffs.exe -c C:/z -b 4096 -p 256 -s 0x100000 spiffs.bin
When I reboot my board I get this:python $IDF_PATH/components/esptool_py/esptool/esptool.py --chip esp32 --port COM8 --baud 921600 write_flash -z 0x210000 spiffs.bin
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,