2022年FAT文件系统C语言接口 .pdf

上传人:Che****ry 文档编号:27189042 上传时间:2022-07-23 格式:PDF 页数:34 大小:356.74KB
返回 下载 相关 举报
2022年FAT文件系统C语言接口 .pdf_第1页
第1页 / 共34页
2022年FAT文件系统C语言接口 .pdf_第2页
第2页 / 共34页
点击查看更多>>
资源描述

《2022年FAT文件系统C语言接口 .pdf》由会员分享,可在线阅读,更多相关《2022年FAT文件系统C语言接口 .pdf(34页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、FAT File System Module FatFs module is an experimental project to implement a FAT file system to small embdded systems. The FatFs module is written in compliance with ANSI C, therefore it is independent of hardware architecture. It can be incorporated into most 8-bit microcontrollers, such as 8051,

2、PIC, AVR, H8, Z80 and etc., without any change. I created two modules in different configurations in consideration of various use. Features of FatFs Module 1. Separated buffer for FAT structure and each file, suitable for fast multiple file accsess. 2. Supports multiple drives/partitions. 3. Support

3、s FAT12, FAT16(+FAT64) and FAT32. (FAT64: FAT16 in 64KB/cluster)4. Supports 8.3 format file name and NT lower case flag. (LFN is not supported) 5. Supports two partitioning rules: FDISK and Super-floppy. 6. Optimized for 8/16-bit microcontrollers. Features of Tiny-FatFs Module (different to FatFs) 1

4、. Very low memory consumption, suitable for small memory system. (RAM:1KB) 2. Supports only single drive. Application Interface FatFs/Tiny-FatFs module provides following functions. f_mount - Register/Unregister a Work Area 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - -

5、- 第 1 页,共 34 页 - - - - - - - - - f_open - Open/Create a File f_close - Close a File f_read - Read File f_write - Write File f_lseek - Move R/W Pointer f_sync - Flush Cached Data f_opendir - Open a Directory f_readdir - Read a Directory Item f_getfree - Get Free Clusters f_stat - Get File Status f_mk

6、dir - Create a Directory f_unlink - Remove a File or Directory f_chmod - Change Attribute f_rename - Rename/Move a File or Directory f_mkfs - Create a File System on the Drive f_mount The f_mount fucntion registers/unregisters a work area to the FatFs module. FRESULT f_mount ( BYTE Drive, /* Logical

7、 drive number */ FATFS* FileSystemObject /* Pointer to the work area */ ); Parameters Drive Logical drive number (0-9) to register/unregister the work area. Always 0 for Tiny-FatFs. FileSystemObject Pointer to the work area (file system object) to be registered. Return Values FR_OK (0) The function

8、succeeded. FR_INV ALID_DRIVE The drive number is invalid. Description The f_mount function registers/unregisters a work area to the FatFs module. The work area must be given to the logical drive with this function before using any file 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理

9、- - - - - - - 第 2 页,共 34 页 - - - - - - - - - function. To unregister a work area, specify a NULL to the FileSystemObject , and then the work area can be discarded. This function only initializes the work area and registers its address to the internal table, any access to the disk I/O layer does not

10、occure. Actual mounting process is performed in any other file funcitons with path name when it is needed. References FATFSReturnf_open The f_open function creates a file object to be used to access the file. FRESULT f_open ( FIL* FileObject, /* Pointer to the blank file object structure */ const ch

11、ar* FileName, /* Pointer to the file neme */ BYTE ModeFlags/* Mode flags */ ); Parameters FileObject Pointer to the file object structure to be created. After the f_open funciton succeeded, the file can be accessed with the file object structure until it is closed. FileName Pointer to a null-termina

12、ted string that specifies the file name to create or open. ModeFlags Specifies the type of access and open method for the file. It is specified by a combination of following flags. ValueDescriptionFA_READSpecifies read access to the object. Data can be read from the file. Combine with FA_WRITE for r

13、ead-write access.FA_WRITESpecifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access.FA_OPEN_EXISTINGOpens the file. The function fails if the file is not existing.FA_OPEN_ALWAYSOpens the file, if it is existing. If not, the function creates the

14、new file.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 34 页 - - - - - - - - - FA_CREATE_NEWCreates a new file. The function fails if the file is already existing.FA_CREATE_ALWAYSCreates a new file. If the file is existing, it is truncated and overwritten.Retur

15、n Values FR_OK (0) The function succeeded and the file object is valid. FR_NO_FILE Could not find the file. FR_NO_PATH Could not find the path. FR_INV ALID_NAME The file name is invalid. FR_INV ALID_DRIVE The drive number is invalid. FR_EXIST The file is already existing. FR_DENIED The required acce

16、ss was denied due to any of following reasons: write mode open of a file that has read-only attribute, file creation under existing a same name directory or read-only file, cannot be created due to the directory table or disk full. FR_NOT_READY The disk drive cannot work due to no medium in the driv

17、e or any other reason. FR_WRITE_PROTECTED Write mode open or creation under the medium is write protected. FR_RW_ERROR The function failed due to a disk error or an internal error. FR_NOT_ENABLED The logical drive has no work area. FR_NO_FILESYSTEM There is no valid FAT partition on the disk. Descri

18、ption The created file object is used for subsequent calls to refer to the file. When close an open file object, use f_close function. Before using any file function, work area (file system object) must be given to each logical drive with f_mount function. All file functions can work after this proc

19、edure. The mode flags, FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW, FA_OPEN_ALWAYS, are not supported in read-only configuration. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 34 页 - - - - - - - - - Example (File Copy) void main () FATFS fs; / Work area (file sy

20、stem object) for logical drive FIL fsrc, fdst; / file objects BYTE buffer4096; / file copy buffer FRESULT res; / FatFs function common result code WORD br, bw; / File R/W count / Register a work area to logical drive 0 f_mount(0, &fs); / Open source file res = f_open(&fsrc, srcfile.dat, FA_OPEN_EXIS

21、TING | FA_READ); if (res) die(res); / Create destination file res = f_open(&fdst, dstfile.dat, FA_CREATE_ALWAYS | FA_WRITE); if (res) die(res); / Copy source to destination for (;) res = f_read(&fsrc, buffer, sizeof(buffer), &br); if (res | br = 0) break; / error or eof res = f_write(&fdst, buffer,

22、br, &bw); if (res | bw = 3. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 34 页 - - - - - - - - - Example / Move to offset of 5000 from top of the file. res = f_lseek(&file, 5000); / Forward 3000 bytes res = f_lseek(&file, file.fptr + 3000); / Rewind 2000 bytes

23、 (take care on overflow) res = f_lseek(&file, file.fptr - 2000); / Move to end of the file res = f_lseek(&file, file.fsize); References f_open, FILReturnf_sync The f_sync function flushes the cached information of a wriiting file. FRESULT f_sync ( FIL* FileObject/* Pointer to the file object */ ); P

24、arameters FileObject Pointer to the open file object to be flushed. Return Values FR_OK (0) The function succeeded. FR_RW_ERROR The function failed due to a disk error or an internal error. FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason. FR_INV ALID_OBJECT

25、The file object is invalid. Description 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 34 页 - - - - - - - - - The f_sync function performs the same process as f_close function but the file is left opened and can continue read/write/seek operations to the file.

26、 This is suitable for applications that open files for a long time in writing mode, such as data logger. Performing f_sync of periodic or immediataly after f_write can minimize risk of data loss due to sudden blackout or unintentional disk removal. This function is not supported in read-only configu

27、ration. References f_closeReturnf_opendir The f_opendir function opens a directory. FRESULT f_opendir ( DIR* DirObject, /* Pointer to the blank directory object structure */ const char* DirName/* Pointer to the directory name */ ); Parameter DirObject Pointer to the blank directory object to be crea

28、ted. DirName Pinter to the null-terminated string that specifies the directory name to be opened. Return Values FR_OK (0) The function succeeded and the directory object is created. It is used for subsequent calls to read the directory entries. FR_NO_FILE Could not find the directory. FR_NO_PATH Cou

29、ld not find the path. FR_INV ALID_NAME The path name is invalid. FR_INV ALID_DRIVE The drive number is invalid. FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 34 页 - - -

30、 - - - - - - FR_RW_ERROR The function failed due to a disk error or an internal error. FR_NOT_ENABLED The logical drive has no work area. FR_NO_FILESYSTEM There is no valid FAT partition on the disk. Description The f_opendir function opens an exsisting directory and creates the directory object for

31、 subsequent calls. The directory object structure can be discarded at any time without any procedure. This function is not supported in minimization level of =2. References f_readdir, DIRReturnf_readdir The f_readdir function reads directory entries. FRESULT f_readdir ( DIR* DirObject, /* Pointer to

32、 the directory object structure */ FILINFO* FileInfo/* Pointer to the file information structure */ ); Parameters DirObject Pointer to the open directory strcture. FileInfo Pointer to the file information structure to store the read item. Return Values FR_OK (0) The function succeeded. FR_NOT_READY

33、The disk drive cannot work due to no medium in the drive or any other reason. FR_RW_ERROR The function failed due to a disk error or an internal error. FR_INV ALID_OBJECT The directory object is invalid. 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 34 页 - -

34、- - - - - - - Description The f_readdir function reads directory entries in sequence. All items in the directory can be read by calling f_readdir function repeatedly. When all directory items have been read and no item to read, the function returns a null string into f_name member without any error.

35、 For details of the file informations, refer to the FILINFO. This function is not supported in minimization level of =2. Sample Code void scan_files (char* path) FILINFO finfo; DIR dirs; int i; if (f_opendir(&dirs, path) = FR_OK) i = strlen(path); while (f_readdir(&dirs, &finfo) = FR_OK) & finfo.fna

36、me0) if (finfo.fattrib & AM_DIR) sprintf(path+i, /%s, &finfo.fname0); scan_files(path); *(path+i) = 0; else printf(%s/%sn, path, &finfo.fname0); References f_opendir, f_stat, FILINFO , DIRReturnf_getfree The f_getfree function gets number of the free clusters. FRESULT f_getfree ( const char* Path, /

37、* Root directory of the drive */ DWORD* Clusters, /* Pointer to the variable to store number of free clusters */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 34 页 - - - - - - - - - FATFS* FileSystemObject/* Pointer to pointer to file system object */ ); Para

38、meters Path Pinter to the null-terminated string that specifies the root directory of the logical drive. Always specify a null-string for Tiny-FatFs. Clusters Pointer to the DWORD variable to store number of free clusters. FileSystemObject Pointer to the pointer that to be stored the pointer to corr

39、esponding file system object. Return Values FR_OK (0) The function succeeded. The *Clustershavs number of free clusters and *FileSystemObject points the file system object. FR_INV ALID_DRIVE The drive number is invalid. FR_NOT_READY The disk drive cannot work due to no medium in the drive or any oth

40、er reason. FR_RW_ERROR The function failed due to a disk error or an internal error. FR_NOT_ENABLED The logical drive has no work area. FR_NO_FILESYSTEM There is no valid FAT partition on the disk. Descriptions The f_getfree function gets number of free clusters on the drive. The sects_clust member

41、in the file system object refreting number of sectors per cluster, so that the free space in unit of sector can be calcurated with this. When _USE_FSINFO option is enabled, this function can return inaccurate free cluster count on FAT32 volume. When _USE_FSINFO option is disabled, this function will

42、 take a time on FAT32 volume. This function is not supported in read-only configuration and minimization level of = 1. Samples Code FATFS *fs; DWORD clust; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 34 页 - - - - - - - - - / Get free clusters res = f_getfre

43、e(, &clust, &fs); if (res) die(res); / Get free space printf(%lu KB total disk space.n %lu KB available on the disk.n, (DWORD)(fs-max_clust - 2) * fs-sects_clust / 2, clust * fs-sects_clust / 2); References FATFSReturnf_stat The f_stat gets the file status. FRESULT f_stat ( const char* FileName, /*

44、Pointer to the file or directory name */ FILINFO* FileInfo/* Pointer to the FILINFO structure */ ); Parameters FileName Pointer to the null-terminated string that specifies the file or directoryto get its information. FileInfo Pointer to the blank FILINFO structure to store the information. Return V

45、alues FR_OK (0) The function succeeded. FR_NO_FILE Could not find the file or directory. FR_NO_PATH Could not find the path. FR_INV ALID_NAME 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 34 页 - - - - - - - - - The file name is invalid. FR_INV ALID_DRIVE The

46、drive number is invalid. FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason. FR_RW_ERROR The function failed due to a disk error or an internal error. FR_NOT_ENABLED The logical drive has no work area. FR_NO_FILESYSTEM There is no valid FAT partition on the dis

47、k. Description The f_stat gets the information of a file or directory. For details of the infomation, refer to the FILINFO structure. This function is not supported in minimization level of = 1. References f_opendir, f_readdir, FILINFOReturnf_mkdir The f_mkdir function creates a new directory. FRESU

48、LT f_mkdir ( const char* DirName /* Pointer to the directory name */ ); Parameter DirName Pointer to the null-terminated string that specifies the directory name to create. Return Value FR_OK (0) The function succeeded. FR_NO_PATH Could not find the path. FR_INV ALID_NAME The path name is invalid. 名

49、师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 34 页 - - - - - - - - - FR_INV ALID_DRIVE The drive number is invalid. FR_DENIED The directory cannot be created due to directory table or disk is full. FR_EXIST A file or directory that has same name is already exi

50、sting. FR_NOT_READY The disk drive cannot work due to no medium in the drive or any other reason. FR_WRITE_PROTECTED The medium is write protected. FR_RW_ERROR The function failed due to a disk error or an internal error. FR_NOT_ENABLED The logical drive has no work area. FR_NO_FILESYSTEM There is n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高考资料

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁