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.06 (C)ChaN, 2008 00003 /---------------------------------------------------------------------------/ 00004 / FatFs module is an experimenal project to implement FAT file system to 00005 / cheap microcontrollers. This is a free software and is opened for education, 00006 / research and development under license policy of following trems. 00007 / 00008 / Copyright (C) 2008, ChaN, all right reserved. 00009 / 00010 / * The FatFs module is a free software and there is no warranty. 00011 / * You can use, modify and/or redistribute it for personal, non-profit or 00012 / commercial use without any restriction under your responsibility. 00013 / * Redistributions of source code must retain the above copyright notice. 00014 / 00015 /---------------------------------------------------------------------------*/ 00016 00017 #ifndef _FATFS 00018 00019 #define _MCU_ENDIAN 2 00020 /* The _MCU_ENDIAN defines which access method is used to the FAT structure. 00021 / 1: Enable word access. 00022 / 2: Disable word access and use byte-by-byte access instead. 00023 / When the architectural byte order of the MCU is big-endian and/or address 00024 / miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2. 00025 / If it is not the case, it can also be set to 1 for good code efficiency. */ 00026 00027 #define _FS_READONLY 0 00028 /* Setting _FS_READONLY to 1 defines read only configuration. This removes 00029 / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename, 00030 / f_truncate and useless f_getfree. */ 00031 00032 #define _FS_MINIMIZE 0 00033 /* The _FS_MINIMIZE option defines minimization level to remove some functions. 00034 / 0: Full function. 00035 / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename are removed. 00036 / 2: f_opendir and f_readdir are removed in addition to level 1. 00037 / 3: f_lseek is removed in addition to level 2. */ 00038 00039 #define _USE_STRFUNC 0 00040 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */ 00041 00042 #define _USE_MKFS 1 00043 /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is 00044 / enabled. */ 00045 00046 #define _DRIVES 4 00047 /* Number of logical drives to be used. This affects the size of internal table. */ 00048 00049 #define _MULTI_PARTITION 0 00050 /* When _MULTI_PARTITION is set to 0, each logical drive is bound to same 00051 / physical drive number and can mount only 1st primaly partition. When it is 00052 / set to 1, each logical drive can mount a partition listed in Drives[]. */ 00053 00054 #define _USE_FSINFO 0 00055 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */ 00056 00057 #define _USE_SJIS 1 00058 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise 00059 / only US-ASCII(7bit) code can be accepted as file/directory name. */ 00060 00061 #define _USE_NTFLAG 1 00062 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. 00063 / Note that the files are always accessed in case insensitive. */ 00064 00065 00066 #include "integer.h" 00067 00068 00069 00070 /* Definitions corresponds to multiple sector size (not tested) */ 00071 #define S_MAX_SIZ 512U /* Do not change */ 00072 #if S_MAX_SIZ > 512U 00073 #define SS(fs) ((fs)->s_size) 00074 #else 00075 #define SS(fs) 512U 00076 #endif 00077 00078 00079 /* File system object structure */ 00080 typedef struct _FATFS { 00081 WORD id; /* File system mount ID */ 00082 WORD n_rootdir; /* Number of root directory entries */ 00083 DWORD winsect; /* Current sector appearing in the win[] */ 00084 DWORD sects_fat; /* Sectors per fat */ 00085 DWORD max_clust; /* Maximum cluster# + 1 */ 00086 DWORD fatbase; /* FAT start sector */ 00087 DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ 00088 DWORD database; /* Data start sector */ 00089 #if !_FS_READONLY 00090 DWORD last_clust; /* Last allocated cluster */ 00091 DWORD free_clust; /* Number of free clusters */ 00092 #if _USE_FSINFO 00093 DWORD fsi_sector; /* fsinfo sector */ 00094 BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */ 00095 BYTE pad2; 00096 #endif 00097 #endif 00098 BYTE fs_type; /* FAT sub type */ 00099 BYTE csize; /* Number of sectors per cluster */ 00100 #if S_MAX_SIZ > 512U 00101 WORD s_size; /* Sector size */ 00102 #endif 00103 BYTE n_fats; /* Number of FAT copies */ 00104 BYTE drive; /* Physical drive number */ 00105 BYTE winflag; /* win[] dirty flag (1:must be written back) */ 00106 BYTE pad1; 00107 BYTE win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */ 00108 } FATFS; 00109 00110 00111 /* Directory object structure */ 00112 typedef struct _DIR { 00113 WORD id; /* Owner file system mount ID */ 00114 WORD index; /* Current index */ 00115 FATFS* fs; /* Pointer to the owner file system object */ 00116 DWORD sclust; /* Start cluster */ 00117 DWORD clust; /* Current cluster */ 00118 DWORD sect; /* Current sector */ 00119 } FATFS_DIR; 00120 00121 00122 /* File object structure */ 00123 typedef struct _FIL { 00124 WORD id; /* Owner file system mount ID */ 00125 BYTE flag; /* File status flags */ 00126 BYTE csect; /* Sector address in the cluster */ 00127 FATFS* fs; /* Pointer to the owner file system object */ 00128 DWORD fptr; /* File R/W pointer */ 00129 DWORD fsize; /* File size */ 00130 DWORD org_clust; /* File start cluster */ 00131 DWORD curr_clust; /* Current cluster */ 00132 DWORD curr_sect; /* Current sector */ 00133 #if _FS_READONLY == 0 00134 DWORD dir_sect; /* Sector containing the directory entry */ 00135 BYTE* dir_ptr; /* Ponter to the directory entry in the window */ 00136 #endif 00137 BYTE buffer[S_MAX_SIZ]; /* File R/W buffer */ 00138 } FIL; 00139 00140 00141 /* File status structure */ 00142 typedef struct _FILINFO { 00143 DWORD fsize; /* Size */ 00144 WORD fdate; /* Date */ 00145 WORD ftime; /* Time */ 00146 BYTE fattrib; /* Attribute */ 00147 char fname[8+1+3+1]; /* Name (8.3 format) */ 00148 } FILINFO; 00149 00150 00151 00152 /* Definitions corresponds to multi partition */ 00153 00154 #if _MULTI_PARTITION != 0 /* Multiple partition cfg */ 00155 00156 typedef struct _PARTITION { 00157 BYTE pd; /* Physical drive # (0-255) */ 00158 BYTE pt; /* Partition # (0-3) */ 00159 } PARTITION; 00160 extern 00161 const PARTITION Drives[]; /* Logical drive# to physical location conversion table */ 00162 #define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */ 00163 #define LD2PT(drv) (Drives[drv].pt) /* Get partition# */ 00164 00165 #else /* Single partition cfg */ 00166 00167 #define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */ 00168 #define LD2PT(drv) 0 /* Always mounts the 1st partition */ 00169 00170 #endif 00171 00172 00173 /* File function return code (FRESULT) */ 00174 00175 typedef enum { 00176 FR_OK = 0, /* 0 */ 00177 FR_NOT_READY, /* 1 */ 00178 FR_NO_FILE, /* 2 */ 00179 FR_NO_PATH, /* 3 */ 00180 FR_INVALID_NAME, /* 4 */ 00181 FR_INVALID_DRIVE, /* 5 */ 00182 FR_DENIED, /* 6 */ 00183 FR_EXIST, /* 7 */ 00184 FR_RW_ERROR, /* 8 */ 00185 FR_WRITE_PROTECTED, /* 9 */ 00186 FR_NOT_ENABLED, /* 10 */ 00187 FR_NO_FILESYSTEM, /* 11 */ 00188 FR_INVALID_OBJECT, /* 12 */ 00189 FR_MKFS_ABORTED /* 13 */ 00190 } FRESULT; 00191 00192 00193 00194 /*-----------------------------------------------------*/ 00195 /* FatFs module application interface */ 00196 00197 FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */ 00198 FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ 00199 FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */ 00200 FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */ 00201 FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */ 00202 FRESULT f_close (FIL*); /* Close an open file object */ 00203 FRESULT f_opendir (FATFS_DIR*, const char*); /* Open an existing directory */ 00204 FRESULT f_readdir (FATFS_DIR*, FILINFO*); /* Read a directory item */ 00205 FRESULT f_stat (const char*, FILINFO*); /* Get file status */ 00206 FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */ 00207 FRESULT f_truncate (FIL*); /* Truncate file */ 00208 FRESULT f_sync (FIL*); /* Flush cached data of a writing file */ 00209 FRESULT f_unlink (const char*); /* Delete an existing file or directory */ 00210 FRESULT f_mkdir (const char*); /* Create a new directory */ 00211 FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */ 00212 FRESULT f_utime (const char*, const FILINFO*); /* Change file/dir timestamp */ 00213 FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */ 00214 FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */ 00215 #if _USE_STRFUNC 00216 #define feof(fp) ((fp)->fptr == (fp)->fsize) 00217 #define EOF -1 00218 int fputc (int, FIL*); /* Put a character to the file */ 00219 int fputs (const char*, FIL*); /* Put a string to the file */ 00220 int fprintf (FIL*, const char*, ...); /* Put a formatted string to the file */ 00221 char* fgets (char*, int, FIL*); /* Get a string from the file */ 00222 #endif 00223 00224 /* User defined function to give a current time to fatfs module */ 00225 00226 DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ 00227 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ 00228 00229 00230 00231 /* File access control and file status flags (FIL.flag) */ 00232 00233 #define FA_READ 0x01 00234 #define FA_OPEN_EXISTING 0x00 00235 #if _FS_READONLY == 0 00236 #define FA_WRITE 0x02 00237 #define FA_CREATE_NEW 0x04 00238 #define FA_CREATE_ALWAYS 0x08 00239 #define FA_OPEN_ALWAYS 0x10 00240 #define FA__WRITTEN 0x20 00241 #define FA__DIRTY 0x40 00242 #endif 00243 #define FA__ERROR 0x80 00244 00245 00246 /* FAT sub type (FATFS.fs_type) */ 00247 00248 #define FS_FAT12 1 00249 #define FS_FAT16 2 00250 #define FS_FAT32 3 00251 00252 00253 /* File attribute bits for directory entry */ 00254 00255 #define AM_RDO 0x01 /* Read only */ 00256 #define AM_HID 0x02 /* Hidden */ 00257 #define AM_SYS 0x04 /* System */ 00258 #define AM_VOL 0x08 /* Volume label */ 00259 #define AM_LFN 0x0F /* LFN entry */ 00260 #define AM_DIR 0x10 /* Directory */ 00261 #define AM_ARC 0x20 /* Archive */ 00262 00263 00264 00265 /* Offset of FAT structure members */ 00266 00267 #define BS_jmpBoot 0 00268 #define BS_OEMName 3 00269 #define BPB_BytsPerSec 11 00270 #define BPB_SecPerClus 13 00271 #define BPB_RsvdSecCnt 14 00272 #define BPB_NumFATs 16 00273 #define BPB_RootEntCnt 17 00274 #define BPB_TotSec16 19 00275 #define BPB_Media 21 00276 #define BPB_FATSz16 22 00277 #define BPB_SecPerTrk 24 00278 #define BPB_NumHeads 26 00279 #define BPB_HiddSec 28 00280 #define BPB_TotSec32 32 00281 #define BS_55AA 510 00282 00283 #define BS_DrvNum 36 00284 #define BS_BootSig 38 00285 #define BS_VolID 39 00286 #define BS_VolLab 43 00287 #define BS_FilSysType 54 00288 00289 #define BPB_FATSz32 36 00290 #define BPB_ExtFlags 40 00291 #define BPB_FSVer 42 00292 #define BPB_RootClus 44 00293 #define BPB_FSInfo 48 00294 #define BPB_BkBootSec 50 00295 #define BS_DrvNum32 64 00296 #define BS_BootSig32 66 00297 #define BS_VolID32 67 00298 #define BS_VolLab32 71 00299 #define BS_FilSysType32 82 00300 00301 #define FSI_LeadSig 0 00302 #define FSI_StrucSig 484 00303 #define FSI_Free_Count 488 00304 #define FSI_Nxt_Free 492 00305 00306 #define MBR_Table 446 00307 00308 #define DIR_Name 0 00309 #define DIR_Attr 11 00310 #define DIR_NTres 12 00311 #define DIR_CrtTime 14 00312 #define DIR_CrtDate 16 00313 #define DIR_FstClusHI 20 00314 #define DIR_WrtTime 22 00315 #define DIR_WrtDate 24 00316 #define DIR_FstClusLO 26 00317 #define DIR_FileSize 28 00318 00319 00320 00321 /* Multi-byte word access macros */ 00322 00323 #if _MCU_ENDIAN == 1 /* Use word access */ 00324 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) 00325 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) 00326 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) 00327 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) 00328 #elif _MCU_ENDIAN == 2 /* Use byte-by-byte access */ 00329 #define LD_WORD(ptr) (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr)) 00330 #define LD_DWORD(ptr) (DWORD)(((DWORD)*(volatile BYTE*)((ptr)+3)<<24)|((DWORD)*(volatile BYTE*)((ptr)+2)<<16)|((WORD)*(volatile BYTE*)((ptr)+1)<<8)|*(volatile BYTE*)(ptr)) 00331 #define ST_WORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8) 00332 #define ST_DWORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(volatile BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(volatile BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24) 00333 #else 00334 #error Do not forget to set _MCU_ENDIAN properly! 00335 #endif 00336 00337 00338 #define _FATFS 00339 #endif /* _FATFS */
Generated on Tue Jul 12 2022 17:50:55 by
