FAT Filesystem Support¶
ESP-IDF uses FatFs library to work with FAT filesystems. FatFs library resides in fatfs
component. Although it can be used directly, many of its features can be accessed via VFS using C standard library and POSIX APIs.
Additionally, FatFs has been modified to support run-time pluggable disk IO layer. This allows mapping of FatFs drives to physical disks at run-time.
Using FatFs with VFS¶
fatfs/src/esp_vfs_fat.h header file defines functions to connect FatFs with VFS. esp_vfs_fat_register()
function 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. esp_vfs_fat_unregister_path()
function deletes the registration with VFS, and frees the FATFS
structure.
Most applications will use the following flow when working with esp_vfs_fat_
functions:
- Call
esp_vfs_fat_register()
, specifying path prefix where the filesystem has to be mounted (e.g."/sdcard"
,"/spiflash"
), FatFs drive number, and a variable which will receive a pointer toFATFS
structure. - Call
ff_diskio_register()
function to register disk IO driver for the drive number used in step 1. - Call FatFs
f_mount
function (and optionallyf_fdisk
,f_mkfs
) to mount the filesystem using the same drive number which was passed toesp_vfs_fat_register()
. See FatFs documentation for more details <http://www.elm-chan.org/fsw/ff/doc/mount.html>. - Call POSIX and C standard library functions to open, read, write, erase, copy files, etc. Use paths starting with the prefix passed to
esp_vfs_register()
(such as"/sdcard/hello.txt"
). - Optionally, call FatFs library functions directly. Use paths without a VFS prefix in this case (
"/hello.txt"
). - Close all open files.
- Call FatFs
f_mount
function for the same drive number, with NULLFATFS*
argument, to unmount the filesystem. - Call FatFs
ff_diskio_register()
with NULLff_diskio_impl_t*
argument and the same drive number. - Call
esp_vfs_fat_unregister_path()
with the path where the file system is mounted to remove FatFs from VFS, and free theFATFS
structure allocated on step 1.
Convenience functions, esp_vfs_fat_sdmmc_mount
and esp_vfs_fat_sdmmc_unmount
, which wrap these steps and also handle SD card initialization, are described in the next section.
-
esp_err_t
esp_vfs_fat_register
(const char *base_path, const char *fat_drive, size_t max_files, FATFS **out_fs)¶ Register FATFS with VFS component.
This function registers given FAT drive in VFS, at the specified base path. If only one drive is used, fat_drive argument can be an empty string. Refer to FATFS library documentation on how to specify FAT drive. This function also allocates FATFS structure which should be used for f_mount call.
- Note
- This function doesn’t mount the drive into FATFS, it just connects POSIX and C standard library IO function with FATFS. You need to mount desired drive into FATFS separately.
- Return
- ESP_OK on success
- ESP_ERR_INVALID_STATE if esp_vfs_fat_register was already called
- ESP_ERR_NO_MEM if not enough memory or too many VFSes already registered
- Parameters
base_path
: path prefix where FATFS should be registeredfat_drive
: FATFS drive specification; if only one drive is used, can be an empty stringmax_files
: maximum number of files which can be open at the same timeout_fs
: pointer to FATFS structure which can be used for FATFS f_mount call is returned via this argument.
-
esp_err_t
esp_vfs_fat_unregister_path
(const char *base_path)¶ Un-register FATFS from VFS.
- Note
- FATFS structure returned by esp_vfs_fat_register is destroyed after this call. Make sure to call f_mount function to unmount it before calling esp_vfs_fat_unregister_ctx. Difference between this function and the one above is that this one will release the correct drive, while the one above will release the last registered one
- Return
- ESP_OK on success
- ESP_ERR_INVALID_STATE if FATFS is not registered in VFS
- Parameters
base_path
: path prefix where FATFS is registered. This is the same used when esp_vfs_fat_register was called
Using FatFs with VFS and SD cards¶
fatfs/src/esp_vfs_fat.h header file also provides a convenience function to perform steps 1–3 and 7–9, and also handle SD card initialization: esp_vfs_fat_sdmmc_mount()
. This function does only limited error handling. Developers are encouraged to look at its source code and incorporate more advanced versions into production applications. esp_vfs_fat_sdmmc_unmount()
function unmounts the filesystem and releases resources acquired by esp_vfs_fat_sdmmc_mount()
.
-
esp_err_t
esp_vfs_fat_sdmmc_mount
(const char *base_path, const sdmmc_host_t *host_config, const void *slot_config, const esp_vfs_fat_mount_config_t *mount_config, sdmmc_card_t **out_card)¶ Convenience function to get FAT filesystem on SD card registered in VFS.
This is an all-in-one function which does the following:
- initializes SDMMC driver or SPI driver with configuration in host_config
- initializes SD card with configuration in slot_config
- mounts FAT partition on SD card using FATFS library, with configuration in mount_config
- registers FATFS library with VFS, with prefix given by base_prefix variable
This function is intended to make example code more compact. For real world applications, developers should implement the logic of probing SD card, locating and mounting partition, and registering FATFS in VFS, with proper error checking and handling of exceptional conditions.
- Return
- ESP_OK on success
- ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount was already called
- ESP_ERR_NO_MEM if memory can not be allocated
- ESP_FAIL if partition can not be mounted
- other error codes from SDMMC or SPI drivers, SDMMC protocol, or FATFS drivers
- Parameters
base_path
: path where partition should be registered (e.g. “/sdcard”)host_config
: Pointer to structure describing SDMMC host. When using SDMMC peripheral, this structure can be initialized using SDMMC_HOST_DEFAULT() macro. When using SPI peripheral, this structure can be initialized using SDSPI_HOST_DEFAULT() macro.slot_config
: Pointer to structure with slot configuration. For SDMMC peripheral, pass a pointer to sdmmc_slot_config_t structure initialized using SDMMC_SLOT_CONFIG_DEFAULT. For SPI peripheral, pass a pointer to sdspi_slot_config_t structure initialized using SDSPI_SLOT_CONFIG_DEFAULT.mount_config
: pointer to structure with extra parameters for mounting FATFSout_card
: if not NULL, pointer to the card information structure will be returned via this argument
-
struct
esp_vfs_fat_mount_config_t
¶ Configuration arguments for esp_vfs_fat_sdmmc_mount and esp_vfs_fat_spiflash_mount functions.
Public Members
-
bool
format_if_mount_failed
¶ If FAT partition can not be mounted, and this parameter is true, create partition table and format the filesystem.
-
int
max_files
¶ Max number of open files.
-
size_t
allocation_unit_size
¶ If format_if_mount_failed is set, and mount fails, format the card with given allocation unit size. Must be a power of 2, between sector size and 128 * sector size. For SD cards, sector size is always 512 bytes. For wear_levelling, sector size is determined by CONFIG_WL_SECTOR_SIZE option.
Using larger allocation unit size will result in higher read/write performance and higher overhead when storing small files.
Setting this field to 0 will result in allocation unit set to the sector size.
-
bool
Using FatFs with VFS in read-only mode¶
Convenience functions, esp_vfs_fat_rawflash_mount()
and esp_vfs_fat_rawflash_unmount()
, are provided by fatfs/src/esp_vfs_fat.h header file in order to perform steps 1-3 and 7-9 for read-only FAT partitions. These are particularly helpful for data partitions written only once during factory provisioning and need not be changed by production application throughout the lifetime.
-
esp_err_t
esp_vfs_fat_rawflash_mount
(const char *base_path, const char *partition_label, const esp_vfs_fat_mount_config_t *mount_config)¶ Convenience function to initialize read-only FAT filesystem and register it in VFS.
This is an all-in-one function which does the following:
- finds the partition with defined partition_label. Partition label should be configured in the partition table.
- mounts FAT partition using FATFS library
- registers FATFS library with VFS, with prefix given by base_prefix variable
- Note
- Wear levelling is not used when FAT is mounted in read-only mode using this function.
- Return
- ESP_OK on success
- ESP_ERR_NOT_FOUND if the partition table does not contain FATFS partition with given label
- ESP_ERR_INVALID_STATE if esp_vfs_fat_rawflash_mount was already called for the same partition
- ESP_ERR_NO_MEM if memory can not be allocated
- ESP_FAIL if partition can not be mounted
- other error codes from SPI flash driver, or FATFS drivers
- Parameters
base_path
: path where FATFS partition should be mounted (e.g. “/spiflash”)partition_label
: label of the partition which should be usedmount_config
: pointer to structure with extra parameters for mounting FATFS
-
esp_err_t
esp_vfs_fat_rawflash_unmount
(const char *base_path, const char *partition_label)¶ Unmount FAT filesystem and release resources acquired using esp_vfs_fat_rawflash_mount.
- Return
- ESP_OK on success
- ESP_ERR_INVALID_STATE if esp_vfs_fat_spiflash_mount hasn’t been called
- Parameters
base_path
: path where partition should be registered (e.g. “/spiflash”)partition_label
: label of partition to be unmounted
FatFS disk IO layer¶
FatFs has been extended with an API to register disk IO driver at runtime.
Implementation of disk IO functions for SD/MMC cards is provided. It can be registered for the given FatFs drive number using ff_diskio_register_sdmmc()
function.
-
void
ff_diskio_register
(BYTE pdrv, const ff_diskio_impl_t *discio_impl)¶ Register or unregister diskio driver for given drive number.
When FATFS library calls one of disk_xxx functions for driver number pdrv, corresponding function in discio_impl for given pdrv will be called.
- Parameters
pdrv
: drive numberdiscio_impl
: pointer to ff_diskio_impl_t structure with diskio functions or NULL to unregister and free previously registered drive
-
struct
ff_diskio_impl_t
¶ Structure of pointers to disk IO driver functions.
See FatFs documentation for details about these functions
Public Members
-
DSTATUS (*
init
)(BYTE pdrv)¶ disk initialization function
-
DSTATUS (*
status
)(BYTE pdrv)¶ disk status check function
-
DRESULT (*
read
)(BYTE pdrv, BYTE *buff, DWORD sector, UINT count)¶ sector read function
-
DRESULT (*
write
)(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)¶ sector write function
-
DRESULT (*
ioctl
)(BYTE pdrv, BYTE cmd, void *buff)¶ function to get info about disk and do some misc operations
-
DSTATUS (*
-
void
ff_diskio_register_sdmmc
(BYTE pdrv, sdmmc_card_t *card)¶ Register SD/MMC diskio driver
- Parameters
pdrv
: drive numbercard
: pointer to sdmmc_card_t structure describing a card; card should be initialized before calling f_mount.