一、概述 相关的代码等等。 二、源代码的结构 件夹。文件夹是option,还有00readme.txt、diskio.c、diskio.h、ff.c、ff.h、integer.h。对比网上 的文章,版本已经不同了,已经没有所谓的tff.c和tff.h了,估计现在都采用条件编译解决这个问题了, 当然文件更少,可能编译选项可能越复杂。 2、00readme.txt的说明 上使用。我们移植时针对具体存储设备提供底层代码。 3、源代码阅读次序 后是diskio.h,了解与介质相关的数据结构和操作函数。再把ff.c和diskio.c两个文件所实现的函数大致 扫描一遍。最后根据用户应用层程序调用函数的次序仔细阅读相关代码。 三、源代码阅读 突的时候。 2、ff.h头文件 #define _CO 根据英文说明,这个数组用于unicode码和OEM码之间的相互转换。 ASCII,GB等)都不兼容。WindowsNT(2000)的内核即使用该编码,所有数据进入内核前转换成UNICODE,退 出内核后在转换成版本相关的编码(通常称为OEM,在简体中文版下即为GB).(百度所得) #define _USE_LFN 1 //这个估计是长文件名支持了,以前的0.06版本好像是不支持。 #define _FS_RPATH 0 //是否文件相对路径选项。 #define _FS_REENTRANT 0 //如果要支持文件系统可重入,必须加入几个函数。 OS_EVENT*, ID and etc.. */ #elif _CO /* Character co #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 da #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+\0)、属性、修改时间等。 接下来是函数的定义,先大概浏览一遍。 FRESULT f_mount (BYTE, FATFS*); //加载文件系统,BYTE参数是ID,后一个是文件系统定义。 FRESULT f_open (FIL*, const XCHAR*, BYTE);//打开文件,第一个参数是文件信息结构,第二个参数是文件名,第三是文件打开模式 FRESULT f_read (FIL*, void*, UINT, UINT*); //文件读取函数,参数1为文件对象(文件打开函数中得到),参数2为文件读取缓冲区,参数3为读取的字节数,参数4意义不清晰,等读到源代码就清楚了。 FRESULT f_write (FIL*, const void*, UINT, UINT*);//写文件,参数跟读差不多 FRESULT f_lseek (FIL*, DWORD); //移动文件的读写指针,参数2应该是移动的数目。 FRESULT f_close (FIL*); /* Close an open file object */ FRESULT f_opendir (DIR*, const XCHAR*); 打开目录,返回目录对象 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 da FRESULT f_unlink (const XCHAR*); 删除目录中的一个文件 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 da FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */ FRESULT f_chdir (const XCHAR*); /* Change current directory */改变当前目录 FRESULT f_chdrive (BYTE); /* Change current drive */ 应该说基本能明白这些函数用于干什么。
#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 //如果定义了重入,则需要实现以下四个函数 BOOL ff_cre_syncobj(BYTE, _SYNC_t*); 创建同步对象 BOOL ff_del_syncobj(_SYNC_t); 删除同步对象 BOOL ff_req_grant(_SYNC_t); 申请同步对象 void ff_rel_grant(_SYNC_t); 释放同步对象。 #endif 3、diskio.h文件 typedef BYTE DSTATUS; typedef DRESULT; //首先定义了两个变量,各个函数都有用到。
BOOL assign_drives (int argc, char *argv[]); //这个函数不知道干吗 DSTATUS disk_initialize (BYTE); //磁盘初始化 DSTATUS disk_status (BYTE); //获取磁盘状态 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*); //磁盘控制 接下来还有一些常数的定义,具体用到时在看。
4、diskio.c的结构 DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */) { DSTATUS stat; int result; switch (drv) { case ATA : result = ATA_disk_initialize(); // translate the reslut co return stat; case MMC : result = MMC_disk_initialize(); // translate the reslut co return stat; case USB : result = USB_disk_initialize(); // translate the reslut co return stat; } return STA_NOINIT; } 函数基本都像这样,drv表示磁盘的类型。没有实现,用户必须实现这部分代码。
5、ff.c文件简单浏览 #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; } //获取文件系统同步对象,不成功返回超时,成功,继续执行。 #define LEAVE_FF(fs, res) { unlock_fs(fs, res); return res; } //释放文件系统同步对象。
Static FATFS *FatFs[_DRIVES]; //定义一个文件系统对象指针数组,当然一般我们也就用到一个元素。 Static WORD LfnBuf[_MAX_LFN + 1]; //这个是与长文件名支持相关的。 #define NAMEBUF(sp,lp) BYTE sp[12]; WCHAR *lp = LfnBuf #define INITBUF(dj,sp,lp) dj.fn = sp; dj.lfn = lp
下面都是函数的定义,很多只在内部使用。 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++; } //接下来还定义了几个内存操作的函数,这个函数实现了从一块内存到另一块的复制,下面还有mem_set()对一块内存进行清0或设置操作;mem_cmp()比较内存的多个字节是否相同,相同返回0;chk_chr()检测字符串中是否存在某个字符,存在则返回该字符。
FRESULT move_window ( FATFS *fs, /* File system object */ DWORD sector /* Sector number to make apperance in the fs->win[] */ )//简单阅读了一下源代码,应该是改变文件系统的当前工作扇区,如果想要操作的扇区就是当前扇区,什么事不做;如果不是,则将原扇区写回;如果是FAT表,还得写入备份区。 这个函数内部使用,外部无法引用。
FRESULT sync ( /* FR_OK: successful, FR_DISK_ERR: failed */ FATFS *fs /* File system object */ )//这个函数用于更新FAT32文件系统的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; 获取簇号码对应的FAT扇区 return LD_DWORD(&fs->win[((WORD)clst * 4) & (SS(fs) - 1)]) & 0x0FFFFFFF; //这个函数应该是获取簇的下一个连接簇。 综合起来,这个函数应该是获取下一簇,感觉这个函数名起得不太好。get_nextcluster感觉更好一点。
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 */ )//上个函数是获取连接簇,这个是写入新的连接信息。
FRESULT remove_chain ( FATFS *fs, /* File system object */ DWORD clst /* Cluster# to remove a chain from */ )//将下一簇号写为0,也就是该文件的簇到此为止,同时系统的自由簇增加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. */ )//跟上一个相反,在该簇的位置写入新的下一簇簇号。 DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */ FATFS *fs, /* File system object */ DWORD clst /* Cluster# to be converted */ ) //这个函数是将簇号转变为对应的扇区号。 clst * fs->csize + fs->database; //这个是算法
FRESULT dir_seek ( DIR *dj, /* Pointer to directory object */ WORD idx /* Directory index number */ )//这个函数的最终目的是根据索引号找到目录项所在簇、所在扇区、并是目录对象的对象指针指向文件系统对象窗口扇区的对应位置。
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 / ) //移动当前目录项,根据索引,源代码简单看了一下,作用还不是很清晰,先放过。
接下来有5个函数与长文件名有关,这里先跳过。
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 */ ) //以上这些函数都是对目录项的操作函数。
FRESULT create_name ( DIR *dj, /* Pointer to the directory object */ const XCHAR **path /* Pointer to pointer to the segment in the path string */) //这个函数太长了,具体用到的时候再说吧。
void get_fileinfo ( /* No return co DIR *dj, /* Pointer to the directory object */ FILINFO *fno /* Pointer to store the file information */) 该函数用于获取文件状态信息。主要是从文件的目录项中获取信息。
FRESULT follow_path ( /* FR_OK(0): successful, !=0: error co DIR *dj, /* Directory object to return last directory and found object */ const XCHAR *path /* Full-path string to find a file or directory */ ) 该函数给定一个全路径,得到相应的目录对象。
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 */) 该函数用于读取BOOT扇区,检查是否FAT文件系统。
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 */) 这个函数的功能不太明白。
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 */ )//检查是否合法的文件系统。
FRESULT f_mount ( BYTE vol, /* Logical drive number to be mounted/unmounted */ FATFS *fs /* Pointer to new file system object (NULL for unmount)*/) 这是一个很重要的函数,装载文件系统。也是从这个函数开始,对外输出供用户调用。 if (vol >= _DRIVES)现在只支持卷号0. FatFs[vol] = fs;将参数文件系统对象指针赋给全局文件对象指针。
后面的函数主要是对文件和目录进行操作,这里就不一一例举了。 |
|