00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _FATFS
00018
00019 #define _MCU_ENDIAN 1
00020
00021
00022
00023
00024
00025
00026
00027 #define _FS_READONLY 0
00028
00029
00030
00031
00032 #define _FS_MINIMIZE 0
00033
00034
00035
00036
00037
00038
00039 #define _USE_STRFUNC 0
00040
00041
00042 #define _USE_MKFS 0
00043
00044
00045
00046 #define _DRIVES 2
00047
00048
00049 #define _MULTI_PARTITION 0
00050
00051
00052
00053
00054 #define _USE_FSINFO 0
00055
00056
00057 #define _USE_SJIS 1
00058
00059
00060
00061 #define _USE_NTFLAG 1
00062
00063
00064
00065
00066 #include "integer.h"
00067
00068
00069
00070
00071 #define S_MAX_SIZ 512U
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
00080 typedef struct _FATFS {
00081 WORD id;
00082 WORD n_rootdir;
00083 DWORD winsect;
00084 DWORD sects_fat;
00085 DWORD max_clust;
00086 DWORD fatbase;
00087 DWORD dirbase;
00088 DWORD database;
00089 #if !_FS_READONLY
00090 DWORD last_clust;
00091 DWORD free_clust;
00092 #if _USE_FSINFO
00093 DWORD fsi_sector;
00094 BYTE fsi_flag;
00095 BYTE pad2;
00096 #endif
00097 #endif
00098 BYTE fs_type;
00099 BYTE csize;
00100 #if S_MAX_SIZ > 512U
00101 WORD s_size;
00102 #endif
00103 BYTE n_fats;
00104 BYTE drive;
00105 BYTE winflag;
00106 BYTE pad1;
00107 BYTE win[S_MAX_SIZ];
00108 } FATFS;
00109
00110
00111
00112 typedef struct _DIR {
00113 WORD id;
00114 WORD index;
00115 FATFS* fs;
00116 DWORD sclust;
00117 DWORD clust;
00118 DWORD sect;
00119 } DIR;
00120
00121
00122
00123 typedef struct _FIL {
00124 WORD id;
00125 BYTE flag;
00126 BYTE csect;
00127 FATFS* fs;
00128 DWORD fptr;
00129 DWORD fsize;
00130 DWORD org_clust;
00131 DWORD curr_clust;
00132 DWORD curr_sect;
00133 #if _FS_READONLY == 0
00134 DWORD dir_sect;
00135 BYTE* dir_ptr;
00136 #endif
00137 BYTE buffer[S_MAX_SIZ];
00138 } FIL;
00139
00140
00141
00142 typedef struct _FILINFO {
00143 DWORD fsize;
00144 WORD fdate;
00145 WORD ftime;
00146 BYTE fattrib;
00147 char fname[8+1+3+1];
00148 } FILINFO;
00149
00150
00151
00152
00153
00154 #if _MULTI_PARTITION != 0
00155
00156 typedef struct _PARTITION {
00157 BYTE pd;
00158 BYTE pt;
00159 } PARTITION;
00160 extern
00161 const PARTITION Drives[];
00162 #define LD2PD(drv) (Drives[drv].pd)
00163 #define LD2PT(drv) (Drives[drv].pt)
00164
00165 #else
00166
00167 #define LD2PD(drv) (drv)
00168 #define LD2PT(drv) 0
00169
00170 #endif
00171
00172
00173
00174
00175 typedef enum {
00176 FR_OK = 0,
00177 FR_NOT_READY,
00178 FR_NO_FILE,
00179 FR_NO_PATH,
00180 FR_INVALID_NAME,
00181 FR_INVALID_DRIVE,
00182 FR_DENIED,
00183 FR_EXIST,
00184 FR_RW_ERROR,
00185 FR_WRITE_PROTECTED,
00186 FR_NOT_ENABLED,
00187 FR_NO_FILESYSTEM,
00188 FR_INVALID_OBJECT,
00189 FR_MKFS_ABORTED
00190 } FRESULT;
00191
00192
00193
00194
00195
00196
00197 FRESULT f_mount (BYTE, FATFS*);
00198 FRESULT f_open (FIL*, const char*, BYTE);
00199 FRESULT f_read (FIL*, void*, UINT, UINT*);
00200 FRESULT f_write (FIL*, const void*, UINT, UINT*);
00201 FRESULT f_lseek (FIL*, DWORD);
00202 FRESULT f_close (FIL*);
00203 FRESULT f_opendir (DIR*, const char*);
00204 FRESULT f_readdir (DIR*, FILINFO*);
00205 FRESULT f_stat (const char*, FILINFO*);
00206 FRESULT f_getfree (const char*, DWORD*, FATFS**);
00207 FRESULT f_truncate (FIL*);
00208 FRESULT f_sync (FIL*);
00209 FRESULT f_unlink (const char*);
00210 FRESULT f_mkdir (const char*);
00211 FRESULT f_chmod (const char*, BYTE, BYTE);
00212 FRESULT f_utime (const char*, const FILINFO*);
00213 FRESULT f_rename (const char*, const char*);
00214 FRESULT f_mkfs (BYTE, BYTE, WORD);
00215 #if _USE_STRFUNC
00216 #define feof(fp) ((fp)->fptr == (fp)->fsize)
00217 #define EOF -1
00218 int fputc (int, FIL*);
00219 int fputs (const char*, FIL*);
00220 int fprintf (FIL*, const char*, ...);
00221 char* fgets (char*, int, FIL*);
00222 #endif
00223
00224
00225
00226 DWORD get_fattime (void);
00227
00228
00229
00230
00231
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
00247
00248 #define FS_FAT12 1
00249 #define FS_FAT16 2
00250 #define FS_FAT32 3
00251
00252
00253
00254
00255 #define AM_RDO 0x01
00256 #define AM_HID 0x02
00257 #define AM_SYS 0x04
00258 #define AM_VOL 0x08
00259 #define AM_LFN 0x0F
00260 #define AM_DIR 0x10
00261 #define AM_ARC 0x20
00262
00263
00264
00265
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
00322
00323 #if _MCU_ENDIAN == 1
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
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