Huseyin Buyukesmeli / FatFS

Dependents:   DISCO-F746NG_uSD Active-WolfMan_V2-5-All-Frank-Board-Functions_copy DISCO-F746NG_uSD DISCO-F746NG_uSD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ff.h Source File

ff.h

00001 /*---------------------------------------------------------------------------/
00002 /  FatFs - FAT file system module include R0.11     (C)ChaN, 2015
00003 /----------------------------------------------------------------------------/
00004 / FatFs module is a free software that opened under license policy of
00005 / following conditions.
00006 /
00007 / Copyright (C) 2015, ChaN, all right reserved.
00008 /
00009 / 1. Redistributions of source code must retain the above copyright notice,
00010 /    this condition and the following disclaimer.
00011 /
00012 / This software is provided by the copyright holder and contributors "AS IS"
00013 / and any warranties related to this software are DISCLAIMED.
00014 / The copyright owner or contributors be NOT LIABLE for any damages caused
00015 / by use of this software.
00016 /---------------------------------------------------------------------------*/
00017 
00018 
00019 #ifndef _FATFS
00020 #define _FATFS  32020   /* Revision ID */
00021 
00022 #ifdef __cplusplus
00023 extern "C" {
00024 #endif
00025 
00026 #include "integer.h"    /* Basic integer types */
00027 #include "ffconf.h"     /* FatFs configuration options */
00028 #if _FATFS != _FFCONF
00029 #error Wrong configuration file (ffconf.h).
00030 #endif
00031 
00032 
00033 
00034 /* Definitions of volume management */
00035 
00036 #if _MULTI_PARTITION        /* Multiple partition configuration */
00037 typedef struct {
00038     BYTE pd;    /* Physical drive number */
00039     BYTE pt;    /* Partition: 0:Auto detect, 1-4:Forced partition) */
00040 } PARTITION;
00041 extern PARTITION VolToPart[];   /* Volume - Partition resolution table */
00042 #define LD2PD(vol) (VolToPart[vol].pd)  /* Get physical drive number */
00043 #define LD2PT(vol) (VolToPart[vol].pt)  /* Get partition index */
00044 
00045 #else                           /* Single partition configuration */
00046 #define LD2PD(vol) (BYTE)(vol)  /* Each logical drive is bound to the same physical drive number */
00047 #define LD2PT(vol) 0            /* Find first valid partition or in SFD */
00048 
00049 #endif
00050 
00051 
00052 
00053 /* Type of path name strings on FatFs API */
00054 
00055 #if _LFN_UNICODE            /* Unicode string */
00056 #if !_USE_LFN
00057 #error _LFN_UNICODE must be 0 at non-LFN cfg.
00058 #endif
00059 #ifndef _INC_TCHAR
00060 typedef WCHAR TCHAR;
00061 #define _T(x) L ## x
00062 #define _TEXT(x) L ## x
00063 #endif
00064 
00065 #else                       /* ANSI/OEM string */
00066 #ifndef _INC_TCHAR
00067 typedef char TCHAR;
00068 #define _T(x) x
00069 #define _TEXT(x) x
00070 #endif
00071 
00072 #endif
00073 
00074 
00075 
00076 /* File system object structure (FATFS) */
00077 
00078 typedef struct {
00079   union{
00080     UINT    d32[_MAX_SS/4]; /* Force 32bits alignement */     
00081     BYTE    d8[_MAX_SS];    /* Disk access window for Directory, FAT (and file data at tiny cfg) */  
00082   }win;
00083     BYTE    fs_type;        /* FAT sub-type (0:Not mounted) */
00084     BYTE    drv;            /* Physical drive number */
00085     BYTE    csize;          /* Sectors per cluster (1,2,4...128) */
00086     BYTE    n_fats;         /* Number of FAT copies (1 or 2) */
00087     BYTE    wflag;          /* win[] flag (b0:dirty) */
00088     BYTE    fsi_flag;       /* FSINFO flags (b7:disabled, b0:dirty) */
00089     WORD    id;             /* File system mount ID */
00090     WORD    n_rootdir;      /* Number of root directory entries (FAT12/16) */
00091 #if _MAX_SS != _MIN_SS
00092     WORD    ssize;          /* Bytes per sector (512, 1024, 2048 or 4096) */
00093 #endif
00094 #if _FS_REENTRANT
00095     _SYNC_t sobj;           /* Identifier of sync object */
00096 #endif
00097 #if !_FS_READONLY
00098     DWORD   last_clust;     /* Last allocated cluster */
00099     DWORD   free_clust;     /* Number of free clusters */
00100 #endif
00101 #if _FS_RPATH
00102     DWORD   cdir;           /* Current directory start cluster (0:root) */
00103 #endif
00104     DWORD   n_fatent;       /* Number of FAT entries, = number of clusters + 2 */
00105     DWORD   fsize;          /* Sectors per FAT */
00106     DWORD   volbase;        /* Volume start sector */
00107     DWORD   fatbase;        /* FAT start sector */
00108     DWORD   dirbase;        /* Root directory start sector (FAT32:Cluster#) */
00109     DWORD   database;       /* Data start sector */
00110     DWORD   winsect;        /* Current sector appearing in the win[] */
00111     
00112 } FATFS;
00113 
00114 
00115 
00116 /* File object structure (FIL) */
00117 
00118 typedef struct {
00119 #if !_FS_TINY
00120   union{  
00121     UINT    d32[_MAX_SS/4]; /* Force 32bits alignement */     
00122     BYTE    d8[_MAX_SS];    /* File data read/write buffer */
00123   }buf;
00124 #endif
00125     FATFS*  fs;             /* Pointer to the related file system object (**do not change order**) */
00126     WORD    id;             /* Owner file system mount ID (**do not change order**) */
00127     BYTE    flag;           /* Status flags */
00128     BYTE    err;            /* Abort flag (error code) */
00129     DWORD   fptr;           /* File read/write pointer (Zeroed on file open) */
00130     DWORD   fsize;          /* File size */
00131     DWORD   sclust;         /* File start cluster (0:no cluster chain, always 0 when fsize is 0) */
00132     DWORD   clust;          /* Current cluster of fpter (not valid when fprt is 0) */
00133     DWORD   dsect;          /* Sector number appearing in buf[] (0:invalid) */
00134 #if !_FS_READONLY
00135     DWORD   dir_sect;       /* Sector number containing the directory entry */
00136     BYTE*   dir_ptr;        /* Pointer to the directory entry in the win[] */
00137 #endif
00138 #if _USE_FASTSEEK
00139     DWORD*  cltbl;          /* Pointer to the cluster link map table (Nulled on file open) */
00140 #endif
00141 #if _FS_LOCK
00142     UINT    lockid;         /* File lock ID origin from 1 (index of file semaphore table Files[]) */
00143 #endif
00144 
00145 } FIL;
00146 
00147 
00148 
00149 /* Directory object structure (DIR) */
00150 
00151 typedef struct {
00152 #if !_FS_TINY
00153   union{  
00154             UINT     d32[_MAX_SS/4];  /* Force 32bits alignement */  
00155             BYTE   d8[_MAX_SS];  /* File data read/write buffer */
00156   }buf;
00157 #endif   
00158     FATFS*  fs;             /* Pointer to the owner file system object (**do not change order**) */
00159     WORD    id;             /* Owner file system mount ID (**do not change order**) */
00160     WORD    index;          /* Current read/write index number */
00161     DWORD   sclust;         /* Table start cluster (0:Root dir) */
00162     DWORD   clust;          /* Current cluster */
00163     DWORD   sect;           /* Current sector */
00164     BYTE*   dir;            /* Pointer to the current SFN entry in the win[] */
00165     BYTE*   fn;             /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
00166 #if _FS_LOCK
00167     UINT    lockid;         /* File lock ID (index of file semaphore table Files[]) */
00168 #endif
00169 #if _USE_LFN
00170     WCHAR*  lfn;            /* Pointer to the LFN working buffer */
00171     WORD    lfn_idx;        /* Last matched LFN index number (0xFFFF:No LFN) */
00172 #endif
00173 #if _USE_FIND
00174     const TCHAR*    pat;    /* Pointer to the name matching pattern */
00175 #endif
00176 } DIR_;
00177 
00178 
00179 
00180 /* File information structure (FILINFO) */
00181 
00182 typedef struct {
00183     DWORD   fsize;          /* File size */
00184     WORD    fdate;          /* Last modified date */
00185     WORD    ftime;          /* Last modified time */
00186     BYTE    fattrib;        /* Attribute */
00187     TCHAR   fname[13];      /* Short file name (8.3 format) */
00188 #if _USE_LFN
00189     TCHAR*  lfname;         /* Pointer to the LFN buffer */
00190     UINT    lfsize;         /* Size of LFN buffer in TCHAR */
00191 #endif
00192 } FILINFO;
00193 
00194 
00195 
00196 /* File function return code (FRESULT) */
00197 
00198 typedef enum {
00199     FR_OK = 0,              /* (0) Succeeded */
00200     FR_DISK_ERR,            /* (1) A hard error occurred in the low level disk I/O layer */
00201     FR_INT_ERR,             /* (2) Assertion failed */
00202     FR_NOT_READY,           /* (3) The physical drive cannot work */
00203     FR_NO_FILE,             /* (4) Could not find the file */
00204     FR_NO_PATH,             /* (5) Could not find the path */
00205     FR_INVALID_NAME,        /* (6) The path name format is invalid */
00206     FR_DENIED,              /* (7) Access denied due to prohibited access or directory full */
00207     FR_EXIST,               /* (8) Access denied due to prohibited access */
00208     FR_INVALID_OBJECT,      /* (9) The file/directory object is invalid */
00209     FR_WRITE_PROTECTED,     /* (10) The physical drive is write protected */
00210     FR_INVALID_DRIVE,       /* (11) The logical drive number is invalid */
00211     FR_NOT_ENABLED,         /* (12) The volume has no work area */
00212     FR_NO_FILESYSTEM,       /* (13) There is no valid FAT volume */
00213     FR_MKFS_ABORTED,        /* (14) The f_mkfs() aborted due to any parameter error */
00214     FR_TIMEOUT,             /* (15) Could not get a grant to access the volume within defined period */
00215     FR_LOCKED,              /* (16) The operation is rejected according to the file sharing policy */
00216     FR_NOT_ENOUGH_CORE,     /* (17) LFN working buffer could not be allocated */
00217     FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
00218     FR_INVALID_PARAMETER    /* (19) Given parameter is invalid */
00219 } FRESULT;
00220 
00221 
00222 
00223 /*--------------------------------------------------------------*/
00224 /* FatFs module application interface                           */
00225 
00226 FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode);             /* Open or create a file */
00227 FRESULT f_close (FIL* fp);                                          /* Close an open file object */
00228 FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br);           /* Read data from a file */
00229 FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw);    /* Write data to a file */
00230 FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
00231 FRESULT f_lseek (FIL* fp, DWORD ofs);                               /* Move file pointer of a file object */
00232 FRESULT f_truncate (FIL* fp);                                       /* Truncate file */
00233 FRESULT f_sync (FIL* fp);                                           /* Flush cached data of a writing file */
00234 FRESULT f_opendir (DIR_* dp, const TCHAR* path);                     /* Open a directory */
00235 FRESULT f_closedir (DIR_* dp);                                       /* Close an open directory */
00236 FRESULT f_readdir (DIR_* dp, FILINFO* fno);                          /* Read a directory item */
00237 FRESULT f_findfirst (DIR_* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern);   /* Find first file */
00238 FRESULT f_findnext (DIR_* dp, FILINFO* fno);                         /* Find next file */
00239 FRESULT f_mkdir (const TCHAR* path);                                /* Create a sub directory */
00240 FRESULT f_unlink (const TCHAR* path);                               /* Delete an existing file or directory */
00241 FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new);    /* Rename/Move a file or directory */
00242 FRESULT f_stat (const TCHAR* path, FILINFO* fno);                   /* Get file status */
00243 FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask);          /* Change attribute of the file/dir */
00244 FRESULT f_utime (const TCHAR* path, const FILINFO* fno);            /* Change times-tamp of the file/dir */
00245 FRESULT f_chdir (const TCHAR* path);                                /* Change current directory */
00246 FRESULT f_chdrive (const TCHAR* path);                              /* Change current drive */
00247 FRESULT f_getcwd (TCHAR* buff, UINT len);                           /* Get current directory */
00248 FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
00249 FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn);   /* Get volume label */
00250 FRESULT f_setlabel (const TCHAR* label);                            /* Set volume label */
00251 FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt);           /* Mount/Unmount a logical drive */
00252 FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au);              /* Create a file system on the volume */
00253 FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work);         /* Divide a physical drive into some partitions */
00254 int f_putc (TCHAR c, FIL* fp);                                      /* Put a character to the file */
00255 int f_puts (const TCHAR* str, FIL* cp);                             /* Put a string to the file */
00256 int f_printf (FIL* fp, const TCHAR* str, ...);                      /* Put a formatted string to the file */
00257 TCHAR* f_gets (TCHAR* buff, int len, FIL* fp);                      /* Get a string from the file */
00258 
00259 #define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize))
00260 #define f_error(fp) ((fp)->err)
00261 #define f_tell(fp) ((fp)->fptr)
00262 #define f_size(fp) ((fp)->fsize)
00263 #define f_rewind(fp) f_lseek((fp), 0)
00264 #define f_rewinddir(dp) f_readdir((dp), 0)
00265 
00266 #ifndef EOF
00267 #define EOF (-1)
00268 #endif
00269 
00270 
00271 
00272 
00273 /*--------------------------------------------------------------*/
00274 /* Additional user defined functions                            */
00275 
00276 /* RTC function */
00277 #if !_FS_READONLY && !_FS_NORTC
00278 DWORD get_fattime (void);
00279 #endif
00280 
00281 /* Unicode support functions */
00282 #if _USE_LFN                            /* Unicode - OEM code conversion */
00283 WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
00284 WCHAR ff_wtoupper (WCHAR chr);          /* Unicode upper-case conversion */
00285 #if _USE_LFN == 3                       /* Memory functions */
00286 void* ff_memalloc (UINT msize);         /* Allocate memory block */
00287 void ff_memfree (void* mblock);         /* Free memory block */
00288 #endif
00289 #endif
00290 
00291 /* Sync functions */
00292 #if _FS_REENTRANT
00293 int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj);   /* Create a sync object */
00294 int ff_req_grant (_SYNC_t sobj);                /* Lock sync object */
00295 void ff_rel_grant (_SYNC_t sobj);               /* Unlock sync object */
00296 int ff_del_syncobj (_SYNC_t sobj);              /* Delete a sync object */
00297 #endif
00298 
00299 
00300 
00301 
00302 /*--------------------------------------------------------------*/
00303 /* Flags and offset address                                     */
00304 
00305 
00306 /* File access control and file status flags (FIL.flag) */
00307 
00308 #define FA_READ             0x01
00309 #define FA_OPEN_EXISTING    0x00
00310 
00311 #if !_FS_READONLY
00312 #define FA_WRITE            0x02
00313 #define FA_CREATE_NEW       0x04
00314 #define FA_CREATE_ALWAYS    0x08
00315 #define FA_OPEN_ALWAYS      0x10
00316 #define FA__WRITTEN         0x20
00317 #define FA__DIRTY           0x40
00318 #endif
00319 
00320 
00321 /* FAT sub type (FATFS.fs_type) */
00322 
00323 #define FS_FAT12    1
00324 #define FS_FAT16    2
00325 #define FS_FAT32    3
00326 
00327 
00328 /* File attribute bits for directory entry */
00329 
00330 #define AM_RDO  0x01    /* Read only */
00331 #define AM_HID  0x02    /* Hidden */
00332 #define AM_SYS  0x04    /* System */
00333 #define AM_VOL  0x08    /* Volume label */
00334 #define AM_LFN  0x0F    /* LFN entry */
00335 #define AM_DIR  0x10    /* Directory */
00336 #define AM_ARC  0x20    /* Archive */
00337 #define AM_MASK 0x3F    /* Mask of defined bits */
00338 
00339 
00340 /* Fast seek feature */
00341 #define CREATE_LINKMAP  0xFFFFFFFF
00342 
00343 
00344 
00345 /*--------------------------------*/
00346 /* Multi-byte word access macros  */
00347 
00348 #if _WORD_ACCESS == 1   /* Enable word access to the FAT structure */
00349 #define LD_WORD(ptr)        (WORD)(*(WORD*)(BYTE*)(ptr))
00350 #define LD_DWORD(ptr)       (DWORD)(*(DWORD*)(BYTE*)(ptr))
00351 #define ST_WORD(ptr,val)    *(WORD*)(BYTE*)(ptr)=(WORD)(val)
00352 #define ST_DWORD(ptr,val)   *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
00353 #else                   /* Use byte-by-byte access to the FAT structure */
00354 #define LD_WORD(ptr)        (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
00355 #define LD_DWORD(ptr)       (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
00356 #define ST_WORD(ptr,val)    *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
00357 #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)
00358 #endif
00359 
00360 #ifdef __cplusplus
00361 }
00362 #endif
00363 
00364 #endif /* _FATFS */