FatFS API¶
This API provides an implementation of the FatFS filesystem. It creates an easy way to interact with files on a connected media device.
In order to use this API, the fatfs
object must be present in the project’s config.json file.
Additionally, either the SPI
or SDIO
child
object should be included to take full advantage of the library. An example is given below:
{
"fs": {
"fatfs": {
"driver": {
"sdio": {}
}
}
}
}
This would enable the FatFS API to interface with a direct SDIO connection to an SD card.
See the Kubos SD Example for an example of how to use this interface.
-
enum
KUBOS_CORE_FF::
FRESULT
¶ File function return code.
Values:
-
FR_OK
= 0¶ (0) Succeeded
-
FR_DISK_ERR
¶ (1) A hard error occurred in the low level disk I/O layer
-
FR_INT_ERR
¶ (2) Assertion failed
-
FR_NOT_READY
¶ (3) The physical drive cannot work
-
FR_NO_FILE
¶ (4) Could not find the file
-
FR_NO_PATH
¶ (5) Could not find the path
-
FR_INVALID_NAME
¶ (6) The path name format is invalid
-
FR_DENIED
¶ (7) Access denied due to prohibited access or directory full
-
FR_EXIST
¶ (8) Access denied due to prohibited access
-
FR_INVALID_OBJECT
¶ (9) The file/directory object is invalid
-
FR_WRITE_PROTECTED
¶ (10) The physical drive is write protected
-
FR_INVALID_DRIVE
¶ (11) The logical drive number is invalid
-
FR_NOT_ENABLED
¶ (12) The volume has no work area
-
FR_NO_FILESYSTEM
¶ (13) There is no valid FAT volume
-
FR_TIMEOUT
¶ (15) Could not get a grant to access the volume within defined period
-
FR_LOCKED
¶ (16) The operation is rejected according to the file sharing policy
-
FR_NOT_ENOUGH_CORE
¶ (17) LFN working buffer could not be allocated
-
FR_TOO_MANY_OPEN_FILES
¶ (18) Number of open files > _FS_LOCK
-
FR_INVALID_PARAMETER
¶ (19) Given parameter is invalid
-
-
typedef char
TCHAR
¶
-
typedef DWORD
FSIZE_t
¶ Type of file size variables and object identifier.
-
FRESULT
f_open
(FIL *fp, const TCHAR *path, BYTE mode)¶ Open (and/or) create a file.
- Return
- FRESULT
- Parameters
fp
: Pointer to filepath
: Path of file to openmode
: File open mode
-
FRESULT
f_read
(FIL *fp, void *buff, UINT btr, UINT *br)¶ Read data from a file.
- Return
- FRESULT
- Parameters
fp
: Pointer to filebuff
: Pointer to storage bufferbtr
: Number of bytes to readbr
: Number of bytes read
-
FRESULT
f_write
(FIL *fp, const void *buff, UINT btw, UINT *bw)¶ Write data to a file.
- Return
- FRESULT
- Parameters
fp
: Pointer to filebuff
: Pointer to storage bufferbtw
: Number of bytes to writebw
: Number of bytes written
-
FRESULT
f_lseek
(FIL *fp, FSIZE_t ofs)¶ Move file pointer of a file object.
- Return
- FRESULT
- Parameters
fp
: Pointer to fileofs
: Byte offset from top of the file to set read/write pointer
-
FRESULT
f_sync
(FIL *fp)¶ Flush cached information of a writing file.
- Return
- FRESULT
- Parameters
fp
: Pointer to file
-
FRESULT
f_opendir
(FAT_DIR *dp, const TCHAR *path)¶ Open file directory.
- Return
- FRESULT
- Parameters
dp
: Pointer to directory object structurepath
: Directory name
-
FRESULT
f_closedir
(FAT_DIR *dp)¶ Close file directory.
- Return
- FRESULT
- Parameters
dp
: Pointer to directory object structure
-
FRESULT
f_readdir
(FAT_DIR *dp, FILINFO *fno)¶ Read a directory item.
- Return
- FRESULT
- Parameters
dp
: Pointer to directory object structurefno
: Pointer to file information structure
-
FRESULT
f_findfirst
(FAT_DIR *dp, FILINFO *fno, const TCHAR *path, const TCHAR *pattern)¶ Find first file match in a directory.
- Return
- FRESULT
- Parameters
dp
: Pointer to blank directory objectfno
: Pointer to file information storage structurepath
: Pointer to the directory name to be openedpattern
: Pointer to the pattern to match
-
FRESULT
f_findnext
(FAT_DIR *dp, FILINFO *fno)¶ Find next file match in a directory.
- Return
- FRESULT
- Parameters
dp
: Pointer to directory objectfno
: Pointer to file information storage structure
-
FRESULT
f_mkdir
(const TCHAR *path)¶ Create a new directory.
- Return
- FRESULT
- Parameters
path
: Directory name to be created
-
FRESULT
f_unlink
(const TCHAR *path)¶ Delete an existing file or directory.
- Return
- FRESULT
- Parameters
path
: Name of file or directory to delete
-
FRESULT
f_rename
(const TCHAR *path_old, const TCHAR *path_new)¶ Rename/move an existing file or directory.
- Return
- FRESULT
- Parameters
path_old
: Current name of file or directorypath_new
: New name of file or directory
-
FRESULT
f_stat
(const TCHAR *path, FILINFO *fno)¶ Get file status.
- Return
- FRESULT
- Parameters
path
: Name of file to checkfno
: Pointer to file info structure
-
FRESULT
f_chmod
(const TCHAR *path, BYTE attr, BYTE mask)¶ Change attributes of a file or directory.
- Return
- FRESULT
- Parameters
path
: Name of file or directoryattr
: New attribute flags to setmask
: Mask specifying which attribute is changed
-
FRESULT
f_utime
(const TCHAR *path, const FILINFO *fno)¶ Change timestamp of file or directory.
- Return
- FRESULT
- Parameters
path
: Name of file/directoryfno
: Pointer to new file info structure
-
FRESULT
f_chdir
(const TCHAR *path)¶ Change current directory.
- Return
- FRESULT
- Parameters
path
: Directory to change to
-
FRESULT
f_chdrive
(const TCHAR *path)¶ Change current drive.
- Return
- FRESULT
- Parameters
path
: Drive to change to
-
FRESULT
f_getcwd
(TCHAR *buff, UINT len)¶ Get current directory.
- Return
- FRESULT
- Parameters
buff
: Buffer for returned path namelen
: Size of buffer
-
FRESULT
f_getfree
(const TCHAR *path, DWORD *nclst, FATFS **fatfs)¶ Get nubmer of free clusters on the drive.
- Return
- FRESULT
- Parameters
path
: Logical drive numbernclst
: Number of free clustersfatfs
: Pointer->pointer->pointer->filesystem object
-
FRESULT
f_getlabel
(const TCHAR *path, TCHAR *label, DWORD *vsn)¶ Get volume label.
- Return
- FRESULT
- Parameters
path
: Drive numberlabel
: Volume labelvsn
: Volume serial number
-
FRESULT
f_setlabel
(const TCHAR *label)¶ Set volume label.
- Return
- FRESULT
- Parameters
label
: Volume label
-
FRESULT
f_forward
(FIL *fp, UINT (*func)(const BYTE *, UINT), UINT btf, UINT *bf, )¶ Forward data to the data streaming device.
- Return
- FRESULT
- Parameters
fp
: Pointer to filefunc
: Data streaming functionbtf
: Number of bytes to forwardbf
: Number of bytes forwarded
-
FRESULT
f_expand
(FIL *fp, FSIZE_t szf, BYTE opt)¶ Allocate a contiguous block to the file.
- Return
- FRESULT
- Parameters
fp
: Pointer to fileszf
: Size to expand toopt
: Operation mode. Prepare only (0) or Allocate now (1).
-
FRESULT
f_mount
(FATFS *fs, const TCHAR *path, BYTE opt)¶ Mount/Unmount a logical drive.
- Return
- FRESULT
- Parameters
fs
: Pointer to file system objectpath
: Logical drive numberopt
: Initialization option. Don’t mount now (0) or force mount (1)
-
FRESULT
f_mkfs
(const TCHAR *path, BYTE sfd, UINT au)¶ Create a new volume on the logical drive.
- Return
- FRESULT
- Parameters
path
: Logical drive numbersfd
: Format optionsau
: Size of the allocation unit
-
FRESULT
f_fdisk
(BYTE pdrv, const DWORD szt[], void *work)¶ Divide a physical drive.
- Return
- FRESULT
- Parameters
pdrv
: Physical drive numberszt
: Partition map tablework
: Work area
-
int
f_putc
(TCHAR c, FIL *fp)¶ Put a character into a file.
- Return
- If successful, number of characters written; otherwise -1
- Parameters
c
: Character to putfp
: Pointer to file
-
int
f_puts
(const TCHAR *str, FIL *fp)¶ Write a string into a file.
- Return
- If successful, number of characters written; otherwise -1
- Parameters
str
: String to writefp
: Pointer to file
-
int
f_printf
(FIL *fp, const TCHAR *str, ...)¶ Write a formatted string into a file.
- Return
- If successful, number of characters written; otherwise -1
- Parameters
fp
: Pointer to filestr
: String to write...
: Optional arguments
-
TCHAR *
f_gets
(TCHAR *buff, int len, FIL *fp)¶ Read a string into a file.
- Return
- If successful, returns
buff
- Parameters
buff
: Read bufferlen
: Size of read bufferfp
: Pointer to file
-
_FATFS
88100¶ Revision ID.
-
LD2PD
(vol) (BYTE)(vol)¶ Definitions of volume management.
Each logical drive is bound to the same physical drive number
-
LD2PT
(vol) 0¶ Find first valid partition or in SFD.
-
_T
(x) x¶
-
_TEXT
(x) x¶
-
f_eof
(fp) ((int)((fp)->fptr == (fp)->obj.objsize))¶ Test for end-of-file on a file.
-
f_error
(fp) ((fp)->err)¶ Test for an error on a file.
-
f_tell
(fp) ((fp)->fptr)¶ Get current read/write pointer of a file.
-
f_size
(fp) ((fp)->obj.objsize)¶ Get size of a file.
-
EOF
(-1)¶ End-of-file indicator.
-
FA_READ
0x01¶ Specifies read access to the object.
Data can be read from the file.
-
FA_WRITE
0x02¶ Specifies write access to the object.
Data can be written to the file. Combine with FA_READ for read-write access.
-
FA_OPEN_EXISTING
0x00¶ Opens the file.
The function fails if the file is not existing. (Default)
-
FA_CREATE_NEW
0x04¶ Creates a new file.
The function fails with FR_EXIST if the file is existing.
-
FA_CREATE_ALWAYS
0x08¶ Creates a new file.
If the file is existing, it will be truncated and overwritten.
-
FA_OPEN_ALWAYS
0x10¶ Opens the file if it is existing.
If not, a new file will be created.
-
_FA_MODIFIED
0x20¶ Flag to mark file as modified.
-
_FA_DIRTY
0x40¶ Flag to mark file as dirty.
-
FS_FAT12
1¶ FAT12 file system.
-
FS_FAT16
2¶ FAT16 file system.
-
FS_FAT32
3¶ FAT32 file system.
-
FS_EXFAT
4¶ exFAT file system
-
AM_RDO
0x01¶ File attribute bit - Read only.
-
AM_HID
0x02¶ File attribute bit - Hidden.
-
AM_SYS
0x04¶ File attribute bit - System.
-
AM_VOL
0x08¶ File attribute bit - Volume label.
-
AM_LFN
0x0F¶ File attribute bit - LFN entry.
-
AM_DIR
0x10¶ File attribute bit - Directory.
-
AM_ARC
0x20¶ File attribute bit - Archive.
-
AM_MASK
0x3F¶ File attribute bit - Mask of defined bits.
-
struct
FATFS
¶ - #include <ff.h>
File system object structure (FATFS)
Public Members
-
BYTE
fs_type
¶ File system type (0:N/A)
-
BYTE
drv
¶ Physical drive number.
-
BYTE
n_fats
¶ Number of FATs (1 or 2)
-
BYTE
wflag
¶ win[] flag (b0:dirty)
-
BYTE
fsi_flag
¶ FSINFO flags (b7:disabled, b0:dirty)
-
WORD
id
¶ File system mount ID.
-
WORD
n_rootdir
¶ Number of root directory entries (FAT12/16)
-
WORD
csize
¶ Cluster size [sectors].
-
DWORD
last_clst
¶ Last allocated cluster.
-
DWORD
free_clst
¶ Number of free clusters.
-
DWORD
n_fatent
¶ Number of FAT entries (number of clusters + 2)
-
DWORD
fsize
¶ Size of an FAT [sectors].
-
DWORD
volbase
¶ Volume base sector.
-
DWORD
fatbase
¶ FAT base sector.
-
DWORD
dirbase
¶ Root directory base sector/cluster.
-
DWORD
database
¶ Data base sector.
-
DWORD
winsect
¶ Current sector appearing in the win[].
-
BYTE
win
[_MAX_SS]¶ Disk access window for Directory, FAT (and file data at tiny cfg)
-
BYTE
-
struct
FIL
¶ - #include <ff.h>
File object structure (FIL)
Public Members
-
BYTE
flag
¶ File status flags.
-
BYTE
err
¶ Abort flag (error code)
-
DWORD
clust
¶ Current cluster of fpter (not valid when fprt is 0)
-
DWORD
sect
¶ Sector number appearing in buf[] (0:invalid)
-
DWORD
dir_sect
¶ Sector number containing the directory entry.
-
BYTE *
dir_ptr
¶ Pointer to the directory entry in the win[].
-
BYTE
buf
[_MAX_SS]¶ File private data read/write window.
-
BYTE