新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > stm32+sdio+fatfs文件系統 源碼分析

        stm32+sdio+fatfs文件系統 源碼分析

        作者: 時間:2016-11-18 來源:網絡 收藏
        一、概述

        1、目的
        在移植之前,先將源代碼大概的閱讀一遍,主要是了解文件系統的結構、各個函數的功能和接口、與移植

        本文引用地址:http://www.104case.com/article/201611/315917.htm

        相關的代碼等等。
        2、準備工作
        在官方網站下載了0.07c版本的源代碼,利用記事本進行閱讀。

        二、源代碼的結構
        1、源代碼組成
        源代碼壓縮包解壓后,共兩個文件夾,doc是說明,src里就是代碼。src文件夾里共五個文件和一個文

        件夾。文件夾是option,還有00readme.txt、diskio.c、diskio.h、ff.c、ff.h、integer.h。對比網上

        的文章,版本已經不同了,已經沒有所謂的tff.c和tff.h了,估計現在都采用條件編譯解決這個問題了,

        當然文件更少,可能編譯選項可能越復雜。

        2、00readme.txt的說明
        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.主要是說不包含底層IO代碼,這是個通用文件系統可以在各種介質

        上使用。我們移植時針對具體存儲設備提供底層代碼。
        接下來做了版權聲明-可以自由使用和傳播。
        然后對版本的變遷做了說明。

        3、源代碼閱讀次序
        先讀integer.h,了解所用的數據類型,然后是ff.h,了解文件系統所用的數據結構和各種函數聲明,然

        后是diskio.h,了解與介質相關的數據結構和操作函數。再把ff.c和diskio.c兩個文件所實現的函數大致

        掃描一遍。最后根據用戶應用層程序調用函數的次序仔細閱讀相關代碼。

        三、源代碼閱讀
        1、integer.h頭文件
        這個文件主要是類型聲明。以下是部分代碼。
        typedef int INT;
        typedef unsigned int UINT;
        typedef signed char CHAR;/* These types must be 8-bit integer */
        都是用typedef做類型定義。移植時可以修改這部分代碼,特別是某些定義與你所在工程的類型定義有沖

        突的時候。

        2、ff.h頭文件
        以下是部分代碼的分析
        #include "integer.h" 使用integer.h的類型定義
        #ifndef _FATFS
        #define _FATFS 0x007C版本號007c,0.07c
        #define _WORD_ACCESS 0//如果定義為1,則可以使用word訪問。
        中間有一些看著說明很容易弄清楚意思。這里就不例舉了。

        #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)跟據這個中國應該是936.
        打開option文件夾看一下。打開cc936.c文件,里面有一個很大的數組static const WCHAR uni2oem[] 。

        根據英文說明,這個數組用于unicode碼和OEM碼之間的相互轉換。
        接下來又有兩個函數ff_convert()和ff_wtoupper()具體執行碼型轉換和將字符轉換為大寫。

        百度一下:看OEM碼什么意思。
        unicode是一種雙字節字符編碼,無論中文還是英文,或者其他語言統一到2個字節。與現有的任何編碼(

        ASCII,GB等)都不兼容。WindowsNT(2000)的內核即使用該編碼,所有數據進入內核前轉換成UNICODE,退

        出內核后在轉換成版本相關的編碼(通常稱為OEM,在簡體中文版下即為GB).(百度所得)
        繼續往下閱讀。

        #define _USE_LFN 1//這個估計是長文件名支持了,以前的0.06版本好像是不支持。
        #define _MAX_LFN 255//最長支持255個雙字節字符。

        #define _FS_RPATH 0//是否文件相對路徑選項。
        /* When _FS_RPATH is set to 1, relative path feature is enabled and f_chdir,
        / f_chdrive function are available. //有些函數會受影響。
        / Note that output of the f_readdir fnction is affected by this option. */

        #define _FS_REENTRANT 0//如果要支持文件系統可重入,必須加入幾個函數。
        #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
        接下來很大一部分都是與語言相關的因素,略過。

        /* Character code support macros */三個宏判斷是否大寫、小寫、數字。
        #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 */雙字節編碼相關的設定,暫時不理會它。

        #if _MULTI_PARTITION /* Multiple partition configuration */

        //該變量定義為1時,支持一個磁盤的多個分區。

        typedef struct _PARTITION {

        BYTE pd; /* Physical drive# */

        BYTE pt; /* Partition # (0-3) */

        } PARTITION;

        Extern const PARTITION Drives[];//如果支持分區,則聲明變量Drivers

        #define LD2PD(drv) (Drives[drv].pd)/* 獲得磁盤對應的物理磁盤

        #define LD2PT(drv) (Drives[drv].pt)/*獲得磁盤對應的分區

        #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//一般扇區長度取512字節。

        #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; /*對應實際驅動號01--- */

        BYTE csize; /* 每個簇的扇區數目 */

        先查一下簇的含義:應該是文件數據分配的基本單位。

        BYTE n_fats; /* 文件分配表的數目 */

        FAT文件系統依次應該是:引導扇區、文件分配表兩個、根目錄區和數據區。

        BYTE wflag; /* win[] dirty flag (1:must be written back) */

        //文件是否改動的標志,為1時要回寫。

        WORD id; /* File system mount ID 文件系統加載ID*/

        WORD n_rootdir; /* 根目錄區目錄項的數目 */

        #if _FS_REENTRANT

        _SYNC_t sobj; /* 允許重入,則定義同步對象 */

        #endif

        #if _MAX_SS != 512

        WORD s_size; /* Sector size */

        #endif

        #if !_FS_READONLY //文件為可寫

        BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */

        //文件需要回寫的標志

        DWORD last_clust; /* Last allocated cluster */

        DWORD free_clust; /* Number of free clusters */

        DWORD fsi_sector; /* fsinfo sector */

        #endif

        #if _FS_RPATH

        DWORD cdir; /* 使用相對路徑,則要存儲文件系統當前目錄

        #endif

        DWORD sects_fat; /*文件分配表占用的扇區

        DWORD max_clust; /* 最大簇數

        DWORD fatbase; /*文件分配表開始扇區

        DWORD dirbase; /* 如果是FAT32,根目錄開始扇區需要首先得到。

        DWORD database; /* 數據區開始扇區

        DWORD winsect; /* Current sector appearing in the win[] */

        //目前的扇區在win[]里面,這個win[]數組暫時還不知道含義。

        BYTE win[_MAX_SS];/* Disk access window for Directory/FAT */

        //這是一個win[512]數組,存儲著一個扇區,好像作為扇區緩沖使用。

        } FATFS;

        typedef struct _DIR_ {

        FATFS* fs;/* Pointer to the owner file system object */指向相應文件系統對象。

        WORD id; /* 文件系統加載ID*/

        WORD index; /* Current read/write index number */目前讀寫索引代碼

        DWORD sclust; /* Table start cluster (0:Static table) */文件數據區開始簇

        DWORD clust; /* Current cluster */ 目前處理的簇

        DWORD sect; /* Current sector */ 目前簇里對應的扇區

        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 */ 指向長文件名緩沖。

        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 */文件狀態標志

        BYTE csect; /* Sector address in the cluster */扇區偏移

        DWORD fptr; /* File R/W pointer */ 讀寫指針

        DWORD fsize; /* File size */

        DWORD org_clust; /* File start cluster */文件開始簇

        DWORD curr_clust; /* Current cluster */當前簇

        DWORD dsect; /* Current data sector */文件當前扇區

        #if !_FS_READONLY

        DWORD dir_sect; /* Sector containing the directory entry */該文件目錄項對應所在的扇區

        BYTE* dir_ptr; /* Ponter to the directory entry in the window */

        #endif

        #if !_FS_TINY

        BYTE buf[_MAX_SS];/* File R/W buffer */文件讀寫緩沖

        #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; 這個結構主要描述文件的狀態信息,包括文件名13個字符(8+.+3+

        主站蜘蛛池模板: 大丰市| 建水县| 托里县| 天柱县| 威海市| 商南县| 汾西县| 宜都市| 什邡市| 出国| 垫江县| 武安市| 舟山市| 正阳县| 云梦县| 张家港市| 泸溪县| 垦利县| 明溪县| 南昌县| 普陀区| 南汇区| 玉树县| 天等县| 将乐县| 大厂| 分宜县| 黄陵县| 诸暨市| 奇台县| 武穴市| 渝中区| 勃利县| 晴隆县| 牙克石市| 萍乡市| 北流市| 玉林市| 师宗县| 白河县| 南江县|