version 2.8
pff.h
1 /*---------------------------------------------------------------------------/
2 / Petit FatFs - FAT file system module include file R0.03 (C)ChaN, 2014
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.
7 /
8 / Copyright (C) 2014, ChaN, all right reserved.
9 /
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.
14 /
15 /----------------------------------------------------------------------------*/
16 
17 #ifndef _PFATFS
18 #define _PFATFS 4004 /* Revision ID */
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include "integer.h"
25 #include "pffconf.h"
26 
27 #if _PFATFS != _PFFCONF
28 #error Wrong configuration file (pffconf.h).
29 #endif
30 
31 #if _FS_FAT32
32 #define CLUST DWORD
33 #else
34 #define CLUST WORD
35 #endif
36 
37 
38 /* File system object structure */
39 
40 typedef struct {
41  BYTE fs_type; /* FAT sub type */
42  BYTE flag; /* File status flags */
43  BYTE csize; /* Number of sectors per cluster */
44  BYTE pad1;
45  WORD n_rootdir; /* Number of root directory entries (0 on FAT32) */
46  CLUST n_fatent; /* Number of FAT entries (= number of clusters + 2) */
47  DWORD fatbase; /* FAT start sector */
48  DWORD dirbase; /* Root directory start sector (Cluster# on FAT32) */
49  DWORD database; /* Data start sector */
50  DWORD fptr; /* File R/W pointer */
51  DWORD fsize; /* File size */
52  CLUST org_clust; /* File start cluster */
53  CLUST curr_clust; /* File current cluster */
54  DWORD dsect; /* File current data sector */
55 } FATFS;
56 
57 
58 
59 /* Directory object structure */
60 
61 typedef struct {
62  WORD index; /* Current read/write index number */
63  BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
64  CLUST sclust; /* Table start cluster (0:Static table) */
65  CLUST clust; /* Current cluster */
66  DWORD sect; /* Current sector */
67 } DIR;
68 
69 
70 
71 /* File status structure */
72 
73 typedef struct {
74  DWORD fsize; /* File size */
75  WORD fdate; /* Last modified date */
76  WORD ftime; /* Last modified time */
77  BYTE fattrib; /* Attribute */
78  char fname[13]; /* File name */
79 } FILINFO;
80 
81 
82 
83 /* File function return code (FRESULT) */
84 
85 typedef enum {
86  FR_OK = 0, /* 0 */
87  FR_DISK_ERR, /* 1 */
88  FR_NOT_READY, /* 2 */
89  FR_NO_FILE, /* 3 */
90  FR_NOT_OPENED, /* 4 */
91  FR_NOT_ENABLED, /* 5 */
92  FR_NO_FILESYSTEM /* 6 */
93 } FRESULT;
94 
95 
96 
97 /*--------------------------------------------------------------*/
98 /* Petit FatFs module application interface */
99 
100 FRESULT pf_mount (FATFS* fs); /* Mount/Unmount a logical drive */
101 FRESULT pf_open (const char* path); /* Open a file */
102 FRESULT pf_read (void* buff, UINT btr, UINT* br); /* Read data from the open file */
103 FRESULT pf_write (const void* buff, UINT btw, UINT* bw); /* Write data to the open file */
104 FRESULT pf_lseek (DWORD ofs); /* Move file pointer of the open file */
105 FRESULT pf_opendir (DIR* dj, const char* path); /* Open a directory */
106 FRESULT pf_readdir (DIR* dj, FILINFO* fno); /* Read a directory item from the open directory */
107 
108 
109 
110 /*--------------------------------------------------------------*/
111 /* Flags and offset address */
112 
113 /* File status flag (FATFS.flag) */
114 
115 #define FA_OPENED 0x01
116 #define FA_WPRT 0x02
117 #define FA__WIP 0x40
118 
119 
120 /* FAT sub type (FATFS.fs_type) */
121 
122 #define FS_FAT12 1
123 #define FS_FAT16 2
124 #define FS_FAT32 3
125 
126 
127 /* File attribute bits for directory entry */
128 
129 #define AM_RDO 0x01 /* Read only */
130 #define AM_HID 0x02 /* Hidden */
131 #define AM_SYS 0x04 /* System */
132 #define AM_VOL 0x08 /* Volume label */
133 #define AM_LFN 0x0F /* LFN entry */
134 #define AM_DIR 0x10 /* Directory */
135 #define AM_ARC 0x20 /* Archive */
136 #define AM_MASK 0x3F /* Mask of defined bits */
137 
138 
139 /*--------------------------------*/
140 /* Multi-byte word access macros */
141 
142 #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
143 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
144 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
145 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
146 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
147 #else /* Use byte-by-byte access to the FAT structure */
148 #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
149 #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
150 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
151 #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)
152 #endif
153 
154 
155 #ifdef __cplusplus
156 }
157 #endif
158 
159 #endif /* _PFATFS */