Rev Author Line No. Line
1234 kaklik 1 /*--------------------------------------------------------------------------/
2 / FatFs - FAT file system module include file R0.06 (C)ChaN, 2008
3 /---------------------------------------------------------------------------/
4 / FatFs module is an experimenal project to implement FAT file system to
5 / cheap microcontrollers. This is a free software and is opened for education,
6 / research and development under license policy of following trems.
7 /
8 / Copyright (C) 2008, ChaN, all right reserved.
9 /
10 / * The FatFs module is a free software and there is no warranty.
11 / * You can use, modify and/or redistribute it for personal, non-profit or
12 / commercial use without any restriction under your responsibility.
13 / * Redistributions of source code must retain the above copyright notice.
14 /
15 /---------------------------------------------------------------------------*/
16  
17 #ifndef _FATFS
18  
19 #define _MCU_ENDIAN 0
20 /* The _MCU_ENDIAN defines which access method is used to the FAT structure.
21 / 1: Enable word access.
22 / 2: Disable word access and use byte-by-byte access instead.
23 / When the architectural byte order of the MCU is big-endian and/or address
24 / miss-aligned access results incorrect behavior, the _MCU_ENDIAN must be set to 2.
25 / If it is not the case, it can also be set to 1 for good code efficiency. */
26  
27 #define _FS_READONLY 0
28 /* Setting _FS_READONLY to 1 defines read only configuration. This removes
29 / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
30 / f_truncate and useless f_getfree. */
31  
32 #define _FS_MINIMIZE 0
33 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
34 / 0: Full function.
35 / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename are removed.
36 / 2: f_opendir and f_readdir are removed in addition to level 1.
37 / 3: f_lseek is removed in addition to level 2. */
38  
39 #define _USE_STRFUNC 0
40 /* To enable string functions, set _USE_STRFUNC to 1 or 2. */
41  
42 #define _USE_MKFS 0
43 /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is
44 / enabled. */
45  
46 #define _DRIVES 2
47 /* Number of logical drives to be used. This affects the size of internal table. */
48  
49 #define _MULTI_PARTITION 0
50 /* When _MULTI_PARTITION is set to 0, each logical drive is bound to same
51 / physical drive number and can mount only 1st primaly partition. When it is
52 / set to 1, each logical drive can mount a partition listed in Drives[]. */
53  
54 #define _USE_FSINFO 0
55 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */
56  
57 #define _USE_SJIS 1
58 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
59 / only US-ASCII(7bit) code can be accepted as file/directory name. */
60  
61 #define _USE_NTFLAG 1
62 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved.
63 / Note that the files are always accessed in case insensitive. */
64  
65  
66 #include "integer.h"
67  
68  
69  
70 /* Definitions corresponds to multiple sector size (not tested) */
71 #define S_MAX_SIZ 512U /* Do not change */
72 #if S_MAX_SIZ > 512U
73 #define SS(fs) ((fs)->s_size)
74 #else
75 #define SS(fs) 512U
76 #endif
77  
78  
79 /* File system object structure */
80 typedef struct _FATFS {
81 WORD id; /* File system mount ID */
82 WORD n_rootdir; /* Number of root directory entries */
83 DWORD winsect; /* Current sector appearing in the win[] */
84 DWORD sects_fat; /* Sectors per fat */
85 DWORD max_clust; /* Maximum cluster# + 1 */
86 DWORD fatbase; /* FAT start sector */
87 DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */
88 DWORD database; /* Data start sector */
89 #if !_FS_READONLY
90 DWORD last_clust; /* Last allocated cluster */
91 DWORD free_clust; /* Number of free clusters */
92 #if _USE_FSINFO
93 DWORD fsi_sector; /* fsinfo sector */
94 BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
95 BYTE pad2;
96 #endif
97 #endif
98 BYTE fs_type; /* FAT sub type */
99 BYTE csize; /* Number of sectors per cluster */
100 #if S_MAX_SIZ > 512U
101 WORD s_size; /* Sector size */
102 #endif
103 BYTE n_fats; /* Number of FAT copies */
104 BYTE drive; /* Physical drive number */
105 BYTE winflag; /* win[] dirty flag (1:must be written back) */
106 BYTE pad1;
107 BYTE win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */
108 } FATFS;
109  
110  
111 /* Directory object structure */
112 typedef struct _DIR {
113 WORD id; /* Owner file system mount ID */
114 WORD index; /* Current index */
115 FATFS* fs; /* Pointer to the owner file system object */
116 DWORD sclust; /* Start cluster */
117 DWORD clust; /* Current cluster */
118 DWORD sect; /* Current sector */
119 } DIR;
120  
121  
122 /* File object structure */
123 typedef struct _FIL {
124 WORD id; /* Owner file system mount ID */
125 BYTE flag; /* File status flags */
126 BYTE csect; /* Sector address in the cluster */
127 FATFS* fs; /* Pointer to the owner file system object */
128 DWORD fptr; /* File R/W pointer */
129 DWORD fsize; /* File size */
130 DWORD org_clust; /* File start cluster */
131 DWORD curr_clust; /* Current cluster */
132 DWORD curr_sect; /* Current sector */
133 #if _FS_READONLY == 0
134 DWORD dir_sect; /* Sector containing the directory entry */
135 BYTE* dir_ptr; /* Ponter to the directory entry in the window */
136 #endif
137 BYTE buffer[S_MAX_SIZ]; /* File R/W buffer */
138 } FIL;
139  
140  
141 /* File status structure */
142 typedef struct _FILINFO {
143 DWORD fsize; /* Size */
144 WORD fdate; /* Date */
145 WORD ftime; /* Time */
146 BYTE fattrib; /* Attribute */
147 char fname[8+1+3+1]; /* Name (8.3 format) */
148 } FILINFO;
149  
150  
151  
152 /* Definitions corresponds to multi partition */
153  
154 #if _MULTI_PARTITION != 0 /* Multiple partition cfg */
155  
156 typedef struct _PARTITION {
157 BYTE pd; /* Physical drive # (0-255) */
158 BYTE pt; /* Partition # (0-3) */
159 } PARTITION;
160 extern
161 const PARTITION Drives[]; /* Logical drive# to physical location conversion table */
162 #define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */
163 #define LD2PT(drv) (Drives[drv].pt) /* Get partition# */
164  
165 #else /* Single partition cfg */
166  
167 #define LD2PD(drv) (drv) /* Physical drive# is equal to logical drive# */
168 #define LD2PT(drv) 0 /* Always mounts the 1st partition */
169  
170 #endif
171  
172  
173 /* File function return code (FRESULT) */
174  
175 typedef enum {
176 FR_OK = 0, /* 0 */
177 FR_NOT_READY, /* 1 */
178 FR_NO_FILE, /* 2 */
179 FR_NO_PATH, /* 3 */
180 FR_INVALID_NAME, /* 4 */
181 FR_INVALID_DRIVE, /* 5 */
182 FR_DENIED, /* 6 */
183 FR_EXIST, /* 7 */
184 FR_RW_ERROR, /* 8 */
185 FR_WRITE_PROTECTED, /* 9 */
186 FR_NOT_ENABLED, /* 10 */
187 FR_NO_FILESYSTEM, /* 11 */
188 FR_INVALID_OBJECT, /* 12 */
189 FR_MKFS_ABORTED /* 13 */
190 } FRESULT;
191  
192  
193  
194 /*-----------------------------------------------------*/
195 /* FatFs module application interface */
196  
197 FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
198 FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */
199 FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
200 FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
201 FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
202 FRESULT f_close (FIL*); /* Close an open file object */
203 FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */
204 FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
205 FRESULT f_stat (const char*, FILINFO*); /* Get file status */
206 FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
207 FRESULT f_truncate (FIL*); /* Truncate file */
208 FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
209 FRESULT f_unlink (const char*); /* Delete an existing file or directory */
210 FRESULT f_mkdir (const char*); /* Create a new directory */
211 FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */
212 FRESULT f_utime (const char*, const FILINFO*); /* Change file/dir timestamp */
213 FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */
214 FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */
215 #if _USE_STRFUNC
216 #define feof(fp) ((fp)->fptr == (fp)->fsize)
217 #define EOF -1
218 int fputc (int, FIL*); /* Put a character to the file */
219 int fputs (const char*, FIL*); /* Put a string to the file */
220 int fprintf (FIL*, const char*, ...); /* Put a formatted string to the file */
221 char* fgets (char*, int, FIL*); /* Get a string from the file */
222 #endif
223  
224 /* User defined function to give a current time to fatfs module */
225  
226 DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
227 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
228  
229  
230  
231 /* File access control and file status flags (FIL.flag) */
232  
233 #define FA_READ 0x01
234 #define FA_OPEN_EXISTING 0x00
235 #if _FS_READONLY == 0
236 #define FA_WRITE 0x02
237 #define FA_CREATE_NEW 0x04
238 #define FA_CREATE_ALWAYS 0x08
239 #define FA_OPEN_ALWAYS 0x10
240 #define FA__WRITTEN 0x20
241 #define FA__DIRTY 0x40
242 #endif
243 #define FA__ERROR 0x80
244  
245  
246 /* FAT sub type (FATFS.fs_type) */
247  
248 #define FS_FAT12 1
249 #define FS_FAT16 2
250 #define FS_FAT32 3
251  
252  
253 /* File attribute bits for directory entry */
254  
255 #define AM_RDO 0x01 /* Read only */
256 #define AM_HID 0x02 /* Hidden */
257 #define AM_SYS 0x04 /* System */
258 #define AM_VOL 0x08 /* Volume label */
259 #define AM_LFN 0x0F /* LFN entry */
260 #define AM_DIR 0x10 /* Directory */
261 #define AM_ARC 0x20 /* Archive */
262  
263  
264  
265 /* Offset of FAT structure members */
266  
267 #define BS_jmpBoot 0
268 #define BS_OEMName 3
269 #define BPB_BytsPerSec 11
270 #define BPB_SecPerClus 13
271 #define BPB_RsvdSecCnt 14
272 #define BPB_NumFATs 16
273 #define BPB_RootEntCnt 17
274 #define BPB_TotSec16 19
275 #define BPB_Media 21
276 #define BPB_FATSz16 22
277 #define BPB_SecPerTrk 24
278 #define BPB_NumHeads 26
279 #define BPB_HiddSec 28
280 #define BPB_TotSec32 32
281 #define BS_55AA 510
282  
283 #define BS_DrvNum 36
284 #define BS_BootSig 38
285 #define BS_VolID 39
286 #define BS_VolLab 43
287 #define BS_FilSysType 54
288  
289 #define BPB_FATSz32 36
290 #define BPB_ExtFlags 40
291 #define BPB_FSVer 42
292 #define BPB_RootClus 44
293 #define BPB_FSInfo 48
294 #define BPB_BkBootSec 50
295 #define BS_DrvNum32 64
296 #define BS_BootSig32 66
297 #define BS_VolID32 67
298 #define BS_VolLab32 71
299 #define BS_FilSysType32 82
300  
301 #define FSI_LeadSig 0
302 #define FSI_StrucSig 484
303 #define FSI_Free_Count 488
304 #define FSI_Nxt_Free 492
305  
306 #define MBR_Table 446
307  
308 #define DIR_Name 0
309 #define DIR_Attr 11
310 #define DIR_NTres 12
311 #define DIR_CrtTime 14
312 #define DIR_CrtDate 16
313 #define DIR_FstClusHI 20
314 #define DIR_WrtTime 22
315 #define DIR_WrtDate 24
316 #define DIR_FstClusLO 26
317 #define DIR_FileSize 28
318  
319  
320  
321 /* Multi-byte word access macros */
322  
323 #if _MCU_ENDIAN == 1 /* Use word access */
324 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
325 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
326 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
327 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
328 #elif _MCU_ENDIAN == 2 /* Use byte-by-byte access */
329 #define LD_WORD(ptr) (WORD)(((WORD)*(volatile BYTE*)((ptr)+1)<<8)|(WORD)*(volatile BYTE*)(ptr))
330 #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))
331 #define ST_WORD(ptr,val) *(volatile BYTE*)(ptr)=(BYTE)(val); *(volatile BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
332 #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)
333 #else
334 #error Do not forget to set _MCU_ENDIAN properly!
335 #endif
336  
337  
338 #define _FATFS
339 #endif /* _FATFS */