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

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:

  1. 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 to FATFS structure.
  2. Call ff_diskio_register function to register disk IO driver for the drive number used in step 1.
  3. Call f_mount function (and optionally f_fdisk, f_mkfs) to mount the filesystem using the same drive number which was passed to esp_vfs_fat_register. See FatFs documentation for more details.
  4. 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").
  5. Optionally, call FatFs library functions directly. Use paths without a VFS prefix in this case ("/hello.txt").
  6. Close all open files.
  7. Call f_mount function for the same drive number, with NULL FATFS* argument, to unmount the filesystem.
  8. Call ff_diskio_register with NULL ff_diskio_impl_t* argument and the same drive number.
  9. Call 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 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.

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.
  • 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
  • base_path: path prefix where FATFS should be registered
  • fat_drive: FATFS drive specification; if only one drive is used, can be an empty string
  • max_files: maximum number of files which can be open at the same time
  • out_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.

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
  • ESP_OK on success
  • ESP_ERR_INVALID_STATE if FATFS is not registered in VFS
  • 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

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 sdmmc_slot_config_t *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 SD/MMC peripheral with configuration in host_config
  • initializes SD/MMC card with configuration in slot_config
  • mounts FAT partition on SD/MMC 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.

  • 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 host, SDMMC protocol, or FATFS drivers
  • base_path: path where partition should be registered (e.g. “/sdcard”)
  • host_config: pointer to structure describing SDMMC host
  • slot_config: pointer to structure with extra SDMMC slot configuration
  • mount_config: pointer to structure with extra parameters for mounting FATFS
  • out_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.

esp_err_t esp_vfs_fat_sdmmc_unmount()

Unmount FAT filesystem and release resources acquired using esp_vfs_fat_sdmmc_mount.

  • ESP_OK on success
  • ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount hasn’t been called

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.

  • pdrv: drive number
  • discio_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

void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t *card)

Register SD/MMC diskio driver

  • pdrv: drive number
  • card: pointer to sdmmc_card_t structure describing a card; card should be initialized before calling f_mount.