1 /*---------------------------------------------------------------------------/
2 / Petit FatFs - FAT file system module include file R0.02a (C)ChaN, 2010
3 /----------------------------------------------------------------------------/
4 / Petit FatFs module is an open source software to implement FAT file system to
5 / small embedded systems. This is a free software and is opened for education,
6 / research and commercial developments under license policy of following trems.
8 / Copyright (C) 2010, ChaN, all right reserved.
10 / * The Petit 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 use UNDER YOUR RESPONSIBILITY.
13 / * Redistributions of source code must retain the above copyright notice.
15 /----------------------------------------------------------------------------*/
19 /*---------------------------------------------------------------------------/
20 / Petit FatFs Configuration Options
22 / CAUTION! Do not forget to make clean the project after any changes to
23 / the configuration options.
25 /----------------------------------------------------------------------------*/
29 #define _USE_READ 1 /* 1:Enable pf_read() */
31 #define _USE_DIR 0 /* 1:Enable pf_opendir() and pf_readdir() */
33 #define _USE_LSEEK 0 /* 1:Enable pf_lseek() */
35 #define _USE_WRITE 0 /* 1:Enable pf_write() */
37 #define _FS_FAT12 1 /* 1:Enable FAT12 support */
38 #define _FS_FAT32 0 /* 1:Enable FAT32 support */
42 /* Defines which code page is used for path name. Supported code pages are:
43 / 932, 936, 949, 950, 437, 720, 737, 775, 850, 852, 855, 857, 858, 862, 866,
44 / 874, 1250, 1251, 1252, 1253, 1254, 1255, 1257, 1258 and 1 (ASCII only).
45 / SBCS code pages except for 1 requiers a case conversion table. This
46 / might occupy 128 bytes on the RAM on some platforms, e.g. avr-gcc. */
49 #define _WORD_ACCESS 0
50 /* The _WORD_ACCESS option defines which access method is used to the word
51 / data in the FAT structure.
53 / 0: Byte-by-byte access. Always compatible with all platforms.
54 / 1: Word access. Do not choose this unless following condition is met.
56 / When the byte order on the memory is big-endian or address miss-aligned
57 / word access results incorrect behavior, the _WORD_ACCESS must be set to 0.
58 / If it is not the case, the value can also be set to 1 to improve the
59 / performance and code efficiency. */
62 /* End of configuration options. Do not change followings without care. */
63 /*--------------------------------------------------------------------------*/
74 /* File system object structure */
77 BYTE fs_type
; /* FAT sub type */
78 BYTE flag
; /* File status flags */
79 BYTE csize
; /* Number of sectors per cluster */
81 WORD n_rootdir
; /* Number of root directory entries (0 on FAT32) */
82 CLUST n_fatent
; /* Number of FAT entries (= number of clusters + 2) */
83 DWORD fatbase
; /* FAT start sector */
84 DWORD dirbase
; /* Root directory start sector (Cluster# on FAT32) */
85 DWORD database
; /* Data start sector */
86 DWORD fptr
; /* File R/W pointer */
87 DWORD fsize
; /* File size */
88 CLUST org_clust
; /* File start cluster */
89 CLUST curr_clust
; /* File current cluster */
90 DWORD dsect
; /* File current data sector */
95 /* Directory object structure */
98 WORD index
; /* Current read/write index number */
99 BYTE
* fn
; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
100 CLUST sclust
; /* Table start cluster (0:Static table) */
101 CLUST clust
; /* Current cluster */
102 DWORD sect
; /* Current sector */
107 /* File status structure */
110 DWORD fsize
; /* File size */
111 WORD fdate
; /* Last modified date */
112 WORD ftime
; /* Last modified time */
113 BYTE fattrib
; /* Attribute */
114 char fname
[13]; /* File name */
119 /* File function return code (FRESULT) */
124 FR_NOT_READY
, /* 2 */
127 FR_NOT_OPENED
, /* 5 */
128 FR_NOT_ENABLED
, /* 6 */
129 FR_NO_FILESYSTEM
/* 7 */
134 /*--------------------------------------------------------------*/
135 /* Petit FatFs module application interface */
137 FRESULT
pf_mount (FATFS
*); /* Mount/Unmount a logical drive */
138 FRESULT
pf_open (const char*); /* Open a file */
139 FRESULT
pf_read (void*, WORD
, WORD
*); /* Read data from the open file */
140 FRESULT
pf_write (const void*, WORD
, WORD
*); /* Write data to the open file */
141 FRESULT
pf_lseek (DWORD
); /* Move file pointer of the open file */
142 FRESULT
pf_opendir (DIR*, const char*); /* Open a directory */
143 FRESULT
pf_readdir (DIR*, FILINFO
*); /* Read a directory item from the open directory */
147 /*--------------------------------------------------------------*/
148 /* Flags and offset address */
150 /* File status flag (FATFS.flag) */
152 #define FA_OPENED 0x01
157 /* FAT sub type (FATFS.fs_type) */
164 /* File attribute bits for directory entry */
166 #define AM_RDO 0x01 /* Read only */
167 #define AM_HID 0x02 /* Hidden */
168 #define AM_SYS 0x04 /* System */
169 #define AM_VOL 0x08 /* Volume label */
170 #define AM_LFN 0x0F /* LFN entry */
171 #define AM_DIR 0x10 /* Directory */
172 #define AM_ARC 0x20 /* Archive */
173 #define AM_MASK 0x3F /* Mask of defined bits */
176 /*--------------------------------*/
177 /* Multi-byte word access macros */
179 #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
180 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
181 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
182 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
183 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
184 #else /* Use byte-by-byte access to the FAT structure */
185 #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
186 #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
187 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
188 #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)