Marco Zecchini
/
Example_RTOS
Rtos API example
Embed:
(wiki syntax)
Show/hide line numbers
ff.h
00001 /*---------------------------------------------------------------------------/ 00002 / FatFs - FAT file system module include R0.11a (C)ChaN, 2015 00003 /----------------------------------------------------------------------------/ 00004 / FatFs module is a free software that opened under license policy of 00005 / following conditions. 00006 / 00007 / Copyright (C) 2015, ChaN, all right reserved. 00008 / 00009 / 1. Redistributions of source code must retain the above copyright notice, 00010 / this condition and the following disclaimer. 00011 / 00012 / This software is provided by the copyright holder and contributors "AS IS" 00013 / and any warranties related to this software are DISCLAIMED. 00014 / The copyright owner or contributors be NOT LIABLE for any damages caused 00015 / by use of this software. 00016 /---------------------------------------------------------------------------*/ 00017 00018 00019 #ifndef _FATFS 00020 #define _FATFS 64180 /* Revision ID */ 00021 00022 #ifdef __cplusplus 00023 extern "C" { 00024 #endif 00025 00026 #include "integer.h" /* Basic integer types */ 00027 #include "ffconf.h" /* FatFs configuration options */ 00028 #if _FATFS != _FFCONF 00029 #error Wrong configuration file (ffconf.h). 00030 #endif 00031 00032 00033 00034 /* Definitions of volume management */ 00035 00036 #if _MULTI_PARTITION /* Multiple partition configuration */ 00037 typedef struct { 00038 BYTE pd; /* Physical drive number */ 00039 BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ 00040 } PARTITION; 00041 extern PARTITION VolToPart[]; /* Volume - Partition resolution table */ 00042 #define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */ 00043 #define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */ 00044 00045 #else /* Single partition configuration */ 00046 #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */ 00047 #define LD2PT(vol) 0 /* Find first valid partition or in SFD */ 00048 00049 #endif 00050 00051 00052 00053 /* Type of path name strings on FatFs API */ 00054 00055 #if _LFN_UNICODE /* Unicode string */ 00056 #if !_USE_LFN 00057 #error _LFN_UNICODE must be 0 at non-LFN cfg. 00058 #endif 00059 #ifndef _INC_TCHAR 00060 typedef WCHAR TCHAR; 00061 #define _T(x) L ## x 00062 #define _TEXT(x) L ## x 00063 #endif 00064 00065 #else /* ANSI/OEM string */ 00066 #ifndef _INC_TCHAR 00067 typedef char TCHAR; 00068 #define _T(x) x 00069 #define _TEXT(x) x 00070 #endif 00071 00072 #endif 00073 00074 00075 00076 /* File system object structure (FATFS) */ 00077 00078 typedef struct { 00079 BYTE fs_type; /* FAT sub-type (0:Not mounted) */ 00080 BYTE drv; /* Physical drive number */ 00081 BYTE csize; /* Sectors per cluster (1,2,4...128) */ 00082 BYTE n_fats; /* Number of FAT copies (1 or 2) */ 00083 BYTE wflag; /* win[] flag (b0:dirty) */ 00084 BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ 00085 WORD id; /* File system mount ID */ 00086 WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ 00087 #if _MAX_SS != _MIN_SS 00088 WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */ 00089 #endif 00090 #if _FS_REENTRANT 00091 _SYNC_t sobj; /* Identifier of sync object */ 00092 #endif 00093 #if !_FS_READONLY 00094 DWORD last_clust; /* Last allocated cluster */ 00095 DWORD free_clust; /* Number of free clusters */ 00096 #endif 00097 #if _FS_RPATH 00098 DWORD cdir; /* Current directory start cluster (0:root) */ 00099 #endif 00100 DWORD n_fatent; /* Number of FAT entries, = number of clusters + 2 */ 00101 DWORD fsize; /* Sectors per FAT */ 00102 DWORD volbase; /* Volume start sector */ 00103 DWORD fatbase; /* FAT start sector */ 00104 DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */ 00105 DWORD database; /* Data start sector */ 00106 DWORD winsect; /* Current sector appearing in the win[] */ 00107 #if _FS_HEAPBUF 00108 BYTE *win; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ 00109 #else 00110 BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ 00111 #endif 00112 } FATFS; 00113 00114 00115 00116 /* File object structure (FIL) */ 00117 00118 typedef struct { 00119 FATFS* fs; /* Pointer to the related file system object (**do not change order**) */ 00120 WORD id; /* Owner file system mount ID (**do not change order**) */ 00121 BYTE flag; /* Status flags */ 00122 BYTE err; /* Abort flag (error code) */ 00123 DWORD fptr; /* File read/write pointer (Zeroed on file open) */ 00124 DWORD fsize; /* File size */ 00125 DWORD sclust; /* File start cluster (0:no cluster chain, always 0 when fsize is 0) */ 00126 DWORD clust; /* Current cluster of fpter (not valid when fprt is 0) */ 00127 DWORD dsect; /* Sector number appearing in buf[] (0:invalid) */ 00128 #if !_FS_READONLY 00129 DWORD dir_sect; /* Sector number containing the directory entry */ 00130 BYTE* dir_ptr; /* Pointer to the directory entry in the win[] */ 00131 #endif 00132 #if _USE_FASTSEEK 00133 DWORD* cltbl; /* Pointer to the cluster link map table (Nulled on file open) */ 00134 #endif 00135 #if _FS_LOCK 00136 UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ 00137 #endif 00138 #if !_FS_TINY 00139 #if _FS_HEAPBUF 00140 BYTE *buf; /* File private data read/write window */ 00141 #else 00142 BYTE buf[_MAX_SS]; /* File private data read/write window */ 00143 #endif 00144 #endif 00145 } FIL; 00146 00147 00148 00149 /* Directory object structure (FATFS_DIR) */ 00150 00151 typedef struct { 00152 FATFS* fs; /* Pointer to the owner file system object (**do not change order**) */ 00153 WORD id; /* Owner file system mount ID (**do not change order**) */ 00154 WORD index; /* Current read/write index number */ 00155 DWORD sclust; /* Table start cluster (0:Root dir) */ 00156 DWORD clust; /* Current cluster */ 00157 DWORD sect; /* Current sector */ 00158 BYTE* dir; /* Pointer to the current SFN entry in the win[] */ 00159 BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */ 00160 #if _FS_LOCK 00161 UINT lockid; /* File lock ID (index of file semaphore table Files[]) */ 00162 #endif 00163 #if _USE_LFN 00164 WCHAR* lfn; /* Pointer to the LFN working buffer */ 00165 WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */ 00166 #endif 00167 #if _USE_FIND 00168 const TCHAR* pat; /* Pointer to the name matching pattern */ 00169 #endif 00170 } FATFS_DIR; 00171 00172 00173 00174 /* File information structure (FILINFO) */ 00175 00176 typedef struct { 00177 DWORD fsize; /* File size */ 00178 WORD fdate; /* Last modified date */ 00179 WORD ftime; /* Last modified time */ 00180 BYTE fattrib; /* Attribute */ 00181 TCHAR fname[13]; /* Short file name (8.3 format) */ 00182 #if _USE_LFN 00183 TCHAR* lfname; /* Pointer to the LFN buffer */ 00184 UINT lfsize; /* Size of LFN buffer in TCHAR */ 00185 #endif 00186 } FILINFO; 00187 00188 00189 00190 /* File function return code (FRESULT) */ 00191 00192 typedef enum { 00193 FR_OK = 0, /* (0) Succeeded */ 00194 FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ 00195 FR_INT_ERR, /* (2) Assertion failed */ 00196 FR_NOT_READY, /* (3) The physical drive cannot work */ 00197 FR_NO_FILE, /* (4) Could not find the file */ 00198 FR_NO_PATH, /* (5) Could not find the path */ 00199 FR_INVALID_NAME, /* (6) The path name format is invalid */ 00200 FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ 00201 FR_EXIST, /* (8) Access denied due to prohibited access */ 00202 FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ 00203 FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ 00204 FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ 00205 FR_NOT_ENABLED, /* (12) The volume has no work area */ 00206 FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ 00207 FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */ 00208 FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ 00209 FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ 00210 FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ 00211 FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_LOCK */ 00212 FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ 00213 } FRESULT; 00214 00215 00216 00217 /*--------------------------------------------------------------*/ 00218 /* FatFs module application interface */ 00219 00220 FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ 00221 FRESULT f_close (FIL* fp); /* Close an open file object */ 00222 FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */ 00223 FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */ 00224 FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ 00225 FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */ 00226 FRESULT f_truncate (FIL* fp); /* Truncate file */ 00227 FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */ 00228 FRESULT f_opendir (FATFS_DIR* dp, const TCHAR* path); /* Open a directory */ 00229 FRESULT f_closedir (FATFS_DIR* dp); /* Close an open directory */ 00230 FRESULT f_readdir (FATFS_DIR* dp, FILINFO* fno); /* Read a directory item */ 00231 FRESULT f_findfirst (FATFS_DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ 00232 FRESULT f_findnext (FATFS_DIR* dp, FILINFO* fno); /* Find next file */ 00233 FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ 00234 FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ 00235 FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ 00236 FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ 00237 FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of the file/dir */ 00238 FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */ 00239 FRESULT f_chdir (const TCHAR* path); /* Change current directory */ 00240 FRESULT f_chdrive (const TCHAR* path); /* Change current drive */ 00241 FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ 00242 FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ 00243 FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ 00244 FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ 00245 FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ 00246 FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */ 00247 FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */ 00248 int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ 00249 int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ 00250 int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ 00251 TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ 00252 00253 #define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize)) 00254 #define f_error(fp) ((fp)->err) 00255 #define f_tell(fp) ((fp)->fptr) 00256 #define f_size(fp) ((fp)->fsize) 00257 #define f_rewind(fp) f_lseek((fp), 0) 00258 #define f_rewinddir(dp) f_readdir((dp), 0) 00259 00260 #ifndef EOF 00261 #define EOF (-1) 00262 #endif 00263 00264 00265 00266 00267 /*--------------------------------------------------------------*/ 00268 /* Additional user defined functions */ 00269 00270 /* RTC function */ 00271 #if !_FS_READONLY && !_FS_NORTC 00272 DWORD get_fattime (void); 00273 #endif 00274 00275 /* Memory functions */ 00276 #if _USE_LFN == 3 || _FS_HEAPBUF 00277 void* ff_memalloc (UINT msize); /* Allocate memory block */ 00278 void ff_memfree (void* mblock); /* Free memory block */ 00279 #endif 00280 00281 /* Unicode support functions */ 00282 #if _USE_LFN /* Unicode - OEM code conversion */ 00283 WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */ 00284 WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */ 00285 #endif 00286 00287 /* Sync functions */ 00288 #if _FS_REENTRANT 00289 int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */ 00290 int ff_req_grant (_SYNC_t sobj); /* Lock sync object */ 00291 void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */ 00292 int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */ 00293 #endif 00294 00295 00296 00297 00298 /*--------------------------------------------------------------*/ 00299 /* Flags and offset address */ 00300 00301 00302 /* File access control and file status flags (FIL.flag) */ 00303 00304 #define FA_READ 0x01 00305 #define FA_OPEN_EXISTING 0x00 00306 00307 #if !_FS_READONLY 00308 #define FA_WRITE 0x02 00309 #define FA_CREATE_NEW 0x04 00310 #define FA_CREATE_ALWAYS 0x08 00311 #define FA_OPEN_ALWAYS 0x10 00312 #define FA__WRITTEN 0x20 00313 #define FA__DIRTY 0x40 00314 #endif 00315 00316 00317 /* FAT sub type (FATFS.fs_type) */ 00318 00319 #define FS_FAT12 1 00320 #define FS_FAT16 2 00321 #define FS_FAT32 3 00322 00323 00324 /* File attribute bits for directory entry */ 00325 00326 #define AM_RDO 0x01 /* Read only */ 00327 #define AM_HID 0x02 /* Hidden */ 00328 #define AM_SYS 0x04 /* System */ 00329 #define AM_VOL 0x08 /* Volume label */ 00330 #define AM_LFN 0x0F /* LFN entry */ 00331 #define AM_DIR 0x10 /* Directory */ 00332 #define AM_ARC 0x20 /* Archive */ 00333 #define AM_MASK 0x3F /* Mask of defined bits */ 00334 00335 00336 /* Fast seek feature */ 00337 #define CREATE_LINKMAP 0xFFFFFFFF 00338 00339 00340 00341 /*--------------------------------*/ 00342 /* Multi-byte word access macros */ 00343 00344 #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */ 00345 #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr)) 00346 #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr)) 00347 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val) 00348 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val) 00349 #else /* Use byte-by-byte access to the FAT structure */ 00350 #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr)) 00351 #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr)) 00352 #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8) 00353 #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) 00354 #endif 00355 00356 #ifdef __cplusplus 00357 } 00358 #endif 00359 00360 #endif /* _FATFS */
Generated on Sun Jul 17 2022 08:25:23 by 1.7.2