spiffs.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. SPIFFS Filesystem
  2. =================
  3. Overview
  4. --------
  5. SPIFFS is a file system intended for SPI NOR flash devices on embedded targets.
  6. It supports wear leveling, file system consistency checks and more.
  7. Notes
  8. -----
  9. - Currently, SPIFFS does not support directories. It produces a flat structure. If SPIFFS is mounted under ``/spiffs``, then creating a file with path ``/spiffs/tmp/myfile.txt`` will create a file called ``/tmp/myfile.txt`` in SPIFFS, instead of ``myfile.txt`` under directory ``/spiffs/tmp``.
  10. - It is not a realtime stack. One write operation might last much longer than another.
  11. - Currently, it does not detect or handle bad blocks.
  12. Tools
  13. -----
  14. spiffsgen.py
  15. ^^^^^^^^^^^^
  16. :component_file:`spiffsgen.py<spiffs/spiffsgen.py>` is a write-only Python SPIFFS implementation used to create filesystem
  17. images from the contents of a host folder. To use ``spiffsgen.py``, simply invoke it from your favorite terminal::
  18. python spiffsgen.py <image_size> <base_dir> <output_file>
  19. - image_size: size of the partition on which the created SPIFFS image will be flashed to
  20. - base_dir: directory to create the SPIFFS image of
  21. - output_file: SPIFFS image output file
  22. Besides the three required arguments: *image_size*, *base_dir* and *output_file*, there are other arguments
  23. that control image generation. Documentation on these arguments exist in the tool's help::
  24. python spiffsgen.py --help
  25. These optional arguments correspond to possible SPIFFS build configuration.
  26. User should make sure that the image is generated with the same arguments/configuration as
  27. SPIFFS was built with, else the user ends up with an invalid image. As a guide, the help output indicates the SPIFFS
  28. build configuration the argument corresponds to. In cases when these arguments
  29. are not specified, the default values shown in the help output are used.
  30. Once the image has been created, it can be flashed using ``esptool.py`` or ``parttool.py``.
  31. Aside from invoking ``spiffsgen.py`` standalone, it is also possible to use it directly from the build system by calling
  32. ``spiffs_create_partition_image``.
  33. Make::
  34. $(eval $(call spiffs_create_partition_image,<partition>,<base_dir>,[FLASH_IN_PROJECT]))
  35. CMake::
  36. spiffs_create_partition_image(<partition> <base_dir> [FLASH_IN_PROJECT])
  37. This is more convenient as the build configuration is automatically passed to the tool,
  38. ensuring that the image generated is valid for that build. An example of this is while the *image_size* is required
  39. for the standalone invocation, only the *partition* name is required when using ``spiffs_create_partition_image`` --
  40. the image size is automatically obtained from the project's partition table.
  41. It is important to note that due to the differences in structure between the two build systems,
  42. when using Make, ``spiffs_create_partition_image`` must be called from the project Makefile;
  43. for CMake, it should be called from one of the component CMakeLists.txt. For both build systems, the image will be created in the build directory
  44. with filename *partition*.bin.
  45. Optionally, user can opt to have the image automatically flashed together with the app binaries, partition tables, etc. on
  46. ``idf.py flash`` or ``make flash`` by specifying ``FLASH_IN_PROJECT``. For example::
  47. spiffs_create_partition_image(my_spiffs_partition my_folder FLASH_IN_PROJECT)
  48. If FLASH_IN_PROJECT is not specified, the image is still generated,
  49. but user has to flash it manually using ``esptool.py``, ``parttool.py`` or a custom build system target.
  50. For an example, see :example:`examples/storage/spiffsgen>`.
  51. mkspiffs
  52. ^^^^^^^^
  53. Another tool for creating SPIFS partition images is `mkspiffs <https://github.com/igrr/mkspiffs>`_.
  54. Like ``spiffsgen.py``, it can be used to create image from a given folder and then flash that image with ``esptool.py``
  55. To do that you need to obtain some parameters:
  56. - Block Size: 4096 (standard for SPI Flash)
  57. - Page Size: 256 (standard for SPI Flash)
  58. - Image Size: Size of the partition in bytes (can be obtained from partition table)
  59. - Partition Offset: Starting address of the partition (can be obtained from partition table)
  60. To pack a folder into 1 Megabyte image::
  61. mkspiffs -c [src_folder] -b 4096 -p 256 -s 0x100000 spiffs.bin
  62. To flash the image to ESP32 at offset 0x110000::
  63. python esptool.py --chip esp32 --port [port] --baud [baud] write_flash -z 0x110000 spiffs.bin
  64. Notes on which SPIFFS tool to use
  65. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  66. The two tools presented above offer very similar functionality. There are, however, reasons to prefer one
  67. over the other depending on the use case.
  68. If the intent is to simply generate a SPIFFS image during build, ``spiffsgen.py`` makes it very convenient
  69. by providing functions/commands from the build system itself. This makes it easy to generate SPIFFS images
  70. that match the build configuration and can be flashed together with the application.
  71. Another case for choosing ``spiffsgen.py`` is when the host has no C/C++ compiler available, since ``mkspiffs``
  72. requires compilation.
  73. On the other hand, ``mkspiffs`` offers unpacking SPIFFS images in addition to image generation. This is not
  74. possible with ``spiffsgen.py``, at least not yet. There might also be environments where a Python interpreter
  75. is not available, but a host compiler is or a pre-compiled ``mkspiffs`` binary
  76. can do the job. However, there is no build system integration for ``mkspiffs`` and the user has to
  77. do the corresponding work: compiling ``mkspiffs`` during build (if a pre-compiled binary is not used), creating build rules/targets
  78. for the output files, passing proper parameters to the tool, etc.
  79. See also
  80. --------
  81. - :doc:`Partition Table documentation <../../api-guides/partition-tables>`
  82. Application Example
  83. -------------------
  84. An example for using SPIFFS is provided in :example:`storage/spiffs` directory. This example initializes and mounts SPIFFS partition, and writes and reads data from it using POSIX and C library APIs. See README.md file in the example directory for more information.
  85. High level API Reference
  86. ------------------------
  87. * :component_file:`spiffs/include/esp_spiffs.h`
  88. .. include:: /_build/inc/esp_spiffs.inc