移植FATFS gaunthan Posted on May 28 2016 ? File System ? ## 概述 **FATFS**是面向小型嵌入式系统的一种通用的FAT文件系统。它完全由ANSI C编写且完全独立于底层I/O,因此可以很容易地、不加修改地将其移植到其他的处理器中,如8051, PIC, AVR, ARM, Z80, 78K等。FATFS支持FAT12、FAT16、FAT32等格式。 FATFS概要图见下:  FATFS的源码可以从其官网下载:http://elm-chan.org/fsw/ff/00index_e.html ## API ### File Access |API|function| |--| |f_open |Open/Create a file| |f_close |Close an open file| |f_read | Read data| |f_write |Write data| |f_lseek | Move read/write pointer, Expand size| |f_truncate | Truncate size| |f_sync |Flush cached data| |f_forward |Forward data to the stream| |f_gets |Read a string| |f_putc |Write a character| |f_puts |Write a string| |f_printf |Write a formatted string| |f_tell |Get current read/write pointer| |f_eof |Test for end-of-file| |f_size |Get size| |f_error |Test for an error| ### Directory Access |API|function| |--| |f_opendir | Open a directory| |f_closedir | Close an open directory| |f_readdir | Read an item| |f_findfirst | Open a directory and read first item found| |f_findnext | Read a next item found| |File/Directory Management| |f_stat | Check existance of a file or sub|directory| |f_unlink | Remove a file or sub|directory| |f_rename | Rename or move a file or sub|directory| |f_chmod | Change attribute of a file or sub|directory| |f_utime | Change timestamp of a file or sub|directory| |f_mkdir | Create a sub|directory| |f_chdir | Change current directory| |f_chdrive | Change current drive| |f_getcwd | Retrieve the current directory and drive| ### Volume Management |API|function| |--| |f_mount | Register/Unregister a work area of a volume| |f_mkfs | Create an FAT volume on the logical drive| |f_fdisk | Create logical drives on the physical drive| |f_getfree | Get total size and free size on the volume| |f_getlabel | Get volume label| |f_setlabel | Set volume label| ## 设备操作接口 FATFS完全分离物理设备,它通过几个接口操作存储设备:  这意味着移植工作只需完成下列接口的实现: |DCI(Device Control Interface)|function| |--| |disk_status | Get device status| |disk_initialize | Initialize device| |disk_read | Read sector(s)| |disk_write | Write sector(s)| |disk_ioctl | Control device dependent features| |get_fattime | Get current time| ## 移植工作 ### 目录结构 解压在官网下载的FATFS[压缩包](http://elm-chan.org/fsw/ff/ff11a.zip),可以见该文件夹下有两个目录,分别是: #### doc 存放着官方文档,有英文(en)与日文(ja)两种格式:  #### src FATFS的源码,具体结构见下图:  其中,option目录下是一些可选的外部源文件,包含了多语言支持需要用到的文件和转换函数。更多信息可以在src目录下的00readme.txt文件中查看。这里介绍一些src目录下比较重要的文件: |文件|内容| |--| |integer.h|包含一些数值类型定义| |diskio.c|包含底层存储介质的操作函数,这些函数需要用户自己实现| |ff.c|独立于底层介质操作文件的函数,实现FatFs模型| |cc936.c|放置在option目录下,是简体中文支持所需要添加的文件,包含了简体中文的GBK和转换函数| |ffconf.h|包含了对文件系统的各种配置。如果需要简体中文支持,需要把该文件内的_CODE_PAGE宏的值更改为936且添加cc936.c到工程中| ### 实现diskio.c FATFS文件系统与底层介质的驱动分离开头,对底层介质的操作都要交给用户去实现,它仅仅提供了一个函数接口而已。根据FATFS帮助文档的说明,用户需要提供的几个函数的原型如下,在diskio.c中定义。 #### 存储介质状态函数 ```c DSTATUS disk_status ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) ``` 用于获取磁盘状态的函数,可以简单地返回`RES_OK`。 #### 存储介质初始化函数 ```c DSTATUS disk_initialize ( BYTE pdrv /* Physical drive nmuber to identify the drive */ ) ``` 文件系统的一些函数会调用这个接口来进行存储介质的初始化,因此,在这里加入我们自己的存储介质的初始化函数。 #### 扇区读取函数 ```c DRESULT disk_read ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE *buff, /* Data buffer to store read data */ DWORD sector, /* Sector address in LBA */ UINT count /* Number of sectors to read */ ) ``` 这个函数是文件系统读取存储介质时会调用的一个函数,因此,在这里加入我们自己的存储介质的读扇区函数。 #### 扇区写入函数 ```c DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address in LBA */ UINT count /* Number of sectors to write */ ) ``` 这个函数是文件系统写入存储介质时会调用的一个函数,因此,在这里加入我们自己的存储介质的写扇区函数。 #### 其它控制功能 ```c DRESULT disk_ioctl ( BYTE pdrv, /* Physical drive nmuber (0..) */ BYTE cmd, /* Control code */ void *buff /* Buffer to send/receive control data */ ) ``` #### 时间接口函数 ```c /** * @brief Get current time */ DWORD get_fattime(void) { return RES_OK; } ``` 在diskio.c文件的最后还得提供用于获取当前时间的函数,因为在ff.c中调用了它。该函数用于记录文件的创建、修改与访问时间,需要用户自己实现。如果不需要用到这部分功能,可以简单返回`RES_OK`即可: ## Reference - [FatFs - Generic FAT File System Module ](http://elm-chan.org/fsw/ff/00index_e.html) 赏 Wechat Pay Alipay SX-UART-KA 串口扩展模块 FAT32