Rev Author Line No. Line
1271 kaklik 1 /*--------------------------------------------------------------------------/
2 / Tiny-FatFs - FAT file system module include file R0.04b (C)ChaN, 2007
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) 2007, 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 / profit 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 1
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 is prohibited, the _MCU_ENDIAN must be set to 2.
25 / If it is not the case, it can 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 / and useless f_getfree. */
31  
32 #define _FS_MINIMIZE 2
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 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 _FAT32 0
40 /* To support FAT32 in addition of FAT12/16, set _FAT32 to 1. */
41  
42 #define _USE_FSINFO 0
43 /* To support FSInfo on FAT32 volume, set _USE_FSINFO to 1. */
44  
45 #define _USE_SJIS 0
46 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
47 / only US-ASCII(7bit) code can be accepted as file/directory name. */
48  
49 #define _USE_NTFLAG 1
50 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved. */
51  
52  
53 #include "integer.h"
54  
55  
56 /* Type definition for cluster number */
57 #if _FAT32
58 typedef DWORD CLUST;
59 #else
60 typedef WORD CLUST;
61 #undef _USE_FSINFO
62 #define _USE_FSINFO 0
63 #endif
64  
65  
66 /* File system object structure */
67 typedef struct _FATFS {
68 WORD id; /* File system mount ID */
69 WORD n_rootdir; /* Number of root directory entries */
70 DWORD winsect; /* Current sector appearing in the win[] */
71 DWORD fatbase; /* FAT start sector */
72 DWORD dirbase; /* Root directory start sector */
73 DWORD database; /* Data start sector */
74 CLUST sects_fat; /* Sectors per fat */
75 CLUST max_clust; /* Maximum cluster# + 1 */
76 #if !_FS_READONLY
77 CLUST last_clust; /* Last allocated cluster */
78 CLUST free_clust; /* Number of free clusters */
79 #if _USE_FSINFO
80 DWORD fsi_sector; /* fsinfo sector */
81 BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
82 BYTE pad1;
83 #endif
84 #endif
85 BYTE fs_type; /* FAT sub type */
86 BYTE sects_clust; /* Sectors per cluster */
87 BYTE n_fats; /* Number of FAT copies */
88 BYTE winflag; /* win[] dirty flag (1:must be written back) */
89 BYTE win[512]; /* Disk access window for Directory/FAT/File */
90 } FATFS;
91  
92  
93 /* Directory object structure */
94 typedef struct _DIR {
95 WORD id; /* Owner file system mount ID */
96 WORD index; /* Current index */
97 FATFS* fs; /* Pointer to the owner file system object */
98 CLUST sclust; /* Start cluster */
99 CLUST clust; /* Current cluster */
100 DWORD sect; /* Current sector */
101 } DIR;
102  
103  
104 /* File object structure */
105 typedef struct _FIL {
106 WORD id; /* Owner file system mount ID */
107 BYTE flag; /* File status flags */
108 BYTE sect_clust; /* Left sectors in cluster */
109 FATFS* fs; /* Pointer to owner file system */
110 DWORD fptr; /* File R/W pointer */
111 DWORD fsize; /* File size */
112 CLUST org_clust; /* File start cluster */
113 CLUST curr_clust; /* Current cluster */
114 DWORD curr_sect; /* Current sector */
115 #if !_FS_READONLY
116 DWORD dir_sect; /* Sector containing the directory entry */
117 BYTE* dir_ptr; /* Ponter to the directory entry in the window */
118 #endif
119 } FIL;
120  
121  
122 /* File status structure */
123 typedef struct _FILINFO {
124 DWORD fsize; /* Size */
125 WORD fdate; /* Date */
126 WORD ftime; /* Time */
127 BYTE fattrib; /* Attribute */
128 char fname[8+1+3+1]; /* Name (8.3 format) */
129 } FILINFO;
130  
131  
132 /* File function return code (FRESULT) */
133  
134 typedef enum {
135 FR_OK = 0, /* 0 */
136 FR_NOT_READY, /* 1 */
137 FR_NO_FILE, /* 2 */
138 FR_NO_PATH, /* 3 */
139 FR_INVALID_NAME, /* 4 */
140 FR_INVALID_DRIVE, /* 5 */
141 FR_DENIED, /* 6 */
142 FR_EXIST, /* 7 */
143 FR_RW_ERROR, /* 8 */
144 FR_WRITE_PROTECTED, /* 9 */
145 FR_NOT_ENABLED, /* 10 */
146 FR_NO_FILESYSTEM, /* 11 */
147 FR_INVALID_OBJECT /* 12 */
148 } FRESULT;
149  
150  
151  
152 /*-----------------------------------------------------*/
153 /* FatFs module application interface */
154  
155 FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
156 FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */
157 FRESULT f_read (FIL*, void*, WORD, WORD*); /* Read data from a file */
158 FRESULT f_write (FIL*, const void*, WORD, WORD*); /* Write data to a file */
159 FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
160 FRESULT f_close (FIL*); /* Close an open file object */
161 FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */
162 FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
163 FRESULT f_stat (const char*, FILINFO*); /* Get file status */
164 FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
165 FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
166 FRESULT f_unlink (const char*); /* Delete an existing file or directory */
167 FRESULT f_mkdir (const char*); /* Create a new directory */
168 FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file/dir attriburte */
169 FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */
170  
171  
172 /* User defined function to give a current time to fatfs module */
173  
174 DWORD get_fattime (void); /* 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) */
175 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
176  
177  
178  
179 /* File access control and file status flags (FIL.flag) */
180  
181 #define FA_READ 0x01
182 #define FA_OPEN_EXISTING 0x00
183 #if !_FS_READONLY
184 #define FA_WRITE 0x02
185 #define FA_CREATE_NEW 0x04
186 #define FA_CREATE_ALWAYS 0x08
187 #define FA_OPEN_ALWAYS 0x10
188 #define FA__WRITTEN 0x20
189 #endif
190 #define FA__ERROR 0x80
191  
192  
193 /* FAT sub type (FATFS.fs_type) */
194  
195 #define FS_FAT12 1
196 #define FS_FAT16 2
197 #define FS_FAT32 3
198  
199  
200 /* File attribute bits for directory entry */
201  
202 #define AM_RDO 0x01 /* Read only */
203 #define AM_HID 0x02 /* Hidden */
204 #define AM_SYS 0x04 /* System */
205 #define AM_VOL 0x08 /* Volume label */
206 #define AM_LFN 0x0F /* LFN entry */
207 #define AM_DIR 0x10 /* Directory */
208 #define AM_ARC 0x20 /* Archive */
209  
210  
211  
212 /* Offset of FAT structure members */
213  
214 #define BS_jmpBoot 0
215 #define BS_OEMName 3
216 #define BPB_BytsPerSec 11
217 #define BPB_SecPerClus 13
218 #define BPB_RsvdSecCnt 14
219 #define BPB_NumFATs 16
220 #define BPB_RootEntCnt 17
221 #define BPB_TotSec16 19
222 #define BPB_Media 21
223 #define BPB_FATSz16 22
224 #define BPB_SecPerTrk 24
225 #define BPB_NumHeads 26
226 #define BPB_HiddSec 28
227 #define BPB_TotSec32 32
228 #define BS_55AA 510
229  
230 #define BS_DrvNum 36
231 #define BS_BootSig 38
232 #define BS_VolID 39
233 #define BS_VolLab 43
234 #define BS_FilSysType 54
235  
236 #define BPB_FATSz32 36
237 #define BPB_ExtFlags 40
238 #define BPB_FSVer 42
239 #define BPB_RootClus 44
240 #define BPB_FSInfo 48
241 #define BPB_BkBootSec 50
242 #define BS_DrvNum32 64
243 #define BS_BootSig32 66
244 #define BS_VolID32 67
245 #define BS_VolLab32 71
246 #define BS_FilSysType32 82
247  
248 #define FSI_LeadSig 0
249 #define FSI_StrucSig 484
250 #define FSI_Free_Count 488
251 #define FSI_Nxt_Free 492
252  
253 #define MBR_Table 446
254  
255 #define DIR_Name 0
256 #define DIR_Attr 11
257 #define DIR_NTres 12
258 #define DIR_CrtTime 14
259 #define DIR_CrtDate 16
260 #define DIR_FstClusHI 20
261 #define DIR_WrtTime 22
262 #define DIR_WrtDate 24
263 #define DIR_FstClusLO 26
264 #define DIR_FileSize 28
265  
266  
267  
268 /* Multi-byte word access macros */
269  
270 #if _MCU_ENDIAN == 1 /* Use word access */
271 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
272 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
273 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
274 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
275 #else
276 #if _MCU_ENDIAN == 2 /* Use byte-by-byte access */
277 #define LD_WORD(ptr) (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
278 #define LD_DWORD(ptr) (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr))
279 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
280 #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)
281 #else
282 #error Do not forget to set _MCU_ENDIAN properly!
283 #endif
284 #endif
285  
286  
287  
288 #define _FATFS
289 #endif /* _FATFS */