Arduino FatFS
Loading...
Searching...
No Matches
ffdef.h
1#pragma once
2#include "ffconf.h"
3
4/* Integer types used for FatFs API */
5
6#if defined(_WIN32) /* Main development platform */
7#define FF_INTDEF 2
8#include <windows.h>
9typedef unsigned __int64 QWORD;
10#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
11 defined(__cplusplus) /* C99 or later */
12#define FF_INTDEF 2
13#include <stdint.h>
14
15typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
16typedef unsigned char BYTE; /* char must be 8-bit */
17typedef uint16_t WORD; /* 16-bit unsigned integer */
18typedef uint32_t DWORD; /* 32-bit unsigned integer */
19typedef uint64_t QWORD; /* 64-bit unsigned integer */
20typedef WORD WCHAR; /* UTF-16 character type */
21#else /* Earlier than C99 */
22#define FF_INTDEF 1
23typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
24typedef unsigned char BYTE; /* char must be 8-bit */
25typedef unsigned short WORD; /* 16-bit unsigned integer */
26typedef unsigned long DWORD; /* 32-bit unsigned integer */
27typedef WORD WCHAR; /* UTF-16 character type */
28#endif
29
30
31/* Definitions of volume management */
32
33#if FF_MULTI_PARTITION /* Multiple partition configuration */
34struct PARTITION {
35 BYTE pd; /* Physical drive number */
36 BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
37} ;
38extern PARTITION VolToPart[]; /* Volume - Partition mapping table */
39#endif
40
41#if FF_STR_VOLUME_ID
42#ifndef FF_VOLUME_STRS
43extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */
44#endif
45#endif
46
47/* Type of path name strings on FatFs API */
48
49#ifndef _INC_TCHAR
50#define _INC_TCHAR
51
52#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
53typedef WCHAR TCHAR;
54#define _T(x) L##x
55#define _TEXT(x) L##x
56#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
57typedef char TCHAR;
58#define _T(x) u8##x
59#define _TEXT(x) u8##x
60#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
61typedef DWORD TCHAR;
62#define _T(x) U##x
63#define _TEXT(x) U##x
64#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
65#error Wrong FF_LFN_UNICODE setting
66#else /* ANSI/OEM code in SBCS/DBCS */
67typedef char TCHAR;
68#define _T(x) x
69#define _TEXT(x) x
70#endif
71
72#endif
73
74/* Type of file size and LBA variables */
75
76#if FF_FS_EXFAT
77#if FF_INTDEF != 2
78#error exFAT feature wants C99 or later
79#endif
80typedef QWORD FSIZE_t;
81#if FF_LBA64
82typedef QWORD LBA_t;
83#else
84typedef DWORD LBA_t;
85#endif
86#else
87#if FF_LBA64
88#error exFAT needs to be enabled when enable 64-bit LBA
89#endif
90typedef DWORD FSIZE_t;
91typedef DWORD LBA_t;
92#endif
93
94namespace fatfs {
95
96/* Filesystem object structure (FATFS) */
97
98struct FATFS {
99 BYTE fs_type; /* Filesystem type (0:not mounted) */
100 BYTE pdrv; /* Associated physical drive */
101 BYTE n_fats; /* Number of FATs (1 or 2) */
102 BYTE wflag; /* win[] flag (b0:dirty) */
103 BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
104 WORD id; /* Volume mount ID */
105 WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
106 WORD csize; /* Cluster size [sectors] */
107#if FF_MAX_SS != FF_MIN_SS
108 WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
109#endif
110#if FF_USE_LFN
111 WCHAR* lfnbuf; /* LFN working buffer */
112#endif
113#if FF_FS_EXFAT
114 BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
115#endif
116#if FF_FS_REENTRANT
117 FF_SYNC_t sobj; /* Identifier of sync object */
118#endif
119#if !FF_FS_READONLY
120 DWORD last_clst; /* Last allocated cluster */
121 DWORD free_clst; /* Number of free clusters */
122#endif
123#if FF_FS_RPATH
124 DWORD cdir; /* Current directory start cluster (0:root) */
125#if FF_FS_EXFAT
126 DWORD
127 cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
128 DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
129 DWORD
130 cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
131#endif
132#endif
133 DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
134 DWORD fsize; /* Size of an FAT [sectors] */
135 LBA_t volbase; /* Volume base sector */
136 LBA_t fatbase; /* FAT base sector */
137 LBA_t dirbase; /* Root directory base sector/cluster */
138 LBA_t database; /* Data base sector */
139#if FF_FS_EXFAT
140 LBA_t bitbase; /* Allocation bitmap base sector */
141#endif
142 LBA_t winsect; /* Current sector appearing in the win[] */
143 BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data
144 at tiny cfg) */
145};
146
147/* Object ID and allocation information (FFOBJID) */
148
149struct FFOBJID {
150 FATFS* fs; /* Pointer to the hosting volume of this object */
151 WORD id; /* Hosting volume mount ID */
152 BYTE attr; /* Object attribute */
153 BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous,
154 =3:fragmented in this session, b2:sub-directory stretched) */
155 DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
156 FSIZE_t objsize; /* Object size (valid when sclust != 0) */
157#if FF_FS_EXFAT
158 DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
159 DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when
160 not zero) */
161 DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
162 DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status
163 (valid when c_scl != 0) */
164 DWORD c_ofs; /* Offset in the containing directory (valid when file object and
165 sclust != 0) */
166#endif
167#if FF_FS_LOCK
168 UINT lockid; /* File lock ID origin from 1 (index of file semaphore table
169 Files[]) */
170#endif
171};
172
173/* File object structure (FIL) */
174
175struct FIL {
176 FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid
177 object pointer) */
178 BYTE flag; /* File status flags */
179 BYTE err; /* Abort flag (error code) */
180 FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
181 DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
182 LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */
183#if !FF_FS_READONLY
184 LBA_t dir_sect; /* Sector number containing the directory entry (not used at
185 exFAT) */
186 BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at
187 exFAT) */
188#endif
189#if FF_USE_FASTSEEK
190 DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by
191 application) */
192#endif
193#if !FF_FS_TINY
194 BYTE buf[FF_MAX_SS]; /* File private data read/write window */
195#endif
196};
197
198/* Directory object structure (DIR) */
199
200struct DIR {
201 FFOBJID obj; /* Object identifier */
202 DWORD dptr; /* Current read/write offset */
203 DWORD clust; /* Current cluster */
204 LBA_t sect; /* Current sector (0:Read operation has terminated) */
205 BYTE* dir; /* Pointer to the directory item in the win[] */
206 BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
207#if FF_USE_LFN
208 DWORD blk_ofs; /* Offset of current entry block being processed
209 (0xFFFFFFFF:Invalid) */
210#endif
211#if FF_USE_FIND
212 const TCHAR* pat; /* Pointer to the name matching pattern */
213#endif
214};
215
216/* File information structure (FILINFO) */
217
218struct FILINFO {
219 FSIZE_t fsize; /* File size */
220 WORD fdate; /* Modified date */
221 WORD ftime; /* Modified time */
222 BYTE fattrib; /* File attribute */
223#if FF_USE_LFN
224 TCHAR altname[FF_SFN_BUF + 1]; /* Altenative file name */
225 TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
226#else
227 TCHAR fname[12 + 1]; /* File name */
228#endif
229};
230
231/* Format parameter structure (MKFS_PARM) */
232
233struct MKFS_PARM {
234 BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
235 BYTE n_fat; /* Number of FATs */
236 UINT align; /* Data area alignment (sector) */
237 UINT n_root; /* Number of root directory entries */
238 DWORD au_size; /* Cluster size (byte) */
239};
240
241/* File function return code (FRESULT) */
242
243enum FRESULT {
244 FR_OK = 0, /* (0) Succeeded */
245 FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
246 FR_INT_ERR, /* (2) Assertion failed */
247 FR_NOT_READY, /* (3) The physical drive cannot work */
248 FR_NO_FILE, /* (4) Could not find the file */
249 FR_NO_PATH, /* (5) Could not find the path */
250 FR_INVALID_NAME, /* (6) The path name format is invalid */
251 FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
252 FR_EXIST, /* (8) Access denied due to prohibited access */
253 FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
254 FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
255 FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
256 FR_NOT_ENABLED, /* (12) The volume has no work area */
257 FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
258 FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
259 FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined
260 period */
261 FR_LOCKED, /* (16) The operation is rejected according to the file sharing
262 policy */
263 FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
264 FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
265 FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
266};
267
268/* Putchar output buffer and work area */
269
270struct putbuff {
271 FIL* fp; /* Ptr to the writing file */
272 int idx, nchr; /* Write index of buf[] (-1:error), number of encoding units
273 written */
274#if FF_USE_LFN && FF_LFN_UNICODE == 1
275 WCHAR hs;
276#elif FF_USE_LFN && FF_LFN_UNICODE == 2
277 BYTE bs[4];
278 UINT wi, ct;
279#endif
280 BYTE buf[64]; /* Write buffer */
281};
282
283/* File lock controls */
284#if FF_FS_LOCK != 0
285#if FF_FS_READONLY
286#error FF_FS_LOCK must be 0 at read-only configuration
287#endif
288struct FILESEM {
289 FATFS *fs; /* Object ID 1, volume (NULL:blank entry) */
290 DWORD clu; /* Object ID 2, containing directory (0:root) */
291 DWORD ofs; /* Object ID 3, offset in the directory */
292 WORD ctr; /* Object open counter, 0:none, 0x01..0xFF:read mode open count, 0x100:write mode */
293};
294
295#endif
296
297}
298
299/*--------------------------------------------------------------*/
300/* Flags and offset address */
301
302/* File access mode and open method flags (3rd argument of f_open) */
303#define FA_READ 0x01
304#define FA_WRITE 0x02
305#define FA_OPEN_EXISTING 0x00
306#define FA_CREATE_NEW 0x04
307#define FA_CREATE_ALWAYS 0x08
308#define FA_OPEN_ALWAYS 0x10
309#define FA_OPEN_APPEND 0x30
310
311/* Fast seek controls (2nd argument of f_lseek) */
312#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
313
314/* Format options (2nd argument of f_mkfs) */
315#define FM_FAT 0x01
316#define FM_FAT32 0x02
317#define FM_EXFAT 0x04
318#define FM_ANY 0x07
319#define FM_SFD 0x08
320
321/* Filesystem type (FATFS.fs_type) */
322#define FS_FAT12 1
323#define FS_FAT16 2
324#define FS_FAT32 3
325#define FS_EXFAT 4
326
327/* File attribute bits for directory entry (FILINFO.fattrib) */
328#define AM_RDO 0x01 /* Read only */
329#define AM_HID 0x02 /* Hidden */
330#define AM_SYS 0x04 /* System */
331#define AM_DIR 0x10 /* Directory */
332#define AM_ARC 0x20 /* Archive */
333
334#ifndef EOF
335#define EOF (-1)
336#endif
337
338#define SZDIRE 32 /* Size of a directory entry */
Definition ffdef.h:200
Definition ffdef.h:98
Definition ffdef.h:149
Definition ffdef.h:218
Definition ffdef.h:175
Definition ffdef.h:233
Definition ffdef.h:270