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