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