FATFS Function Usage Notes

Keywords: IoT stm32

1.FRESULT f_opendir(DIR *dp,const TCHAR *path);
Function: Open a directory

parameterfunction
dpA pointer to an empty directory object structure that stores directory information about to be opened
pathRoute

The return value FR_OK(0) indicates that the function was executed successfully and the directory object structure was created for subsequent calls to the read directory.
The structure type of parameter dp is as follows:

typedef struct {
	_FDID	obj;			/* Object identifier ,There is an objsize record of the open file byte size*/
	DWORD	dptr;			/* Current read/write offset */
	DWORD	clust;			/* Current cluster */
	DWORD	sect;			/* Current sector */
	BYTE*	dir;			/* Pointer to the directory item in the win[] */
	BYTE*	fn;				/* Pointer to the SFN (in/out) {body[8],ext[3],status[1]} */
#if _USE_LFN != 0
	DWORD	blk_ofs;		/* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
	WCHAR*	lfn;			/* Pointer to the LFN working buffer */
#endif
#if _USE_FIND
	const TCHAR* pat;		/* Pointer to the name matching pattern */
#endif
} DIR;

2.FRESULT f_readdir(DIR *DirObject,FILINFO *FileInfo);
Function: Read a file in the directory and store the file information in FileInfo. If we call this function continuously after opening the directory, we can read the file from beginning to end in order, refresh the file information into FileInfo, and return an empty string to f_name[] of the file information object when there is no file to read at the endHere, you can determine if all the files in the directory have been read.

parameterfunction
DirObjectPoint to a directory object structure that exists (instantiates) (created by opening the directory function)
FileInfoPoints to an empty file information object structure, created by this function

The return value FR_OK(0) indicates that the function executed successfully.
FILINFO structure type:

typedef struct {
	FSIZE_t	fsize;			/* File size (in bytes) */
	WORD	fdate;			/* modification date */
	WORD	ftime;			/* Modification Time */
	BYTE	fattrib;		/* File Properties */
#if _USE_LFN != 0
	TCHAR	altname[13];			/* Altenative file name */
	TCHAR	fname[_MAX_LFN + 1];	/* Primary file name */
#else
	TCHAR	fname[13];		/* File name (up to 13 bytes) */
#endif
} FILINFO;

3.FRESULT f_closedir (DIR *dp);

parameterfunction
dpPointer to existing directory object structure, destroy directory object

4.FRESULT f_ open(FIL* FileObject, const TCHAR* FileName, BYTE ModeFlags);

parameterfunction
FileObjectA pointer to an empty file object structure, created by this function
FileNameExisting file name passed in by user
ModeFlagsThere are several modes to choose from: FA_READ, FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS, FA_CREATE_NEW. Several modes can exist through'or'simultaneously

FIL structure type:

typedef struct {
	_FDID	obj;		/* Object identifier */
	BYTE	flag;			/* File status flags */
	BYTE	err;			/* Abort flag (error code) */
	FSIZE_t	fptr;			/* File read/write pointer (File opens at 0) */
	DWORD	clust;		/* Current cluster of fpter (not valid when fprt is 0) */
	DWORD	sect;			/* Sector number appearing in buf[] (0:invalid) */
#if !_FS_READONLY
	DWORD	dir_sect;		/* Sector number containing the directory entry */
	BYTE*	dir_ptr;		/* Pointer to the directory entry in the win[] */
#endif
#if _USE_FASTSEEK
	DWORD*	cltbl;			/* Pointer to the cluster link map table (Nulled on file open) */
#endif
#if !_FS_TINY
	BYTE	buf[_MAX_SS];	/* File private data read/write window */
#endif
} FIL;

5.FRESULT f_read (FIL* fp,void* buff,UINT btr,UINT* br);

parameterfunction
fpPoint to an existing file object
buffPoint to data cache
btrThe number of bytes expected to be read
brPointer to UINT, the UINT variable holds the actual number of bytes read

The return value FR_OK(0) indicates that the function was successfully executed. Normally, after opening a file with the f_open function, the byte size of the file can be obtained from obj inside the FIL structure, which, combined with the size of Ram, reads the contents of the file once or several times with the file size known. The f_read function can be called several times in a row, when (*br)Equal to 0 means the file has been read. Note that f_open uses FA_READ mode before using it.
6.FRESULT f_write (FIL* fp,const void* buff,UINT btw,UINT* bw);

parameterfunction
fpPoint to an existing file object
buffPoint to data source array
btwThe number of bytes expected to be written
bwPointer to UINT, the UINT variable holds the actual number of bytes written

The return value FR_OK(0) indicates that the function executed successfully. bw bytes are written at the end of the file.
7.FRESULT f_lseek (FIL* fp,FSIZE_t ofs);

parameterfunction
fpPoint to an existing file object
ofsOffset (in bytes)

Return value FR_OK(0)Indicates that the function executed successfully. An application scenario can be as follows: open a file, call the function to offset the cursor by n bytes, then call the file write function or the file read function to read and write data, which means that writing a file does not necessarily need to be written at the end of the file, as long as the offset is adjusted by the f_lseek function, data can be written at a certain location in the file.Reading a file can skip the file information header and read the contents of the file directly.
8.FRESULT f_sync (FIL* fp);

parameterfunction
fpPoint to an existing file object

The return value FR_OK(0) indicates that the function executed successfully. The f_sync function performs the same processing as the f_close function, except that the file remains open after execution, the file object remains valid, and the file can continue to be read/written/moved; when the file is in long-term write mode,For example, calling this function periodically when data is recorded, or immediately after data is written, can reduce the loss caused by unexpected circumstances such as power outages.
9.FRESULT f_close (FIL* fp);

parameterfunction
FpPoint to an existing file object

The return value FR_OK(0) indicates that the function executed successfully. Close a file. When the open file function is executed, the f_close should be called to close the file without using the file.
10.FRESULT f_unlink (const TCHAR* path);

parameterfunction
pathPoint to a file or path

The return value FR_OK(0) indicates that the function executed successfully. Delete a file.
11.FRESULT f_mkdir (const TCHAR* path);

parameterfunction
pathFolder to create under path

The return value FR_OK(0) indicates that the function executed successfully. Creating a folder under a path, such as creating an empty folder named one under the root directory when the path is "0:/one". If you want to create a new file inside the empty folder, you can use the f_open function and select FA_CREATE_NEW for the mode.

Posted by carlosx2 on Sat, 18 Sep 2021 20:35:46 -0700