Lightweight SD card FAT file system. Originaled by chan http://elm-chan.org/fsw/ff/00index_p.html

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pff.h Source File

pff.h

00001 /* PFF for mbed porting by Takuya Urakawa, 2013 */
00002 
00003 /*---------------------------------------------------------------------------/
00004 /  Petit FatFs - FAT file system module include file  R0.02a   (C)ChaN, 2010
00005 /----------------------------------------------------------------------------/
00006 / Petit FatFs module is an open source software to implement FAT file system to
00007 / small embedded systems. This is a free software and is opened for education,
00008 / research and commercial developments under license policy of following trems.
00009 /
00010 /  Copyright (C) 2010, ChaN, all right reserved.
00011 /
00012 / * The Petit FatFs module is a free software and there is NO WARRANTY.
00013 / * No restriction on use. You can use, modify and redistribute it for
00014 /   personal, non-profit or commercial use UNDER YOUR RESPONSIBILITY.
00015 / * Redistributions of source code must retain the above copyright notice.
00016 /
00017 /----------------------------------------------------------------------------*/
00018 
00019 #include "integer.h"
00020 
00021 /*---------------------------------------------------------------------------/
00022 / Petit FatFs Configuration Options
00023 /
00024 / CAUTION! Do not forget to make clean the project after any changes to
00025 / the configuration options.
00026 /
00027 /----------------------------------------------------------------------------*/
00028 #ifndef _FATFS
00029 #define _FATFS
00030 
00031 #define _USE_READ   1   /* 1:Enable pf_read() */
00032 
00033 #define _USE_DIR    1   /* 1:Enable pf_opendir() and pf_readdir() */
00034 
00035 #define _USE_LSEEK  1   /* 1:Enable pf_lseek() */
00036 
00037 #define _USE_WRITE  1   /* 1:Enable pf_write() */
00038 
00039 #define _FS_FAT12   1   /* 1:Enable FAT12 support */
00040 #define _FS_FAT32   1   /* 1:Enable FAT32 support */
00041 
00042 
00043 #define _CODE_PAGE  1
00044 /* Defines which code page is used for path name. Supported code pages are:
00045 /  932, 936, 949, 950, 437, 720, 737, 775, 850, 852, 855, 857, 858, 862, 866,
00046 /  874, 1250, 1251, 1252, 1253, 1254, 1255, 1257, 1258 and 1 (ASCII only).
00047 /  SBCS code pages except for 1 requiers a case conversion table. This
00048 /  might occupy 128 bytes on the RAM on some platforms, e.g. avr-gcc. */
00049 
00050 
00051 #define _WORD_ACCESS    0
00052 /* The _WORD_ACCESS option defines which access method is used to the word
00053 /  data in the FAT structure.
00054 /
00055 /   0: Byte-by-byte access. Always compatible with all platforms.
00056 /   1: Word access. Do not choose this unless following condition is met.
00057 /
00058 /  When the byte order on the memory is big-endian or address miss-aligned
00059 /  word access results incorrect behavior, the _WORD_ACCESS must be set to 0.
00060 /  If it is not the case, the value can also be set to 1 to improve the
00061 /  performance and code efficiency. */
00062 
00063 
00064 /* End of configuration options. Do not change followings without care.     */
00065 /*--------------------------------------------------------------------------*/
00066 
00067 
00068 
00069 #if _FS_FAT32
00070 #define CLUST   DWORD
00071 #else
00072 #define CLUST   WORD
00073 #endif
00074 
00075 /* File system object structure */
00076 
00077 typedef struct {
00078     BYTE    fs_type;    /* FAT sub type */
00079     BYTE    flag;       /* File status flags */
00080     BYTE    csize;      /* Number of sectors per cluster */
00081     BYTE    pad1;
00082     WORD    n_rootdir;  /* Number of root directory entries (0 on FAT32) */
00083     CLUST   n_fatent;   /* Number of FAT entries (= number of clusters + 2) */
00084     DWORD   fatbase;    /* FAT start sector */
00085     DWORD   dirbase;    /* Root directory start sector (Cluster# on FAT32) */
00086     DWORD   database;   /* Data start sector */
00087     DWORD   fptr;       /* File R/W pointer */
00088     DWORD   fsize;      /* File size */
00089     CLUST   org_clust;  /* File start cluster */
00090     CLUST   curr_clust; /* File current cluster */
00091     DWORD   dsect;      /* File current data sector */
00092 } FATFS;
00093 
00094 
00095 
00096 /* Directory object structure */
00097 
00098 typedef struct {
00099     WORD    index;      /* Current read/write index number */
00100     BYTE*   fn;         /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
00101     CLUST   sclust;     /* Table start cluster (0:Static table) */
00102     CLUST   clust;      /* Current cluster */
00103     DWORD   sect;       /* Current sector */
00104 } PFFDIR;
00105 
00106 
00107 
00108 /* File status structure */
00109 
00110 typedef struct {
00111     DWORD   fsize;      /* File size */
00112     WORD    fdate;      /* Last modified date */
00113     WORD    ftime;      /* Last modified time */
00114     BYTE    fattrib;    /* Attribute */
00115     char    fname[13];  /* File name */
00116 } FILINFO;
00117 
00118 
00119 
00120 /* File function return code (FRESULT) */
00121 
00122 typedef enum {
00123     FR_OK = 0,          /* 0 */
00124     FR_DISK_ERR,        /* 1 */
00125     FR_NOT_READY,       /* 2 */
00126     FR_NO_FILE,         /* 3 */
00127     FR_NO_PATH,         /* 4 */
00128     FR_NOT_OPENED,      /* 5 */
00129     FR_NOT_ENABLED,     /* 6 */
00130     FR_NO_FILESYSTEM    /* 7 */
00131 } FRESULT;
00132 
00133 
00134 
00135 /*--------------------------------------------------------------*/
00136 /* Petit FatFs module application interface                     */
00137 
00138 FRESULT pf_mount (FATFS*);                      /* Mount/Unmount a logical drive */
00139 FRESULT pf_open (const char*);                  /* Open a file */
00140 FRESULT pf_read (void*, WORD, WORD*);           /* Read data from the open file */
00141 FRESULT pf_write (const void*, WORD, WORD*);    /* Write data to the open file */
00142 FRESULT pf_lseek (DWORD);                       /* Move file pointer of the open file */
00143 FRESULT pf_opendir (PFFDIR*, const char*);          /* Open a directory */
00144 FRESULT pf_readdir (PFFDIR*, FILINFO*);         /* Read a directory item from the open directory */
00145 
00146 
00147 /*--------------------------------------------------------------*/
00148 /* Flags and offset address                                     */
00149 
00150 /* File status flag (FATFS.flag) */
00151 
00152 #define FA_OPENED   0x01
00153 #define FA_WPRT     0x02
00154 #define FA__WIP     0x40
00155 
00156 
00157 /* FAT sub type (FATFS.fs_type) */
00158 
00159 #define FS_FAT12    1
00160 #define FS_FAT16    2
00161 #define FS_FAT32    3
00162 
00163 
00164 /* File attribute bits for directory entry */
00165 
00166 #define AM_RDO  0x01    /* Read only */
00167 #define AM_HID  0x02    /* Hidden */
00168 #define AM_SYS  0x04    /* System */
00169 #define AM_VOL  0x08    /* Volume label */
00170 #define AM_LFN  0x0F    /* LFN entry */
00171 #define AM_DIR  0x10    /* Directory */
00172 #define AM_ARC  0x20    /* Archive */
00173 #define AM_MASK 0x3F    /* Mask of defined bits */
00174 
00175 
00176 /*--------------------------------*/
00177 /* Multi-byte word access macros  */
00178 
00179 #if _WORD_ACCESS == 1   /* Enable word access to the FAT structure */
00180 #define LD_WORD(ptr)        (WORD)(*(WORD*)(BYTE*)(ptr))
00181 #define LD_DWORD(ptr)       (DWORD)(*(DWORD*)(BYTE*)(ptr))
00182 #define ST_WORD(ptr,val)    *(WORD*)(BYTE*)(ptr)=(WORD)(val)
00183 #define ST_DWORD(ptr,val)   *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
00184 #else                   /* Use byte-by-byte access to the FAT structure */
00185 #define LD_WORD(ptr)        (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
00186 #define LD_DWORD(ptr)       (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
00187 #define ST_WORD(ptr,val)    *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
00188 #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)
00189 #endif
00190 
00191 
00192 #endif /* _FATFS */