ff.h
Go to the documentation of this file.
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 /* Multiple partition configuration */
36 typedef struct {
37  BYTE pd; /* Physical drive number */
38  BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
39 } PARTITION;
40 extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
41 #define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
42 #define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
43 
44 #else /* Single partition configuration */
45 #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
46 #define LD2PT(vol) 0 /* Find first valid partition or in SFD */
47 
48 #endif
49 
50 
51 
52 /* Type of path name strings on FatFs API */
53 
54 #if _LFN_UNICODE /* Unicode string */
55 #if !_USE_LFN
56 #error _LFN_UNICODE must be 0 at non-LFN cfg.
57 #endif
58 #ifndef _INC_TCHAR
59 typedef WCHAR TCHAR;
60 #define _T(x) L ## x
61 #define _TEXT(x) L ## x
62 #endif
63 
64 #else /* ANSI/OEM string */
65 #ifndef _INC_TCHAR
66 typedef char TCHAR;
67 #define _T(x) x
68 #define _TEXT(x) x
69 #endif
70 
71 #endif
72 
73 
74 
75 /* File system object structure (FATFS) */
76 
77 typedef struct {
78  BYTE fs_type; /* FAT sub-type (0:Not mounted) */
79  BYTE drv; /* Physical drive number */
80  BYTE csize; /* Sectors per cluster (1,2,4...128) */
81  BYTE n_fats; /* Number of FAT copies (1 or 2) */
82  BYTE wflag; /* win[] flag (b0:dirty) */
83  BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
84  WORD id; /* File system mount ID */
85  WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
86 #if _MAX_SS != _MIN_SS
87  WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */
88 #endif
89 #if _FS_REENTRANT
90  _SYNC_t sobj; /* Identifier of sync object */
91 #endif
92 #if !_FS_READONLY
93  DWORD last_clust; /* Last allocated cluster */
94  DWORD free_clust; /* Number of free clusters */
95 #endif
96 #if _FS_RPATH
97  DWORD cdir; /* Current directory start cluster (0:root) */
98 #endif
99  DWORD n_fatent; /* Number of FAT entries, = number of clusters + 2 */
100  DWORD fsize; /* Sectors per FAT */
101  DWORD volbase; /* Volume start sector */
102  DWORD fatbase; /* FAT start sector */
103  DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
104  DWORD database; /* Data start sector */
105  DWORD winsect; /* Current sector appearing in the win[] */
106  BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
107 } FATFS;
108 
109 
110 
111 /* File object structure (FIL) */
112 
113 typedef struct {
114  FATFS* fs; /* Pointer to the related file system object (**do not change order**) */
115  WORD id; /* Owner file system mount ID (**do not change order**) */
116  BYTE flag; /* Status flags */
117  BYTE err; /* Abort flag (error code) */
118  DWORD fptr; /* File read/write pointer (Zeroed on file open) */
119  DWORD fsize; /* File size */
120  DWORD sclust; /* File start cluster (0:no cluster chain, always 0 when fsize is 0) */
121  DWORD clust; /* Current cluster of fpter (not valid when fprt is 0) */
122  DWORD dsect; /* Sector number appearing in buf[] (0:invalid) */
123 #if !_FS_READONLY
124  DWORD dir_sect; /* Sector number containing the directory entry */
125  BYTE* dir_ptr; /* Pointer to the directory entry in the win[] */
126 #endif
127 #if _USE_FASTSEEK
128  DWORD* cltbl; /* Pointer to the cluster link map table (Nulled on file open) */
129 #endif
130 #if _FS_LOCK
131  UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
132 #endif
133 #if !_FS_TINY
134  BYTE buf[_MAX_SS]; /* File private data read/write window */
135 #endif
136 } FIL;
137 
138 
139 
140 /* Directory object structure (DIR) */
141 
142 typedef struct {
143  FATFS* fs; /* Pointer to the owner file system object (**do not change order**) */
144  WORD id; /* Owner file system mount ID (**do not change order**) */
145  WORD index; /* Current read/write index number */
146  DWORD sclust; /* Table start cluster (0:Root dir) */
147  DWORD clust; /* Current cluster */
148  DWORD sect; /* Current sector */
149  BYTE* dir; /* Pointer to the current SFN entry in the win[] */
150  BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
151 #if _FS_LOCK
152  UINT lockid; /* File lock ID (index of file semaphore table Files[]) */
153 #endif
154 #if _USE_LFN
155  WCHAR* lfn; /* Pointer to the LFN working buffer */
156  WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */
157 #endif
158 } DIR;
159 
160 
161 
162 /* File status structure (FILINFO) */
163 
164 typedef struct {
165  DWORD fsize; /* File size */
166  WORD fdate; /* Last modified date */
167  WORD ftime; /* Last modified time */
168  BYTE fattrib; /* Attribute */
169  TCHAR fname[13]; /* Short file name (8.3 format) */
170 #if _USE_LFN
171  TCHAR* lfname; /* Pointer to the LFN buffer */
172  UINT lfsize; /* Size of LFN buffer in TCHAR */
173 #endif
174 } FILINFO;
175 
176 
177 
178 /* File function return code (FRESULT) */
179 
180 typedef enum {
181  FR_OK = 0, /* (0) Succeeded */
182  FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
183  FR_INT_ERR, /* (2) Assertion failed */
184  FR_NOT_READY, /* (3) The physical drive cannot work */
185  FR_NO_FILE, /* (4) Could not find the file */
186  FR_NO_PATH, /* (5) Could not find the path */
187  FR_INVALID_NAME, /* (6) The path name format is invalid */
188  FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
189  FR_EXIST, /* (8) Access denied due to prohibited access */
190  FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
191  FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
192  FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
193  FR_NOT_ENABLED, /* (12) The volume has no work area */
194  FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
195  FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
196  FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
197  FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
198  FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
199  FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
200  FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
201 } FRESULT;
202 
203 
204 
205 /*--------------------------------------------------------------*/
206 /* FatFs module application interface */
207 
208 FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
209 FRESULT f_close (FIL* fp); /* Close an open file object */
210 FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */
211 FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */
212 FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
213 FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */
214 FRESULT f_truncate (FIL* fp); /* Truncate file */
215 FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */
216 FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
217 FRESULT f_closedir (DIR* dp); /* Close an open directory */
218 FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
219 FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
220 FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
221 FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
222 FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
223 FRESULT f_chmod (const TCHAR* path, BYTE value, BYTE mask); /* Change attribute of the file/dir */
224 FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */
225 FRESULT f_chdir (const TCHAR* path); /* Change current directory */
226 FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
227 FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
228 FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
229 FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
230 FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
231 FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
232 FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */
233 FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */
234 int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
235 int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
236 int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
237 TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
238 
239 #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
240 #define f_error(fp) ((fp)->err)
241 #define f_tell(fp) ((fp)->fptr)
242 #define f_size(fp) ((fp)->fsize)
243 
244 #ifndef EOF
245 #define EOF (-1)
246 #endif
247 
248 
249 
250 
251 /*--------------------------------------------------------------*/
252 /* Additional user defined functions */
253 
254 /* RTC function */
255 #if !_FS_READONLY
256 DWORD get_fattime (void);
257 #endif
258 
259 /* Unicode support functions */
260 #if _USE_LFN /* Unicode - OEM code conversion */
261 WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
262 WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
263 #if _USE_LFN == 3 /* Memory functions */
264 void* ff_memalloc (UINT msize); /* Allocate memory block */
265 void ff_memfree (void* mblock); /* Free memory block */
266 #endif
267 #endif
268 
269 /* Sync functions */
270 #if _FS_REENTRANT
271 int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */
272 int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
273 void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
274 int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */
275 #endif
276 
277 
278 
279 
280 /*--------------------------------------------------------------*/
281 /* Flags and offset address */
282 
283 
284 /* File access control and file status flags (FIL.flag) */
285 
286 #define FA_READ 0x01
287 #define FA_OPEN_EXISTING 0x00
288 
289 #if !_FS_READONLY
290 #define FA_WRITE 0x02
291 #define FA_CREATE_NEW 0x04
292 #define FA_CREATE_ALWAYS 0x08
293 #define FA_OPEN_ALWAYS 0x10
294 #define FA__WRITTEN 0x20
295 #define FA__DIRTY 0x40
296 #endif
297 
298 
299 /* FAT sub type (FATFS.fs_type) */
300 
301 #define FS_FAT12 1
302 #define FS_FAT16 2
303 #define FS_FAT32 3
304 
305 
306 /* File attribute bits for directory entry */
307 
308 #define AM_RDO 0x01 /* Read only */
309 #define AM_HID 0x02 /* Hidden */
310 #define AM_SYS 0x04 /* System */
311 #define AM_VOL 0x08 /* Volume label */
312 #define AM_LFN 0x0F /* LFN entry */
313 #define AM_DIR 0x10 /* Directory */
314 #define AM_ARC 0x20 /* Archive */
315 #define AM_MASK 0x3F /* Mask of defined bits */
316 
317 
318 /* Fast seek feature */
319 #define CREATE_LINKMAP 0xFFFFFFFF
320 
321 
322 
323 /*--------------------------------*/
324 /* Multi-byte word access macros */
325 
326 #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
327 
328 #if 0
329 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
330 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
331 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
332 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
333 #else
334 #endif
335 static inline WORD LD_WORD(void *p) { WORD *_rdword = (WORD*)p; return *_rdword; }
336 static inline DWORD LD_DWORD(void *p) { DWORD *_rddword = (DWORD*)p; return *_rddword; }
337 static inline void ST_WORD(void *p, WORD val) { WORD *wrword = (WORD*) p; *wrword = val; }
338 static inline void ST_DWORD(void *p, DWORD val) { DWORD *wrdword = (DWORD*) p; *wrdword = val; }
339 #else /* Use byte-by-byte access to the FAT structure */
340 #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
341 #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
342 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
343 #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)
344 #endif
345 
346 #ifdef __cplusplus
347 }
348 #endif
349 
350 #endif /* _FATFS */
FRESULT f_chdrive(const TCHAR *path)
FATFS * fs
Definition: ff.h:143
int f_puts(const TCHAR *str, FIL *cp)
unsigned short WORD
Definition: integer.h:20
unsigned short WCHAR
Definition: integer.h:21
WORD index
Definition: ff.h:145
DWORD n_fatent
Definition: ff.h:99
DWORD sect
Definition: ff.h:148
int f_putc(TCHAR c, FIL *fp)
DWORD database
Definition: ff.h:104
#define _MAX_SS
Definition: ffconf.h:156
WCHAR * lfn
Definition: ff.h:155
WORD fdate
Definition: ff.h:166
int ff_req_grant(_SYNC_t sobj)
Definition: reentrant.c:71
DWORD last_clust
Definition: ff.h:93
Each real time kernel port consists of three files that contain the core kernel components and are common to every and one or more files that are specific to a particular microcontroller and or compiler The FreeRTOS Source directory contains the three files that are common to every port list c
Definition: readme.txt:2
Definition: ff.h:164
Definition: ff.h:187
FRESULT f_chmod(const TCHAR *path, BYTE value, BYTE mask)
Definition: ff.c:3584
FRESULT f_fdisk(BYTE pdrv, const DWORD szt[], void *work)
DWORD fsize
Definition: ff.h:119
Definition: ff.h:77
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition: ff.c:2368
FRESULT f_getlabel(const TCHAR *path, TCHAR *label, DWORD *vsn)
WORD ftime
Definition: ff.h:167
WORD id
Definition: ff.h:115
WORD id
Definition: ff.h:144
BYTE flag
Definition: ff.h:116
char TCHAR
Definition: ff.h:66
Definition: ff.h:192
Definition: ff.h:188
WORD lfn_idx
Definition: ff.h:156
unsigned int unsigned int len
Definition: startup.cpp:197
FRESULT f_chdir(const TCHAR *path)
WCHAR ff_wtoupper(WCHAR chr)
Definition: ccsbcs.c:528
TCHAR * lfname
Definition: ff.h:171
BYTE * dir
Definition: ff.h:149
BYTE csize
Definition: ff.h:80
FRESULT f_readdir(DIR *dp, FILINFO *fno)
Definition: ff.c:3240
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition: ff.c:2550
DWORD sclust
Definition: ff.h:146
WCHAR ff_convert(WCHAR chr, UINT dir)
Definition: ccsbcs.c:501
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs)
Definition: ff.c:3317
BYTE fs_type
Definition: ff.h:78
FRESULT f_mkdir(const TCHAR *path)
Definition: ff.c:3510
Definition: ff.h:183
FRESULT f_close(FIL *fp)
Definition: ff.c:2822
Definition: ff.h:142
Definition: ff.h:182
unsigned long DWORD
Definition: integer.h:29
FRESULT f_lseek(FIL *fp, DWORD ofs)
Definition: ff.c:2994
int ff_cre_syncobj(BYTE vol, _SYNC_t *sobj)
Definition: reentrant.c:27
Definition: ff.h:189
FRESULT f_truncate(FIL *fp)
Definition: ff.c:3384
void ff_rel_grant(_SYNC_t sobj)
Definition: reentrant.c:93
FRESULT f_forward(FIL *fp, UINT(*func)(const BYTE *, UINT), UINT btf, UINT *bf)
Definition: ff.h:185
unsigned char BYTE
Definition: integer.h:16
BYTE n_fats
Definition: ff.h:81
UINT lfsize
Definition: ff.h:172
DWORD winsect
Definition: ff.h:105
FRESULT f_closedir(DIR *dp)
Definition: ff.c:3208
Definition: str.hpp:100
DWORD fatbase
Definition: ff.h:102
Definition: ff.h:186
FRESULT f_stat(const TCHAR *path, FILINFO *fno)
Definition: ff.c:3282
FRESULT f_rename(const TCHAR *path_old, const TCHAR *path_new)
Definition: ff.c:3669
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
Definition: ff.c:2651
FRESULT
Definition: ff.h:180
Definition: ff.h:193
FRESULT f_sync(FIL *fp)
Definition: ff.c:2773
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition: ff.c:2415
WORD id
Definition: ff.h:84
DWORD sclust
Definition: ff.h:120
_SYNC_t sobj
Definition: ff.h:90
BYTE drv
Definition: ff.h:79
FRESULT f_mkfs(const TCHAR *path, BYTE sfd, UINT au)
Definition: ff.c:3976
Definition: ff.h:196
DWORD get_fattime(void)
Definition: fatfs_time.c:8
Definition: ff.h:199
BYTE fsi_flag
Definition: ff.h:83
BYTE * dir_ptr
Definition: ff.h:125
BYTE fattrib
Definition: ff.h:168
Definition: ff.h:195
DWORD dsect
Definition: ff.h:122
unsigned int UINT
Definition: integer.h:25
DWORD clust
Definition: ff.h:147
FRESULT f_setlabel(const TCHAR *label)
#define _SYNC_t
Definition: ffconf.h:204
BYTE err
Definition: ff.h:117
DWORD volbase
Definition: ff.h:101
Definition: ff.h:197
DWORD fsize
Definition: ff.h:165
WORD n_rootdir
Definition: ff.h:85
FRESULT f_utime(const TCHAR *path, const FILINFO *fno)
Definition: ff.c:3627
Definition: ff.h:113
TCHAR * f_gets(TCHAR *buff, int len, FIL *fp)
int f_printf(FIL *fp, const TCHAR *str,...)
DWORD free_clust
Definition: ff.h:94
Definition: ff.h:181
BYTE wflag
Definition: ff.h:82
DWORD fptr
Definition: ff.h:118
DWORD clust
Definition: ff.h:121
Definition: ff.h:198
Definition: ff.h:200
Definition: ff.h:184
FATFS * fs
Definition: ff.h:114
DWORD dir_sect
Definition: ff.h:124
FRESULT f_opendir(DIR *dp, const TCHAR *path)
Definition: ff.c:3152
FRESULT f_unlink(const TCHAR *path)
Definition: ff.c:3440
FRESULT f_getcwd(TCHAR *buff, UINT len)
BYTE * fn
Definition: ff.h:150
Definition: ff.h:194
DWORD fsize
Definition: ff.h:100
Definition: ff.h:190
DWORD dirbase
Definition: ff.h:103
int ff_del_syncobj(_SYNC_t sobj)
Definition: reentrant.c:53
Definition: ff.h:191