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.
8 / Copyright (C) 2007, ChaN, all right reserved.
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.
15 /---------------------------------------------------------------------------*/
21 /* The _MCU_ENDIAN defines which access method is used to the FAT structure.
22 / 1: Enable word access.
23 / 2: Disable word access and use byte-by-byte access instead.
24 / When the architectural byte order of the MCU is big-endian and/or address
25 / miss-aligned access is prohibited, the _MCU_ENDIAN must be set to 2.
26 / If it is not the case, it can be set to 1 for good code efficiency. */
28 #define _FS_READONLY 1
29 /* Setting _FS_READONLY to 1 defines read only configuration. This removes
30 / writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename
31 / and useless f_getfree. */
33 #define _FS_MINIMIZE 0
34 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
36 / 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod and f_rename are removed.
37 / 2: f_opendir and f_readdir are removed in addition to level 1.
38 / 3: f_lseek is removed in addition to level 2. */
41 /* To enable FAT32 support in addition of FAT12/16, set _FAT32 to 1. */
44 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */
47 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
48 / only US-ASCII(7bit) code can be accepted as file/directory name. */
51 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved.
52 / Note that the files are always accessed in case insensitive. */
58 /* Type definition for cluster number */
68 /* File system object structure */
71 WORD id
; /* File system mount ID */
72 WORD n_rootdir
; /* Number of root directory entries */
73 DWORD winsect
; /* Current sector appearing in the win[] */
74 DWORD fatbase
; /* FAT start sector */
75 DWORD dirbase
; /* Root directory start sector */
76 DWORD database
; /* Data start sector */
77 CLUST sects_fat
; /* Sectors per fat */
78 CLUST max_clust
; /* Maximum cluster# + 1 */
80 CLUST last_clust
; /* Last allocated cluster */
81 CLUST free_clust
; /* Number of free clusters */
83 DWORD fsi_sector
; /* fsinfo sector */
84 BYTE fsi_flag
; /* fsinfo dirty flag (1:must be written back) */
88 BYTE fs_type
; /* FAT sub type */
89 BYTE sects_clust
; /* Sectors per cluster */
90 BYTE n_fats
; /* Number of FAT copies */
91 BYTE winflag
; /* win[] dirty flag (1:must be written back) */
92 BYTE win
[512]; /* Disk access window for Directory/FAT/File */
97 /* Directory object structure */
100 WORD id
; /* Owner file system mount ID */
101 WORD index
; /* Current index */
102 FATFS
* fs
; /* Pointer to the owner file system object */
103 CLUST sclust
; /* Start cluster */
104 CLUST clust
; /* Current cluster */
105 DWORD sect
; /* Current sector */
110 /* File object structure */
113 WORD id
; /* Owner file system mount ID */
114 BYTE flag
; /* File status flags */
115 BYTE sect_clust
; /* Left sectors in 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 */
123 DWORD dir_sect
; /* Sector containing the directory entry */
124 BYTE
* dir_ptr
; /* Ponter to the directory entry in the window */
130 /* File status structure */
131 typedef struct _FILINFO
133 DWORD fsize
; /* Size */
134 WORD fdate
; /* Date */
135 WORD ftime
; /* Time */
136 BYTE fattrib
; /* Attribute */
137 char fname
[8+1+3+1]; /* Name (8.3 format) */
142 /* File function return code (FRESULT) */
146 FR_NOT_READY
, /* 1 */
149 FR_INVALID_NAME
, /* 4 */
150 FR_INVALID_DRIVE
, /* 5 */
154 FR_WRITE_PROTECTED
, /* 9 */
155 FR_NOT_ENABLED
, /* 10 */
156 FR_NO_FILESYSTEM
, /* 11 */
157 FR_INVALID_OBJECT
/* 12 */
162 /*-----------------------------------------------------*/
163 /* FatFs module application interface */
165 FRESULT
f_mount (BYTE
, FATFS
*); /* Mount/Unmount a logical drive */
166 FRESULT
f_open (FIL
*, const char*, BYTE
); /* Open or create a file */
167 FRESULT
f_read (FIL
*, void*, WORD
, WORD
*); /* Read data from a file */
168 FRESULT
f_write (FIL
*, const void*, WORD
, WORD
*); /* Write data to a file */
169 FRESULT
f_lseek (FIL
*, DWORD
); /* Move file pointer of a file object */
170 FRESULT
f_close (FIL
*); /* Close an open file object */
171 FRESULT
f_opendir (DIR*, const char*); /* Open an existing directory */
172 FRESULT
f_readdir (DIR*, FILINFO
*); /* Read a directory item */
173 FRESULT
f_stat (const char*, FILINFO
*); /* Get file status */
174 FRESULT
f_getfree (const char*, DWORD
*, FATFS
**); /* Get number of free clusters on the drive */
175 FRESULT
f_sync (FIL
*); /* Flush cached data of a writing file */
176 FRESULT
f_unlink (const char*); /* Delete an existing file or directory */
177 FRESULT
f_mkdir (const char*); /* Create a new directory */
178 FRESULT
f_chmod (const char*, BYTE
, BYTE
); /* Change file/dir attriburte */
179 FRESULT
f_rename (const char*, const char*); /* Rename/Move a file or directory */
182 /* User defined function to give a current time to fatfs module */
184 DWORD
get_fattime (void); /* 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) */
185 /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
189 /* File access control and file status flags (FIL.flag) */
192 #define FA_OPEN_EXISTING 0x00
194 #define FA_WRITE 0x02
195 #define FA_CREATE_NEW 0x04
196 #define FA_CREATE_ALWAYS 0x08
197 #define FA_OPEN_ALWAYS 0x10
198 #define FA__WRITTEN 0x20
200 #define FA__ERROR 0x80
203 /* FAT sub type (FATFS.fs_type) */
210 /* File attribute bits for directory entry */
212 #define AM_RDO 0x01 /* Read only */
213 #define AM_HID 0x02 /* Hidden */
214 #define AM_SYS 0x04 /* System */
215 #define AM_VOL 0x08 /* Volume label */
216 #define AM_LFN 0x0F /* LFN entry */
217 #define AM_DIR 0x10 /* Directory */
218 #define AM_ARC 0x20 /* Archive */
222 /* Offset of FAT structure members */
226 #define BPB_BytsPerSec 11
227 #define BPB_SecPerClus 13
228 #define BPB_RsvdSecCnt 14
229 #define BPB_NumFATs 16
230 #define BPB_RootEntCnt 17
231 #define BPB_TotSec16 19
233 #define BPB_FATSz16 22
234 #define BPB_SecPerTrk 24
235 #define BPB_NumHeads 26
236 #define BPB_HiddSec 28
237 #define BPB_TotSec32 32
241 #define BS_BootSig 38
244 #define BS_FilSysType 54
246 #define BPB_FATSz32 36
247 #define BPB_ExtFlags 40
249 #define BPB_RootClus 44
250 #define BPB_FSInfo 48
251 #define BPB_BkBootSec 50
252 #define BS_DrvNum32 64
253 #define BS_BootSig32 66
254 #define BS_VolID32 67
255 #define BS_VolLab32 71
256 #define BS_FilSysType32 82
258 #define FSI_LeadSig 0
259 #define FSI_StrucSig 484
260 #define FSI_Free_Count 488
261 #define FSI_Nxt_Free 492
263 #define MBR_Table 446
268 #define DIR_CrtTime 14
269 #define DIR_CrtDate 16
270 #define DIR_FstClusHI 20
271 #define DIR_WrtTime 22
272 #define DIR_WrtDate 24
273 #define DIR_FstClusLO 26
274 #define DIR_FileSize 28
278 /* Multi-byte word access macros */
280 #if _MCU_ENDIAN == 1 /* Use word access */
281 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
282 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
283 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
284 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
286 #if _MCU_ENDIAN == 2 /* Use byte-by-byte access */
287 #define LD_WORD(ptr) (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
288 #define LD_DWORD(ptr) (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr))
289 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
290 #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)
292 #error Do not forget to set _MCU_ENDIAN properly!