Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ff.h 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*---------------------------------------------------------------------------/
  2. / FatFs - FAT file system module include file R0.09a (C)ChaN, 2012
  3. /----------------------------------------------------------------------------/
  4. / FatFs module is a generic FAT file system module for small embedded systems.
  5. / This is a free software that opened for education, research and commercial
  6. / developments under license policy of following terms.
  7. /
  8. / Copyright (C) 2012, ChaN, all right reserved.
  9. /
  10. / * The 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 product UNDER YOUR RESPONSIBILITY.
  13. / * Redistributions of source code must retain the above copyright notice.
  14. /
  15. /----------------------------------------------------------------------------*/
  16. #ifndef _FATFS
  17. #define _FATFS 4004 /* Revision ID */
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "integer.h" /* Basic integer types */
  22. #include "ffconf.h" /* FatFs configuration options */
  23. #if _FATFS != _FFCONF
  24. #error Wrong configuration file (ffconf.h).
  25. #endif
  26. /* Definitions of volume management */
  27. #if _MULTI_PARTITION /* Multiple partition configuration */
  28. typedef struct {
  29. BYTE pd; /* Physical drive number */
  30. BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
  31. } PARTITION;
  32. extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
  33. #define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
  34. #define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
  35. #else /* Single partition configuration */
  36. #define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
  37. #define LD2PT(vol) 0 /* Always mounts the 1st partition or in SFD */
  38. #endif
  39. /* Type of path name strings on FatFs API */
  40. #if _LFN_UNICODE /* Unicode string */
  41. #if !_USE_LFN
  42. #error _LFN_UNICODE must be 0 in non-LFN cfg.
  43. #endif
  44. #ifndef _INC_TCHAR
  45. typedef WCHAR TCHAR;
  46. #define _T(x) L ## x
  47. #define _TEXT(x) L ## x
  48. #endif
  49. #else /* ANSI/OEM string */
  50. #ifndef _INC_TCHAR
  51. typedef char TCHAR;
  52. #define _T(x) x
  53. #define _TEXT(x) x
  54. #endif
  55. #endif
  56. /* File system object structure (FATFS) */
  57. typedef struct {
  58. BYTE fs_type; /* FAT sub-type (0:Not mounted) */
  59. BYTE drv; /* Physical drive number */
  60. BYTE csize; /* Sectors per cluster (1,2,4...128) */
  61. BYTE n_fats; /* Number of FAT copies (1,2) */
  62. BYTE wflag; /* win[] dirty flag (1:must be written back) */
  63. BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
  64. WORD id; /* File system mount ID */
  65. WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
  66. #if _MAX_SS != 512
  67. WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */
  68. #endif
  69. #if _FS_REENTRANT
  70. _SYNC_t sobj; /* Identifier of sync object */
  71. #endif
  72. #if !_FS_READONLY
  73. DWORD last_clust; /* Last allocated cluster */
  74. DWORD free_clust; /* Number of free clusters */
  75. DWORD fsi_sector; /* fsinfo sector (FAT32) */
  76. #endif
  77. #if _FS_RPATH
  78. DWORD cdir; /* Current directory start cluster (0:root) */
  79. #endif
  80. DWORD n_fatent; /* Number of FAT entries (= number of clusters + 2) */
  81. DWORD fsize; /* Sectors per FAT */
  82. DWORD fatbase; /* FAT start sector */
  83. DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
  84. DWORD database; /* Data start sector */
  85. DWORD winsect; /* Current sector appearing in the win[] */
  86. BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and Data on tiny cfg) */
  87. } FATFS;
  88. /* File object structure (FIL) */
  89. typedef struct {
  90. FATFS* fs; /* Pointer to the related file system object */
  91. WORD id; /* File system mount ID of the related file system object */
  92. BYTE flag; /* File status flags */
  93. BYTE pad1;
  94. DWORD fptr; /* File read/write pointer (0ed on file open) */
  95. DWORD fsize; /* File size */
  96. DWORD sclust; /* File data start cluster (0:no data cluster, always 0 when fsize is 0) */
  97. DWORD clust; /* Current cluster of fpter */
  98. DWORD dsect; /* Current data sector of fpter */
  99. #if !_FS_READONLY
  100. DWORD dir_sect; /* Sector containing the directory entry */
  101. BYTE* dir_ptr; /* Pointer to the directory entry in the window */
  102. #endif
  103. #if _USE_FASTSEEK
  104. DWORD* cltbl; /* Pointer to the cluster link map table (null on file open) */
  105. #endif
  106. #if _FS_LOCK
  107. UINT lockid; /* File lock ID (index of file semaphore table Files[]) */
  108. #endif
  109. #if !_FS_TINY
  110. BYTE buf[_MAX_SS]; /* File data read/write buffer */
  111. #endif
  112. } FIL;
  113. /* Directory object structure (DIR) */
  114. typedef struct {
  115. FATFS* fs; /* Pointer to the owner file system object */
  116. WORD id; /* Owner file system mount ID */
  117. WORD index; /* Current read/write index number */
  118. DWORD sclust; /* Table start cluster (0:Root dir) */
  119. DWORD clust; /* Current cluster */
  120. DWORD sect; /* Current sector */
  121. BYTE* dir; /* Pointer to the current SFN entry in the win[] */
  122. BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
  123. #if _USE_LFN
  124. WCHAR* lfn; /* Pointer to the LFN working buffer */
  125. WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */
  126. #endif
  127. } FATFS_DIR;
  128. /* File status structure (FILINFO) */
  129. typedef struct {
  130. DWORD fsize; /* File size */
  131. WORD fdate; /* Last modified date */
  132. WORD ftime; /* Last modified time */
  133. BYTE fattrib; /* Attribute */
  134. TCHAR fname[13]; /* Short file name (8.3 format) */
  135. #if _USE_LFN
  136. TCHAR* lfname; /* Pointer to the LFN buffer */
  137. UINT lfsize; /* Size of LFN buffer in TCHAR */
  138. #endif
  139. } FILINFO;
  140. /* File function return code (FRESULT) */
  141. typedef enum {
  142. FR_OK = 0, /* (0) Succeeded */
  143. FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  144. FR_INT_ERR, /* (2) Assertion failed */
  145. FR_NOT_READY, /* (3) The physical drive cannot work */
  146. FR_NO_FILE, /* (4) Could not find the file */
  147. FR_NO_PATH, /* (5) Could not find the path */
  148. FR_INVALID_NAME, /* (6) The path name format is invalid */
  149. FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  150. FR_EXIST, /* (8) Access denied due to prohibited access */
  151. FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  152. FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  153. FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  154. FR_NOT_ENABLED, /* (12) The volume has no work area */
  155. FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  156. FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
  157. FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  158. FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  159. FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  160. FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
  161. FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  162. } FRESULT;
  163. /*--------------------------------------------------------------*/
  164. /* FatFs module application interface */
  165. FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
  166. FRESULT f_open (FIL*, const TCHAR*, BYTE); /* Open or create a file */
  167. FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
  168. FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
  169. FRESULT f_close (FIL*); /* Close an open file object */
  170. FRESULT f_opendir (FATFS_DIR*, const TCHAR*); /* Open an existing directory */
  171. FRESULT f_readdir (FATFS_DIR*, FILINFO*); /* Read a directory item */
  172. FRESULT f_stat (const TCHAR*, FILINFO*); /* Get file status */
  173. FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
  174. FRESULT f_getfree (const TCHAR*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
  175. FRESULT f_truncate (FIL*); /* Truncate file */
  176. FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
  177. FRESULT f_unlink (const TCHAR*); /* Delete an existing file or directory */
  178. FRESULT f_mkdir (const TCHAR*); /* Create a new directory */
  179. FRESULT f_chmod (const TCHAR*, BYTE, BYTE); /* Change attribute of the file/dir */
  180. FRESULT f_utime (const TCHAR*, const FILINFO*); /* Change times-tamp of the file/dir */
  181. FRESULT f_rename (const TCHAR*, const TCHAR*); /* Rename/Move a file or directory */
  182. FRESULT f_chdrive (BYTE); /* Change current drive */
  183. FRESULT f_chdir (const TCHAR*); /* Change current directory */
  184. FRESULT f_getcwd (TCHAR*, UINT); /* Get current directory */
  185. FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */
  186. FRESULT f_mkfs (BYTE, BYTE, UINT); /* Create a file system on the drive */
  187. FRESULT f_fdisk (BYTE, const DWORD[], void*); /* Divide a physical drive into some partitions */
  188. int f_putc (TCHAR, FIL*); /* Put a character to the file */
  189. int f_puts (const TCHAR*, FIL*); /* Put a string to the file */
  190. int f_printf (FIL*, const TCHAR*, ...); /* Put a formatted string to the file */
  191. TCHAR* f_gets (TCHAR*, int, FIL*); /* Get a string from the file */
  192. #define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
  193. #define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
  194. #define f_tell(fp) ((fp)->fptr)
  195. #define f_size(fp) ((fp)->fsize)
  196. #ifndef EOF
  197. #define EOF (-1)
  198. #endif
  199. /*--------------------------------------------------------------*/
  200. /* Additional user defined functions */
  201. /* RTC function */
  202. #if !_FS_READONLY
  203. DWORD get_fattime (void);
  204. #endif
  205. /* Unicode support functions */
  206. #if _USE_LFN /* Unicode - OEM code conversion */
  207. WCHAR ff_convert (WCHAR, UINT); /* OEM-Unicode bidirectional conversion */
  208. WCHAR ff_wtoupper (WCHAR); /* Unicode upper-case conversion */
  209. #if _USE_LFN == 3 /* Memory functions */
  210. void* ff_memalloc (UINT); /* Allocate memory block */
  211. void ff_memfree (void*); /* Free memory block */
  212. #endif
  213. #endif
  214. /* Sync functions */
  215. #if _FS_REENTRANT
  216. int ff_cre_syncobj (BYTE, _SYNC_t*);/* Create a sync object */
  217. int ff_req_grant (_SYNC_t); /* Lock sync object */
  218. void ff_rel_grant (_SYNC_t); /* Unlock sync object */
  219. int ff_del_syncobj (_SYNC_t); /* Delete a sync object */
  220. #endif
  221. /*--------------------------------------------------------------*/
  222. /* Flags and offset address */
  223. /* File access control and file status flags (FIL.flag) */
  224. #define FA_READ 0x01
  225. #define FA_OPEN_EXISTING 0x00
  226. #define FA__ERROR 0x80
  227. #if !_FS_READONLY
  228. #define FA_WRITE 0x02
  229. #define FA_CREATE_NEW 0x04
  230. #define FA_CREATE_ALWAYS 0x08
  231. #define FA_OPEN_ALWAYS 0x10
  232. #define FA__WRITTEN 0x20
  233. #define FA__DIRTY 0x40
  234. #endif
  235. /* FAT sub type (FATFS.fs_type) */
  236. #define FS_FAT12 1
  237. #define FS_FAT16 2
  238. #define FS_FAT32 3
  239. /* File attribute bits for directory entry */
  240. #define AM_RDO 0x01 /* Read only */
  241. #define AM_HID 0x02 /* Hidden */
  242. #define AM_SYS 0x04 /* System */
  243. #define AM_VOL 0x08 /* Volume label */
  244. #define AM_LFN 0x0F /* LFN entry */
  245. #define AM_DIR 0x10 /* Directory */
  246. #define AM_ARC 0x20 /* Archive */
  247. #define AM_MASK 0x3F /* Mask of defined bits */
  248. /* Fast seek feature */
  249. #define CREATE_LINKMAP 0xFFFFFFFF
  250. /*--------------------------------*/
  251. /* Multi-byte word access macros */
  252. #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
  253. #define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
  254. #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
  255. #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
  256. #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
  257. #else /* Use byte-by-byte access to the FAT structure */
  258. #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
  259. #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
  260. #define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
  261. #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)
  262. #endif
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif /* _FATFS */