MAV'RIC
ff.h
1 /*---------------------------------------------------------------------------/
2 / FatFs - FAT file system module include file R0.10b (C)ChaN, 2014
3 /----------------------------------------------------------------------------/
4 / FatFs module is a generic FAT file system module for small embedded systems.
5 / This is a free software that opened for education, research and commercial
6 / developments under license policy of following terms.
7 /
8 / Copyright (C) 2014, ChaN, all right reserved.
9 /
10 / * The FatFs module is a free software and there is NO WARRANTY.
11 / * No restriction on use. You can use, modify and redistribute it for
12 / personal, non-profit or commercial product UNDER YOUR RESPONSIBILITY.
13 / * Redistributions of source code must retain the above copyright notice.
14 /
15 /----------------------------------------------------------------------------*/
16 
17 #ifndef _FATFS
18 #define _FATFS 8051 /* Revision ID */
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include "integer.h" /* Basic integer types */
25 #include "ffconf.h" /* FatFs configuration options */
26 
27 #if _FATFS != _FFCONF
28 #error Wrong configuration file (ffconf.h).
29 #endif
30 
31 
32 
33 /* Definitions of volume management */
34 
35 #if _MULTI_PARTITION
36 
39 typedef struct
40 {
41  BYTE pd;
42  BYTE pt;
43 } PARTITION;
44 
45 extern PARTITION VolToPart[];
46 #define LD2PD(vol) (VolToPart[vol].pd)
47 #define LD2PT(vol) (VolToPart[vol].pt)
48 
49 #else
50 #define LD2PD(vol) (BYTE)(vol)
51 #define LD2PT(vol) 0
52 
53 #endif
54 
55 /* Type of path name strings on FatFs API */
56 
57 #if _LFN_UNICODE /* Unicode string */
58 #if !_USE_LFN
59 #error _LFN_UNICODE must be 0 at non-LFN cfg.
60 #endif
61 #ifndef _INC_TCHAR
62 typedef WCHAR TCHAR;
63 #define _T(x) L ## x
64 #define _TEXT(x) L ## x
65 #endif
66 
67 #else /* ANSI/OEM string */
68 #ifndef _INC_TCHAR
69 typedef char TCHAR;
70 #define _T(x) x
71 #define _TEXT(x) x
72 #endif
73 
74 #endif
75 
76 
77 
81 typedef struct
82 {
83  BYTE fs_type;
84  BYTE drv;
85  BYTE csize;
86  BYTE n_fats;
87  BYTE wflag;
88  BYTE fsi_flag;
89  WORD id;
90  WORD n_rootdir;
91 #if _MAX_SS != _MIN_SS
92  WORD ssize;
93 #endif
94 #if _FS_REENTRANT
95  _SYNC_t sobj;
96 #endif
97 #if !_FS_READONLY
98  DWORD last_clust;
99  DWORD free_clust;
100 #endif
101 #if _FS_RPATH
102  DWORD cdir;
103 #endif
104  DWORD n_fatent;
105  DWORD fsize;
106  DWORD volbase;
107  DWORD fatbase;
108  DWORD dirbase;
109  DWORD database;
110  DWORD winsect;
111  BYTE win[_MAX_SS];
112 } FATFS;
113 
114 
115 
119 typedef struct
120 {
122  WORD id;
123  BYTE flag;
124  BYTE err;
125  DWORD fptr;
126  DWORD fsize;
127  DWORD sclust;
128  DWORD clust;
129  DWORD dsect;
130 #if !_FS_READONLY
131  DWORD dir_sect;
132  BYTE* dir_ptr;
133 #endif
134 #if _USE_FASTSEEK
135  DWORD* cltbl;
136 #endif
137 #if _FS_LOCK
138  UINT lockid;
139 #endif
140 #if !_FS_TINY
141  BYTE buf[_MAX_SS];
142 #endif
143 } FIL;
144 
145 
146 
150 typedef struct
151 {
153  WORD id;
154  WORD index;
155  DWORD sclust;
156  DWORD clust;
157  DWORD sect;
158  BYTE* dir;
159  BYTE* fn;
160 #if _FS_LOCK
161  UINT lockid;
162 #endif
163 #if _USE_LFN
164  WCHAR* lfn;
165  WORD lfn_idx;
166 #endif
167 } DIR;
168 
169 
173 typedef struct
174 {
175  DWORD fsize;
176  WORD fdate;
177  WORD ftime;
178  BYTE fattrib;
179  TCHAR fname[13];
180 #if _USE_LFN
181  TCHAR* lfname;
182  UINT lfsize;
183 #endif
184 } FILINFO;
185 
186 
190 typedef enum
191 {
192  FR_OK = 0,
193  FR_DISK_ERR,
194  FR_INT_ERR,
195  FR_NOT_READY,
196  FR_NO_FILE,
197  FR_NO_PATH,
198  FR_INVALID_NAME,
199  FR_DENIED,
200  FR_EXIST,
201  FR_INVALID_OBJECT,
202  FR_WRITE_PROTECTED,
203  FR_INVALID_DRIVE,
204  FR_NOT_ENABLED,
205  FR_NO_FILESYSTEM,
206  FR_MKFS_ABORTED,
207  FR_TIMEOUT,
208  FR_LOCKED,
209  FR_NOT_ENOUGH_CORE,
210  FR_TOO_MANY_OPEN_FILES,
211  FR_INVALID_PARAMETER
212 } FRESULT;
213 
214 
215 /*--------------------------------------------------------------*/
216 /* FatFs module application interface */
217 
218 FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
219 FRESULT f_close (FIL* fp); /* Close an open file object */
220 FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */
221 FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */
222 FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
223 FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */
224 FRESULT f_truncate (FIL* fp); /* Truncate file */
225 FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */
226 FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
227 FRESULT f_closedir (DIR* dp); /* Close an open directory */
228 FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
229 FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
230 FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
231 FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
232 FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
233 FRESULT f_chmod (const TCHAR* path, BYTE value, BYTE mask); /* Change attribute of the file/dir */
234 FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */
235 FRESULT f_chdir (const TCHAR* path); /* Change current directory */
236 FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
237 FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
238 FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
239 FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
240 FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
241 FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
242 FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */
243 FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */
244 int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
245 int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
246 int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
247 TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
248 
249 #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
250 #define f_error(fp) ((fp)->err)
251 #define f_tell(fp) ((fp)->fptr)
252 #define f_size(fp) ((fp)->fsize)
253 
254 #ifndef EOF
255 #define EOF (-1)
256 #endif
257 
258 
259 /*--------------------------------------------------------------*/
260 /* Additional user defined functions */
261 
262 /* RTC function */
263 #if !_FS_READONLY
264 DWORD get_fattime (void);
265 #endif
266 
267 /* Unicode support functions */
268 #if _USE_LFN /* Unicode - OEM code conversion */
269 WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
270 WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
271 #if _USE_LFN == 3 /* Memory functions */
272 void* ff_memalloc (UINT msize); /* Allocate memory block */
273 void ff_memfree (void* mblock); /* Free memory block */
274 #endif
275 #endif
276 
277 /* Sync functions */
278 #if _FS_REENTRANT
279 int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */
280 int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
281 void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
282 int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */
283 #endif
284 
285 
286 /*--------------------------------------------------------------*/
287 /* Flags and offset address */
288 
289 
290 /* File access control and file status flags (FIL.flag) */
291 
292 #define FA_READ 0x01
293 #define FA_OPEN_EXISTING 0x00
294 
295 #if !_FS_READONLY
296 #define FA_WRITE 0x02
297 #define FA_CREATE_NEW 0x04
298 #define FA_CREATE_ALWAYS 0x08
299 #define FA_OPEN_ALWAYS 0x10
300 #define FA__WRITTEN 0x20
301 #define FA__DIRTY 0x40
302 #endif
303 
304 
305 /* FAT sub type (FATFS.fs_type) */
306 
307 #define FS_FAT12 1
308 #define FS_FAT16 2
309 #define FS_FAT32 3
310 
311 
312 /* File attribute bits for directory entry */
313 
314 #define AM_RDO 0x01 /* Read only */
315 #define AM_HID 0x02 /* Hidden */
316 #define AM_SYS 0x04 /* System */
317 #define AM_VOL 0x08 /* Volume label */
318 #define AM_LFN 0x0F /* LFN entry */
319 #define AM_DIR 0x10 /* Directory */
320 #define AM_ARC 0x20 /* Archive */
321 #define AM_MASK 0x3F /* Mask of defined bits */
322 
323 
324 /* Fast seek feature */
325 #define CREATE_LINKMAP 0xFFFFFFFF
326 
327 
328 /*--------------------------------*/
329 /* Multi-byte word access macros */
330 
331 #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
332 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
333 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
334 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
335 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
336 #else /* Use byte-by-byte access to the FAT structure */
337 #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
338 #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
339 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
340 #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)
341 #endif
342 
343 #ifdef __cplusplus
344 }
345 #endif
346 
347 #endif /* _FATFS */
FATFS * fs
Pointer to the owner file system object (do not change order)
Definition: ff.h:152
WORD index
Current read/write index number.
Definition: ff.h:154
DWORD n_fatent
Number of FAT entries, = number of clusters + 2.
Definition: ff.h:104
DWORD sect
Current sector.
Definition: ff.h:157
DWORD database
Data start sector.
Definition: ff.h:109
WCHAR * lfn
Pointer to the LFN working buffer.
Definition: ff.h:164
WORD fdate
Last modified date.
Definition: ff.h:176
DWORD last_clust
Last allocated cluster.
Definition: ff.h:98
File status structure (FILINFO)
Definition: ff.h:173
DWORD fsize
File size.
Definition: ff.h:126
File system object structure (FATFS)
Definition: ff.h:81
WORD ftime
Last modified time.
Definition: ff.h:177
WORD id
Owner file system mount ID (do not change order)
Definition: ff.h:122
WORD id
Owner file system mount ID (do not change order)
Definition: ff.h:153
BYTE flag
Status flags.
Definition: ff.h:123
WORD lfn_idx
Last matched LFN index number (0xFFFF:No LFN)
Definition: ff.h:165
TCHAR * lfname
Pointer to the LFN buffer.
Definition: ff.h:181
BYTE * dir
Pointer to the current SFN entry in the win[].
Definition: ff.h:158
BYTE csize
Sectors per cluster (1,2,4...128)
Definition: ff.h:85
DWORD sclust
Table start cluster (0:Root dir)
Definition: ff.h:155
BYTE fs_type
FAT sub-type (0:Not mounted)
Definition: ff.h:83
Directory object structure (DIR)
Definition: ff.h:150
BYTE n_fats
Number of FAT copies (1 or 2)
Definition: ff.h:86
UINT lfsize
Size of LFN buffer in TCHAR.
Definition: ff.h:182
DWORD winsect
Current sector appearing in the win[].
Definition: ff.h:110
DWORD fatbase
FAT start sector.
Definition: ff.h:107
WORD id
File system mount ID.
Definition: ff.h:89
DWORD sclust
File start cluster (0:no cluster chain, always 0 when fsize is 0)
Definition: ff.h:127
BYTE drv
Physical drive number.
Definition: ff.h:84
BYTE fsi_flag
FSINFO flags (b7:disabled, b0:dirty)
Definition: ff.h:88
BYTE * dir_ptr
Pointer to the directory entry in the win[].
Definition: ff.h:132
BYTE fattrib
Attribute.
Definition: ff.h:178
DWORD dsect
Sector number appearing in buf[] (0:invalid)
Definition: ff.h:129
DWORD clust
Current cluster.
Definition: ff.h:156
BYTE err
Abort flag (error code)
Definition: ff.h:124
DWORD get_fattime(void)
The time to write the metadata of the files.
Definition: diskio.c:388
DWORD volbase
Volume start sector.
Definition: ff.h:106
DWORD fsize
File size.
Definition: ff.h:175
WORD n_rootdir
Number of root directory entries (FAT12/16)
Definition: ff.h:90
File object structure (FIL)
Definition: ff.h:119
DWORD free_clust
Number of free clusters.
Definition: ff.h:99
BYTE wflag
win[] flag (b0:dirty)
Definition: ff.h:87
DWORD fptr
File read/write pointer (Zeroed on file open)
Definition: ff.h:125
DWORD clust
Current cluster of fpter (not valid when fprt is 0)
Definition: ff.h:128
FATFS * fs
Pointer to the related file system object (do not change order)
Definition: ff.h:121
DWORD dir_sect
Sector number containing the directory entry.
Definition: ff.h:131
BYTE * fn
Pointer to the SFN (in/out) {file[8],ext[3],status[1]}.
Definition: ff.h:159
DWORD fsize
Sectors per FAT.
Definition: ff.h:105
DWORD dirbase
Root directory start sector (FAT32:Cluster#)
Definition: ff.h:108