00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _FATFS
00018
00019 #define _MCU_ENDIAN 2
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 _FAT32 1
00040
00041
00042 #define _USE_FSINFO 0
00043
00044
00045 #define _USE_SJIS 1
00046
00047
00048
00049 #define _USE_NTFLAG 1
00050
00051
00052
00053
00054 #include "integer.h"
00055
00056
00057
00058 #if _FAT32
00059 typedef DWORD CLUST;
00060 #else
00061 typedef WORD CLUST;
00062 #undef _USE_FSINFO
00063 #define _USE_FSINFO 0
00064 #endif
00065
00066
00067
00068 typedef struct _FATFS {
00069 WORD id;
00070 WORD n_rootdir;
00071 DWORD winsect;
00072 DWORD fatbase;
00073 DWORD dirbase;
00074 DWORD database;
00075 CLUST sects_fat;
00076 CLUST max_clust;
00077 #if !_FS_READONLY
00078 CLUST last_clust;
00079 CLUST free_clust;
00080 #if _USE_FSINFO
00081 DWORD fsi_sector;
00082 BYTE fsi_flag;
00083 BYTE pad1;
00084 #endif
00085 #endif
00086 BYTE fs_type;
00087 BYTE sects_clust;
00088 BYTE n_fats;
00089 BYTE winflag;
00090 BYTE win[512];
00091 } FATFS;
00092
00093
00094
00095 typedef struct _DIR {
00096 WORD id;
00097 WORD index;
00098 FATFS* fs;
00099 CLUST sclust;
00100 CLUST clust;
00101 DWORD sect;
00102 } DIR;
00103
00104
00105
00106 typedef struct _FIL {
00107 WORD id;
00108 BYTE flag;
00109 BYTE sect_clust;
00110 FATFS* fs;
00111 DWORD fptr;
00112 DWORD fsize;
00113 CLUST org_clust;
00114 CLUST curr_clust;
00115 DWORD curr_sect;
00116 #if !_FS_READONLY
00117 DWORD dir_sect;
00118 BYTE* dir_ptr;
00119 #endif
00120 } FIL;
00121
00122
00123
00124 typedef struct _FILINFO {
00125 DWORD fsize;
00126 WORD fdate;
00127 WORD ftime;
00128 BYTE fattrib;
00129 char fname[8+1+3+1];
00130 } FILINFO;
00131
00132
00133
00134
00135 typedef enum {
00136 FR_OK = 0,
00137 FR_NOT_READY,
00138 FR_NO_FILE,
00139 FR_NO_PATH,
00140 FR_INVALID_NAME,
00141 FR_INVALID_DRIVE,
00142 FR_DENIED,
00143 FR_EXIST,
00144 FR_RW_ERROR,
00145 FR_WRITE_PROTECTED,
00146 FR_NOT_ENABLED,
00147 FR_NO_FILESYSTEM,
00148 FR_INVALID_OBJECT
00149 } FRESULT;
00150
00151
00152
00153
00154
00155
00156 FRESULT f_mount (BYTE, FATFS*);
00157 FRESULT f_open (FIL*, const char*, BYTE);
00158 FRESULT f_read (FIL*, void*, WORD, WORD*);
00159 FRESULT f_write (FIL*, const void*, WORD, WORD*);
00160 FRESULT f_lseek (FIL*, DWORD);
00161 FRESULT f_close (FIL*);
00162 FRESULT f_opendir (DIR*, const char*);
00163 FRESULT f_readdir (DIR*, FILINFO*);
00164 FRESULT f_stat (const char*, FILINFO*);
00165 FRESULT f_getfree (const char*, DWORD*, FATFS**);
00166 FRESULT f_sync (FIL*);
00167 FRESULT f_unlink (const char*);
00168 FRESULT f_mkdir (const char*);
00169 FRESULT f_chmod (const char*, BYTE, BYTE);
00170 FRESULT f_rename (const char*, const char*);
00171
00172
00173
00174
00175 DWORD get_fattime (void);
00176
00177
00178
00179
00180
00181
00182 #define FA_READ 0x01
00183 #define FA_OPEN_EXISTING 0x00
00184 #if !_FS_READONLY
00185 #define FA_WRITE 0x02
00186 #define FA_CREATE_NEW 0x04
00187 #define FA_CREATE_ALWAYS 0x08
00188 #define FA_OPEN_ALWAYS 0x10
00189 #define FA__WRITTEN 0x20
00190 #endif
00191 #define FA__ERROR 0x80
00192
00193
00194
00195
00196 #define FS_FAT12 1
00197 #define FS_FAT16 2
00198 #define FS_FAT32 3
00199
00200
00201
00202
00203 #define AM_RDO 0x01
00204 #define AM_HID 0x02
00205 #define AM_SYS 0x04
00206 #define AM_VOL 0x08
00207 #define AM_LFN 0x0F
00208 #define AM_DIR 0x10
00209 #define AM_ARC 0x20
00210
00211
00212
00213
00214
00215 #define BS_jmpBoot 0
00216 #define BS_OEMName 3
00217 #define BPB_BytsPerSec 11
00218 #define BPB_SecPerClus 13
00219 #define BPB_RsvdSecCnt 14
00220 #define BPB_NumFATs 16
00221 #define BPB_RootEntCnt 17
00222 #define BPB_TotSec16 19
00223 #define BPB_Media 21
00224 #define BPB_FATSz16 22
00225 #define BPB_SecPerTrk 24
00226 #define BPB_NumHeads 26
00227 #define BPB_HiddSec 28
00228 #define BPB_TotSec32 32
00229 #define BS_55AA 510
00230
00231 #define BS_DrvNum 36
00232 #define BS_BootSig 38
00233 #define BS_VolID 39
00234 #define BS_VolLab 43
00235 #define BS_FilSysType 54
00236
00237 #define BPB_FATSz32 36
00238 #define BPB_ExtFlags 40
00239 #define BPB_FSVer 42
00240 #define BPB_RootClus 44
00241 #define BPB_FSInfo 48
00242 #define BPB_BkBootSec 50
00243 #define BS_DrvNum32 64
00244 #define BS_BootSig32 66
00245 #define BS_VolID32 67
00246 #define BS_VolLab32 71
00247 #define BS_FilSysType32 82
00248
00249 #define FSI_LeadSig 0
00250 #define FSI_StrucSig 484
00251 #define FSI_Free_Count 488
00252 #define FSI_Nxt_Free 492
00253
00254 #define MBR_Table 446
00255
00256 #define DIR_Name 0
00257 #define DIR_Attr 11
00258 #define DIR_NTres 12
00259 #define DIR_CrtTime 14
00260 #define DIR_CrtDate 16
00261 #define DIR_FstClusHI 20
00262 #define DIR_WrtTime 22
00263 #define DIR_WrtDate 24
00264 #define DIR_FstClusLO 26
00265 #define DIR_FileSize 28
00266
00267
00268
00269
00270
00271 #if _MCU_ENDIAN == 1
00272 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
00273 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
00274 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
00275 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
00276 #else
00277 #if _MCU_ENDIAN == 2
00278 #define LD_WORD(ptr) (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
00279 #define LD_DWORD(ptr) (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr))
00280 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
00281 #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)
00282 #else
00283 #error Do not forget to set _MCU_ENDIAN properly!
00284 #endif
00285 #endif
00286
00287
00288
00289 #define _FATFS
00290 #endif