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

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