打印
[应用相关]

【转】数码相框工程手记【FATFS文件系统的移植】

[复制链接]
5706|12
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
figo20042005|  楼主 | 2012-7-11 10:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
首先感谢ChaN为我们带来如些高效和便于移植的文件系统。
关于作者
ChaN     Ageo city, Saitama prefecture, Japan
作者网站:http://elm-chan.org/
大家可以上这个网站下载最新的源代码。作者一直在更新。网站很简洁,也很贴心。
关于源代码
我在作者的网站上下载到了最新的版本 R0.08a ,并且移植到了我的STM32上。
下载 (25.56 KB)
2010-11-14 17:10


源码里有两个文件夹。
DOC:说明文件,帮助文件等。
SRC:文件系统的源文件 。
--option文件夹。地区语言支持的可选文件
--diskio.h是硬件层
--ff.c和ff.h是FatFs的文件系统层和文件系统的API层
--integer.h是文件系统所用到的数据类型的定义
--00readme.txt简要的介绍了FatFSHE FatFs/Tiny,包括他们所支持的API,怎么配置等等
关于移植:
移植些文件系统还是很容易的。
1、编写底层驱动。打开diskio.h便可以看到需要我们编写的底层驱动
2、在integer.h中定义文件系统所用到的数据类型。
3、文件系统的配置。
  • int assign_drives (int, int);
  • 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*);

复制代码

需要我们写的有:初始化,硬盘状态,多扇区读/写等函数。大家只要把底层搞定,一切都OK啦!关于文件系统的配置,我在后面有解释哦!大家休息休息断续听我讲。
关于代码解释:
首先是FATFS的配置文件 ffconf.h
本人英文水平有限,如果有任何问题,欢迎指正!
  • /*---------------------------------------------------------------------------/
  • / FatFs - 文件系统配置文件 R0.08a (C)ChaN, 2010
  • /----------------------------------------------------------------------------*/
  • #ifndef _FFCONF
  • #define _FFCONF 8255 /* 版本ID */
  • #define _FS_TINY 0 /* 0:正常 or 1:微小的 */
  • #define _FS_READONLY 0 /* 0:读写 or 1:只读 */
  • #define _FS_MINIMIZE 0 /* 0 to 3 */
  • /* The _FS_MINIMIZE option defines minimization level to remove some functions.
  • / 0: 全功能.
  • / 1: 去除f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
  • / 2: 在1的基础上去除f_opendir and f_readdir.
  • / 3: 在2基础上去除f_lseek*/
  • #define _USE_STRFUNC 0 /* 0:Disable or 1/2:Enable */
  • /* 使能字符串的功能, set _USE_STRFUNC to 1 or 2. */
  • #define _USE_MKFS 0 /* 0:Disable or 1:Enable */
  • /* 使能f_mkfs功能 set _USE_MKFS to 1 and set _FS_READONLY to 0 */
  • #define _USE_FORWARD 0 /* 0:Disable or 1:Enable */
  • /* 使能 f_forward 功能, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
  • #define _USE_FASTSEEK 0 /* 0:Disable or 1:Enable */
  • /* 使能fast seek 功能, set _USE_FASTSEEK to 1. */
  • /*---------------------------------------------------------------------------/
  • / 文件名和地区的配置
  • /----------------------------------------------------------------------------*/
  • #define _CODE_PAGE 932/* 选择支持的语言 936 - 简体中文 GBK */
  • #define _USE_LFN 0
  • #define _MAX_LFN 255
  • /* 是否支持长文件名 0为不支持*/
  • #define _LFN_UNICODE 0 /* 0:ANSI/OEM or 1:Unicode */
  • /* 转换字符编码为Unicode*/
  • #define _FS_RPATH 0 /* 0 to 2 */
  • /*---------------------------------------------------------------------------/
  • / 物理设备的配置
  • /----------------------------------------------------------------------------*/
  • #define _VOLUMES 1 /* 本地磁盘的数量 */
  • #define _MAX_SS 512 /* 512, 1024, 2048 or 4096 */
  • /* Maximum sector size to be handled. */
  • #define _MULTI_PARTITION 0 /* 0:Single partition or 1:Multiple partition */
  • /* When set to 0, each volume is bound to the same physical drive number and
  • / it can mount only first primaly partition. When it is set to 1, each volume
  • / is tied to the partitions listed in VolToPart[]. */
  • #define _USE_ERASE 0 /* 0:Disable or 1:Enable */
  • /* 使能扇区擦除, set _USE_ERASE to 1. */
  • /*---------------------------------------------------------------------------/
  • / 系统配置
  • /----------------------------------------------------------------------------*/
  • #define _WORD_ACCESS 0 /* 0 or 1 */
  • /* Set 0 first and it is always compatible with all platforms. The _WORD_ACCESS
  • / option defines which access method is used to the word data on the FAT volume.
  • /
  • / 0: Byte-by-byte access.
  • / 1: Word access. Do not choose this unless following condition is met.
  • /
  • / When the byte order on the memory is big-endian or address miss-aligned word
  • / access results incorrect behavior, the _WORD_ACCESS must be set to 0.
  • / If it is not the case, the value can also be set to 1 to improve the
  • / performance and code size. */
  • /* Include a header file here to define sync object types on the O/S */
  • /* #include <windows.h>, <ucos_ii.h.h>, <semphr.h> or ohters. */
  • #define _FS_REENTRANT 0 /* 0:Disable or 1:Enable */
  • #define _FS_TIMEOUT 1000 /* Timeout period in unit of time ticks */
  • #define _SYNC_t HANDLE /* O/S dependent type of sync object. e.g. HANDLE, OS_EVENT*, ID and etc.. */
  • /* 设置文件系统的可重入性 */
  • #define _FS_SHARE 0 /* 0:Disable or >=1:Enable *//* 定义多少文件可以被同时打开 */
  • #endif /* _FFCONFIG */

复制代码


STM32 需要的integer.h
  • /* These types must be 16-bit, 32-bit or larger integer */
  • typedef int INT;
  • typedef unsigned int UINT;
  • /* These types must be 8-bit integer */
  • typedef char CHAR;
  • typedef unsigned char UCHAR;
  • typedef unsigned char BYTE;
  • /* These types must be 16-bit integer */
  • typedef short SHORT;
  • typedef unsigned short USHORT;
  • typedef unsigned short WORD;
  • typedef unsigned short WCHAR;
  • /* These types must be 32-bit integer */
  • typedef long LONG;
  • typedef unsigned long ULONG;
  • typedef unsigned long DWORD;

复制代码

文件系统的API层
下载 (51.16 KB)
2010-11-14 19:36


源代码里其实都有解释,解释的也挺清楚。我解释几个要用到的函数。
f_mount   挂载文件
  • FRESULT f_mount (
  • BYTE vol,  /* Logical drive number to be mounted/unmounted */
  • FATFS *fs  /* Pointer to new file system object (NULL for unmount)*/
  • )

复制代码

其中FRESULT定义如下:
  • typedef enum {
  • FR_OK = 0,    /* (0) Succeeded */
  • FR_DISK_ERR,   /* (1) A hard error occured in the low level disk I/O layer */
  • FR_INT_ERR,    /* (2) Assertion failed */
  • FR_NOT_READY,   /* (3) The physical drive cannot work */
  • FR_NO_FILE,    /* (4) Could not find the file */
  • FR_NO_PATH,    /* (5) Could not find the path */
  • FR_INVALID_NAME,  /* (6) The path name format is invalid */
  • FR_DENIED,    /* (7) Acces denied due to prohibited access or directory full */
  • FR_EXIST,    /* (8) Acces denied due to prohibited access */
  • FR_INVALID_OBJECT,  /* (9) The file/directory object is invalid */
  • FR_WRITE_PROTECTED,  /* (10) The physical drive is write protected */
  • FR_INVALID_DRIVE,  /* (11) The logical drive number is invalid */
  • FR_NOT_ENABLED,   /* (12) The volume has no work area */
  • FR_NO_FILESYSTEM,  /* (13) There is no valid FAT volume on the physical drive */
  • FR_MKFS_ABORTED,  /* (14) The f_mkfs() aborted due to any parameter error */
  • FR_TIMEOUT,    /* (15) Could not get a grant to access the volume within defined period */
  • FR_LOCKED,    /* (16) The operation is rejected according to the file shareing policy */
  • FR_NOT_ENOUGH_CORE,  /* (17) LFN working buffer could not be allocated */
  • FR_TOO_MANY_OPEN_FILES /* (18) Number of open files > _FS_SHARE */
  • } FRESULT;

复制代码

每个函数都有一个返回值,返回0为成功!如果大家在作用的时候返回的不是0就是有问题哦,大家可以在这个定义里面看,是出了什么问题。
下面我解释中间的两个参数。
1、BYTE vol 本地要挂载磁盘的数目
2、文件系统的一个指针,用于填写一些信息【姑且这样说吧】
所以大家在使用系统系统的时候,必须写义一个FRESULT型数据。要一个FATFS型的指针
f_open 打开文件
  • FRESULT f_open (
  • FIL *fp,   /* Pointer to the blank file object */
  • const TCHAR *path, /* Pointer to the file name */
  • BYTE mode   /* Access mode and file open mode flags */
  • )

复制代码

需要定义  FIL 型指针 用于记录文件的一些信息
path 不能使用通配符 可以这样使用'\' '/'
mode 打开的扇区数,为512的倍数


如果在移植的过程中你有问题,欢迎回帖
E-mail:lovelyboy_zjb@163.com
QQ:331799388

2010年10月14日  于 湖师
沙发
//YZC| | 2012-7-11 13:56 | 只看该作者
2010年10月14日  于 湖师

使用特权

评论回复
板凳
elec921| | 2012-7-11 22:04 | 只看该作者
那个网站相当不错

使用特权

评论回复
地板
sinadz| | 2012-7-11 22:08 | 只看该作者
这时间太久远了

使用特权

评论回复
5
火箭球迷| | 2012-7-11 22:28 | 只看该作者
这时间太久远了
sinadz 发表于 2012-7-11 22:08

的确是有点旧

使用特权

评论回复
6
zhangleiat185| | 2012-7-11 23:59 | 只看该作者
………………不说了

使用特权

评论回复
7
xzy520xr| | 2013-9-21 16:12 | 只看该作者
写的挺好的,支持

使用特权

评论回复
8
gxgclg| | 2013-9-21 18:43 | 只看该作者
很好的读书笔记

使用特权

评论回复
9
火箭球迷| | 2013-9-21 18:51 | 只看该作者
支持原创笔记

使用特权

评论回复
10
xsgy123| | 2013-9-21 19:02 | 只看该作者
相当不错的工程手记

使用特权

评论回复
11
MCUWYL| | 2013-9-21 19:45 | 只看该作者
围观、、

使用特权

评论回复
12
asian21| | 2015-11-16 22:42 | 只看该作者
可好?

使用特权

评论回复
13
wahahaheihei| | 2015-11-17 09:18 | 只看该作者
FR_WRITE_PROTECTED,  /* (10) The physical drive is write protected */
FR_INVALID_DRIVE,  /* (11) The logical drive number is invalid */
写保护和逻辑驱动?这个什么意思用于什么呢

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:need to study

16

主题

1576

帖子

3

粉丝