以文本方式查看主題 - 曙海教育集團(tuán)論壇 (http://www.hufushizhe.com/bbs/index.asp) -- ARM技術(shù)討論專(zhuān)區(qū) (http://www.hufushizhe.com/bbs/list.asp?boardid=14) ---- ARM Cortex-M3文件系統(tǒng) 源碼分析 (http://www.hufushizhe.com/bbs/dispbbs.asp?boardid=14&id=1442) |
-- 作者:wangxinxin -- 發(fā)布時(shí)間:2010-11-18 9:03:08 -- ARM Cortex-M3文件系統(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ù)用戶(hù)應(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. OEM code page什么意思不大明白。 / 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)的編碼(通常稱(chēng)為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 0x81 #define _DF1E 0xFE #define _DS1S 0x40 #define _DS1E 0x7E #define _DS2S 0x80 #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ù)用于干什么。 |