changing circuitry to disable RTC, update initialization to match
[fw/openalt] / fatfs / ff.h
1 /*--------------------------------------------------------------------------/
2 /  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.
7 /
8 /  Copyright (C) 2007, 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 /   profit use without any restriction under your responsibility.
13 / * Redistributions of source code must retain the above copyright notice.
14 /
15 /---------------------------------------------------------------------------*/
16
17 #ifndef _FATFS_H_
18 #define _FATFS_H_
19
20 #define _MCU_ENDIAN 2
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. */
27
28 #define _FS_READONLY    0
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. */
32
33 #define _FS_MINIMIZE    0
34 /* The _FS_MINIMIZE option defines minimization level to remove some functions.
35 /  0: Full function.
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. */
39
40 #define _DRIVES     1
41 /* Number of logical drives to be used. This affects the size of internal table. */
42
43 #define _USE_MKFS   1
44 /* When _USE_MKFS is set to 1 and _FS_READONLY is set to 0, f_mkfs function is
45 /  enabled. */
46
47 #define _MULTI_PARTITION    0
48 /* When _MULTI_PARTITION is set to 0, each logical drive is bound to same
49 /  physical drive number and can mount only 1st primaly partition. When it is
50 /  set to 1, each logical drive can mount a partition listed in Drives[]. */
51
52 #define _USE_FSINFO 1
53 /* To enable FSInfo support on FAT32 volume, set _USE_FSINFO to 1. */
54
55 #define _USE_SJIS   1
56 /* When _USE_SJIS is set to 1, Shift-JIS code transparency is enabled, otherwise
57 /  only US-ASCII(7bit) code can be accepted as file/directory name. */
58
59 #define _USE_NTFLAG 1
60 /* When _USE_NTFLAG is set to 1, upper/lower case of the file name is preserved.
61 /  Note that the files are always accessed in case insensitive. */
62
63 #include "sysdefs.h"
64
65 //
66 //  Definitions corresponds to multiple sector size (not tested) 
67 //
68 #define S_MAX_SIZ   512         /* Do not change */
69 #if S_MAX_SIZ > 512
70 #define S_SIZ   (fs->s_size)
71 #else
72 #define S_SIZ   512
73 #endif
74
75 //
76 //  File system object structure 
77 //
78 typedef struct _FATFS
79 {
80     U16 id;             /* File system mount ID */
81     U16 n_rootdir;      /* Number of root directory entries */
82     U32 winsect;        /* Current sector appearing in the win[] */
83     U32 sects_fat;      /* Sectors per fat */
84     U32 max_clust;      /* Maximum cluster# + 1 */
85     U32 fatbase;        /* FAT start sector */
86     U32 dirbase;        /* Root directory start sector (cluster# for FAT32) */
87     U32 database;       /* Data start sector */
88 #if _FS_READONLY == 0
89     U32 last_clust;     /* Last allocated cluster */
90     U32 free_clust;     /* Number of free clusters */
91 #if _USE_FSINFO
92     U32 fsi_sector;     /* fsinfo sector */
93     U8  fsi_flag;       /* fsinfo dirty flag (1:must be written back) */
94     U8  pad2;
95 #endif
96 #endif
97     U8  fs_type;        /* FAT sub type */
98     U8  sects_clust;    /* Sectors per cluster */
99 #if S_MAX_SIZ > 512
100     U16 s_size;         /* Sector size */
101 #endif
102     U8  n_fats;         /* Number of FAT copies */
103     U8  drive;          /* Physical drive number */
104     U8  winflag;        /* win[] dirty flag (1:must be written back) */
105     U8  pad1;
106     U8  win[S_MAX_SIZ]; /* Disk access window for Directory/FAT */
107
108 /* __attribute__ ((packed)) */ FATFS;
109
110 //
111 //  Directory object structure 
112 //
113 typedef struct _DIR 
114 {
115     U16 id;         /* Owner file system mount ID */
116     U16 index;      /* Current index */
117     FATFS*  fs;     /* Pointer to the owner file system object */
118     U32 sclust;     /* Start cluster */
119     U32 clust;      /* Current cluster */
120     U32 sect;       /* Current sector */
121
122 /* __attribute__ ((packed)) */ DIR;
123
124 //
125 //  File object structure 
126 //
127 typedef struct _FIL 
128 {
129     U16 id;             /* Owner file system mount ID */
130     U8  flag;           /* File status flags */
131     U8  sect_clust;     /* Left sectors in cluster */
132     FATFS* fs;          /* Pointer to the owner file system object */
133     U32 fptr;           /* File R/W pointer */
134     U32 fsize;          /* File size */
135     U32 org_clust;      /* File start cluster */
136     U32 curr_clust;     /* Current cluster */
137     U32 curr_sect;      /* Current sector */
138 #if _FS_READONLY == 0
139     U32 dir_sect;       /* Sector containing the directory entry */
140     U8* dir_ptr;        /* Ponter to the directory entry in the window */
141 #endif
142     U8  buffer [S_MAX_SIZ];  /* File R/W buffer */
143
144 /* __attribute__ ((packed)) */ FIL;
145
146 //
147 //  File status structure 
148 //
149 typedef struct _FILINFO 
150 {
151     U32 fsize;              /* Size */
152     U16 fdate;              /* Date */
153     U16 ftime;              /* Time */
154     U8 fattrib;             /* Attribute */
155     char fname [8+1+3+1];   /* Name (8.3 format) */
156
157 /* __attribute__ ((packed)) */ FILINFO;
158
159 //
160 //  Definitions corresponds to multi partition 
161 //
162 #if _MULTI_PARTITION != 0   /* Multiple partition cfg */
163
164 typedef struct _PARTITION 
165 {
166     U8 pd;  /* Physical drive # (0-255) */
167     U8 pt;  /* Partition # (0-3) */
168
169 /* __attribute__ ((packed)) */ PARTITION;
170
171 extern const PARTITION Drives [];   /* Logical drive# to physical location conversion table */
172 #define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */
173 #define LD2PT(drv) (Drives[drv].pt) /* Get partition# */
174
175 #else                       /* Single partition cfg */
176
177 #define LD2PD(drv) (drv)        /* Physical drive# is equal to logical drive# */
178 #define LD2PT(drv) 0            /* Always mounts the 1st partition */
179
180 #endif
181
182 //
183 //  File function return code (FRESULT) 
184 //
185 typedef enum 
186 {
187     FR_OK = 0,          /* 0 */
188     FR_NOT_READY,       /* 1 */
189     FR_NO_FILE,         /* 2 */
190     FR_NO_PATH,         /* 3 */
191     FR_INVALID_NAME,    /* 4 */
192     FR_INVALID_DRIVE,   /* 5 */
193     FR_DENIED,          /* 6 */
194     FR_EXIST,           /* 7 */
195     FR_RW_ERROR,        /* 8 */
196     FR_WRITE_PROTECTED, /* 9 */
197     FR_NOT_ENABLED,     /* 10 */
198     FR_NO_FILESYSTEM,   /* 11 */
199     FR_INVALID_OBJECT,  /* 12 */
200     FR_MKFS_ABORTED     /* 13 */
201
202 FRESULT;
203
204 //
205 //  FatFs module application interface
206 //
207 void    f_printerror (FRESULT f);               /* Print error code */
208 FRESULT f_mount (U8, FATFS*);                   /* Mount/Unmount a logical drive */
209 FRESULT f_open (FIL*, const char*, U8);         /* Open or create a file */
210 FRESULT f_read (FIL*, void*, U16, U16*);        /* Read data from a file */
211 FRESULT f_write (FIL*, const void*, U16, U16*); /* Write data to a file */
212 FRESULT f_lseek (FIL*, U32);                    /* Move file pointer of a file object */
213 FRESULT f_close (FIL*);                         /* Close an open file object */
214 FRESULT f_opendir (DIR*, const char*);          /* Open an existing directory */
215 FRESULT f_readdir (DIR*, FILINFO*);             /* Read a directory item */
216 FRESULT f_stat (const char*, FILINFO*);         /* Get file status */
217 FRESULT f_getfree (const char*, U32*, FATFS**); /* Get number of free clusters on the drive */
218 FRESULT f_sync (FIL*);                          /* Flush cached data of a writing file */
219 FRESULT f_unlink (const char*);                 /* Delete an existing file or directory */
220 FRESULT f_mkdir (const char*);                  /* Create a new directory */
221 FRESULT f_chmod (const char*, U8, U8);          /* Change file/dir attriburte */
222 FRESULT f_rename (const char*, const char*);    /* Rename/Move a file or directory */
223 FRESULT f_mkfs (U8, U8, U8);                    /* Create a file system on the drive */
224
225 //
226 //  User defined function to give a current time to fatfs module 
227 //
228 U32 get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
229                         /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
230
231 //
232 //  File access control and file status flags (FIL.flag)
233 //
234 #define FA_OPEN_EXISTING    0x00
235 #define FA_READ             0x01
236 #if _FS_READONLY == 0
237 #define FA_WRITE            0x02
238 #define FA_CREATE_NEW       0x04
239 #define FA_CREATE_ALWAYS    0x08
240 #define FA_OPEN_ALWAYS      0x10
241 #define FA__WRITTEN         0x20
242 #define FA__DIRTY           0x40
243 #endif
244 #define FA__ERROR           0x80
245
246 //
247 //  FAT sub type (FATFS.fs_type) 
248 //
249 #define FS_FAT12    1
250 #define FS_FAT16    2
251 #define FS_FAT32    3
252
253 //
254 //  File attribute bits for directory entry 
255 //
256 #define AM_RDO  0x01    /* Read only */
257 #define AM_HID  0x02    /* Hidden */
258 #define AM_SYS  0x04    /* System */
259 #define AM_VOL  0x08    /* Volume label */
260 #define AM_LFN  0x0F    /* LFN entry */
261 #define AM_DIR  0x10    /* Directory */
262 #define AM_ARC  0x20    /* Archive */
263
264 //
265 //  Offset of FAT structure members 
266 //
267 #define BS_jmpBoot          0
268 #define BS_OEMName          3
269 #define BPB_BytsPerSec      11
270 #define BPB_SecPerClus      13
271 #define BPB_RsvdSecCnt      14
272 #define BPB_NumFATs         16
273 #define BPB_RootEntCnt      17
274 #define BPB_TotSec16        19
275 #define BPB_Media           21
276 #define BPB_FATSz16         22
277 #define BPB_SecPerTrk       24
278 #define BPB_NumHeads        26
279 #define BPB_HiddSec         28
280 #define BPB_TotSec32        32
281 #define BS_55AA             510
282
283 #define BS_DrvNum           36
284 #define BS_BootSig          38
285 #define BS_VolID            39
286 #define BS_VolLab           43
287 #define BS_FilSysType       54
288
289 #define BPB_FATSz32         36
290 #define BPB_ExtFlags        40
291 #define BPB_FSVer           42
292 #define BPB_RootClus        44
293 #define BPB_FSInfo          48
294 #define BPB_BkBootSec       50
295 #define BS_DrvNum32         64
296 #define BS_BootSig32        66
297 #define BS_VolID32          67
298 #define BS_VolLab32         71
299 #define BS_FilSysType32     82
300
301 #define FSI_LeadSig         0
302 #define FSI_StrucSig        484
303 #define FSI_Free_Count      488
304 #define FSI_Nxt_Free        492
305
306 #define MBR_Table           446
307
308 #define DIR_Name            0
309 #define DIR_Attr            11
310 #define DIR_NTres           12
311 #define DIR_CrtTime         14
312 #define DIR_CrtDate         16
313 #define DIR_FstClusHI       20
314 #define DIR_WrtTime         22
315 #define DIR_WrtDate         24
316 #define DIR_FstClusLO       26
317 #define DIR_FileSize        28
318
319 //
320 //  Multi-byte word access macros  
321 //  
322 #if _MCU_ENDIAN == 1    /* Use word access */
323 #define LD_U16(ptr)     (U16)(*(U16*)(U8*)(ptr))
324 #define LD_U32(ptr)     (U32)(*(U32*)(U8*)(ptr))
325 #define ST_U16(ptr,val) *(U16*)(U8*)(ptr)=(U16)(val)
326 #define ST_U32(ptr,val) *(U32*)(U8*)(ptr)=(U32)(val)
327 #else
328 #if _MCU_ENDIAN == 2    /* Use byte-by-byte access */
329 #define LD_U16(ptr)     (U16)(((U16)*(U8*)((ptr)+1)<<8)|(U16)*(U8*)(ptr))
330 #define LD_U32(ptr)     (U32)(((U32)*(U8*)((ptr)+3)<<24)|((U32)*(U8*)((ptr)+2)<<16)|((U16)*(U8*)((ptr)+1)<<8)|*(U8*)(ptr))
331 #define ST_U16(ptr,val) *(U8*)(ptr)=(U8)(val); *(U8*)((ptr)+1)=(U8)((U16)(val)>>8)
332 #define ST_U32(ptr,val) *(U8*)(ptr)=(U8)(val); *(U8*)((ptr)+1)=(U8)((U16)(val)>>8); *(U8*)((ptr)+2)=(U8)((U32)(val)>>16); *(U8*)((ptr)+3)=(U8)((U32)(val)>>24)
333 #else
334 #error Do not forget to set _MCU_ENDIAN properly!
335 #endif
336 #endif
337
338 #endif