old FATFilesystem
Fork of FatFileSystem by
Embed:
(wiki syntax)
Show/hide line numbers
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 #ifdef __cplusplus 00069 extern "C" { 00070 #endif 00071 00072 /* Definitions corresponds to multiple sector size (not tested) */ 00073 #define S_MAX_SIZ 512U /* Do not change */ 00074 #if S_MAX_SIZ > 512U 00075 #define SS(fs) ((fs)->s_size) 00076 #else 00077 #define SS(fs) 512U 00078 #endif 00079 00080 00081 /* File system object structure */ 00082 typedef struct _FATFS { 00083 WORD id; /* File system mount ID */ 00084 WORD n_rootdir; /* Number of root directory entries */ 00085 DWORD winsect; /* Current sector appearing in the win[] */ 00086 DWORD sects_fat; /* Sectors per fat */ 00087 DWORD max_clust; /* Maximum cluster# + 1 */ 00088 DWORD fatbase; /* FAT start sector */ 00089 DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ 00090 DWORD database; /* Data start sector */ 00091 #if !_FS_READONLY 00092 DWORD last_clust; /* Last allocated cluster */ 00093 DWORD free_clust; /* Number of free clusters */ 00094 #if _USE_FSINFO 00095 DWORD fsi_sector; /* fsinfo sector */ 00096 BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */ 00097 BYTE pad2; 00098 #endif 00099 #endif 00100 BYTE fs_type; /* FAT sub type */ 00101 BYTE csize; /* Number of sectors per cluster */ 00102 #if S_MAX_SIZ > 512U 00103 WORD s_size; /* Sector size */ 00104 #endif 00105 BYTE n_fats; /* Number of FAT copies */ 00106 BYTE drive; /* Physical drive number */ 00107 BYTE winflag; /* win[] dirty flag (1:must be written back) */ 00108 BYTE pad1; 00109 BYTE win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */ 00110 } FATFS; 00111 00112 00113 /* Directory object structure */ 00114 typedef struct _DIR { 00115 WORD id; /* Owner file system mount ID */ 00116 WORD index; /* Current index */ 00117 FATFS* fs; /* Pointer to the owner file system object */ 00118 DWORD sclust; /* Start cluster */ 00119 DWORD clust; /* Current cluster */ 00120 DWORD sect; /* Current sector */ 00121 } FATFS_DIR; 00122 00123 00124 /* File object structure */ 00125 typedef struct _FIL { 00126 WORD id; /* Owner file system mount ID */ 00127 BYTE flag; /* File status flags */ 00128 BYTE csect; /* Sector address in the cluster */ 00129 FATFS* fs; /* Pointer to the owner file system object */ 00130 DWORD fptr; /* File R/W pointer */ 00131 DWORD fsize; /* File size */ 00132 DWORD org_clust; /* File start cluster */ 00133 DWORD curr_clust; /* Current cluster */ 00134 DWORD curr_sect; /* Current sector */ 00135 #if _FS_READONLY == 0 00136 DWORD dir_sect; /* Sector containing the directory entry */ 00137 BYTE* dir_ptr; /* Ponter to the directory entry in the window */ 00138 #endif 00139 BYTE buffer[S_MAX_SIZ]; /* File R/W buffer */ 00140 } FIL; 00141 00142 00143 /* File status structure */ 00144 typedef struct _FILINFO { 00145 DWORD fsize; /* Size */ 00146 WORD fdate; /* Date */ 00147 WORD ftime; /* Time */ 00148 BYTE fattrib; /* Attribute */ 00149 char fname[8+1+3+1]; /* Name (8.3 format) */ 00150 } FILINFO; 00151 00152 00153 00154 /* Definitions corresponds to multi partition */ 00155 00156 #if _MULTI_PARTITION != 0 /* Multiple partition cfg */ 00157 00158 typedef struct _PARTITION { 00159 BYTE pd; /* Physical drive # (0-255) */ 00160 BYTE pt; /* Partition # (0-3) */ 00161 } PARTITION; 00162 extern 00163 const PARTITION Drives[]; /* Logical drive# to physical location conversion table */ 00164 #define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */ 00165 #define LD2PT(drv) (Drives[drv].pt) /* Get partition# */ 00166 00167 #else /* Single partition cfg */ 00168 00169 #define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */ 00170 #define LD2PT(drv) 0 /* Always mounts the 1st partition */ 00171 00172 #endif 00173 00174 00175 /* File function return code (FRESULT) */ 00176 00177 typedef enum { 00178 FR_OK = 0, /* 0 */ 00179 FR_NOT_READY, /* 1 */ 00180 FR_NO_FILE, /* 2 */ 00181 FR_NO_PATH, /* 3 */ 00182 FR_INVALID_NAME, /* 4 */ 00183 FR_INVALID_DRIVE, /* 5 */ 00184 FR_DENIED, /* 6 */ 00185 FR_EXIST, /* 7 */ 00186 FR_RW_ERROR, /* 8 */ 00187 FR_WRITE_PROTECTED, /* 9 */ 00188 FR_NOT_ENABLED, /* 10 */ 00189 FR_NO_FILESYSTEM, /* 11 */ 00190 FR_INVALID_OBJECT, /* 12 */ 00191 FR_MKFS_ABORTED /* 13 */ 00192 } FRESULT; 00193 00194 00195 00196 /*-----------------------------------------------------*/ 00197 /* FatFs module application interface */ 00198 00199 FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */ 00200 FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ 00201 FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */ 00202 FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */ 00203 FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */ 00204 FRESULT f_close (FIL*); /* Close an open file object */ 00205 FRESULT f_opendir (FATFS_DIR*, const char*); /* Open an existing directory */ 00206 FRESULT f_readdir (FATFS_DIR*, FILINFO*); /* Read a directory item */ 00207 FRESULT f_stat (const char*, FILINFO*); /* Get file status */ 00208 FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */ 00209 FRESULT f_truncate (FIL*); /* Truncate file */ 00210 FRESULT f_sync (FIL*); /* Flush cached data of a writing file */ 00211 FRESULT f_unlink (const char*); /* Delete an existing file or directory */ 00212 FRESULT f_mkdir (const char*); /* Create a new directory */ 00213 FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */ 00214 FRESULT f_utime (const char*, const FILINFO*); /* Change file/dir timestamp */ 00215 FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */ 00216 FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */ 00217 #if _USE_STRFUNC 00218 #define feof(fp) ((fp)->fptr == (fp)->fsize) 00219 #define EOF -1 00220 int fputc (int, FIL*); /* Put a character to the file */ 00221 int fputs (const char*, FIL*); /* Put a string to the file */ 00222 int fprintf (FIL*, const char*, ...); /* Put a formatted string to the file */ 00223 char* fgets (char*, int, FIL*); /* Get a string from the file */ 00224 #endif 00225 00226 /* User defined function to give a current time to fatfs module */ 00227 00228 DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */ 00229 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ 00230 00231 00232 00233 /* File access control and file status flags (FIL.flag) */ 00234 00235 #define FA_READ 0x01 00236 #define FA_OPEN_EXISTING 0x00 00237 #if _FS_READONLY == 0 00238 #define FA_WRITE 0x02 00239 #define FA_CREATE_NEW 0x04 00240 #define FA_CREATE_ALWAYS 0x08 00241 #define FA_OPEN_ALWAYS 0x10 00242 #define FA__WRITTEN 0x20 00243 #define FA__DIRTY 0x40 00244 #endif 00245 #define FA__ERROR 0x80 00246 00247 00248 /* FAT sub type (FATFS.fs_type) */ 00249 00250 #define FS_FAT12 1 00251 #define FS_FAT16 2 00252 #define FS_FAT32 3 00253 00254 00255 /* File attribute bits for directory entry */ 00256 00257 #define AM_RDO 0x01 /* Read only */ 00258 #define AM_HID 0x02 /* Hidden */ 00259 #define AM_SYS 0x04 /* System */ 00260 #define AM_VOL 0x08 /* Volume label */ 00261 #define AM_LFN 0x0F /* LFN entry */ 00262 #define AM_DIR 0x10 /* Directory */ 00263 #define AM_ARC 0x20 /* Archive */ 00264 00265 00266 00267 /* Offset of FAT structure members */ 00268 00269 #define BS_jmpBoot 0 00270 #define BS_OEMName 3 00271 #define BPB_BytsPerSec 11 00272 #define BPB_SecPerClus 13 00273 #define BPB_RsvdSecCnt 14 00274 #define BPB_NumFATs 16 00275 #define BPB_RootEntCnt 17 00276 #define BPB_TotSec16 19 00277 #define BPB_Media 21 00278 #define BPB_FATSz16 22 00279 #define BPB_SecPerTrk 24 00280 #define BPB_NumHeads 26 00281 #define BPB_HiddSec 28 00282 #define BPB_TotSec32 32 00283 #define BS_55AA 510 00284 00285 #define BS_DrvNum 36 00286 #define BS_BootSig 38 00287 #define BS_VolID 39 00288 #define BS_VolLab 43 00289 #define BS_FilSysType 54 00290 00291 #define BPB_FATSz32 36 00292 #define BPB_ExtFlags 40 00293 #define BPB_FSVer 42 00294 #define BPB_RootClus 44 00295 #define BPB_FSInfo 48 00296 #define BPB_BkBootSec 50 00297 #define BS_DrvNum32 64 00298 #define BS_BootSig32 66 00299 #define BS_VolID32 67 00300 #define BS_VolLab32 71 00301 #define BS_FilSysType32 82 00302 00303 #define FSI_LeadSig 0 00304 #define FSI_StrucSig 484 00305 #define FSI_Free_Count 488 00306 #define FSI_Nxt_Free 492 00307 00308 #define MBR_Table 446 00309 00310 #define DIR_Name 0 00311 #define DIR_Attr 11 00312 #define DIR_NTres 12 00313 #define DIR_CrtTime 14 00314 #define DIR_CrtDate 16 00315 #define DIR_FstClusHI 20 00316 #define DIR_WrtTime 22 00317 #define DIR_WrtDate 24 00318 #define DIR_FstClusLO 26 00319 #define DIR_FileSize 28 00320 00321 00322 00323 /* Multi-byte word access macros */ 00324 00325 #if _MCU_ENDIAN == 1 /* Use word access */ 00326 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) 00327 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) 00328 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) 00329 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) 00330 #elif _MCU_ENDIAN == 2 /* Use byte-by-byte access */ 00331 #define LD_WORD(ptr) (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr)) 00332 #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)) 00333 #define ST_WORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8) 00334 #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) 00335 #else 00336 #error Do not forget to set _MCU_ENDIAN properly! 00337 #endif 00338 00339 #ifdef __cplusplus 00340 }; 00341 #endif 00342 00343 #define _FATFS 00344 #endif /* _FATFS */
Generated on Mon Jul 18 2022 00:47:31 by 1.7.2