您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页FATFS简介

FATFS简介

来源:爱go旅游网
FATFS简介

stm32+sdio+fatfs⽂件系统 源码分析

⼀、概述1、⽬的

在移植之前,先将源代码⼤概的阅读⼀遍,主要是了解⽂件系统的结构、各个函数的功能和接⼝、与移植相关的代码等等。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+\\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 data of a writing file */将缓冲区数据写回⽂件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 data to the stream */ 这个函数还要提供⼀个回调函数。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 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;}

函数基本都像这样,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 code */

DIR *dj, /* Pointer to the directory object */ FILINFO *fno /* Pointer to store the file information */)该函数⽤于获取⽂件状态信息。主要是从⽂件的⽬录项中获取信息。

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 */)

该函数给定⼀个全路径,得到相应的⽬录对象。

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;将参数⽂件系统对象指针赋给全局⽂件对象指针。

后⾯的函数主要是对⽂件和⽬录进⾏操作,这⾥就不⼀⼀例举了。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务