I am trying to use spiffs to store an html file but I can’t seem to fread from it. The set up as well as fopen works, but fread returns 0.
My Partition Table
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x10000, 0x6000
otadata, data, ota, 0x16000, 0x2000
phy_init, data, phy, 0x18000, 0x1000
ota_0, 0, ota_0, 0x20000, 1500K
ota_1, 0, ota_1, , 1500K
storage, data, nvs, , 0x10000
spiffs, data, spiffs, 0x327000, 0x30000
sdkconfig
#
# SPIFFS Configuration
#
CONFIG_SPIFFS_MAX_PARTITIONS=3
#
# SPIFFS Cache Configuration
#
CONFIG_SPIFFS_CACHE=y
CONFIG_SPIFFS_CACHE_WR=y
CONFIG_SPIFFS_CACHE_STATS=
CONFIG_SPIFFS_PAGE_CHECK=y
CONFIG_SPIFFS_GC_MAX_RUNS=10
CONFIG_SPIFFS_GC_STATS=
CONFIG_SPIFFS_PAGE_SIZE=256
CONFIG_SPIFFS_OBJ_NAME_LEN=32
CONFIG_SPIFFS_USE_MAGIC=y
CONFIG_SPIFFS_USE_MAGIC_LENGTH=y
CONFIG_SPIFFS_META_LENGTH=4
CONFIG_SPIFFS_USE_MTIME=y
#
# Debug Configuration
#
CONFIG_SPIFFS_DBG=
CONFIG_SPIFFS_API_DBG=
CONFIG_SPIFFS_GC_DBG=
CONFIG_SPIFFS_CACHE_DBG=
CONFIG_SPIFFS_CHECK_DBG=
CONFIG_SPIFFS_TEST_VISUALISATION=
Steps I took
./mkspiffs -c ./components/wifi/resources -b 4096 -p 256 -s 0x30000 spiffs.bin
./components/wifi/resources has two files in it, a .txt and a .html
python ./amazon-freertos/vendors/espressif/esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port /dev/cu.SLAB_USBtoUART --baud 115200 write_flash -z 0x327000 spiffs.bin
The code:
esp_err_t init_spiffs(){
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.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(MAIN, "Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
ESP_LOGE(MAIN, "Failed to find SPIFFS partition");
} else {
ESP_LOGE(MAIN, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
}
return ret;
}
size_t total = 0, used = 0;
ret = esp_spiffs_info(NULL, &total, &used);
if (ret != ESP_OK) {
ESP_LOGE(MAIN, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
} else {
ESP_LOGI(MAIN, "Partition size: total: %d, used: %d", total, used);
}
return ESP_OK;
}
...
int app_main( void ){
// Initialize SPIFFS
ret = init_spiffs();
if(ret != ESP_OK){
ESP_LOGI(MAIN, "init_spiffs(): %s", esp_err_to_name(ret));
return 0;
}
ESP_LOGI(MAIN, "Opening file");
FILE* f = fopen("/spiffs/foo.txt", "r");
if (f == NULL) {
ESP_LOGI(MAIN, "Failed to open file for writing");
}
char buf[100] = {0};
int bytes_read = fread(buf, 1, 100, f);
ESP_LOGI(MAIN, "bytes read: %i", bytes_read);
for(int i = 0; i < 100; i++){
printf("%c\n", buf[i]);
}
Console output:
I (340) MAIN: Partition size: total: 173441, used: 28614
I (340) MAIN: Opening file
I (340) MAIN: bytes read: 0
Any help would be appreciated. Thanks!