ff.h 11 KB

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