rt-thread之elmfat文件系统分析

上一文主要是讲到RTT的文件系统顶层dfs框架及其实现,接下来这篇博文主要是讲述其中间层的一个具体文件系统--elmfat文件系统。

rt-thread的elmfat文件系统是一个开源的小型嵌入式文件系统,它的官网是http://elm-chan.org/fsw/ff/00index_e.html,RTT当前版本V1.1.0版本下的elmfat文件系统是从它的R0.08b版本移植而来。本文不打算详情介绍其实现原理,读者如有兴趣可以到其官网下载源码来做学习研究。

1 elmfat文件系统提供哪些接口
从elmfat文件系统的官网来看,elmfat提供以下接口:

f_mount - Register/Unregister a work area
f_open - Open/Create a file
f_close - Close a file
f_read - Read file
f_write - Write file
f_lseek - Move read/write pointer, Expand file size
f_truncate - Truncate file size
f_sync - Flush cached data
f_opendir - Open a directory
f_readdir - Read a directory item
f_getfree - Get free clusters
f_stat - Get file status
f_mkdir - Create a directory
f_unlink - Remove a file or directory
f_chmod - Change attribute
f_utime - Change timestamp
f_rename - Rename/Move a file or directory
f_chdir - Change current directory
f_chdrive - Change current drive
f_getcwd - Retrieve the current directory
f_getlabel - Get volume label
f_setlabel - Set volume label
f_forward - Forward file data to the stream directly
f_mkfs - Create a file system on the drive
f_fdisk - Divide a physical drive
f_gets - Read a string
f_putc - Write a character
f_puts - Write a string
f_printf - Write a formatted string
f_tell - Get the current read/write pointer
f_eof - Test for end-of-file on a file
f_size - Get size of a file
f_error - Test for an error on a file
同时elmfat也需要移植时用户提供以下函数的实现:
disk_initialize - Initialize disk drive
disk_status - Get disk status
disk_read - Read sector(s)
disk_write - Write sector(s)
disk_ioctl - Control device dependent features
get_fattime - Get current time
同时elmfat文件系统提供ffconf.h头文件以供用户配置elmfat文件系统的一些特性,这里就不做详情介绍。

2 RTT如何移植elmfat的?
首先在elmfat提供的ffconfig.h中RTT根据自身特点可配置自己需要的FAT特性。其次从上一篇文章的3.2可知,在RTT文件系统的初始化第二步骤时,即RTT在进行elm_init时,向dfs注册dfs_elm。RTT在移植elmfat文件系统时,专门为其添加了一个dfs_elm.c文件,这个文件一方面实现elmfat文件系统的初始化(即刚提到的elm_init),将上层的文件操作映射到elmfat提供的文件操作接口上。此外,此文件还为elmfat文件系统提供了其所需要的接口函数实现(见第1章提到的disk_initialize等6个用户需要提供的函数 实现):

#include "diskio.h"
 
/* Initialize a Drive */
DSTATUS disk_initialize(BYTE drv)
{
    return 0;
}
 
/* Return Disk Status */
DSTATUS disk_status(BYTE drv)
{
    return 0;
}
这两个函数直接置空。
/* Read Sector(s) */
DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, BYTE count)
{
    rt_size_t result;
    rt_device_t device = disk[drv];//获取设备
 
    result = rt_device_read(device, sector, buff, count);//通过设备驱动提供读取函数实现读取操作
    if (result == count)
    {
        return RES_OK;
    }
 
    return RES_ERROR;
}
dfs_elm.c文件内部通过一设备数组dsk来虚拟磁盘。
其定义如下:

static rt_device_t disk[_VOLUMES] = {0};
每个磁盘在进行挂载操作时记录一文件系统设备。
这里需要注意地是,sector表示的是起始扇区编号,count表示需要读取扇区的数量,此函数是一次读多个扇区的操作。

/* Write Sector(s) */
DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
{
    rt_size_t result;
    rt_device_t device = disk[drv];
 
    result = rt_device_write(device, sector, buff, count);
    if (result == count)
    {
        return RES_OK;
    }
 
    return RES_ERROR;
}

同样,elmfat文件系统是通过设备驱动来实现其写的操作,与读取操作类似,写操作同样是对多个扇区的写操作。
/* Miscellaneous Functions */
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)//磁盘控制接口
{
    rt_device_t device = disk[drv];
 
    if (device == RT_NULL)
        return RES_ERROR;
 
    if (ctrl == GET_SECTOR_COUNT)//获取扇区个数
    {
        struct rt_device_blk_geometry geometry;
 
        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
 
        *(DWORD *)buff = geometry.sector_count;
        if (geometry.sector_count == 0)
            return RES_ERROR;
    }
    else if (ctrl == GET_SECTOR_SIZE)//获取扇区大小
    {
        struct rt_device_blk_geometry geometry;
 
        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
 
        *(WORD *)buff = (WORD)(geometry.bytes_per_sector);
    }
    else if (ctrl == GET_BLOCK_SIZE) /* Get erase block size in unit of sectors (DWORD) *///获取一个块的大小
    {
        struct rt_device_blk_geometry geometry;
 
        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
 
        *(DWORD *)buff = geometry.block_size / geometry.bytes_per_sector;
    }
    else if (ctrl == CTRL_SYNC)//同步操作
    {
        rt_device_control(device, RT_DEVICE_CTRL_BLK_SYNC, RT_NULL);
    }
    else if (ctrl == CTRL_ERASE_SECTOR)//擦除扇区操作
    {
        rt_device_control(device, RT_DEVICE_CTRL_BLK_ERASE, buff);
    }
 
    return RES_OK;
}

由以上实现可以,磁盘操作函数共提供5种操作。
rt_time_t get_fattime(void)
{
    return 0;
}

这一函数,同样置空。
#if _FS_REENTRANT
int ff_cre_syncobj(BYTE drv, _SYNC_t *m)//创建同步对象接口的实现
{
    char name[8];
    rt_mutex_t mutex;
 
    rt_snprintf(name, sizeof(name), "fat%d", drv);
    mutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
    if (mutex != RT_NULL)
    {
        *m = mutex;
        return RT_TRUE;
    }
 
    return RT_FALSE;
}
 
int ff_del_syncobj(_SYNC_t m)//删除同步对象的实现
{
    if (m != RT_NULL)
        rt_mutex_delete(m);
 
    return RT_TRUE;
}
 
int ff_req_grant(_SYNC_t m)//获取同步对象的实现
{
    if (rt_mutex_take(m, _FS_TIMEOUT) == RT_EOK)
        return RT_TRUE;
 
    return RT_FALSE;
}
 
void ff_rel_grant(_SYNC_t m)//释放同步对象的实现
{
    rt_mutex_release(m);
}
 
#endif
elmfat文件系统需要用户为其提供一套同步对象的实现,在RTT中用互斥锁来实现。
/* Memory functions */
#if _USE_LFN == 3
/* Allocate memory block */
void *ff_memalloc(UINT size)//内存动态分配实现
{
    return rt_malloc(size);
}
 
/* Free memory block */
void ff_memfree(void *mem)//内存释放实现
{
    rt_free(mem);
}
#endif /* _USE_LFN == 3 */

同样,如果用户配置使用了长文件名特性,那么用户得为elmfat文件系统提供动态分配和动态释放的函数实现。


最后,RTT根据自身特点做了些小修改,这里就不做详情介绍,读者可以通过比较两者区别来学习设计者的思路。
————————————————
版权声明:本文为CSDN博主「flydream0」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/flydream0/article/details/8841770

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章