FATFS学习

FATFS学习

一、数据类型

1.FATFS主要数据结构

  1. struct FATFS
/* File system object structure (FATFS) */
typedef struct {
	BYTE	fs_type;		/* FAT sub-type (0:Not mounted),FAT子类型,一般在mount时用,0:未挂载 */
	BYTE	drv;			/* Physical drive number,物理驱动器号,一般为0 */
	BYTE	csize;			/* Sectors per cluster (1,2,4...128) 每个簇的扇区数目 */
	BYTE	n_fats;			/* Number of FAT copies (1 or 2) 文件分配表的数目,FAT文件系统依次为:引导扇区、两个文件分配表、根目录区和数据区 */
	BYTE	wflag;			/* win[] flag (b0:dirty) 标记文件是否被改动过,为1时要回写 */
	BYTE	fsi_flag;		/* FSINFO flags (b7:disabled, b0:dirty) 标记文件系统信息是否被改动过,为1时要回写 */
	WORD	id;				/* File system mount ID文件系统挂载ID */
	WORD	n_rootdir;		/* Number of root directory entries (FAT12/16)根目录区入口(目录项)的个数(用于FAT12/16) */
#if _MAX_SS != _MIN_SS
	WORD	ssize;			/* Bytes per sector (512, 1024, 2048 or 4096)每扇区的字节数(用于扇区大于512的flash) */
#endif
#if _FS_REENTRANT
	_SYNC_t	sobj;			/* Identifier of sync object允许重入,即定义同步对象,用在tiny中 */
#endif
#if !_FS_READONLY           //是否只读开关 0:可读可写  1:只读
	DWORD	last_clust;		/* Last allocated cluster最后一个被分配的簇 */
	DWORD	free_clust;		/* Number of free clusters空闲簇数目 */
#endif
#if _FS_RPATH
	DWORD	cdir;			/* Current directory start cluster (0:root)允许相对路径时用,存储当前目录起始簇 */
#endif
	DWORD	n_fatent;		/* Number of FAT entries, = number of clusters + 2  FAT目录数(簇的数目+2) */
	DWORD	fsize;			/* Sectors per FAT */
	DWORD	volbase;		/* Volume start sector */
	DWORD	fatbase;		/* FAT start sector */
	DWORD	dirbase;		/* Root directory start sector (FAT32:Cluster#) 根目录起始扇区 */
	DWORD	database;		/* Data start sector 数据目录起始扇区 */
	DWORD	winsect;		/* Current sector appearing in the win[] 当前缓冲区中存储的扇区号 */
	BYTE	win[_MAX_SS];	/* Disk access window for Directory, FAT (and file data at tiny cfg) 单个扇区缓存 */
} FATFS;
  1. struct FIL
typedef struct {
	FATFS*	fs;				/* Pointer to the related file system object (**do not change order**) 所在的fs指针 */
	WORD	id;				/* Owner file system mount ID (**do not change order**) 所在的fs挂载编号 */
	BYTE	flag;			/* Status flags 文件状态 */
	BYTE	err;			/* Abort flag (error code) */
	DWORD	fptr;			/* File read/write pointer (Zeroed on file open) 文件读写指针 */
	DWORD	fsize;			/* File size 大小 */
	DWORD	sclust;			/* File start cluster (0:no cluster chain, always 0 when fsize is 0)  文件起始簇(fsize=0时为0) */
	DWORD	clust;			/* Current cluster of fpter (not valid when fprt is 0) 当前簇 */
	DWORD	dsect;			/* Sector number appearing in buf[] (0:invalid) 当前数据扇区 */
#if !_FS_READONLY
	DWORD	dir_sect;		/* Sector number containing the directory entry 包含目录项的扇区 */
	BYTE*	dir_ptr;		/* Pointer to the directory entry in the win[] */
#endif
#if _USE_FASTSEEK
	DWORD*	cltbl;			/* Pointer to the cluster link map table (Nulled on file open) 指向簇链接映射表的指针 */
#endif
#if _FS_LOCK
	UINT	lockid;			/* File lock ID origin from 1 (index of file semaphore table Files[]) */
#endif
#if !_FS_TINY
	BYTE	buf[_MAX_SS];	/* File private data read/write window */
#endif
} FIL;
  1. struct DIR
typedef struct {
	FATFS*	fs;				/* Pointer to the owner file system object (**do not change order**) */
	WORD	id;				/* Owner file system mount ID (**do not change order**) */
	WORD	index;			/* Current read/write index number 当前读写索引号 */
	DWORD	sclust;			/* Table start cluster (0:Root dir) 文件数据区开始簇 */
	DWORD	clust;			/* Current cluster 当前簇 */
	DWORD	sect;			/* Current sector */
	BYTE*	dir;			/* Pointer to the current SFN entry in the win[] 扇区缓存中当前SFN入口指针,SFN含义未知,猜测和LFN类似,与文件名相关 */
	BYTE*	fn;				/* Pointer to the SFN (in/out) {file[8],ext[3],status[1]}  */
#if _FS_LOCK
	UINT	lockid;			/* File lock ID (index of file semaphore table Files[]) */
#endif
#if _USE_LFN
	WCHAR*	lfn;			/* Pointer to the LFN working buffer */
	WORD	lfn_idx;		/* Last matched LFN index number (0xFFFF:No LFN) */
#endif
} DIR;

二、API解读

官网API目录
http://elm-chan.org/fsw/ff/00index_e.html

1.卷管理和系统配置

//初始化
res=f_mount(fs[0],"0:",1);              //挂载FLASH.
if(res == FR_NO_FILESYSTEM)				//挂载不成功,没有文件系统
{
    res = f_mkfs("0:", 1, 512);			//创建文件系统
    if(res == 0)
        res = f_setlabel((const TCHAR *)"0:ALIENTEK");    //设置Flash磁盘的名字为:ALIENTEK
}
  • f_mount:用于挂载文件系统,每次执行文件系统操作的时候,都要先执行这个函数。不过该函数只需要调用一次。

FRESULT f_mount (FATFS* fs , const TCHAR* path , BYTE opt)

fs:指向要注册和清除的文件系统对象的指针。空指针注销已注册的文件系统对象。
path:指向指定逻辑驱动器的以空字符结尾的字符串的指针。不带驱动器号的字符串表示默认驱动器。
opt:安装选项。0:现在不挂载(要在第一次访问卷时挂载),1:强制挂载卷以检查它是否准备好工作。

  • f_mkfs:用于格式化文件系统,在磁盘刚做全片擦除之后用这个函数来创建文件系统。

FRESULT f_mkfs(BYTE Drive, BYTE PartitioningRule, UINT AllocSize)

Drive: 逻辑驱动器号,取值 0~9
PartitioningRule: 取值 0,分区表被创建到主引导记录,主 DOS 分区被创建,然后分区上 FAT卷被创建,这就是 FDISK 格式化,用作硬盘和记忆卡;取值 1, FAT 卷起始于第一个扇区,驱动器上没有分区表,这属于 SFD 格式化,用作软盘和光盘
AllocSize: 强制分配单位(簇)大小(以字节为单位),取值应在扇区大小和 128 倍的扇区大小之前,如果指定了非法值,簇大小由卷大小决定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值