FATFS文件系统框架及源码分析
FATFS是一個(gè)為小型嵌入式系統(tǒng)設(shè)計(jì)的通用FAT(File Allocation Table)文件系統(tǒng)模塊。FatFs 的編寫(xiě)遵循ANSI C,并且完全與磁盤(pán)I/O層分開(kāi)。因此,它獨(dú)立(不依賴)于硬件架構(gòu)。它可以被嵌入到低成本的微控制器中,如AVR, 8051, PIC, ARM, Z80, 68K 等等,而不需要做任何修改。
1.FatFS文件系統(tǒng)包含了文件
ff.h:文件系統(tǒng)實(shí)現(xiàn)頭文件,定義有文件系統(tǒng)所需的數(shù)據(jù)結(jié)構(gòu)diskio.h:底層驅(qū)動(dòng)頭文件,就一些狀態(tài)宏的定義和底層驅(qū)動(dòng)函數(shù)的申明
integer.h:僅實(shí)現(xiàn)數(shù)據(jù)類(lèi)型重定義,增加系統(tǒng)的可移植性
ffconf.h:文件系統(tǒng)配置
ff.c:文件系統(tǒng)實(shí)現(xiàn)。
diskio.c :底層驅(qū)動(dòng)
2.源代碼閱讀次序:
先讀integer.h,了解所用的數(shù)據(jù)類(lèi)型,然后是ff.h,了解文件系統(tǒng)所用的數(shù)據(jù)結(jié)構(gòu)和各種函數(shù)聲明,
然后是diskio.h,了解與介質(zhì)相關(guān)的數(shù)據(jù)結(jié)構(gòu)和操作函數(shù)。
再把ff.c和diskio.c兩個(gè)文件所實(shí)現(xiàn)的函數(shù)大致掃描一遍。
最后根據(jù)用戶應(yīng)用層程序調(diào)用函數(shù)的次序仔細(xì)閱讀相關(guān)代碼。
3.FatFs 提供下面的函數(shù)API:
f_mount - 注冊(cè)/注銷(xiāo)一個(gè)工作區(qū)域(Work Area)
f_open - 打開(kāi)/創(chuàng)建一個(gè)文件f_close - 關(guān)閉一個(gè)文件
f_read - 讀文件f_write - 寫(xiě)文件
f_lseek - 移動(dòng)文件讀/寫(xiě)指針
f_truncate - 截?cái)辔募?
f_sync - 沖洗緩沖數(shù)據(jù) Flush Cached Data
f_opendir - 打開(kāi)一個(gè)目錄
f_readdir - 讀取目錄條目
f_getfree - 獲取空閑簇 Get Free Clusters
f_stat - 獲取文件狀態(tài)
f_mkdir - 創(chuàng)建一個(gè)目錄
f_unlink - 刪除一個(gè)文件或目錄
f_chmod - 改變屬性(Attribute)
f_utime - 改變時(shí)間戳(Timestamp)
f_rename - 重命名/移動(dòng)一個(gè)文件或文件夾
f_mkfs - 在驅(qū)動(dòng)器上創(chuàng)建一個(gè)文件系統(tǒng)
f_forward - 直接轉(zhuǎn)移文件數(shù)據(jù)到一個(gè)數(shù)據(jù)流 Forward file data to the stream directly
f_gets - 讀一個(gè)字符串
f_putc - 寫(xiě)一個(gè)字符
f_puts - 寫(xiě)一個(gè)字符串
f_printf - 寫(xiě)一個(gè)格式化的字符磁盤(pán)I/O接口
f_tell - 獲取當(dāng)前讀/寫(xiě)指針
f_eof - 測(cè)試一個(gè)文件是否到達(dá)文件末尾
f_size - 獲取一個(gè)文件大小
f_error - 測(cè)試一個(gè)文件是否出錯(cuò)
因?yàn)镕atFs模塊完全與磁盤(pán)I/O層分開(kāi),因此需要下面的函數(shù)來(lái)實(shí)現(xiàn)底層物理磁盤(pán)的讀寫(xiě)與獲取當(dāng)前時(shí)間。底層磁盤(pán)I/O模塊并不是FatFs的一部分,并且必須由用戶提供。
disk_initialize - Initialize disk drive 初始化磁盤(pán)驅(qū)動(dòng)器
disk_status - Get disk status 獲取磁盤(pán)狀態(tài)
disk_read - Read sector(s) 讀扇區(qū)
disk_write - Write sector(s) 寫(xiě)扇區(qū)
disk_ioctl - Control device dependent features 設(shè)備相關(guān)的控制特性
get_fattime - Get current time 獲取當(dāng)前時(shí)間
4.FatFS系統(tǒng)特性
打開(kāi)文件數(shù)量:無(wú)限制,與可用內(nèi)存有關(guān)。 卷(volume)數(shù)量:最多10個(gè)。?
文件大小:與FAT規(guī)范有關(guān)(最大4G-1字節(jié))。?
卷大小:與FAT規(guī)范有關(guān)(在512字節(jié)/扇區(qū)上,最大2T字節(jié))?
簇(Cluster)大小:與FAT規(guī)范有關(guān)(在512字節(jié)/扇區(qū)上,最大64K字節(jié)) 扇區(qū)(Sector)大小:與FAT規(guī)范有關(guān)(最大4K字節(jié))
5.
fatfs文件系統(tǒng)源碼分析
一、概述?
1、目的
在移植之前,先將源代碼大概的閱讀一遍,主要是了解文件系統(tǒng)的結(jié)構(gòu)、各個(gè)函數(shù)的功能和接口、與移植相關(guān)的代碼等等。
2、準(zhǔn)備工作
在官方網(wǎng)站下載了0.07c版本的源代碼,利用記事本進(jìn)行閱讀。
二、源代碼的結(jié)構(gòu)
1、源代碼組成
?? 源代碼壓縮包解壓后,共兩個(gè)文件夾,doc是說(shuō)明,src里就是代碼。src文件夾里共五個(gè)文件和一個(gè)文件夾。文件夾是option,還有00readme.txt、diskio.c、diskio.h、ff.c、ff.h、integer.h。對(duì)比網(wǎng)上的文章,版本已經(jīng)不同了,已經(jīng)沒(méi)有所謂的tff.c和tff.h了,估計(jì)現(xiàn)在都采用條件編譯解決這個(gè)問(wèn)題了,當(dāng)然文件更少,可能編譯選項(xiàng)可能越復(fù)雜。
2、00readme.txt的說(shuō)明
? Low level disk I/O module is not included in this archive because the FatFs module is only a generic file system layer and not depend on any specific storage device. You have to provide a low level disk I/O module that written to control your storage device.主要是說(shuō)不包含底層IO代碼,這是個(gè)通用文件系統(tǒng)可以在各種介質(zhì)上使用。我們移植時(shí)針對(duì)具體存儲(chǔ)設(shè)備提供底層代碼。接下來(lái)做了版權(quán)聲明-可以自由使用和傳播。然后對(duì)版本的變遷做了說(shuō)明。
3、源代碼閱讀次序
? 先讀integer.h,了解所用的數(shù)據(jù)類(lèi)型,然后是ff.h,了解文件系統(tǒng)所用的數(shù)據(jù)結(jié)構(gòu)和各種函數(shù)聲明,然后是diskio.h,了解與介質(zhì)相關(guān)的數(shù)據(jù)結(jié)構(gòu)和操作函數(shù)。再把ff.c和diskio.c兩個(gè)文件所實(shí)現(xiàn)的函數(shù)大致掃描一遍。最后根據(jù)用戶應(yīng)用層程序調(diào)用函數(shù)的次序仔細(xì)閱讀相關(guān)代碼。
三、源代碼閱讀
1、integer.h頭文件?
這個(gè)文件主要是類(lèi)型聲明。以下是部分代碼。
typedef int??? INT;
typedef unsigned int UINT;
typedef signed char? CHAR;/* These types must be 8-bit integer */
都是用typedef做類(lèi)型定義。移植時(shí)可以修改這部分代碼,特別是某些定義與你所在工程的類(lèi)型定義有沖突的時(shí)候。
2、ff.h頭文件
以下是部分代碼的分析
#include “integer.h”?使用integer.h的類(lèi)型定義
#ifndef _FATFS
#define _FATFS 0x007C??版本號(hào)007c,0.07c
#define _WORD_ACCESS 0?//如果定義為1,則可以使用word訪問(wèn)。中間有一些看著說(shuō)明很容易弄清楚意思。這里就不例舉了。
#define _CODE_PAGE 936
/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
/?? 936? – Simplified Chinese GBK (DBCS, OEM, Windows)跟據(jù)這個(gè)中國(guó)應(yīng)該是936.
打開(kāi)option文件夾看一下。打開(kāi)cc936.c文件,里面有一個(gè)很大的數(shù)組static const WCHAR uni2oem[] 。
根據(jù)英文說(shuō)明,這個(gè)數(shù)組用于unicode碼和OEM碼之間的相互轉(zhuǎn)換。
接下來(lái)又有兩個(gè)函數(shù)ff_convert()和ff_wtoupper()具體執(zhí)行碼型轉(zhuǎn)換和將字符轉(zhuǎn)換為大寫(xiě)。百度一下:看OEM碼什么意思。
unicode是一種雙字節(jié)字符編碼,無(wú)論中文還是英文,或者其他語(yǔ)言統(tǒng)一到2個(gè)字節(jié)。與現(xiàn)有的任何編碼(ASCII,GB等)都不兼容。WindowsNT(2000)的內(nèi)核即使用該編碼,所有數(shù)據(jù)進(jìn)入內(nèi)核前轉(zhuǎn)換成UNICODE,退出內(nèi)核后在轉(zhuǎn)換成版本相關(guān)的編碼(通常稱為OEM,在簡(jiǎn)體中文版下即為GB).(百度所得)
繼續(xù)往下閱讀。
#define _USE_LFN 1???//這個(gè)估計(jì)是長(zhǎng)文件名支持了,以前的0.06版本好像是不支持。
#define _MAX_LFN 255?//最長(zhǎng)支持255個(gè)雙字節(jié)字符。
#define _FS_RPATH 0??//是否文件相對(duì)路徑選項(xiàng)。
/* When _FS_RPATH is set to 1, relative path feature is enabled and f_chdir,
/? f_chdrive function are available.? //有些函數(shù)會(huì)受影響。
/? Note that output of the f_readdir fnction is affected by this option. */
#define _FS_REENTRANT 0??//如果要支持文件系統(tǒng)可重入,必須加入幾個(gè)函數(shù)。
#define _TIMEOUT? 1000?/* Timeout period in unit of time ticks of the OS */
#define?_SYNC_t?? HANDLE?/* Type of sync object used on the OS. e.g. HANDLE,
OS_EVENT*, ID and etc.. */
/* To make the FatFs module re-entrant, set _FS_REENTRANT to 1 and add user
/? provided synchronization handlers, ff_req_grant, ff_rel_grant, ff_del_syncobj
/??and ff_cre_syncobj function to the project. */
#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
#define _DF1S 0×81
#define _DF1E 0xFE
#define _DS1S 0×40
#define _DS1E 0x7E
#define _DS2S 0×80
#define _DS2E 0xFE
接下來(lái)很大一部分都是與語(yǔ)言相關(guān)的因素,略過(guò)。
/* Character code support macros */?三個(gè)宏判斷是否大寫(xiě)、小寫(xiě)、數(shù)字。
#define IsUpper(c) (((c)>=’A')&&((c)<=’Z'))
#define IsLower(c) (((c)>=’a')&&((c)<=’z'))
#define IsDigit(c) (((c)>=’0′)&&((c)<=’9′))
#if _DF1S???? /* DBCS configuration */雙字節(jié)編碼相關(guān)的設(shè)定,暫時(shí)不理會(huì)它。
#if _MULTI_PARTITION???????? /* Multiple partition configuration */
//該變量定義為1時(shí),支持一個(gè)磁盤(pán)的多個(gè)分區(qū)。
typedef struct _PARTITION {
?????? BYTE pd;???? /* Physical drive# */
?????? BYTE pt;????? /* Partition # (0-3) */
} PARTITION;
Extern? const? PARTITION Drives[];//如果支持分區(qū),則聲明變量Drivers??
#define LD2PD(drv) (Drives[drv].pd)??????/* 獲得磁盤(pán)對(duì)應(yīng)的物理磁盤(pán)
#define LD2PT(drv) (Drives[drv].pt)???????/*獲得磁盤(pán)對(duì)應(yīng)的分區(qū)
#else???????????????????????????????????????? /* Single partition configuration */
#define LD2PD(drv) (drv)? /* Physical drive# is equal to the logical drive# */
#define LD2PT(drv) 0??????? /* Always mounts the 1st partition */
#if _MAX_SS == 512??//一般扇區(qū)長(zhǎng)度取512字節(jié)。
#define?? SS(fs)???? 512U
#if _LFN_UNICODE && _USE_LFN
typedef WCHAR XCHAR;?????? /* Unicode */ XCHAR是文件名的碼型所用。
#else
typedef char XCHAR;??????? /* SBCS, DBCS */
#endif
typedef struct _FATFS_ {
?????? BYTE??? fs_type;???????? /* FAT sub type */
?????? BYTE??? drive;???????????? /*對(duì)應(yīng)實(shí)際驅(qū)動(dòng)號(hào)01— */
?????? BYTE??? csize;???????????? /* 每個(gè)簇的扇區(qū)數(shù)目?*/
先查一下簇的含義:應(yīng)該是文件數(shù)據(jù)分配的基本單位。
?????? BYTE??? n_fats;?????????? /*?文件分配表的數(shù)目?*/
FAT文件系統(tǒng)依次應(yīng)該是:引導(dǎo)扇區(qū)、文件分配表兩個(gè)、根目錄區(qū)和數(shù)據(jù)區(qū)。
?????? BYTE??? wflag;??????????? /* win[] dirty flag (1:must be written back) */
//文件是否改動(dòng)的標(biāo)志,為1時(shí)要回寫(xiě)。
?????? WORD? id;???????????????? /* File system mount ID 文件系統(tǒng)加載ID*/
?????? WORD? n_rootdir;????? /* 根目錄區(qū)目錄項(xiàng)的數(shù)目?*/
#if _FS_REENTRANT
?????? _SYNC_t???? sobj;????????????? /* 允許重入,則定義同步對(duì)象?*/
#endif
#if _MAX_SS != 512
?????? WORD? s_size;?????????? /* Sector size */
#endif
#if !_FS_READONLY? //文件為可寫(xiě)
?????? BYTE??? fsi_flag;?? /* fsinfo dirty flag (1:must be written back) */
//文件需要回寫(xiě)的標(biāo)志
?????? DWORD????? last_clust;????? /* Last allocated cluster */
?????? DWORD????? free_clust;????? /* Number of free clusters */
?????? DWORD????? fsi_sector;????? /* fsinfo sector */
#endif
#if _FS_RPATH
?????? DWORD????? cdir;????????????? /* 使用相對(duì)路徑,則要存儲(chǔ)文件系統(tǒng)當(dāng)前目錄
#endif
?????? DWORD????? sects_fat;?????? /*文件分配表占用的扇區(qū)
?????? DWORD????? max_clust;???? /* 最大簇?cái)?shù)
?????? DWORD????? fatbase;? /*文件分配表開(kāi)始扇區(qū)
?????? DWORD????? dirbase;? /*? 如果是FAT32,根目錄開(kāi)始扇區(qū)需要首先得到。
?????? DWORD????? database;?????? /* 數(shù)據(jù)區(qū)開(kāi)始扇區(qū)
?????? DWORD????? winsect;? /* Current sector appearing in the win[] */
//目前的扇區(qū)在win[]里面,這個(gè)win[]數(shù)組暫時(shí)還不知道含義。
?????? BYTE??? win[_MAX_SS];/* Disk access window for Directory/FAT */
//這是一個(gè)win[512]數(shù)組,存儲(chǔ)著一個(gè)扇區(qū),好像作為扇區(qū)緩沖使用。
} FATFS;
typedef struct _DIR_ {
?????? FATFS* fs;/* Pointer to the owner file system object */指向相應(yīng)文件系統(tǒng)對(duì)象。
?????? WORD? id;???????????????? /* 文件系統(tǒng)加載ID*/
?????? WORD? index;???? /* Current read/write index number */目前讀寫(xiě)索引代碼
?????? DWORD????? sclust;???? /* Table start cluster (0:Static table) */文件數(shù)據(jù)區(qū)開(kāi)始簇
?????? DWORD????? clust;???????????? /* Current cluster */ 目前處理的簇
?????? DWORD????? sect;????????????? /* Current sector */ 目前簇里對(duì)應(yīng)的扇區(qū)
?????? BYTE*? dir;? /* Pointer to the current SFN entry in the win[] */
?????? BYTE*? fn;???????????????? /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
#if _USE_LFN
?????? WCHAR*???? lfn;?? /* Pointer to the LFN working buffer */ 指向長(zhǎng)文件名緩沖。
?????? WORD? lfn_idx;?? /* Last matched LFN index number (0xFFFF:No LFN) */
#endif
} DIR;
typedef struct _FIL_ {
?????? FATFS* fs;????????????????? /* Pointer to the owner file system object */
?????? WORD? id;???????????????? /* Owner file system mount ID */
?????? BYTE??? flag;??????? /* File status flags */文件狀態(tài)標(biāo)志
?????? BYTE??? csect;??????????? /* Sector address in the cluster */扇區(qū)偏移
?????? DWORD????? fptr;??????? /* File R/W pointer */ 讀寫(xiě)指針
?????? DWORD????? fsize;????????????? /* File size */
?????? DWORD????? org_clust;????? /* File start cluster */文件開(kāi)始簇
?????? DWORD????? curr_clust;???? /* Current cluster */當(dāng)前簇
?????? DWORD????? dsect;??????????? /* Current data sector */文件當(dāng)前扇區(qū)
#if !_FS_READONLY
?????? DWORD????? dir_sect; /* Sector containing the directory entry */該文件目錄項(xiàng)對(duì)應(yīng)所在的扇區(qū)
?????? BYTE*? dir_ptr;?? /* Ponter to the directory entry in the window */
#endif
#if !_FS_TINY
?????? BYTE??? buf[_MAX_SS];/* File R/W buffer */文件讀寫(xiě)緩沖
#endif
} FIL;
/* File status structure */
typedef struct _FILINFO_ {
?????? DWORD????? fsize;????????????? /* File size */
?????? WORD? fdate;???????????? /* Last modified date */
?????? WORD? ftime;???????????? /* Last modified time */
?????? BYTE??? fattrib;??? /* Attribute */
?????? char fname[13];???? /* Short file name (8.3 format) */
#if _USE_LFN
?????? XCHAR*????? lfname;????????? /* Pointer to the LFN buffer */
?????? int?? lfsize;???????????? /* Size of LFN buffer [chrs] */
#endif
} FILINFO; 這個(gè)結(jié)構(gòu)主要描述文件的狀態(tài)信息,包括文件名13個(gè)字符(8+.+3+\0)、屬性、修改時(shí)間等。
接下來(lái)是函數(shù)的定義,先大概瀏覽一遍。
FRESULT f_mount (BYTE, FATFS*);??? //加載文件系統(tǒng),BYTE參數(shù)是ID,后一個(gè)是文件系統(tǒng)定義。
FRESULT f_open (FIL*, const XCHAR*, BYTE);//打開(kāi)文件,第一個(gè)參數(shù)是文件信息結(jié)構(gòu),第二個(gè)參數(shù)是文件名,第三是文件打開(kāi)模式
FRESULT f_read (FIL*, void*, UINT, UINT*);?? //文件讀取函數(shù),參數(shù)1為文件對(duì)象(文件打開(kāi)函數(shù)中得到),參數(shù)2為文件讀取緩沖區(qū),參數(shù)3為讀取的字節(jié)數(shù),參數(shù)4意義不清晰,等讀到源代碼就清楚了。
FRESULT f_write (FIL*, const void*, UINT, UINT*);//寫(xiě)文件,參數(shù)跟讀差不多
FRESULT f_lseek (FIL*, DWORD); //移動(dòng)文件的讀寫(xiě)指針,參數(shù)2應(yīng)該是移動(dòng)的數(shù)目。
FRESULT f_close (FIL*);??????????????? /* Close an open file object */
FRESULT f_opendir (DIR*, const XCHAR*);????? 打開(kāi)目錄,返回目錄對(duì)象
FRESULT f_readdir (DIR*, FILINFO*);????????????? 讀取目錄,獲得文件信息
FRESULT f_stat (const XCHAR*, FILINFO*);??????????????????????? /* Get file status */
FRESULT f_getfree (const XCHAR*, DWORD*, FATFS**);?? /* Get number of free clusters on the drive */
FRESULT f_truncate (FIL*);?????????????????? /* Truncate file */
FRESULT f_sync (FIL*);?? /* Flush cached data of a writing file */將緩沖區(qū)數(shù)據(jù)寫(xiě)回文件
FRESULT f_unlink (const XCHAR*);??????????? 刪除目錄中的一個(gè)文件
FRESULT???? f_mkdir (const XCHAR*);??????? /* Create a new directory */
FRESULT f_chmod (const XCHAR*, BYTE, BYTE); /* Change attriburte of the file/dir */
FRESULT f_utime (const XCHAR*, const FILINFO*);????? /* Change timestamp of the file/dir */
FRESULT f_rename (const XCHAR*, const XCHAR*);??? /* Rename/Move a file or directory */
FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */ 這個(gè)函數(shù)還要提供一個(gè)回調(diào)函數(shù)。
FRESULT f_mkfs (BYTE, BYTE, WORD);????????? /* Create a file system on the drive */
FRESULT f_chdir (const XCHAR*);????? /* Change current directory */改變當(dāng)前目錄
FRESULT f_chdrive (BYTE);?????????? /* Change current drive */
應(yīng)該說(shuō)基本能明白這些函數(shù)用于干什么。
#if _USE_STRFUNC
int f_putc (int, FIL*);??????????????????????????????????????????????????? /* Put a character to the file */
int f_puts (const char*, FIL*);?????????????????????????????????????? /* Put a string to the file */
int f_printf (FIL*, const char*, …);???????????????????????? /* Put a formatted string to the file */
char* f_gets (char*, int, FIL*);????????????????????????????? /* Get a string from the file */
#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
#if _FS_REENTRANT? //如果定義了重入,則需要實(shí)現(xiàn)以下四個(gè)函數(shù)
BOOL ff_cre_syncobj(BYTE, _SYNC_t*); 創(chuàng)建同步對(duì)象
BOOL ff_del_syncobj(_SYNC_t);? 刪除同步對(duì)象
BOOL ff_req_grant(_SYNC_t);? 申請(qǐng)同步對(duì)象
void ff_rel_grant(_SYNC_t); 釋放同步對(duì)象。
#endif
3、diskio.h文件
typedef BYTE????? DSTATUS;
typedef?? DRESULT;? //首先定義了兩個(gè)變量,各個(gè)函數(shù)都有用到。
BOOL assign_drives (int argc, char *argv[]); //這個(gè)函數(shù)不知道干嗎
DSTATUS disk_initialize (BYTE); //磁盤(pán)初始化
DSTATUS disk_status (BYTE); //獲取磁盤(pán)狀態(tài)
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
#if?? _READONLY == 0
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
#endif
DRESULT disk_ioctl (BYTE, BYTE, void*); //磁盤(pán)控制
接下來(lái)還有一些常數(shù)的定義,具體用到時(shí)在看。
4、diskio.c的結(jié)構(gòu)
DSTATUS disk_initialize (?? BYTE drv???? /* Physical drive nmuber (0..) */)
{
?????? DSTATUS stat;
?????? int result;
?????? switch (drv) {
?????? case ATA :
????????????? result = ATA_disk_initialize();
????????????? // translate the reslut code here
????????????? return stat;
?????? case MMC :
????????????? result = MMC_disk_initialize();
????????????? // translate the reslut code here
????????????? return stat;
?????? case USB :
????????????? result = USB_disk_initialize();
????????????? // translate the reslut code here
????????????? return stat;
?????? }
?????? return STA_NOINIT;
}
函數(shù)基本都像這樣,drv表示磁盤(pán)的類(lèi)型。沒(méi)有實(shí)現(xiàn),用戶必須實(shí)現(xiàn)這部分代碼。
5、ff.c文件簡(jiǎn)單瀏覽
#include “ff.h”???????????????????? /* FatFs configurations and declarations */
#include “diskio.h”????????????? /* Declarations of low level disk I/O functions */
#define?? ENTER_FF(fs)?????????? { if (!lock_fs(fs)) return FR_TIMEOUT; } //獲取文件系統(tǒng)同步對(duì)象,不成功返回超時(shí),成功,繼續(xù)執(zhí)行。
#define?? LEAVE_FF(fs, res)???? { unlock_fs(fs, res); return res; } //釋放文件系統(tǒng)同步對(duì)象。
Static? FATFS *FatFs[_DRIVES]; //定義一個(gè)文件系統(tǒng)對(duì)象指針數(shù)組,當(dāng)然一般我們也就用到一個(gè)元素。
Static WORD LfnBuf[_MAX_LFN + 1];? //這個(gè)是與長(zhǎng)文件名支持相關(guān)的。
#define?? NAMEBUF(sp,lp)????? BYTE sp[12]; WCHAR *lp = LfnBuf
#define INITBUF(dj,sp,lp) dj.fn = sp; dj.lfn = lp
下面都是函數(shù)的定義,很多只在內(nèi)部使用。
Static? void mem_cpy (void* dst, const void* src, int cnt) {
?????? char *d = (char*)dst;
?????? const char?*s = (const char *)src;
?????? while (cnt–) *d++ = *s++;
} //接下來(lái)還定義了幾個(gè)內(nèi)存操作的函數(shù),這個(gè)函數(shù)實(shí)現(xiàn)了從一塊內(nèi)存到另一塊的復(fù)制,下面還有mem_set()對(duì)一塊內(nèi)存進(jìn)行清0或設(shè)置操作;mem_cmp()比較內(nèi)存的多個(gè)字節(jié)是否相同,相同返回0;chk_chr()檢測(cè)字符串中是否存在某個(gè)字符,存在則返回該字符。
FRESULT move_window (
?????? FATFS *fs,?????????? /* File system object */
?????? DWORD sector?? /* Sector number to make apperance in the fs->win[] */
)//簡(jiǎn)單閱讀了一下源代碼,應(yīng)該是改變文件系統(tǒng)的當(dāng)前工作扇區(qū),如果想要操作的扇區(qū)就是當(dāng)前扇區(qū),什么事不做;如果不是,則將原扇區(qū)寫(xiě)回;如果是FAT表,還得寫(xiě)入備份區(qū)。
這個(gè)函數(shù)內(nèi)部使用,外部無(wú)法引用。
FRESULT sync (? /* FR_OK: successful, FR_DISK_ERR: failed */
?????? FATFS *fs???? /* File system object */
)//這個(gè)函數(shù)用于更新FAT32文件系統(tǒng)的FSI_Sector。什么含義還不太清楚。
DWORD get_fat (?????? /* 0xFFFFFFFF:Disk error, 1:Interal error, Else:Cluster status */
?????? FATFS *fs,??? /* File system object */
?????? DWORD clst?????? /* Cluster# to get the link information */
)
?????? if (move_window(fs, fsect + (clst / (SS(fs) / 4)))) break; 獲取簇號(hào)碼對(duì)應(yīng)的FAT扇區(qū)
?????? return LD_DWORD(&fs->win[((WORD)clst * 4) & (SS(fs) - 1)]) & 0x0FFFFFFF; //這個(gè)函數(shù)應(yīng)該是獲取簇的下一個(gè)連接簇。
綜合起來(lái),這個(gè)函數(shù)應(yīng)該是獲取下一簇,感覺(jué)這個(gè)函數(shù)名起得不太好。get_nextcluster感覺(jué)更好一點(diǎn)。
FRESULT put_fat (
?????? FATFS *fs,??? /* File system object */
?????? DWORD clst,????? /* Cluster# to be changed in range of 2 to fs->max_clust – 1 */
?????? DWORD val /* New value to mark the cluster */
)//上個(gè)函數(shù)是獲取連接簇,這個(gè)是寫(xiě)入新的連接信息。
FRESULT remove_chain (
?????? FATFS *fs,????????????????? /* File system object */
?????? DWORD clst???????????????????? /* Cluster# to remove a chain from */
)//將下一簇號(hào)寫(xiě)為0,也就是該文件的簇到此為止,同時(shí)系統(tǒng)的自由簇增加1.
DWORD create_chain (???? /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
?????? FATFS *fs,????????????????? /* File system object */
?????? DWORD clst???????????????????? /* Cluster# to stretch. 0 means create a new chain. */
)//跟上一個(gè)相反,在該簇的位置寫(xiě)入新的下一簇簇號(hào)。
DWORD clust2sect (? /* !=0: Sector number, 0: Failed – invalid cluster# */
?????? FATFS *fs,?????????? /* File system object */
?????? DWORD clst????????????? /* Cluster# to be converted */
) //這個(gè)函數(shù)是將簇號(hào)轉(zhuǎn)變?yōu)閷?duì)應(yīng)的扇區(qū)號(hào)。
clst * fs->csize + fs->database; //這個(gè)是算法
FRESULT dir_seek (
?????? DIR *dj,??????? /* Pointer to directory object */
?????? WORD idx?????????? /* Directory index number */
)//這個(gè)函數(shù)的最終目的是根據(jù)索引號(hào)找到目錄項(xiàng)所在簇、所在扇區(qū)、并是目錄對(duì)象的對(duì)象指針指向文件系統(tǒng)對(duì)象窗口扇區(qū)的對(duì)應(yīng)位置。
FRESULT dir_next (?? /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not streach */
?????? DIR *dj,??????? /* Pointer to directory object */
?????? BOOL streach????? /* FALSE: Do not streach table, TRUE: Streach table if needed /
) //移動(dòng)當(dāng)前目錄項(xiàng),根據(jù)索引,源代碼簡(jiǎn)單看了一下,作用還不是很清晰,先放過(guò)。
接下來(lái)有5個(gè)函數(shù)與長(zhǎng)文件名有關(guān),這里先跳過(guò)。
FRESULT dir_find (
?????? DIR *dj??????????????? /* Pointer to the directory object linked to the file name */
)//
FRESULT dir_read (
?????? DIR *dj??????????????? /* Pointer to the directory object that pointing the entry to be read */
)
FRESULT dir_register (????? /* FR_OK:Successful, FR_DENIED:No free?entry or too many SFN collision, FR_DISK_ERR:Disk error */
?????? DIR *dj?????????????????????? /* Target directory with object name to be created */
)
FRESULT dir_remove (???? /* FR_OK: Successful, FR_DISK_ERR: A disk error */
?????? DIR *dj?????????????????????? /* Directory object pointing the entry to be removed */
)
//以上這些函數(shù)都是對(duì)目錄項(xiàng)的操作函數(shù)。
FRESULT create_name (
?????? DIR *dj,?????????????? /* Pointer to the directory object */
?????? const XCHAR **path? /* Pointer to pointer to the segment in the path string */)
//這個(gè)函數(shù)太長(zhǎng)了,具體用到的時(shí)候再說(shuō)吧。
void get_fileinfo (???????? /* No return code */
?????? DIR *dj,?????????????? /* Pointer to the directory object */
?????? FILINFO *fno????????? /* Pointer to store the file information */)
該函數(shù)用于獲取文件狀態(tài)信息。主要是從文件的目錄項(xiàng)中獲取信息。
FRESULT follow_path (???? /* FR_OK(0): successful, !=0: error code */
?????? DIR *dj,?????????????? /* Directory object to return last directory and found object */
?????? const XCHAR *path?? /* Full-path string to find a file or directory */
)
該函數(shù)給定一個(gè)全路徑,得到相應(yīng)的目錄對(duì)象。
BYTE check_fs (? /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
?????? FATFS *fs,??? /* File system object */
?????? DWORD sect????? /* Sector# (lba) to check if it is an FAT boot record or not */)
該函數(shù)用于讀取BOOT扇區(qū),檢查是否FAT文件系統(tǒng)。
FRESULT auto_mount (???? /* FR_OK(0): successful, !=0: any error occured */
?????? const XCHAR **path,?????? /* Pointer to pointer to the path name (drive number) */
?????? FATFS **rfs,????????????? /* Pointer to pointer to the found file system object */
?????? BYTE chk_wp?????????????????? /* !=0: Check media write protection for write access */)
這個(gè)函數(shù)的功能不太明白。
FRESULT validate (??? /* FR_OK(0): The object is valid, !=0: Invalid */
?????? FATFS *fs,?????????? /* Pointer to the file system object */
?????? WORD id?????????????????? /* Member id of the target object to be checked */
)//檢查是否合法的文件系統(tǒng)。
FRESULT f_mount (
?????? BYTE vol,??????????? /* Logical drive number to be mounted/unmounted */
?????? FATFS *fs??????????? /* Pointer to new file system object (NULL for unmount)*/)
這是一個(gè)很重要的函數(shù),裝載文件系統(tǒng)。也是從這個(gè)函數(shù)開(kāi)始,對(duì)外輸出供用戶調(diào)用。
if (vol >= _DRIVES)現(xiàn)在只支持卷號(hào)0.
FatFs[vol] = fs;將參數(shù)文件系統(tǒng)對(duì)象指針賦給全局文件對(duì)象指針。
后面的函數(shù)主要是對(duì)文件和目錄進(jìn)行操作,這里就不一一例舉了。
?
?
一、概述?
1、目的
在移植之前,先將源代碼大概的閱讀一遍,主要是了解文件系統(tǒng)的結(jié)構(gòu)、各個(gè)函數(shù)的功能和接口、與移植相關(guān)的代碼等等。
2、準(zhǔn)備工作
在官方網(wǎng)站下載了0.07c版本的源代碼,利用記事本進(jìn)行閱讀。
二、源代碼的結(jié)構(gòu)
1、源代碼組成
?? 源代碼壓縮包解壓后,共兩個(gè)文件夾,doc是說(shuō)明,src里就是代碼。src文件夾里共五個(gè)文件和一個(gè)文件夾。文件夾是option,還有00readme.txt、diskio.c、diskio.h、ff.c、ff.h、integer.h。對(duì)比網(wǎng)上的文章,版本已經(jīng)不同了,已經(jīng)沒(méi)有所謂的tff.c和tff.h了,估計(jì)現(xiàn)在都采用條件編譯解決這個(gè)問(wèn)題了,當(dāng)然文件更少,可能編譯選項(xiàng)可能越復(fù)雜。
2、00readme.txt的說(shuō)明
? Low level disk I/O module is not included in this archive because the FatFs module is only a generic file system layer and not depend on any specific storage device. You have to provide a low level disk I/O module that written to control your storage device.主要是說(shuō)不包含底層IO代碼,這是個(gè)通用文件系統(tǒng)可以在各種介質(zhì)上使用。我們移植時(shí)針對(duì)具體存儲(chǔ)設(shè)備提供底層代碼。接下來(lái)做了版權(quán)聲明-可以自由使用和傳播。然后對(duì)版本的變遷做了說(shuō)明。
3、源代碼閱讀次序
? 先讀integer.h,了解所用的數(shù)據(jù)類(lèi)型,然后是ff.h,了解文件系統(tǒng)所用的數(shù)據(jù)結(jié)構(gòu)和各種函數(shù)聲明,然后是diskio.h,了解與介質(zhì)相關(guān)的數(shù)據(jù)結(jié)構(gòu)和操作函數(shù)。再把ff.c和diskio.c兩個(gè)文件所實(shí)現(xiàn)的函數(shù)大致掃描一遍。最后根據(jù)用戶應(yīng)用層程序調(diào)用函數(shù)的次序仔細(xì)閱讀相關(guān)代碼。
三、源代碼閱讀
1、integer.h頭文件?
這個(gè)文件主要是類(lèi)型聲明。以下是部分代碼。
typedef int??? INT;
typedef unsigned int UINT;
typedef signed char? CHAR;/* These types must be 8-bit integer */
都是用typedef做類(lèi)型定義。移植時(shí)可以修改這部分代碼,特別是某些定義與你所在工程的類(lèi)型定義有沖突的時(shí)候。
2、ff.h頭文件
以下是部分代碼的分析
#include “integer.h”?使用integer.h的類(lèi)型定義
#ifndef _FATFS
#define _FATFS 0x007C??版本號(hào)007c,0.07c
#define _WORD_ACCESS 0?//如果定義為1,則可以使用word訪問(wèn)。中間有一些看著說(shuō)明很容易弄清楚意思。這里就不例舉了。
#define _CODE_PAGE 936
/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
/?? 936? – Simplified Chinese GBK (DBCS, OEM, Windows)跟據(jù)這個(gè)中國(guó)應(yīng)該是936.
打開(kāi)option文件夾看一下。打開(kāi)cc936.c文件,里面有一個(gè)很大的數(shù)組static const WCHAR uni2oem[] 。
根據(jù)英文說(shuō)明,這個(gè)數(shù)組用于unicode碼和OEM碼之間的相互轉(zhuǎn)換。
接下來(lái)又有兩個(gè)函數(shù)ff_convert()和ff_wtoupper()具體執(zhí)行碼型轉(zhuǎn)換和將字符轉(zhuǎn)換為大寫(xiě)。百度一下:看OEM碼什么意思。
unicode是一種雙字節(jié)字符編碼,無(wú)論中文還是英文,或者其他語(yǔ)言統(tǒng)一到2個(gè)字節(jié)。與現(xiàn)有的任何編碼(ASCII,GB等)都不兼容。WindowsNT(2000)的內(nèi)核即使用該編碼,所有數(shù)據(jù)進(jìn)入內(nèi)核前轉(zhuǎn)換成UNICODE,退出內(nèi)核后在轉(zhuǎn)換成版本相關(guān)的編碼(通常稱為OEM,在簡(jiǎn)體中文版下即為GB).(百度所得)
繼續(xù)往下閱讀。
#define _USE_LFN 1???//這個(gè)估計(jì)是長(zhǎng)文件名支持了,以前的0.06版本好像是不支持。
#define _MAX_LFN 255?//最長(zhǎng)支持255個(gè)雙字節(jié)字符。
#define _FS_RPATH 0??//是否文件相對(duì)路徑選項(xiàng)。
/* When _FS_RPATH is set to 1, relative path feature is enabled and f_chdir,
/? f_chdrive function are available.? //有些函數(shù)會(huì)受影響。
/? Note that output of the f_readdir fnction is affected by this option. */
#define _FS_REENTRANT 0??//如果要支持文件系統(tǒng)可重入,必須加入幾個(gè)函數(shù)。
#define _TIMEOUT? 1000?/* Timeout period in unit of time ticks of the OS */
#define?_SYNC_t?? HANDLE?/* Type of sync object used on the OS. e.g. HANDLE,
OS_EVENT*, ID and etc.. */
/* To make the FatFs module re-entrant, set _FS_REENTRANT to 1 and add user
/? provided synchronization handlers, ff_req_grant, ff_rel_grant, ff_del_syncobj
/??and ff_cre_syncobj function to the project. */
#elif _CODE_PAGE == 936 /* Simplified Chinese GBK */
#define _DF1S 0×81
#define _DF1E 0xFE
#define _DS1S 0×40
#define _DS1E 0x7E
#define _DS2S 0×80
#define _DS2E 0xFE
接下來(lái)很大一部分都是與語(yǔ)言相關(guān)的因素,略過(guò)。
/* Character code support macros */?三個(gè)宏判斷是否大寫(xiě)、小寫(xiě)、數(shù)字。
#define IsUpper(c) (((c)>=’A')&&((c)<=’Z'))
#define IsLower(c) (((c)>=’a')&&((c)<=’z'))
#define IsDigit(c) (((c)>=’0′)&&((c)<=’9′))
#if _DF1S???? /* DBCS configuration */雙字節(jié)編碼相關(guān)的設(shè)定,暫時(shí)不理會(huì)它。
#if _MULTI_PARTITION???????? /* Multiple partition configuration */
//該變量定義為1時(shí),支持一個(gè)磁盤(pán)的多個(gè)分區(qū)。
typedef struct _PARTITION {
?????? BYTE pd;???? /* Physical drive# */
?????? BYTE pt;????? /* Partition # (0-3) */
} PARTITION;
Extern? const? PARTITION Drives[];//如果支持分區(qū),則聲明變量Drivers??
#define LD2PD(drv) (Drives[drv].pd)??????/* 獲得磁盤(pán)對(duì)應(yīng)的物理磁盤(pán)
#define LD2PT(drv) (Drives[drv].pt)???????/*獲得磁盤(pán)對(duì)應(yīng)的分區(qū)
#else???????????????????????????????????????? /* Single partition configuration */
#define LD2PD(drv) (drv)? /* Physical drive# is equal to the logical drive# */
#define LD2PT(drv) 0??????? /* Always mounts the 1st partition */
#if _MAX_SS == 512??//一般扇區(qū)長(zhǎng)度取512字節(jié)。
#define?? SS(fs)???? 512U
#if _LFN_UNICODE && _USE_LFN
typedef WCHAR XCHAR;?????? /* Unicode */ XCHAR是文件名的碼型所用。
#else
typedef char XCHAR;??????? /* SBCS, DBCS */
#endif
typedef struct _FATFS_ {
?????? BYTE??? fs_type;???????? /* FAT sub type */
?????? BYTE??? drive;???????????? /*對(duì)應(yīng)實(shí)際驅(qū)動(dòng)號(hào)01— */
?????? BYTE??? csize;???????????? /* 每個(gè)簇的扇區(qū)數(shù)目?*/
先查一下簇的含義:應(yīng)該是文件數(shù)據(jù)分配的基本單位。
?????? BYTE??? n_fats;?????????? /*?文件分配表的數(shù)目?*/
FAT文件系統(tǒng)依次應(yīng)該是:引導(dǎo)扇區(qū)、文件分配表兩個(gè)、根目錄區(qū)和數(shù)據(jù)區(qū)。
?????? BYTE??? wflag;??????????? /* win[] dirty flag (1:must be written back) */
//文件是否改動(dòng)的標(biāo)志,為1時(shí)要回寫(xiě)。
?????? WORD? id;???????????????? /* File system mount ID 文件系統(tǒng)加載ID*/
?????? WORD? n_rootdir;????? /* 根目錄區(qū)目錄項(xiàng)的數(shù)目?*/
#if _FS_REENTRANT
?????? _SYNC_t???? sobj;????????????? /* 允許重入,則定義同步對(duì)象?*/
#endif
#if _MAX_SS != 512
?????? WORD? s_size;?????????? /* Sector size */
#endif
#if !_FS_READONLY? //文件為可寫(xiě)
?????? BYTE??? fsi_flag;?? /* fsinfo dirty flag (1:must be written back) */
//文件需要回寫(xiě)的標(biāo)志
?????? DWORD????? last_clust;????? /* Last allocated cluster */
?????? DWORD????? free_clust;????? /* Number of free clusters */
?????? DWORD????? fsi_sector;????? /* fsinfo sector */
#endif
#if _FS_RPATH
?????? DWORD????? cdir;????????????? /* 使用相對(duì)路徑,則要存儲(chǔ)文件系統(tǒng)當(dāng)前目錄
#endif
?????? DWORD????? sects_fat;?????? /*文件分配表占用的扇區(qū)
?????? DWORD????? max_clust;???? /* 最大簇?cái)?shù)
?????? DWORD????? fatbase;? /*文件分配表開(kāi)始扇區(qū)
?????? DWORD????? dirbase;? /*? 如果是FAT32,根目錄開(kāi)始扇區(qū)需要首先得到。
?????? DWORD????? database;?????? /* 數(shù)據(jù)區(qū)開(kāi)始扇區(qū)
?????? DWORD????? winsect;? /* Current sector appearing in the win[] */
//目前的扇區(qū)在win[]里面,這個(gè)win[]數(shù)組暫時(shí)還不知道含義。
?????? BYTE??? win[_MAX_SS];/* Disk access window for Directory/FAT */
//這是一個(gè)win[512]數(shù)組,存儲(chǔ)著一個(gè)扇區(qū),好像作為扇區(qū)緩沖使用。
} FATFS;
typedef struct _DIR_ {
?????? FATFS* fs;/* Pointer to the owner file system object */指向相應(yīng)文件系統(tǒng)對(duì)象。
?????? WORD? id;???????????????? /* 文件系統(tǒng)加載ID*/
?????? WORD? index;???? /* Current read/write index number */目前讀寫(xiě)索引代碼
?????? DWORD????? sclust;???? /* Table start cluster (0:Static table) */文件數(shù)據(jù)區(qū)開(kāi)始簇
?????? DWORD????? clust;???????????? /* Current cluster */ 目前處理的簇
?????? DWORD????? sect;????????????? /* Current sector */ 目前簇里對(duì)應(yīng)的扇區(qū)
?????? BYTE*? dir;? /* Pointer to the current SFN entry in the win[] */
?????? BYTE*? fn;???????????????? /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
#if _USE_LFN
?????? WCHAR*???? lfn;?? /* Pointer to the LFN working buffer */ 指向長(zhǎng)文件名緩沖。
?????? WORD? lfn_idx;?? /* Last matched LFN index number (0xFFFF:No LFN) */
#endif
} DIR;
typedef struct _FIL_ {
?????? FATFS* fs;????????????????? /* Pointer to the owner file system object */
?????? WORD? id;???????????????? /* Owner file system mount ID */
?????? BYTE??? flag;??????? /* File status flags */文件狀態(tài)標(biāo)志
?????? BYTE??? csect;??????????? /* Sector address in the cluster */扇區(qū)偏移
?????? DWORD????? fptr;??????? /* File R/W pointer */ 讀寫(xiě)指針
?????? DWORD????? fsize;????????????? /* File size */
?????? DWORD????? org_clust;????? /* File start cluster */文件開(kāi)始簇
?????? DWORD????? curr_clust;???? /* Current cluster */當(dāng)前簇
?????? DWORD????? dsect;??????????? /* Current data sector */文件當(dāng)前扇區(qū)
#if !_FS_READONLY
?????? DWORD????? dir_sect; /* Sector containing the directory entry */該文件目錄項(xiàng)對(duì)應(yīng)所在的扇區(qū)
?????? BYTE*? dir_ptr;?? /* Ponter to the directory entry in the window */
#endif
#if !_FS_TINY
?????? BYTE??? buf[_MAX_SS];/* File R/W buffer */文件讀寫(xiě)緩沖
#endif
} FIL;
/* File status structure */
typedef struct _FILINFO_ {
?????? DWORD????? fsize;????????????? /* File size */
?????? WORD? fdate;???????????? /* Last modified date */
?????? WORD? ftime;???????????? /* Last modified time */
?????? BYTE??? fattrib;??? /* Attribute */
?????? char fname[13];???? /* Short file name (8.3 format) */
#if _USE_LFN
?????? XCHAR*????? lfname;????????? /* Pointer to the LFN buffer */
?????? int?? lfsize;???????????? /* Size of LFN buffer [chrs] */
#endif
} FILINFO; 這個(gè)結(jié)構(gòu)主要描述文件的狀態(tài)信息,包括文件名13個(gè)字符(8+.+3+\0)、屬性、修改時(shí)間等。
接下來(lái)是函數(shù)的定義,先大概瀏覽一遍。
FRESULT f_mount (BYTE, FATFS*);??? //加載文件系統(tǒng),BYTE參數(shù)是ID,后一個(gè)是文件系統(tǒng)定義。
FRESULT f_open (FIL*, const XCHAR*, BYTE);//打開(kāi)文件,第一個(gè)參數(shù)是文件信息結(jié)構(gòu),第二個(gè)參數(shù)是文件名,第三是文件打開(kāi)模式
FRESULT f_read (FIL*, void*, UINT, UINT*);?? //文件讀取函數(shù),參數(shù)1為文件對(duì)象(文件打開(kāi)函數(shù)中得到),參數(shù)2為文件讀取緩沖區(qū),參數(shù)3為讀取的字節(jié)數(shù),參數(shù)4意義不清晰,等讀到源代碼就清楚了。
FRESULT f_write (FIL*, const void*, UINT, UINT*);//寫(xiě)文件,參數(shù)跟讀差不多
FRESULT f_lseek (FIL*, DWORD); //移動(dòng)文件的讀寫(xiě)指針,參數(shù)2應(yīng)該是移動(dòng)的數(shù)目。
FRESULT f_close (FIL*);??????????????? /* Close an open file object */
FRESULT f_opendir (DIR*, const XCHAR*);????? 打開(kāi)目錄,返回目錄對(duì)象
FRESULT f_readdir (DIR*, FILINFO*);????????????? 讀取目錄,獲得文件信息
FRESULT f_stat (const XCHAR*, FILINFO*);??????????????????????? /* Get file status */
FRESULT f_getfree (const XCHAR*, DWORD*, FATFS**);?? /* Get number of free clusters on the drive */
FRESULT f_truncate (FIL*);?????????????????? /* Truncate file */
FRESULT f_sync (FIL*);?? /* Flush cached data of a writing file */將緩沖區(qū)數(shù)據(jù)寫(xiě)回文件
FRESULT f_unlink (const XCHAR*);??????????? 刪除目錄中的一個(gè)文件
FRESULT???? f_mkdir (const XCHAR*);??????? /* Create a new directory */
FRESULT f_chmod (const XCHAR*, BYTE, BYTE); /* Change attriburte of the file/dir */
FRESULT f_utime (const XCHAR*, const FILINFO*);????? /* Change timestamp of the file/dir */
FRESULT f_rename (const XCHAR*, const XCHAR*);??? /* Rename/Move a file or directory */
FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */ 這個(gè)函數(shù)還要提供一個(gè)回調(diào)函數(shù)。
FRESULT f_mkfs (BYTE, BYTE, WORD);????????? /* Create a file system on the drive */
FRESULT f_chdir (const XCHAR*);????? /* Change current directory */改變當(dāng)前目錄
FRESULT f_chdrive (BYTE);?????????? /* Change current drive */
應(yīng)該說(shuō)基本能明白這些函數(shù)用于干什么。
#if _USE_STRFUNC
int f_putc (int, FIL*);??????????????????????????????????????????????????? /* Put a character to the file */
int f_puts (const char*, FIL*);?????????????????????????????????????? /* Put a string to the file */
int f_printf (FIL*, const char*, …);???????????????????????? /* Put a formatted string to the file */
char* f_gets (char*, int, FIL*);????????????????????????????? /* Get a string from the file */
#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
#if _FS_REENTRANT? //如果定義了重入,則需要實(shí)現(xiàn)以下四個(gè)函數(shù)
BOOL ff_cre_syncobj(BYTE, _SYNC_t*); 創(chuàng)建同步對(duì)象
BOOL ff_del_syncobj(_SYNC_t);? 刪除同步對(duì)象
BOOL ff_req_grant(_SYNC_t);? 申請(qǐng)同步對(duì)象
void ff_rel_grant(_SYNC_t); 釋放同步對(duì)象。
#endif
3、diskio.h文件
typedef BYTE????? DSTATUS;
typedef?? DRESULT;? //首先定義了兩個(gè)變量,各個(gè)函數(shù)都有用到。
BOOL assign_drives (int argc, char *argv[]); //這個(gè)函數(shù)不知道干嗎
DSTATUS disk_initialize (BYTE); //磁盤(pán)初始化
DSTATUS disk_status (BYTE); //獲取磁盤(pán)狀態(tài)
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
#if?? _READONLY == 0
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
#endif
DRESULT disk_ioctl (BYTE, BYTE, void*); //磁盤(pán)控制
接下來(lái)還有一些常數(shù)的定義,具體用到時(shí)在看。
4、diskio.c的結(jié)構(gòu)
DSTATUS disk_initialize (?? BYTE drv???? /* Physical drive nmuber (0..) */)
{
?????? DSTATUS stat;
?????? int result;
?????? switch (drv) {
?????? case ATA :
????????????? result = ATA_disk_initialize();
????????????? // translate the reslut code here
????????????? return stat;
?????? case MMC :
????????????? result = MMC_disk_initialize();
????????????? // translate the reslut code here
????????????? return stat;
?????? case USB :
????????????? result = USB_disk_initialize();
????????????? // translate the reslut code here
????????????? return stat;
?????? }
?????? return STA_NOINIT;
}
函數(shù)基本都像這樣,drv表示磁盤(pán)的類(lèi)型。沒(méi)有實(shí)現(xiàn),用戶必須實(shí)現(xiàn)這部分代碼。
5、ff.c文件簡(jiǎn)單瀏覽
#include “ff.h”???????????????????? /* FatFs configurations and declarations */
#include “diskio.h”????????????? /* Declarations of low level disk I/O functions */
#define?? ENTER_FF(fs)?????????? { if (!lock_fs(fs)) return FR_TIMEOUT; } //獲取文件系統(tǒng)同步對(duì)象,不成功返回超時(shí),成功,繼續(xù)執(zhí)行。
#define?? LEAVE_FF(fs, res)???? { unlock_fs(fs, res); return res; } //釋放文件系統(tǒng)同步對(duì)象。
Static? FATFS *FatFs[_DRIVES]; //定義一個(gè)文件系統(tǒng)對(duì)象指針數(shù)組,當(dāng)然一般我們也就用到一個(gè)元素。
Static WORD LfnBuf[_MAX_LFN + 1];? //這個(gè)是與長(zhǎng)文件名支持相關(guān)的。
#define?? NAMEBUF(sp,lp)????? BYTE sp[12]; WCHAR *lp = LfnBuf
#define INITBUF(dj,sp,lp) dj.fn = sp; dj.lfn = lp
下面都是函數(shù)的定義,很多只在內(nèi)部使用。
Static? void mem_cpy (void* dst, const void* src, int cnt) {
?????? char *d = (char*)dst;
?????? const char?*s = (const char *)src;
?????? while (cnt–) *d++ = *s++;
} //接下來(lái)還定義了幾個(gè)內(nèi)存操作的函數(shù),這個(gè)函數(shù)實(shí)現(xiàn)了從一塊內(nèi)存到另一塊的復(fù)制,下面還有mem_set()對(duì)一塊內(nèi)存進(jìn)行清0或設(shè)置操作;mem_cmp()比較內(nèi)存的多個(gè)字節(jié)是否相同,相同返回0;chk_chr()檢測(cè)字符串中是否存在某個(gè)字符,存在則返回該字符。
FRESULT move_window (
?????? FATFS *fs,?????????? /* File system object */
?????? DWORD sector?? /* Sector number to make apperance in the fs->win[] */
)//簡(jiǎn)單閱讀了一下源代碼,應(yīng)該是改變文件系統(tǒng)的當(dāng)前工作扇區(qū),如果想要操作的扇區(qū)就是當(dāng)前扇區(qū),什么事不做;如果不是,則將原扇區(qū)寫(xiě)回;如果是FAT表,還得寫(xiě)入備份區(qū)。
這個(gè)函數(shù)內(nèi)部使用,外部無(wú)法引用。
FRESULT sync (? /* FR_OK: successful, FR_DISK_ERR: failed */
?????? FATFS *fs???? /* File system object */
)//這個(gè)函數(shù)用于更新FAT32文件系統(tǒng)的FSI_Sector。什么含義還不太清楚。
DWORD get_fat (?????? /* 0xFFFFFFFF:Disk error, 1:Interal error, Else:Cluster status */
?????? FATFS *fs,??? /* File system object */
?????? DWORD clst?????? /* Cluster# to get the link information */
)
?????? if (move_window(fs, fsect + (clst / (SS(fs) / 4)))) break; 獲取簇號(hào)碼對(duì)應(yīng)的FAT扇區(qū)
?????? return LD_DWORD(&fs->win[((WORD)clst * 4) & (SS(fs) - 1)]) & 0x0FFFFFFF; //這個(gè)函數(shù)應(yīng)該是獲取簇的下一個(gè)連接簇。
綜合起來(lái),這個(gè)函數(shù)應(yīng)該是獲取下一簇,感覺(jué)這個(gè)函數(shù)名起得不太好。get_nextcluster感覺(jué)更好一點(diǎn)。
FRESULT put_fat (
?????? FATFS *fs,??? /* File system object */
?????? DWORD clst,????? /* Cluster# to be changed in range of 2 to fs->max_clust – 1 */
?????? DWORD val /* New value to mark the cluster */
)//上個(gè)函數(shù)是獲取連接簇,這個(gè)是寫(xiě)入新的連接信息。
FRESULT remove_chain (
?????? FATFS *fs,????????????????? /* File system object */
?????? DWORD clst???????????????????? /* Cluster# to remove a chain from */
)//將下一簇號(hào)寫(xiě)為0,也就是該文件的簇到此為止,同時(shí)系統(tǒng)的自由簇增加1.
DWORD create_chain (???? /* 0:No free cluster, 1:Internal error, 0xFFFFFFFF:Disk error, >=2:New cluster# */
?????? FATFS *fs,????????????????? /* File system object */
?????? DWORD clst???????????????????? /* Cluster# to stretch. 0 means create a new chain. */
)//跟上一個(gè)相反,在該簇的位置寫(xiě)入新的下一簇簇號(hào)。
DWORD clust2sect (? /* !=0: Sector number, 0: Failed – invalid cluster# */
?????? FATFS *fs,?????????? /* File system object */
?????? DWORD clst????????????? /* Cluster# to be converted */
) //這個(gè)函數(shù)是將簇號(hào)轉(zhuǎn)變?yōu)閷?duì)應(yīng)的扇區(qū)號(hào)。
clst * fs->csize + fs->database; //這個(gè)是算法
FRESULT dir_seek (
?????? DIR *dj,??????? /* Pointer to directory object */
?????? WORD idx?????????? /* Directory index number */
)//這個(gè)函數(shù)的最終目的是根據(jù)索引號(hào)找到目錄項(xiàng)所在簇、所在扇區(qū)、并是目錄對(duì)象的對(duì)象指針指向文件系統(tǒng)對(duì)象窗口扇區(qū)的對(duì)應(yīng)位置。
FRESULT dir_next (?? /* FR_OK:Succeeded, FR_NO_FILE:End of table, FR_DENIED:EOT and could not streach */
?????? DIR *dj,??????? /* Pointer to directory object */
?????? BOOL streach????? /* FALSE: Do not streach table, TRUE: Streach table if needed /
) //移動(dòng)當(dāng)前目錄項(xiàng),根據(jù)索引,源代碼簡(jiǎn)單看了一下,作用還不是很清晰,先放過(guò)。
接下來(lái)有5個(gè)函數(shù)與長(zhǎng)文件名有關(guān),這里先跳過(guò)。
FRESULT dir_find (
?????? DIR *dj??????????????? /* Pointer to the directory object linked to the file name */
)//
FRESULT dir_read (
?????? DIR *dj??????????????? /* Pointer to the directory object that pointing the entry to be read */
)
FRESULT dir_register (????? /* FR_OK:Successful, FR_DENIED:No free?entry or too many SFN collision, FR_DISK_ERR:Disk error */
?????? DIR *dj?????????????????????? /* Target directory with object name to be created */
)
FRESULT dir_remove (???? /* FR_OK: Successful, FR_DISK_ERR: A disk error */
?????? DIR *dj?????????????????????? /* Directory object pointing the entry to be removed */
)
//以上這些函數(shù)都是對(duì)目錄項(xiàng)的操作函數(shù)。
FRESULT create_name (
?????? DIR *dj,?????????????? /* Pointer to the directory object */
?????? const XCHAR **path? /* Pointer to pointer to the segment in the path string */)
//這個(gè)函數(shù)太長(zhǎng)了,具體用到的時(shí)候再說(shuō)吧。
void get_fileinfo (???????? /* No return code */
?????? DIR *dj,?????????????? /* Pointer to the directory object */
?????? FILINFO *fno????????? /* Pointer to store the file information */)
該函數(shù)用于獲取文件狀態(tài)信息。主要是從文件的目錄項(xiàng)中獲取信息。
FRESULT follow_path (???? /* FR_OK(0): successful, !=0: error code */
?????? DIR *dj,?????????????? /* Directory object to return last directory and found object */
?????? const XCHAR *path?? /* Full-path string to find a file or directory */
)
該函數(shù)給定一個(gè)全路徑,得到相應(yīng)的目錄對(duì)象。
BYTE check_fs (? /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */
?????? FATFS *fs,??? /* File system object */
?????? DWORD sect????? /* Sector# (lba) to check if it is an FAT boot record or not */)
該函數(shù)用于讀取BOOT扇區(qū),檢查是否FAT文件系統(tǒng)。
FRESULT auto_mount (???? /* FR_OK(0): successful, !=0: any error occured */
?????? const XCHAR **path,?????? /* Pointer to pointer to the path name (drive number) */
?????? FATFS **rfs,????????????? /* Pointer to pointer to the found file system object */
?????? BYTE chk_wp?????????????????? /* !=0: Check media write protection for write access */)
這個(gè)函數(shù)的功能不太明白。
FRESULT validate (??? /* FR_OK(0): The object is valid, !=0: Invalid */
?????? FATFS *fs,?????????? /* Pointer to the file system object */
?????? WORD id?????????????????? /* Member id of the target object to be checked */
)//檢查是否合法的文件系統(tǒng)。
FRESULT f_mount (
?????? BYTE vol,??????????? /* Logical drive number to be mounted/unmounted */
?????? FATFS *fs??????????? /* Pointer to new file system object (NULL for unmount)*/)
這是一個(gè)很重要的函數(shù),裝載文件系統(tǒng)。也是從這個(gè)函數(shù)開(kāi)始,對(duì)外輸出供用戶調(diào)用。
if (vol >= _DRIVES)現(xiàn)在只支持卷號(hào)0.
FatFs[vol] = fs;將參數(shù)文件系統(tǒng)對(duì)象指針賦給全局文件對(duì)象指針。
后面的函數(shù)主要是對(duì)文件和目錄進(jìn)行操作,這里就不一一例舉了。
?
?
總結(jié)
以上是生活随笔為你收集整理的FATFS文件系统框架及源码分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: FATFS里的FILINFO结构体详解
- 下一篇: 函数指针和函数指针数组及其应用