esp-idf/examples/storage/spiffs
Ivan Grokhotkov b1bb90a596 Merge branch 'bugfix/partition_tables_update_note' into 'master'
global: update note in the partition tables

See merge request espressif/esp-idf!5636
2019-07-29 19:00:44 +08:00
..
main examples: use new component registration api 2019-06-21 19:53:29 +08:00
CMakeLists.txt cmake: make main a component again 2018-09-11 09:44:12 +08:00
Makefile Add SPIFFS Component to IDF 2017-09-11 19:56:40 +03:00
README.md build system: Use CMake-based build system as default when describing commands 2019-07-08 17:31:27 +10:00
partitions_example.csv global: update note in the partition tables 2019-07-27 10:28:16 +02:00
sdkconfig.defaults examples: remove non-existent options from sdkconfig.defaults 2019-07-29 04:57:38 +02:00

README.md

SPIFFS example

(See the README.md file in the upper level 'examples' directory for more information about examples.)

This example demonstrates how to use SPIFFS with ESP32. Example does the following steps:

  1. Use an "all-in-one" esp_vfs_spiffs_register function to:
    • initialize SPIFFS,
    • mount SPIFFS filesystem using SPIFFS library (and format, if the filesystem can not be mounted),
    • register SPIFFS filesystem in VFS, enabling C standard library and POSIX functions to be used.
  2. Create a file using fopen and write to it using fprintf.
  3. Rename the file. Before renaming, check if destination file already exists using stat function, and remove it using unlink function.
  4. Open renamed file for reading, read back the line, and print it to the terminal.

SPIFFS partition size is set in partitions_example.csv file. See Partition Tables documentation for more information.

How to use example

Hardware required

This example does not require any special hardware, and can be run on any common development board.

Build and flash

Replace PORT with serial port name:

idf.py -p PORT flash monitor

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Example output

Here is an example console output. In this case format_if_mount_failed parameter was set to true in the source code. SPIFFS was unformatted, so the initial mount has failed. SPIFFS was then formatted, and mounted again.

I (324) example: Initializing SPIFFS
W (324) SPIFFS: mount failed, -10025. formatting...
I (19414) example: Partition size: total: 896321, used: 0
I (19414) example: Opening file
I (19504) example: File written
I (19544) example: Renaming file
I (19584) example: Reading file
I (19584) example: Read from file: 'Hello World!'
I (19584) example: SPIFFS unmounted

To erase the contents of SPIFFS partition, run idf.py erase_flash command. Then upload the example again as described above.