File not opening from SPIFFS
Posted: Sat Jul 24, 2021 1:49 am
I'm trying to read a file from SPIFFS but getting NULL from fopen. I'm able to stat the file, but open isn't working.
partitions.csv:
Data directory is called spiffs and contains:
Image built with:
Written to flash with:
Code that tries to read the file:
Log output (snippets):
partitions.csv:
Code: Select all
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 24K,
phy_init, data, phy, 0xf000, 4K,
factory, app, factory, 0x10000, 1500K,
spiffs, data, spiffs, , 1M, ,
Code: Select all
total 288
-rw-r--r-- 1 bcolbert staff 20K Jul 23 08:41 drip.raw
-rw-r--r-- 1 bcolbert staff 118K Jul 23 08:41 sonar.raw
Code: Select all
mkspiffs -c spiffs -b 4096 -p 256 -s 0x100000 spiffs.bin
Code: Select all
esptool.py --chip esp32 write_flash -z 0x187000 spiffs.bin
Code: Select all
ESP_LOGI(BT_APP_CORE_TAG, "Initializing SPIFFS");
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = false
};
// 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(BT_APP_CORE_TAG, "Failed to mount or format filesystem");
} else if (ret == ESP_ERR_NOT_FOUND) {
ESP_LOGE(BT_APP_CORE_TAG, "Failed to find SPIFFS partition");
} else {
ESP_LOGE(BT_APP_CORE_TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
}
return;
}
size_t total = 0, used = 0;
ret = esp_spiffs_info(conf.partition_label, &total, &used);
if (ret != ESP_OK) {
ESP_LOGE(BT_APP_CORE_TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
} else {
ESP_LOGI(BT_APP_CORE_TAG, "Partition size: total: %d, used: %d", total, used);
}
struct stat st;
if (stat(filename, &st) == 0) {
// Delete it if it exists
ESP_LOGI(BT_APP_CORE_TAG, "!!! %s is there", filename);
}
ESP_LOGI(BT_APP_CORE_TAG, "Opening file %s", filename);
FILE* fd = fopen(filename, "r");
if (fd) {
ESP_LOGE(BT_APP_CORE_TAG, "Failed to open file for reading");
return;
}
else{
while (!feof(fd)) // to read file
{
int n = fread(snd_buff, 1, BLOCK_SIZE, fd);
ESP_LOGI(BT_APP_CORE_TAG, "%d bytes read and written %02x,%02x", n, ((uint16_t*)snd_buff)[0], ((uint16_t*)snd_buff)[1]);
}
fclose(fd);
}
// All done, unmount partition and disable SPIFFS
esp_vfs_spiffs_unregister(conf.partition_label);
ESP_LOGI(BT_APP_CORE_TAG, "SPIFFS unmounted");
I (60) boot: Partition Table:
I (63) boot: ## Label Usage Type ST Offset Length
I (71) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (78) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (86) boot: 2 factory factory app 00 00 00010000 00177000
I (93) boot: 3 spiffs Unknown data 01 82 00187000 00100000
I (101) boot: End of partition table
Any tips? How do I read a file from SPIFFS?I (11261) BT_APP_CORE: Initializing SPIFFS
I (13761) BT_APP_CORE: Partition size: total: 956561, used: 143572
I (13881) BT_APP_CORE: !!! /spiffs/drip.raw is there
I (13881) BT_APP_CORE: Opening file /spiffs/drip.raw
E (13881) BT_APP_CORE: Failed to open file for reading