esp-idf/docs/en/api-reference/storage/fatfs.rst

148 lines
8.6 KiB
ReStructuredText
Raw Normal View History

2017-01-08 15:09:09 -05:00
FAT Filesystem Support
======================
:link_to_translation:`zh_CN:[中文]`
2022-05-16 23:30:18 -04:00
ESP-IDF uses the `FatFs <http://elm-chan.org/fsw/ff/00index_e.html>`_ library to work with FAT filesystems. FatFs resides in the ``fatfs`` component. Although the library can be used directly, many of its features can be accessed via VFS using the C standard library and POSIX API functions.
2019-06-17 02:23:52 -04:00
Additionally, FatFs has been modified to support the runtime pluggable disk I/O layer. This allows mapping of FatFs drives to physical disks at runtime.
2017-01-08 15:09:09 -05:00
Using FatFs with VFS
--------------------
The header file :component_file:`fatfs/vfs/esp_vfs_fat.h` defines the functions for connecting FatFs and VFS.
2019-06-17 02:23:52 -04:00
The function :cpp:func:`esp_vfs_fat_register` allocates a ``FATFS`` structure and registers a given path prefix in VFS. Subsequent operations on files starting with this prefix are forwarded to FatFs APIs.
2019-06-17 02:23:52 -04:00
The function :cpp:func:`esp_vfs_fat_unregister_path` deletes the registration with VFS, and frees the ``FATFS`` structure.
2017-01-08 15:09:09 -05:00
2019-06-17 02:23:52 -04:00
Most applications use the following workflow when working with ``esp_vfs_fat_`` functions:
2017-01-08 15:09:09 -05:00
2019-06-17 02:23:52 -04:00
1. Call :cpp:func:`esp_vfs_fat_register` to specify:
2022-05-16 23:30:18 -04:00
- Path prefix where to mount the filesystem (e.g., ``"/sdcard"``, ``"/spiflash"``)
2019-06-17 02:23:52 -04:00
- FatFs drive number
- A variable which will receive the pointer to the ``FATFS`` structure
2017-01-08 15:09:09 -05:00
2019-06-17 02:23:52 -04:00
2. Call :cpp:func:`ff_diskio_register` to register the disk I/O driver for the drive number used in Step 1.
2017-01-08 15:09:09 -05:00
2023-02-07 23:02:17 -05:00
3. Call the FatFs function ``f_mount``, and optionally ``f_fdisk``, ``f_mkfs``, to mount the filesystem using the same drive number which was passed to :cpp:func:`esp_vfs_fat_register`. For more information, see `FatFs documentation <http://elm-chan.org/fsw/ff/doc/mount.html>`_.
2017-01-08 15:09:09 -05:00
4. Call the C standard library and POSIX API functions to perform such actions on files as open, read, write, erase, copy, etc. Use paths starting with the path prefix passed to :cpp:func:`esp_vfs_register` (for example, ``"/sdcard/hello.txt"``). The filesystem uses `8.3 filenames <https://en.wikipedia.org/wiki/8.3_filename>`_ format (SFN) by default. If you need to use long filenames (LFN), enable the :ref:`CONFIG_FATFS_LONG_FILENAMES` option. More details on the FatFs filenames are available `here <http://elm-chan.org/fsw/ff/doc/filename.html>`_.
2017-01-08 15:09:09 -05:00
2022-05-16 23:30:18 -04:00
5. Optionally, by enabling the option :ref:`CONFIG_FATFS_USE_FASTSEEK`, you can use the POSIX lseek function to perform it faster. The fast seek will not work for files in write mode, so to take advantage of fast seek, you should open (or close and then reopen) the file in read-only mode.
2017-01-08 15:09:09 -05:00
6. Optionally, call the FatFs library functions directly. In this case, use paths without a VFS prefix (for example, ``"/hello.txt"``).
2017-01-08 15:09:09 -05:00
7. Close all open files.
2017-01-08 15:09:09 -05:00
2022-05-16 23:30:18 -04:00
8. Call the FatFs function ``f_mount`` for the same drive number with NULL ``FATFS*`` argument to unmount the filesystem.
2017-01-08 15:09:09 -05:00
9. Call the FatFs function :cpp:func:`ff_diskio_register` with NULL ``ff_diskio_impl_t*`` argument and the same drive number to unregister the disk I/O driver.
10. Call :cpp:func:`esp_vfs_fat_unregister_path` with the path where the file system is mounted to remove FatFs from VFS, and free the ``FATFS`` structure allocated in Step 1.
2017-01-08 15:09:09 -05:00
2022-05-16 23:30:18 -04:00
The convenience functions ``esp_vfs_fat_sdmmc_mount``, ``esp_vfs_fat_sdspi_mount``, and ``esp_vfs_fat_sdcard_unmount`` wrap the steps described above and also handle SD card initialization. These functions are described in the next section.
2017-01-08 15:09:09 -05:00
.. doxygenfunction:: esp_vfs_fat_register
.. doxygenfunction:: esp_vfs_fat_unregister_path
2017-01-08 15:09:09 -05:00
2022-05-16 23:30:18 -04:00
Using FatFs with VFS and SD Cards
2017-01-08 15:09:09 -05:00
---------------------------------
2022-05-16 23:30:18 -04:00
The header file :component_file:`fatfs/vfs/esp_vfs_fat.h` defines convenience functions :cpp:func:`esp_vfs_fat_sdmmc_mount`, :cpp:func:`esp_vfs_fat_sdspi_mount`, and :cpp:func:`esp_vfs_fat_sdcard_unmount`. These functions perform Steps 13 and 79 respectively and handle SD card initialization, but provide only limited error handling. Developers are encouraged to check its source code and incorporate more advanced features into production applications.
2019-06-17 02:23:52 -04:00
The convenience function :cpp:func:`esp_vfs_fat_sdmmc_unmount` unmounts the filesystem and releases the resources acquired by :cpp:func:`esp_vfs_fat_sdmmc_mount`.
2017-01-08 15:09:09 -05:00
.. doxygenfunction:: esp_vfs_fat_sdmmc_mount
.. doxygenfunction:: esp_vfs_fat_sdmmc_unmount
.. doxygenfunction:: esp_vfs_fat_sdspi_mount
.. doxygenstruct:: esp_vfs_fat_mount_config_t
2017-01-08 15:09:09 -05:00
:members:
.. doxygenfunction:: esp_vfs_fat_sdcard_unmount
2017-01-08 15:09:09 -05:00
2022-05-16 23:30:18 -04:00
Using FatFs with VFS in Read-Only Mode
--------------------------------------
The header file :component_file:`fatfs/vfs/esp_vfs_fat.h` also defines the convenience functions :cpp:func:`esp_vfs_fat_spiflash_mount_ro` and :cpp:func:`esp_vfs_fat_spiflash_unmount_ro`. These functions perform Steps 1-3 and 7-9 respectively for read-only FAT partitions. These are particularly helpful for data partitions written only once during factory provisioning which will not be changed by production application throughout the lifetime of the hardware.
.. doxygenfunction:: esp_vfs_fat_spiflash_mount_ro
.. doxygenfunction:: esp_vfs_fat_spiflash_unmount_ro
2019-06-17 02:23:52 -04:00
2022-05-16 23:30:18 -04:00
FatFS Disk IO Layer
2017-01-08 15:09:09 -05:00
-------------------
2019-06-17 02:23:52 -04:00
FatFs has been extended with API functions that register the disk I/O driver at runtime.
2017-01-08 15:09:09 -05:00
2022-05-16 23:30:18 -04:00
These APIs provide implementation of disk I/O functions for SD/MMC cards and can be registered for the given FatFs drive number using the function :cpp:func:`ff_diskio_register_sdmmc`.
2017-01-08 15:09:09 -05:00
.. doxygenfunction:: ff_diskio_register
.. doxygenstruct:: ff_diskio_impl_t
:members:
.. doxygenfunction:: ff_diskio_register_sdmmc
.. doxygenfunction:: ff_diskio_register_wl_partition
.. doxygenfunction:: ff_diskio_register_raw_partition
2021-09-21 18:32:54 -04:00
2022-05-16 23:30:18 -04:00
FatFs Partition Generator
2021-09-21 18:32:54 -04:00
-------------------------
2022-05-16 23:30:18 -04:00
We provide a partition generator for FatFs (:component_file:`wl_fatfsgen.py<fatfs/wl_fatfsgen.py>`) which is integrated into the build system and could be easily used in the user project.
2021-09-21 18:32:54 -04:00
The tool is used to create filesystem images on a host and populate it with content of the specified host folder.
2022-05-16 23:30:18 -04:00
The script is based on the partition generator (:component_file:`fatfsgen.py<fatfs/fatfsgen.py>`). Apart from generating partition, it can also initialize wear levelling.
The latest version supports both short and long file names, FAT12 and FAT16. The long file names are limited to 255 characters and can contain multiple periods (``.``) characters within the filename and additional characters ``+``, ``,``, ``;``, ``=``, ``[`` and ``]``.
2021-09-21 18:32:54 -04:00
2022-05-16 23:30:18 -04:00
Build System Integration with FatFs Partition Generator
2021-09-21 18:32:54 -04:00
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2022-05-16 23:30:18 -04:00
It is possible to invoke FatFs generator directly from the CMake build system by calling ``fatfs_create_spiflash_image``::
fatfs_create_spiflash_image(<partition> <base_dir> [FLASH_IN_PROJECT])
2022-05-16 23:30:18 -04:00
If you prefer generating partition without wear levelling support, you can use ``fatfs_create_rawflash_image``::
2021-09-21 18:32:54 -04:00
fatfs_create_rawflash_image(<partition> <base_dir> [FLASH_IN_PROJECT])
2021-09-21 18:32:54 -04:00
``fatfs_create_spiflash_image`` respectively ``fatfs_create_rawflash_image`` must be called from project's CMakeLists.txt.
2022-05-16 23:30:18 -04:00
If you decide for any reason to use ``fatfs_create_rawflash_image`` (without wear levelling support), beware that it supports mounting only in read-only mode in the device.
2021-09-21 18:32:54 -04:00
The arguments of the function are as follows:
1. partition - the name of the partition as defined in the partition table (e.g. :example_file:`storage/fatfsgen/partitions_example.csv`).
2021-09-21 18:32:54 -04:00
2022-05-16 23:30:18 -04:00
2. base_dir - the directory that will be encoded to FatFs partition and optionally flashed into the device. Beware that you have to specify the suitable size of the partition in the partition table.
2021-09-21 18:32:54 -04:00
2022-05-16 23:30:18 -04:00
3. flag ``FLASH_IN_PROJECT`` - optionally, users can have the image automatically flashed together with the app binaries, partition tables, etc. on ``idf.py flash -p <PORT>`` by specifying ``FLASH_IN_PROJECT``.
2021-09-21 18:32:54 -04:00
4. flag ``PRESERVE_TIME`` - optionally, users can force preserving the timestamps from the source folder to the target image. Without preserving the time, every timestamp will be set to the FATFS default initial time (1st January 1980).
2021-09-21 18:32:54 -04:00
For example::
fatfs_create_spiflash_image(my_fatfs_partition my_folder FLASH_IN_PROJECT)
2021-09-21 18:32:54 -04:00
If FLASH_IN_PROJECT is not specified, the image will still be generated, but you will have to flash it manually using ``esptool.py`` or a custom build system target.
For an example, see :example:`storage/fatfsgen`.
2022-04-04 09:33:00 -04:00
2022-05-16 23:30:18 -04:00
FatFs Partition Analyzer
2022-04-04 09:33:00 -04:00
------------------------
(:component_file:`fatfsparse.py<fatfs/fatfsparse.py>`) is a partition analyzing tool for FatFs.
2022-04-04 09:33:00 -04:00
It is a reverse tool of (:component_file:`fatfsgen.py<fatfs/fatfsgen.py>`), i.e. it can generate the folder structure on the host based on the FatFs image.
2022-04-04 09:33:00 -04:00
Usage::
./fatfsparse.py [-h] [--long-name-support] [--wear-leveling] fatfs_image.img