FATFS ff.h

  1 /*---------------------------------------------------------------------------/
  2 /  FatFs - FAT file system module include file  R0.09     (C)ChaN, 2011
  3 /----------------------------------------------------------------------------/
  4 / FatFs module is a generic FAT file system module for small embedded systems.
  5 / This is a free software that opened for education, research and commercial
  6 / developments under license policy of following trems.
  7 /
  8 /  Copyright (C) 2011, ChaN, all right reserved.
  9 /
 10 / * The FatFs module is a free software and there is NO WARRANTY.
 11 / * No restriction on use. You can use, modify and redistribute it for
 12 /   personal, non-profit or commercial product UNDER YOUR RESPONSIBILITY.
 13 / * Redistributions of source code must retain the above copyright notice.
 14 /
 15 /----------------------------------------------------------------------------*/
 16
 17 #ifndef _FATFS
 18 #define _FATFS    6502    /* Revision ID */  //版本号
 19
 20 #ifdef __cplusplus    //C++
 21 extern "C" {
 22 #endif
 23
 24 #include "integer.h"    /* Basic integer types */    //数据类型宏定义
 25 #include "ffconf.h"        /* FatFs configuration options */  //配置选项文件
 26
 27 #if _FATFS != _FFCONF
 28 #error Wrong configuration file (ffconf.h).
 29 #endif
 30
 31 /* Definitions of volume management 容量管理定义*/
 32
 33 #if _MULTI_PARTITION        /* Multiple partition(分区) configuration */
 34 typedef struct {
 35     BYTE pd;    /* Physical drive number */
 36     BYTE pt;    /* Partition(分区): 0:Auto detect(自动检测), 1-4:Forced partition(强制分区)) */
 37 } PARTITION;
 38 extern PARTITION VolToPart[];    /* Volume - Partition resolution table */
 39 #define LD2PD(vol) (VolToPart[vol].pd)    /* Get physical drive number */
 40 #define LD2PT(vol) (VolToPart[vol].pt)    /* Get partition index(索引) */
 41
 42 #else                        /* Single partition configuration 单分区配置*/
 43 #define LD2PD(vol) (vol)    /* Each logical drive is bound to the same physical drive number */
 44 #define LD2PT(vol) 0        /* Always mounts the 1st partition or in SFD */
 45
 46 #endif
 47
 48
 49
 50 /* Type of path name strings on FatFs API */
 51
 52 #if _LFN_UNICODE            /* Unicode string */
 53 #if !_USE_LFN
 54 #error _LFN_UNICODE must be 0 in non-LFN cfg.
 55 #endif
 56 #ifndef _INC_TCHAR
 57 typedef WCHAR TCHAR;
 58 #define _T(x) L ## x
 59 #define _TEXT(x) L ## x   //L##x表示L连接x
 60 #endif
 61
 62 #else                        /* ANSI/OEM string */
 63 #ifndef _INC_TCHAR
 64 typedef char TCHAR;
 65 #define _T(x) x
 66 #define _TEXT(x) x
 67 #endif
 68
 69 #endif
 70
 71
 72
 73 /* File system object structure (FATFS) */
 74
 75 typedef struct {
 76     BYTE    fs_type;          /* FAT sub-type(子类型) (0:Not mounted) 0表示没有挂载 */
 77     BYTE    drv;              /* Physical drive number */
 78     BYTE    csize;            /* Sectors per cluster (1,2,4...128)每个簇的扇区数目 */
 79     BYTE    n_fats;           /* Number of FAT copies (1,2) 文件分配表数目*/
 80     BYTE    wflag;            /* win[] dirty flag (1:must be written back) */
 81     BYTE    fsi_flag;         /* fsinfo dirty flag (1:must be written back) */
 82     WORD    id;                  /* File system mount ID */
 83     WORD    n_rootdir;  /* Number of root directory entries (FAT12/16) */
 84 #if _MAX_SS != 512
 85     WORD    ssize;            /* Bytes per sector (512, 1024, 2048 or 4096) */
 86 #endif
 87 #if _FS_REENTRANT
 88     _SYNC_t    sobj;            /* Identifier of sync object */
 89 #endif
 90 #if !_FS_READONLY
 91     DWORD    last_clust;        /* Last allocated cluster */
 92     DWORD    free_clust;        /* Number of free clusters */
 93     DWORD    fsi_sector;        /* fsinfo sector (FAT32) */
 94 #endif
 95 #if _FS_RPATH
 96     DWORD    cdir;              /* Current directory start cluster (0:root) */
 97 #endif
 98     DWORD    n_fatent;        /* Number of FAT entries (= number of clusters + 2) */
 99     DWORD    fsize;            /* Sectors per FAT */
100     DWORD    fatbase;        /* FAT start sector */
101     DWORD    dirbase;        /* Root directory start sector (FAT32:Cluster#) */
102     DWORD    database;        /* Data start sector */
103     DWORD    winsect;        /* Current sector appearing in the win[] */
104     BYTE    win[_MAX_SS];    /* Disk access window for Directory, FAT (and Data on tiny cfg) */
105 } FATFS;
106
107
108
109 /* File object structure (FIL) */
110
111 typedef struct {
112     FATFS*    fs;                /* Pointer to the owner file system object */
113     WORD    id;                /* Owner file system mount ID */
114     BYTE    flag;            /* File status flags */
115     BYTE    pad1;
116     DWORD    fptr;            /* File read/write pointer (0 on file open) */
117     DWORD    fsize;            /* File size */
118     DWORD    sclust;            /* File start cluster (0 when fsize==0) */
119     DWORD    clust;            /* Current cluster */
120     DWORD    dsect;            /* Current data sector */
121 #if !_FS_READONLY
122     DWORD    dir_sect;        /* Sector containing the directory entry */
123     BYTE*    dir_ptr;        /* Ponter to the directory entry in the window */
124 #endif
125 #if _USE_FASTSEEK
126     DWORD*    cltbl;            /* Pointer to the cluster link map table (null on file open) */
127 #endif
128 #if _FS_SHARE
129     UINT    lockid;            /* File lock ID (index of file semaphore table) */
130 #endif
131 #if !_FS_TINY
132     BYTE    buf[_MAX_SS];    /* File data read/write buffer */
133 #endif
134 } FIL;
135
136
137
138 /* Directory object structure (DIR) */
139
140 typedef struct {
141     FATFS*    fs;                /* Pointer to the owner file system object */
142     WORD    id;                /* Owner file system mount ID */
143     WORD    index;            /* Current read/write index number */
144     DWORD    sclust;            /* Table start cluster (0:Root dir) */
145     DWORD    clust;            /* Current cluster */
146     DWORD    sect;            /* Current sector */
147     BYTE*    dir;            /* Pointer to the current SFN entry in the win[] */
148     BYTE*    fn;                /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
149 #if _USE_LFN
150     WCHAR*    lfn;            /* Pointer to the LFN working buffer */
151     WORD    lfn_idx;        /* Last matched LFN index number (0xFFFF:No LFN) */
152 #endif
153 } DIR;
154
155
156
157 /* File status structure (FILINFO) */
158
159 typedef struct {
160     DWORD    fsize;            /* File size */
161     WORD    fdate;            /* Last modified date */
162     WORD    ftime;            /* Last modified time */
163     BYTE    fattrib;        /* Attribute */
164     TCHAR    fname[13];        /* Short file name (8.3 format) */
165 #if _USE_LFN
166     TCHAR*    lfname;            /* Pointer to the LFN buffer */
167     UINT     lfsize;            /* Size of LFN buffer in TCHAR */
168 #endif
169 } FILINFO;
170
171
172
173 /* File function return code (FRESULT) */
174
175 typedef enum {
176     FR_OK = 0,                /* (0) Succeeded */
177     FR_DISK_ERR,            /* (1) A hard error occured in the low level disk I/O layer */
178     FR_INT_ERR,                /* (2) Assertion failed */
179     FR_NOT_READY,            /* (3) The physical drive cannot work */
180     FR_NO_FILE,                /* (4) Could not find the file */
181     FR_NO_PATH,                /* (5) Could not find the path */
182     FR_INVALID_NAME,        /* (6) The path name format is invalid */
183     FR_DENIED,                /* (7) Acces denied due to prohibited access or directory full */
184     FR_EXIST,                /* (8) Acces denied due to prohibited access */
185     FR_INVALID_OBJECT,        /* (9) The file/directory object is invalid */
186     FR_WRITE_PROTECTED,        /* (10) The physical drive is write protected */
187     FR_INVALID_DRIVE,        /* (11) The logical drive number is invalid */
188     FR_NOT_ENABLED,            /* (12) The volume has no work area */
189     FR_NO_FILESYSTEM,        /* (13) There is no valid FAT volume */
190     FR_MKFS_ABORTED,        /* (14) The f_mkfs() aborted due to any parameter error */
191     FR_TIMEOUT,                /* (15) Could not get a grant to access the volume within defined period */
192     FR_LOCKED,                /* (16) The operation is rejected according to the file shareing policy */
193     FR_NOT_ENOUGH_CORE,        /* (17) LFN working buffer could not be allocated */
194     FR_TOO_MANY_OPEN_FILES,    /* (18) Number of open files > _FS_SHARE */
195     FR_INVALID_PARAMETER    /* (19) Given parameter is invalid */
196 } FRESULT;
197
198
199
200 /*--------------------------------------------------------------*/
201 /* FatFs module application interface                           */
202
203 FRESULT f_mount (BYTE, FATFS*);                        /* Mount/Unmount a logical drive */
204 FRESULT f_open (FIL*, const TCHAR*, BYTE);            /* Open or create a file */
205 FRESULT f_read (FIL*, void*, UINT, UINT*);            /* Read data from a file */
206 FRESULT f_lseek (FIL*, DWORD);                        /* Move file pointer of a file object */
207 FRESULT f_close (FIL*);                                /* Close an open file object */
208 FRESULT f_opendir (DIR*, const TCHAR*);                /* Open an existing directory */
209 FRESULT f_readdir (DIR*, FILINFO*);                    /* Read a directory item */
210 FRESULT f_stat (const TCHAR*, FILINFO*);            /* Get file status */
211 FRESULT f_write (FIL*, const void*, UINT, UINT*);    /* Write data to a file */
212 FRESULT f_getfree (const TCHAR*, DWORD*, FATFS**);    /* Get number of free clusters on the drive */
213 FRESULT f_truncate (FIL*);                            /* Truncate file */
214 FRESULT f_sync (FIL*);                                /* Flush cached data of a writing file */
215 FRESULT f_unlink (const TCHAR*);                    /* Delete an existing file or directory */
216 FRESULT    f_mkdir (const TCHAR*);                        /* Create a new directory */
217 FRESULT f_chmod (const TCHAR*, BYTE, BYTE);            /* Change attriburte of the file/dir */
218 FRESULT f_utime (const TCHAR*, const FILINFO*);        /* Change timestamp of the file/dir */
219 FRESULT f_rename (const TCHAR*, const TCHAR*);        /* Rename/Move a file or directory */
220 FRESULT f_chdrive (BYTE);                            /* Change current drive */
221 FRESULT f_chdir (const TCHAR*);                        /* Change current directory */
222 FRESULT f_getcwd (TCHAR*, UINT);                    /* Get current directory */
223 FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*);    /* Forward data to the stream */
224 FRESULT f_mkfs (BYTE, BYTE, UINT);                    /* Create a file system on the drive */
225 FRESULT    f_fdisk (BYTE, const DWORD[], void*);        /* Divide a physical drive into some partitions */
226 int f_putc (TCHAR, FIL*);                            /* Put a character to the file */
227 int f_puts (const TCHAR*, FIL*);                    /* Put a string to the file */
228 int f_printf (FIL*, const TCHAR*, ...);                /* Put a formatted string to the file */
229 TCHAR* f_gets (TCHAR*, int, FIL*);                    /* Get a string from the file */
230
231 #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
232 #define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
233 #define f_tell(fp) ((fp)->fptr)
234 #define f_size(fp) ((fp)->fsize)
235
236 #ifndef EOF
237 #define EOF (-1)
238 #endif
239
240
241
242
243 /*--------------------------------------------------------------*/
244 /* Additional user defined functions                            */
245
246 /* RTC function */
247 #if !_FS_READONLY
248 DWORD get_fattime (void);
249 #endif
250
251 /* Unicode support functions */
252 #if _USE_LFN                        /* Unicode - OEM code conversion */
253 WCHAR ff_convert (WCHAR, UINT);        /* OEM-Unicode bidirectional conversion */
254 WCHAR ff_wtoupper (WCHAR);            /* Unicode upper-case conversion */
255 #if _USE_LFN == 3                    /* Memory functions */
256 void* ff_memalloc (UINT);            /* Allocate memory block */
257 void ff_memfree (void*);            /* Free memory block */
258 #endif
259 #endif
260
261 /* Sync functions */
262 #if _FS_REENTRANT
263 int ff_cre_syncobj (BYTE, _SYNC_t*);/* Create a sync object */
264 int ff_req_grant (_SYNC_t);            /* Lock sync object */
265 void ff_rel_grant (_SYNC_t);        /* Unlock sync object */
266 int ff_del_syncobj (_SYNC_t);        /* Delete a sync object */
267 #endif
268
269
270
271
272 /*--------------------------------------------------------------*/
273 /* Flags and offset address                                     */
274
275
276 /* File access control and file status flags (FIL.flag) */
277
278 #define    FA_READ                0x01
279 #define    FA_OPEN_EXISTING    0x00
280 #define FA__ERROR            0x80
281
282 #if !_FS_READONLY
283 #define    FA_WRITE            0x02
284 #define    FA_CREATE_NEW        0x04
285 #define    FA_CREATE_ALWAYS    0x08
286 #define    FA_OPEN_ALWAYS        0x10
287 #define FA__WRITTEN            0x20
288 #define FA__DIRTY            0x40
289 #endif
290
291
292 /* FAT sub type (FATFS.fs_type) */
293
294 #define FS_FAT12    1
295 #define FS_FAT16    2
296 #define FS_FAT32    3
297
298
299 /* File attribute bits for directory entry */
300
301 #define    AM_RDO    0x01    /* Read only */
302 #define    AM_HID    0x02    /* Hidden */
303 #define    AM_SYS    0x04    /* System */
304 #define    AM_VOL    0x08    /* Volume label */
305 #define AM_LFN    0x0F    /* LFN entry */
306 #define AM_DIR    0x10    /* Directory */
307 #define AM_ARC    0x20    /* Archive */
308 #define AM_MASK    0x3F    /* Mask of defined bits */
309
310
311 /* Fast seek feature */
312 #define CREATE_LINKMAP    0xFFFFFFFF
313
314
315
316 /*--------------------------------*/
317 /* Multi-byte word access macros  */
318
319 #if _WORD_ACCESS == 1    /* Enable word access to the FAT structure */
320 #define    LD_WORD(ptr)        (WORD)(*(WORD*)(BYTE*)(ptr))
321 #define    LD_DWORD(ptr)        (DWORD)(*(DWORD*)(BYTE*)(ptr))
322 #define    ST_WORD(ptr,val)    *(WORD*)(BYTE*)(ptr)=(WORD)(val)
323 #define    ST_DWORD(ptr,val)    *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
324 #else                    /* Use byte-by-byte access to the FAT structure */
325 #define    LD_WORD(ptr)        (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
326 #define    LD_DWORD(ptr)        (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
327 #define    ST_WORD(ptr,val)    *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
328 #define    ST_DWORD(ptr,val)    *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8); *((BYTE*)(ptr)+2)=(BYTE)((DWORD)(val)>>16); *((BYTE*)(ptr)+3)=(BYTE)((DWORD)(val)>>24)
329 #endif
330
331 #ifdef __cplusplus
332 }
333 #endif
334
335 #endif /* _FATFS */
时间: 2024-08-26 18:22:19

FATFS ff.h的相关文章

Zynq Fatfs文件系统应用笔记

Zynq Fatfs文件系统应用笔 Hello,panda 笔记介绍基于所描述的Zynq Fatfs基于Xilinx xilffsv3.0和Sdpsv2.4,文件系统采用在Bare-Metal和轻量级操作系统中常用的FatFs,版本为v0.10b. 在开始介绍FatFs文件系统在Zynq实现之前一定要先对FAT32文件系统有一个清晰的了解. 1 FAT32文件系统 应用笔记针对SD上的FAT32文件系统,在对文件系统作详细介绍之前,先回顾一下硬盘的结构,如图1: 图1 硬盘结构 对一个机械硬盘而

S3C2416裸机开发系列十七_GCC下Fatfs的移植

S3C2416裸机开发系列十七 GCC下Fatfs的移植 象棋小子    1048272975 对于固态存储器,其存储容量可以很大,往往需要一款文件系统对存储器用户数据进行组织文件的管理.它对文件存储器空间进行组织和分配,负责文件的存储并对存入的文件进行保护和检索.在嵌入式系统中,往往需要采用windows兼容的文件系统,像相机的照片.视频监控.语音产品等,很多都需要从windows计算机上提取资源或在windows计算机上进一步处理.Fatfs由于其开源免费,支持fat32,受到了广泛的应用,

随想录(fatfs的学习)

[ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 上学的时候就对文件系统非常有兴趣.可是苦于没有合适的fs代码能够学习.市面上的fs代码,要么太大.像linux一样,动不动就是几万行,几十万行.要么就是没有开源,你仅仅会使用它的接口,却不太清楚里面是怎么实现的. 一直到后来工作的时候,发现了fatfs这么一个开源库代码. fatfs大小合适.内容也比較紧凑.仅仅要做好底层的接口移植就能够使用了.眼下fatfs用来管理最多的还是sd卡设备,n

STM32-移植FATFS的NANDFLASH驱动

一,建立工程FATFS源码 1,在http://elm-chan.org/fsw/ff/00index_e.html上下载ff007c.zip,并把ff007c.zip里面的 src文件夹复制到D:\works\EK-STM3210E-UCOSII下,并改名为Fatfs: 2,在IDE工程中右击选择“Add Group”建立“FATFS”文件组,并在“FATFS”上右击选择“Add Files”添加 D:\works\EK-STM3210E-UCOSII\Fatfs下的C文件: 3,把D:\wo

FATFS外置UNICODE GBK双向转换码表(转)

源:FATFS外置UNICODE GBK双向转换码表 将UtoG,GtoU双向码表放到存储卡里面实现长文件名,因为FATFS长文件名需要unicode支持, 首先将UtoG.sys,GtoU.sys两个文件放到SD卡根目录,注意,一定要在根目录,并且是短文件名,因为长文件名需要UNICODE支持,此时的FATFS还是不支持长文件名的,但是当初始化UNICODE码表后就可以支持长文件名了. 两个码表下载地址:http://download.csdn.net/detail/cp1300/552673

STM32 下 FatFs的移植,实现了擦写均衡,坏块管理,硬件 ECC,ECC纠错

最近因项目需要,做一个数据采集的单片机平台.需要移植 FatFs .现在把最后成果贴上来. 在 STM32 单片机上,成功移植 FatFs 0.12b,使用的 Nand Flash 芯片为 K9F2G08 . 特点: 系统配合 FatFs 实现了擦写均衡,坏块管理,硬件ECC,软件 ECC 纠错 文件组成: FatFs 目录 +---- ffconf.h 对 FatFs 进行配置的文件 +---- ff.h +---- ff.c 是 FatFs 的实现文件,擦写均衡 在 f_getfree 函数

FatFs文件系统的移植

FatFs 的底层可以写一次命令,读写多个扇区.FatFs的设计的读写的思想就很好,小块的数据,我就经过Buffer来存储,大块的数据,我就直接进行存取,那样速度,效率高了很多,看图: FatFs文件系统的结构也很清晰,也是看图: 补充一点,FatFs的作者写了两个,一个是正宗的FatFs,比较适合大的RAM的设备,另一个是FatFs/Tiny,比较适合小RAM的系统,比如单片机,FatFs/Tiny占用较小的RAM,代价是更慢的读写速度和更少的API函数.不过两个都支持FAT12,FAT16,

FatFS简介

---恢复内容开始--- 1.1 简介 随着信息技术的发展,当今社会的信息量越来越大,以往由单片机构成的系统简单地对 存储媒介按地址.按字节的读/写已经不能满足人们实际应用的需要,于是利用文件系统对 存储媒介进行管理成了今后单片机系统的一个发展方向.目前常用的文件系统主要有微软的 FATl2.FATl6.FAT32.NTFS 以及Linux 系统下的EXT2 和EXT3 等.由于微软Windows 的广泛应用,在当前的消费类电子产品中,用得最多的还是FAT 文件系统,如U 盘.MP3. MP4

STM32单片机图片解码

图片解码首先是最简单的bmp图片解码,关于bmp的结构可自行查阅,代码如下 #ifndef __BMPDECODE_H_ #define __BMPDECODE_H_ #include "ff.h" #include "lcd.h" #include "stdlib.h" #include "usb_type.h" //重定义区 typedef char CHAR; //数据类型重定义,便于移植 typedef short S