Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

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