I’m trying to create a SPIFFS-based file server, using the PlatformIO IDE, running the esp32 (ESP-IDF) framework. To my knowledge, I’ve completed all the required steps:
My partitions.csv
file contains a partition for spiffs:
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
factory, app, factory, , 0x200000,
storage, data, spiffs, , 0x100000,
My CMakeLists.txt
file contains the instruction spiffs_create_partition_image(spiffs data)
My files to upload are in a directory named data
When I select ‘Build Filesystem Image’ and ‘Upload Filesystem Image’, everything appears correct, and the image is written to the correct memory location (0x207000).
Processing esp32dev (platform: espressif32; framework: espidf; board: esp32dev)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.3.2) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-espidf 3.40300.0 (4.3.0)
- tool-cmake 3.16.4
- tool-esptoolpy 1.30100.210531 (3.1.0)
- tool-idf 1.0.1
- tool-mconf 1.4060000.20190628 (406.0.0)
- tool-mkspiffs 2.230.0 (2.30)
- tool-ninja 1.9.0
- toolchain-esp32ulp 1.22851.191205 (2.28.51)
- toolchain-xtensa32 2.80400.210211 (8.4.0)
Reading CMake configuration...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Building SPIFFS image from 'data' directory to .pio\build\esp32dev\spiffs.bin
/favicon.ico
/HereIsText.txt
/testimg.png
/upload_script.html
================================================================================= [SUCCESS] Took 14.05 seconds =================================================================================
Processing esp32dev (platform: espressif32; framework: espidf; board: esp32dev)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 (3.3.2) > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-espidf 3.40300.0 (4.3.0)
- tool-cmake 3.16.4
- tool-esptoolpy 1.30100.210531 (3.1.0)
- tool-idf 1.0.1
- tool-mconf 1.4060000.20190628 (406.0.0)
- tool-mkspiffs 2.230.0 (2.30)
- tool-ninja 1.9.0
- toolchain-esp32ulp 1.22851.191205 (2.28.51)
- toolchain-xtensa32 2.80400.210211 (8.4.0)
Reading CMake configuration...
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 0 compatible libraries
Scanning dependencies...
No dependencies
Building in release mode
Building SPIFFS image from 'data' directory to .pio\build\esp32dev\spiffs.bin
/favicon.ico
/HereIsText.txt
/testimg.png
/upload_script.html
Looking for upload port...
Auto-detected: COM16
Uploading .pio\build\esp32dev\spiffs.bin
esptool.py v3.1
Serial port COM16
Connecting......
Chip is ESP32-D0WD-V3 (revision 3)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 94:b9:7e:40:24:b8
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00207000 to 0x00306fff...
Compressed 1048576 bytes to 58234...
Writing at 0x00207000... (25 %)
Writing at 0x0020f666... (50 %)
Writing at 0x002189b9... (75 %)
Writing at 0x0022338b... (100 %)
Wrote 1048576 bytes (58234 compressed) at 0x00207000 in 7.7 seconds (effective 1085.1 kbit/s)...
Hash of data verified.
Leaving...
Hard resetting via RTS pin...
================================================================================= [SUCCESS] Took 23.57 seconds =================================================================================
However, when I attempt to use the following function (TAG is #defined as “file_server”):
esp_err_t init_spiffs(void)
{
ESP_LOGI(TAG, "Initializing SPIFFS");
esp_vfs_spiffs_conf_t conf = {
.base_path = "/spiffs",
.partition_label = NULL,
.max_files = 5, // This decides the maximum number of files that can be created on the storage
.format_if_mount_failed = true
};
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 (%s)", esp_err_to_name(ret));
}
return ESP_FAIL;
}
size_t total = 0, used = 0;
ret = esp_spiffs_info(NULL, &total, &used);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
return ESP_FAIL;
}
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
FILE* f = fopen("/spiffs/HereIsText.txt", "r");
if(f == NULL){
ESP_LOGE(TAG, "Failed to open BANANA file");
}
fclose(f);
return ESP_OK;
}
I get the resulting output:
␛[0;32mI (1570) file_server: Initializing SPIFFS␛[0m
␛[0;33mW (1580) SPIFFS: mount failed, -10025. formatting...␛[0m
␛[0;32mI (12630) file_server: Partition size: total: 901953, used: 0␛[0m
␛[0;31mE (12690) file_server: Failed to open BANANA file␛[0m
As shown, SPIFFS fails to mount and formats the memory, after which I am unable to load any of my files. As indicated by used: 0
, everything seems to get wiped. Am I missing a step somewhere?