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