From 5366852335044c1e68a5c32548d3051cc943552f Mon Sep 17 00:00:00 2001 From: Leo C Date: Tue, 19 Aug 2014 15:31:33 +0200 Subject: Import fatfs R0.10b FatFs Module Source Files R0.10b Author: (C)ChaN, 2014 (http://elm-chan.org) URL: http://elm-chan.org/fsw/ff/ff10b.zip --- fatfs/doc/00index_e.html | 118 ++++++++++++++++ fatfs/doc/00index_j.html | 118 ++++++++++++++++ fatfs/doc/css_e.css | 65 +++++++++ fatfs/doc/css_j.css | 68 ++++++++++ fatfs/doc/css_p.css | 1 + fatfs/doc/en/appnote.html | 275 ++++++++++++++++++++++++++++++++++++++ fatfs/doc/en/chdir.html | 81 +++++++++++ fatfs/doc/en/chdrive.html | 60 +++++++++ fatfs/doc/en/chmod.html | 89 +++++++++++++ fatfs/doc/en/close.html | 66 +++++++++ fatfs/doc/en/closedir.html | 64 +++++++++ fatfs/doc/en/dinit.html | 46 +++++++ fatfs/doc/en/dioctl.html | 90 +++++++++++++ fatfs/doc/en/dread.html | 71 ++++++++++ fatfs/doc/en/dstat.html | 48 +++++++ fatfs/doc/en/dwrite.html | 76 +++++++++++ fatfs/doc/en/eof.html | 62 +++++++++ fatfs/doc/en/error.html | 62 +++++++++ fatfs/doc/en/fattime.html | 57 ++++++++ fatfs/doc/en/fdisk.html | 97 ++++++++++++++ fatfs/doc/en/filename.html | 78 +++++++++++ fatfs/doc/en/forward.html | 139 +++++++++++++++++++ fatfs/doc/en/getcwd.html | 70 ++++++++++ fatfs/doc/en/getfree.html | 95 +++++++++++++ fatfs/doc/en/getlabel.html | 82 ++++++++++++ fatfs/doc/en/gets.html | 65 +++++++++ fatfs/doc/en/lseek.html | 129 ++++++++++++++++++ fatfs/doc/en/mkdir.html | 80 +++++++++++ fatfs/doc/en/mkfs.html | 73 ++++++++++ fatfs/doc/en/mount.html | 83 ++++++++++++ fatfs/doc/en/open.html | 173 ++++++++++++++++++++++++ fatfs/doc/en/opendir.html | 75 +++++++++++ fatfs/doc/en/printf.html | 93 +++++++++++++ fatfs/doc/en/putc.html | 62 +++++++++ fatfs/doc/en/puts.html | 62 +++++++++ fatfs/doc/en/rc.html | 87 ++++++++++++ fatfs/doc/en/read.html | 74 +++++++++++ fatfs/doc/en/readdir.html | 125 +++++++++++++++++ fatfs/doc/en/rename.html | 85 ++++++++++++ fatfs/doc/en/sdir.html | 40 ++++++ fatfs/doc/en/setlabel.html | 89 +++++++++++++ fatfs/doc/en/sfatfs.html | 54 ++++++++ fatfs/doc/en/sfile.html | 49 +++++++ fatfs/doc/en/sfileinfo.html | 70 ++++++++++ fatfs/doc/en/size.html | 62 +++++++++ fatfs/doc/en/stat.html | 74 +++++++++++ fatfs/doc/en/sync.html | 66 +++++++++ fatfs/doc/en/tell.html | 62 +++++++++ fatfs/doc/en/truncate.html | 66 +++++++++ fatfs/doc/en/unlink.html | 75 +++++++++++ fatfs/doc/en/utime.html | 99 ++++++++++++++ fatfs/doc/en/write.html | 74 +++++++++++ fatfs/doc/img/app1.c | 43 ++++++ fatfs/doc/img/app2.c | 70 ++++++++++ fatfs/doc/img/app3.c | 106 +++++++++++++++ fatfs/doc/img/app4.c | 318 ++++++++++++++++++++++++++++++++++++++++++++ fatfs/doc/img/f1.png | Bin 0 -> 1414 bytes fatfs/doc/img/f2.png | Bin 0 -> 1458 bytes fatfs/doc/img/f3.png | Bin 0 -> 1039 bytes fatfs/doc/img/f4.png | Bin 0 -> 2335 bytes fatfs/doc/img/f5.png | Bin 0 -> 2479 bytes fatfs/doc/img/f6.png | Bin 0 -> 1436 bytes fatfs/doc/img/f7.png | Bin 0 -> 11388 bytes fatfs/doc/img/funcs.png | Bin 0 -> 14357 bytes fatfs/doc/img/layers.png | Bin 0 -> 2401 bytes fatfs/doc/img/layers3.png | Bin 0 -> 2379 bytes fatfs/doc/img/modules.png | Bin 0 -> 7932 bytes fatfs/doc/img/rwtest.png | Bin 0 -> 19068 bytes fatfs/doc/img/rwtest2.png | Bin 0 -> 8153 bytes fatfs/doc/img/rwtest3.png | Bin 0 -> 3011 bytes fatfs/doc/ja/appnote.html | 282 +++++++++++++++++++++++++++++++++++++++ fatfs/doc/ja/chdir.html | 81 +++++++++++ fatfs/doc/ja/chdrive.html | 61 +++++++++ fatfs/doc/ja/chmod.html | 83 ++++++++++++ fatfs/doc/ja/close.html | 66 +++++++++ fatfs/doc/ja/closedir.html | 64 +++++++++ fatfs/doc/ja/dinit.html | 46 +++++++ fatfs/doc/ja/dioctl.html | 95 +++++++++++++ fatfs/doc/ja/dread.html | 71 ++++++++++ fatfs/doc/ja/dstat.html | 48 +++++++ fatfs/doc/ja/dwrite.html | 76 +++++++++++ fatfs/doc/ja/eof.html | 62 +++++++++ fatfs/doc/ja/error.html | 62 +++++++++ fatfs/doc/ja/fattime.html | 57 ++++++++ fatfs/doc/ja/fdisk.html | 97 ++++++++++++++ fatfs/doc/ja/filename.html | 76 +++++++++++ fatfs/doc/ja/forward.html | 140 +++++++++++++++++++ fatfs/doc/ja/getcwd.html | 70 ++++++++++ fatfs/doc/ja/getfree.html | 95 +++++++++++++ fatfs/doc/ja/getlabel.html | 82 ++++++++++++ fatfs/doc/ja/gets.html | 65 +++++++++ fatfs/doc/ja/lseek.html | 129 ++++++++++++++++++ fatfs/doc/ja/mkdir.html | 80 +++++++++++ fatfs/doc/ja/mkfs.html | 73 ++++++++++ fatfs/doc/ja/mount.html | 82 ++++++++++++ fatfs/doc/ja/open.html | 171 ++++++++++++++++++++++++ fatfs/doc/ja/opendir.html | 76 +++++++++++ fatfs/doc/ja/printf.html | 93 +++++++++++++ fatfs/doc/ja/putc.html | 62 +++++++++ fatfs/doc/ja/puts.html | 62 +++++++++ fatfs/doc/ja/rc.html | 85 ++++++++++++ fatfs/doc/ja/read.html | 75 +++++++++++ fatfs/doc/ja/readdir.html | 126 ++++++++++++++++++ fatfs/doc/ja/rename.html | 82 ++++++++++++ fatfs/doc/ja/sdir.html | 40 ++++++ fatfs/doc/ja/setlabel.html | 89 +++++++++++++ fatfs/doc/ja/sfatfs.html | 55 ++++++++ fatfs/doc/ja/sfile.html | 48 +++++++ fatfs/doc/ja/sfileinfo.html | 70 ++++++++++ fatfs/doc/ja/size.html | 62 +++++++++ fatfs/doc/ja/stat.html | 74 +++++++++++ fatfs/doc/ja/sync.html | 66 +++++++++ fatfs/doc/ja/tell.html | 62 +++++++++ fatfs/doc/ja/truncate.html | 67 ++++++++++ fatfs/doc/ja/unlink.html | 74 +++++++++++ fatfs/doc/ja/utime.html | 99 ++++++++++++++ fatfs/doc/ja/write.html | 75 +++++++++++ fatfs/doc/updates.txt | 146 ++++++++++++++++++++ 118 files changed, 8763 insertions(+) create mode 100644 fatfs/doc/00index_e.html create mode 100644 fatfs/doc/00index_j.html create mode 100644 fatfs/doc/css_e.css create mode 100644 fatfs/doc/css_j.css create mode 100644 fatfs/doc/css_p.css create mode 100644 fatfs/doc/en/appnote.html create mode 100644 fatfs/doc/en/chdir.html create mode 100644 fatfs/doc/en/chdrive.html create mode 100644 fatfs/doc/en/chmod.html create mode 100644 fatfs/doc/en/close.html create mode 100644 fatfs/doc/en/closedir.html create mode 100644 fatfs/doc/en/dinit.html create mode 100644 fatfs/doc/en/dioctl.html create mode 100644 fatfs/doc/en/dread.html create mode 100644 fatfs/doc/en/dstat.html create mode 100644 fatfs/doc/en/dwrite.html create mode 100644 fatfs/doc/en/eof.html create mode 100644 fatfs/doc/en/error.html create mode 100644 fatfs/doc/en/fattime.html create mode 100644 fatfs/doc/en/fdisk.html create mode 100644 fatfs/doc/en/filename.html create mode 100644 fatfs/doc/en/forward.html create mode 100644 fatfs/doc/en/getcwd.html create mode 100644 fatfs/doc/en/getfree.html create mode 100644 fatfs/doc/en/getlabel.html create mode 100644 fatfs/doc/en/gets.html create mode 100644 fatfs/doc/en/lseek.html create mode 100644 fatfs/doc/en/mkdir.html create mode 100644 fatfs/doc/en/mkfs.html create mode 100644 fatfs/doc/en/mount.html create mode 100644 fatfs/doc/en/open.html create mode 100644 fatfs/doc/en/opendir.html create mode 100644 fatfs/doc/en/printf.html create mode 100644 fatfs/doc/en/putc.html create mode 100644 fatfs/doc/en/puts.html create mode 100644 fatfs/doc/en/rc.html create mode 100644 fatfs/doc/en/read.html create mode 100644 fatfs/doc/en/readdir.html create mode 100644 fatfs/doc/en/rename.html create mode 100644 fatfs/doc/en/sdir.html create mode 100644 fatfs/doc/en/setlabel.html create mode 100644 fatfs/doc/en/sfatfs.html create mode 100644 fatfs/doc/en/sfile.html create mode 100644 fatfs/doc/en/sfileinfo.html create mode 100644 fatfs/doc/en/size.html create mode 100644 fatfs/doc/en/stat.html create mode 100644 fatfs/doc/en/sync.html create mode 100644 fatfs/doc/en/tell.html create mode 100644 fatfs/doc/en/truncate.html create mode 100644 fatfs/doc/en/unlink.html create mode 100644 fatfs/doc/en/utime.html create mode 100644 fatfs/doc/en/write.html create mode 100644 fatfs/doc/img/app1.c create mode 100644 fatfs/doc/img/app2.c create mode 100644 fatfs/doc/img/app3.c create mode 100644 fatfs/doc/img/app4.c create mode 100644 fatfs/doc/img/f1.png create mode 100644 fatfs/doc/img/f2.png create mode 100644 fatfs/doc/img/f3.png create mode 100644 fatfs/doc/img/f4.png create mode 100644 fatfs/doc/img/f5.png create mode 100644 fatfs/doc/img/f6.png create mode 100644 fatfs/doc/img/f7.png create mode 100644 fatfs/doc/img/funcs.png create mode 100644 fatfs/doc/img/layers.png create mode 100644 fatfs/doc/img/layers3.png create mode 100644 fatfs/doc/img/modules.png create mode 100644 fatfs/doc/img/rwtest.png create mode 100644 fatfs/doc/img/rwtest2.png create mode 100644 fatfs/doc/img/rwtest3.png create mode 100644 fatfs/doc/ja/appnote.html create mode 100644 fatfs/doc/ja/chdir.html create mode 100644 fatfs/doc/ja/chdrive.html create mode 100644 fatfs/doc/ja/chmod.html create mode 100644 fatfs/doc/ja/close.html create mode 100644 fatfs/doc/ja/closedir.html create mode 100644 fatfs/doc/ja/dinit.html create mode 100644 fatfs/doc/ja/dioctl.html create mode 100644 fatfs/doc/ja/dread.html create mode 100644 fatfs/doc/ja/dstat.html create mode 100644 fatfs/doc/ja/dwrite.html create mode 100644 fatfs/doc/ja/eof.html create mode 100644 fatfs/doc/ja/error.html create mode 100644 fatfs/doc/ja/fattime.html create mode 100644 fatfs/doc/ja/fdisk.html create mode 100644 fatfs/doc/ja/filename.html create mode 100644 fatfs/doc/ja/forward.html create mode 100644 fatfs/doc/ja/getcwd.html create mode 100644 fatfs/doc/ja/getfree.html create mode 100644 fatfs/doc/ja/getlabel.html create mode 100644 fatfs/doc/ja/gets.html create mode 100644 fatfs/doc/ja/lseek.html create mode 100644 fatfs/doc/ja/mkdir.html create mode 100644 fatfs/doc/ja/mkfs.html create mode 100644 fatfs/doc/ja/mount.html create mode 100644 fatfs/doc/ja/open.html create mode 100644 fatfs/doc/ja/opendir.html create mode 100644 fatfs/doc/ja/printf.html create mode 100644 fatfs/doc/ja/putc.html create mode 100644 fatfs/doc/ja/puts.html create mode 100644 fatfs/doc/ja/rc.html create mode 100644 fatfs/doc/ja/read.html create mode 100644 fatfs/doc/ja/readdir.html create mode 100644 fatfs/doc/ja/rename.html create mode 100644 fatfs/doc/ja/sdir.html create mode 100644 fatfs/doc/ja/setlabel.html create mode 100644 fatfs/doc/ja/sfatfs.html create mode 100644 fatfs/doc/ja/sfile.html create mode 100644 fatfs/doc/ja/sfileinfo.html create mode 100644 fatfs/doc/ja/size.html create mode 100644 fatfs/doc/ja/stat.html create mode 100644 fatfs/doc/ja/sync.html create mode 100644 fatfs/doc/ja/tell.html create mode 100644 fatfs/doc/ja/truncate.html create mode 100644 fatfs/doc/ja/unlink.html create mode 100644 fatfs/doc/ja/utime.html create mode 100644 fatfs/doc/ja/write.html create mode 100644 fatfs/doc/updates.txt (limited to 'fatfs/doc') diff --git a/fatfs/doc/00index_e.html b/fatfs/doc/00index_e.html new file mode 100644 index 0000000..aa249b1 --- /dev/null +++ b/fatfs/doc/00index_e.html @@ -0,0 +1,118 @@ + + +
+ + + + + + +FatFs is a generic FAT file system module for small embedded systems. The FatFs is written in compliance with ANSI C and completely separated from the disk I/O layer. Therefore it is independent of hardware architecture. It can be incorporated into small microcontrollers with limited resource, such as AVR, 8051, PIC, ARM, Z80, 68k and etc. Also Petit FatFs module for tiny microcontrollers is available here.
+ +FatFs module provides following functions to the applications. In other words, this list describes what FatFs can do to access the FAT volumes.
+Since the FatFs module is a file system driver, it is completely separated from physical devices, such as memory card, harddisk and any type of storage devices. The low level device control module is not a part of FatFs module. FatFs accesses the storage device via a simple interface described below. These functions are provided by implementer. Sample implementations for some platforms are also available in the downloads.
+The FatFs module is a free software opened for education, research and development. You can use, modify and/or redistribute it for personal projects or commercial products without any restriction under your responsibility. For further information, refer to the application note.
+FatFsは小規模な組み込みシステム向けの汎用FATファイルシステム・モジュールです。ANSI C準拠でハードウェア・アーキテクチャには依存しないので、必要なワーク・エリアが確保できれば、8051, PIC, AVR, SH, Z80, 68k, H8, ARMなど安価なマイコンでも使用可能です。FatFsをシュリンクしたぷちFatFs↗もあります。
+FatFsモジュールは、アプリケーション・レイヤに対し、次のファイル操作関数(API)を提供します。つまり、このリストはFatFsにできることをシンプルに示しています。
+FatFsモジュールは、単なるファイル・システム・レイヤなので、ストレージ・デバイス制御レイヤは含まれません。使用するストレージに対応した制御関数は、ユーザによって提供される必要があります。FatFsモジュールは、下位レイヤに対し少なくとも次のインターフェースを要求します。OS関連機能を有効にしたときは、これに加えてプロセス/メモリ関連関数も必要になります。サンプル・プロジェクトに下位レイヤの実装例を示します。
+FatFsモジュールはフリー・ソフトウェアとして教育・研究・開発用に公開しています。どのような利用目的(個人利用から商用まで)でも使用・改変・配布について一切の制限はありませんが、全て利用者の責任の下での利用とします。詳しくはアプリケーション・ノートを参照してください。
+The FatFs module is assuming following conditions on portability.
+The dependency diagram shown below is a typical configuration of the embedded system with FatFs module.
+ +(a) If a working disk module with FatFs API is provided, no additional function is needed. (b) To attach existing disk drivers with different API, glue functions are needed to translate the APIs between FatFs and the drivers.
+ + +You need to provide only low level disk I/O functions that required by FatFs module and nothing else. If a working disk module for the target is already existing, you need to write only glue functions to attach it to the FatFs module. If not, you need to port any other disk module or write it from scratch. Most of defined functions are not that always required. For example, disk write function is not required in read-only configuration. Following table shows which function is required depends on configuration options.
+Function | Required when: | Note |
---|---|---|
disk_status disk_initialize disk_read | Always | Disk I/O functions. Samples available in ffsample.zip. There are many implementations on the web. |
disk_write get_fattime disk_ioctl (CTRL_SYNC) | _FS_READONLY == 0 | |
disk_ioctl (GET_SECTOR_COUNT) disk_ioctl (GET_BLOCK_SIZE) | _USE_MKFS == 1 | |
disk_ioctl (GET_SECTOR_SIZE) | _MAX_SS != _MIN_SS | |
disk_ioctl (CTRL_ERASE_SECTOR) | _USE_ERASE == 1 | |
ff_convert ff_wtoupper | _USE_LFN >= 1 | Unicode support functions. Available in option/unicode.c. |
ff_cre_syncobj ff_del_syncobj ff_req_grant ff_rel_grant | _FS_REENTRANT == 1 | O/S dependent functions. Samples available in option/syscall.c. |
ff_mem_alloc ff_mem_free | _USE_LFN == 3 |
ARM7 32bit | ARM7 Thumb | CM3 Thumb-2 | AVR | H8/300H | PIC24 | RL78 | V850ES | SH-2A | RX600 | IA-32 | |
---|---|---|---|---|---|---|---|---|---|---|---|
Compiler | GCC | GCC | GCC | GCC | CH38 | C30 | CC78K0R | CA850 | SHC | RXC | VC6 |
_WORD_ACCESS | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 1 |
text (Full, R/W) | 10675 | 7171 | 6617 | 13355 | 10940 | 11722 | 13262 | 8113 | 9048 | 6032 | 7952 |
text (Min, R/W) | 6727 | 4631 | 4331 | 8569 | 7262 | 7720 | 9088 | 5287 | 5800 | 3948 | 5183 |
text (Full, R/O) | 4731 | 3147 | 2889 | 6235 | 5170 | 5497 | 6482 | 3833 | 3972 | 2862 | 3719 |
text (Min, R/O) | 3559 | 2485 | 2295 | 4575 | 4064 | 4240 | 5019 | 2993 | 3104 | 2214 | 2889 |
bss | V*4 + 2 | V*4 + 2 | V*4 + 2 | V*2 + 2 | V*4 + 2 | V*2 + 2 | V*2 + 2 | V*4 + 2 | V*4 + 2 | V*4 + 2 | V*4 + 2 |
Work area (_FS_TINY == 0) | V*560 + F*550 | V*560 + F*550 | V*560 + F*550 | V*560 + F*544 | V*560 + F*550 | V*560 + F*544 | V*560 + F*544 | V*560 + F*544 | V*560 + F*550 | V*560 + F*550 | V*560 + F*550 |
Work area (_FS_TINY == 1) | V*560 + F*36 | V*560 + F*36 | V*560 + F*36 | V*560 + F*32 | V*560 + F*36 | V*560 + F*32 | V*560 + F*32 | V*560 + F*36 | V*560 + F*36 | V*560 + F*36 | V*560 + F*36 |
These are the memory usage on some target systems with following condition. The memory sizes are in unit of byte, V denotes number of volumes and F denotes number of open files. All samples are optimezed in code size.
++FatFs R0.10a options: +_FS_READONLY 0 (R/W) or 1 (R/O) +_FS_MINIMIZE 0 (Full function) or 3 (Minimized function) +_USE_STRFUNC 0 (Disable string functions) +_USE_MKFS 0 (Disable f_mkfs function) +_USE_FORWARD 0 (Disable f_forward function) +_USE_FASTSEEK 0 (Disable fast seek feature) +_CODE_PAGE 932 (Japanese Shift-JIS) +_USE_LFN 0 (Disable LFN feature) +_MAX_SS 512 (Fixed sector size) +_FS_RPATH 0 (Disable relative path feature) +_FS_LABEL 0 (Disable volume label functions) +_VOLUMES V (Number of logical drives to be used) +_MULTI_PARTITION 0 (Single partition per drive) +_FS_REENTRANT 0 (Disable thread safe) +_FS_LOCK 0 (Disable file lock control) ++
Follwing table shows which API function is removed by configuration options for the module size reduction.
+Function | _FS_MINIMIZE | _FS_READONLY | _USE_STRFUNC | _FS_RPATH | _FS_LABEL | _USE_MKFS | _USE_FORWARD | _MULTI_PARTITION | |||||||||||
0 | 1 | 2 | 3 | 0 | 1 | 0 | 1/2 | 0 | 1 | 2 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | |
f_mount | |||||||||||||||||||
f_open | |||||||||||||||||||
f_close | |||||||||||||||||||
f_read | |||||||||||||||||||
f_write | x | ||||||||||||||||||
f_sync | x | ||||||||||||||||||
f_lseek | x | ||||||||||||||||||
f_opendir | x | x | |||||||||||||||||
f_closedir | x | x | |||||||||||||||||
f_readdir | x | x | |||||||||||||||||
f_stat | x | x | x | ||||||||||||||||
f_getfree | x | x | x | x | |||||||||||||||
f_truncate | x | x | x | x | |||||||||||||||
f_unlink | x | x | x | x | |||||||||||||||
f_mkdir | x | x | x | x | |||||||||||||||
f_chmod | x | x | x | x | |||||||||||||||
f_utime | x | x | x | x | |||||||||||||||
f_rename | x | x | x | x | |||||||||||||||
f_chdir | x | ||||||||||||||||||
f_chdrive | x | ||||||||||||||||||
f_getcwd | x | x | |||||||||||||||||
f_getlabel | x | ||||||||||||||||||
f_setlabel | x | x | |||||||||||||||||
f_forward | x | ||||||||||||||||||
f_mkfs | x | x | |||||||||||||||||
f_fdisk | x | x | x | ||||||||||||||||
f_putc | x | x | |||||||||||||||||
f_puts | x | x | |||||||||||||||||
f_printf | x | x | |||||||||||||||||
f_gets | x |
FatFs module supports LFN (long file name). The two different file names, SFN (short file name) and LFN, of a file is transparent on the API except for f_readdir() function. The LFN feature is disabled by default. To enable it, set _USE_LFN to 1, 2 or 3, and add option/unicode.c to the project. The LFN feature requiers a certain working buffer in addition. The buffer size can be configured by _MAX_LFN according to the available memory. The length of an LFN will reach up to 255 characters, so that the _MAX_LFN should be set to 255 for full featured LFN operation. If the size of working buffer is insufficient for the input file name, the file function fails with FR_INVALID_NAME. When enable the LFN feature with re-entrant configuration, _USE_LFN must be set to 2 or 3. In this case, the file function allocates the working buffer on the stack or heap. The working buffer occupies (_MAX_LFN + 1) * 2 bytes.
+Code page | Program size |
---|---|
SBCS | +3.7K |
932(Shift-JIS) | +62K |
936(GBK) | +177K |
949(Korean) | +139K |
950(Big5) | +111K |
When the LFN feature is enabled, the module size will be increased depends on the selected code page. Right table shows how many bytes increased when LFN feature is enabled with some code pages. Especially, in the CJK region, tens of thousands of characters are being used. Unfortunately, it requires a huge OEM-Unicode bidirectional conversion table and the module size will be drastically increased that shown in the table. As the result, the FatFs with LFN feature with those code pages will not able to be implemented to most 8-bit microcontrollers.
+Note that the LFN feature on the FAT file system is a patent of Microsoft Corporation. This is not the case on FAT32 but most FAT32 drivers come with the LFN feature. FatFs can swich the LFN feature off by configuration option. When enable LFN feature on the commercial products, a license from Microsoft may be required depends on the final destination.
+By default, FatFs uses ANSI/OEM code set on the API under LFN configuration. FatFs can also switch the character encoding to Unicode on the API by _LFN_UNICODE option. This means that the FatFs supports the True-LFN feature. For more information, refer to the description in the file name.
+The file operations to the different volume is always re-entrant and can work simultaneously. The file operations to the same volume is not re-entrant but it can also be configured to thread-safe by _FS_REENTRANT option. In this case, also the OS dependent synchronization object control functions, ff_cre_syncobj(), ff_del_syncobj(), ff_req_grant() and ff_rel_grant() must be added to the project. There are some examples in the option/syscall.c.
+When a file function is called while the volume is in use by any other task, the file function is suspended until that task leaves the file function. If wait time exceeded a period defined by _TIMEOUT, the file function will abort with FR_TIMEOUT. The timeout feature might not be supported by some RTOS.
+There is an exception for f_mount(), f_mkfs(), f_fdisk() function. These functions are not re-entrant to the same volume or corresponding physical drive. When use these functions, all other tasks must unmount the volume and avoid to access the volume.
+Note that this section describes on the re-entrancy of the FatFs module itself but also the low level disk I/O layer will need to be re-entrant.
+FatFs module does not support the read/write collision control of duplicated open to a file. The duplicated open is permitted only when each of open method to a file is read mode. The duplicated open with one or more write mode to a file is always prohibited, and also open file must not be renamed and deleted. A violation of these rules can cause data colluption.
+The file lock control can also be available by _FS_LOCK option. The value defines the number of open objects to manage simultaneously. In this case, if any open, rename or remove that violating the file shareing rule that described above is attempted, the file function will fail with FR_LOCKED. If number of open objects, files and sub-directories, gets larger than _FS_LOCK, the f_open(), f_optndir() function will fail with FR_TOO_MANY_OPEN_FILES.
+For good read/write throughput on the small embedded systems with limited size of memory, application programmer should consider what process is done in the FatFs module. The file data on the volume is transferred in following sequence by f_read() function.
+Figure 1. Sector misaligned read (short)
+
+
Figure 2. Sector misaligned read (long)
+
+
Figure 3. Fully sector aligned read
+
+
The file I/O buffer is a sector buffer to read/write a partial data on the sector. The sector buffer is either file private sector buffer on each file object or shared sector buffer in the file system object. The buffer configuration option _FS_TINY determins which sector buffer is used for the file data transfer. When tiny buffer (1) is selected, data memory consumption is reduced 512 bytes each file object. In this case, FatFs module uses only a sector buffer in the file system object for file data transfer and FAT/directory access. The disadvantage of the tiny buffer configuration is: the FAT data cached in the sector buffer will be lost by file data transfer and it must be reloaded at every cluster boundary. However it will be suitable for most application from view point of the decent performance and low memory comsumption.
+Figure 1 shows that a partial sector, sector misaligned part of the file, is transferred via the file I/O buffer. At long data transfer shown in Figure 2, middle of transfer data that covers one or more sector is transferred to the application buffer directly. Figure 3 shows that the case of entier transfer data is aligned to the sector boundary. In this case, file I/O buffer is not used. On the direct transfer, the maximum extent of sectors are read with disk_read() function at a time but the multiple sector transfer is divided at cluster boundary even if it is contiguous.
+Therefore taking effort to sector aligned read/write accesss eliminates buffered data transfer and the read/write performance will be improved. Besides the effect, cached FAT data will not be flushed by file data transfer at the tiny configuration, so that it can achieve same performance as non-tiny configuration with small memory footprint.
+To maximize the write performance of flash memory media, such as SDC, CFC and U Disk, it must be controlled in consideration of its characteristitcs.
+The write throughput of the flash memory media becomes the worst at single sector write transaction. The write throughput increases as the number of sectors per a write transaction. This effect more appers at faster interface speed and the performance ratio often becomes grater than ten. This graph is clearly explaining how fast is multiple block write (W:16K, 32 sectors) than single block write (W:100, 1 sector), and also larger card tends to be slow at single block write. The number of write transactions also affects the life time of the flash memory media. Therefore the application program should write the data in large block as possible. The ideal write chunk size and alighment is size of sector, and size of cluster is the best. Of course all layers between the application and the storage device must have consideration on multiple sector write, however most of open-source disk drivers lack it. Do not split a multiple sector write request into single sector write transactions or the write throughput gets poor. Note that FatFs module and its sample disk drivers supprt multiple sector read/write feature.
+When remove a file with f_remove() function, the data clusters occupied by the file are marked 'free' on the FAT. But the data sectors containing the file data are not that applied any process, so that the file data left occupies a part of the flash memory array as 'live block'. If the file data is forced erased on removing the file, those data blocks will be turned in to the free block pool. This may skip internal block erase operation to the data block on next write operation. As the result the write performance might be improved. FatFs can manage this feature by setting _USE_ERASE to 1. Note that this is an expectation of internal process of the flash memory storage and not that always effective. Also f_remove() function will take a time when remove a large file. Most applications will not need this feature.
+If a write operation to the FAT volume is interrupted due to any accidental failure, such as sudden blackout, incorrect disk removal and unrecoverable disk error, the FAT structure on the volume can be broken. Following images shows the critical section of the FatFs module.
+An interruption in the red section can cause a cross link; as a result, the object being changed can be lost. If an interruption in the yellow section is occured, there is one or more possibility listed below.
+Each case does not affect the files that not opened in write mode. To minimize risk of data loss, the critical section can be minimized by minimizing the time that file is opened in write mode or using f_sync() function as shown in Figure 5.
+These are examples of extended use of FatFs APIs. New item will be added whenever a useful code is found.
+ +FatFs has being developped as a personal project of author, ChaN. It is free from the code anyone else wrote. Following code block shows a copy of the FatFs license document that included in the source files.
+/*----------------------------------------------------------------------------/ +/ FatFs - FAT file system module R0.10b (C)ChaN, 2014 +/-----------------------------------------------------------------------------/ +/ FatFs module is a generic FAT file system module for small embedded systems. +/ This is a free software that opened for education, research and commercial +/ developments under license policy of following trems. +/ +/ Copyright (C) 2014, ChaN, all right reserved. +/ +/ * The FatFs module is a free software and there is NO WARRANTY. +/ * No restriction on use. You can use, modify and redistribute it for +/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY. +/ * Redistributions of source code must retain the above copyright notice. +/ +/-----------------------------------------------------------------------------/+
Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. Because FatFs is for embedded projects, the conditions of redistributions in binary form, such as embedded code, hex file, binary library or any forms without source code, are not specified in order to extend usability for commercial products. The documentation of the distributions need not include about FatFs and its license document, and it may also. This is equivalent to the BSD 1-Clause License. Of course FatFs is compatible with the projects under GNU GPL. When redistribute the FatFs with any modification or branch it as a folk, the license can also be changed to GNU GPL or BSD-style license.
+The f_chdir function changes the current directory of a drive.
++FRESULT f_chdir ( + const TCHAR* path /* [IN] Path name */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_PATH, +FR_INVALID_NAME, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+The f_chdir() function changes the current directory of the logical drive. The current directory of a drive is initialized to the root directory when the drive is auto-mounted. Note that the current directory is retained in the each file system object so that it also affects other tasks that using the volume.
+Available when _FS_RPATH >= 1.
++ /* Change current direcoty of the current drive (dir1 under root dir) */ + f_chdir("/dir1"); + + /* Change current direcoty of drive 2 (parent dir) */ + f_chdir("2:.."); ++
The f_chdrive function changes the current drive.
++FRESULT f_chdrive ( + const TCHAR* path /* [IN] Logical drive number */ +); ++
+FR_OK, +FR_INVALID_DRIVE +
+The f_chdrive() function changes the current drive. The initial value of the current drive number is 0. Note that the current drive is retained in a static variable so that it also affects other tasks that using the file functions.
+Available when _FS_RPATH >= 1 and _VOLUMES >= 2.
+The f_chmod function changes the attribute of a file or sub-directory.
++FRESULT f_chmod ( + const TCHAR* path, /* [IN] Object name */ + BYTE attr, /* [IN] Attribute flags */ + BYTE mask /* [IN] Attribute masks */ +); ++
Attribute | Description |
---|---|
AM_RDO | Read only |
AM_ARC | Archive |
AM_SYS | System |
AM_HID | Hidden |
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+The f_chmod() function changes the attribute of a file or sub-directory.
+Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
+
+ /* Set read-only flag, clear archive flag and others are left unchanged. */
+ f_chmod("file.txt", AR_RDO, AR_RDO | AR_ARC);
+
+The f_close function closes an open file.
++FRESULT f_close ( + FIL* fp /* [IN] Pointer to the file object */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The f_close() function closes an open file object. If any data has been written to the file, the cached information of the file is written back to the volume. After the function succeeded, the file object is no longer valid and it can be discarded.
+Note that if the file object is in read-only mode and _FS_LOCK option is not enabled, the file object can also be discarded without this process. However this is not recommended for future compatibility.
+Always available.
+The f_closedir function closes an open directory.
++FRESULT f_closedir ( + DIR* dp /* [IN] Pointer to the directory object */ +); ++
+FR_OK, +FR_INT_ERR, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The f_closedir() function closes an open directory object. After the function succeeded, the directory object is no longer valid and it can be discarded.
+Note that the directory object can also be discarded without this process if _FS_LOCK option is not enabled. However this is not recommended for future compatibility.
+Available when _FS_MINIMIZE <= 1.
+The disk_initialize function initializes the storage device.
++DSTATUS disk_initialize ( + BYTE pdrv /* [IN] Physical drive number */ +); ++
This function returns a disk status as the result. For details of the disk status, refer to the disk_status() function.
+This function initializes a storage device and put it ready to generic read/write data. When the function succeeded, STA_NOINIT flag in the return value is cleared.
+Application program MUST NOT call this function, or FAT structure on the volume can be broken. To re-initialize the file system, use f_mount() function instead. This function is called at volume mount process by FatFs module to manage the media change.
+The disk_ioctl function cntrols device specific features and miscellaneous functions other than generic read/write.
++DRESULT disk_ioctl ( + BYTE pdrv, /* [IN] Drive number */ + BYTE cmd, /* [IN] Control command code */ + void* buff /* [I/O] Parameter and data buffer */ +); ++
The FatFs module requires only five device independent commands described below.
+Command | Description |
---|---|
CTRL_SYNC | Make sure that the device has finished pending write process. If the disk I/O module has a write back cache, the dirty buffers must be written back to the media immediately. Nothing to do for this command if each write operation to the media is completed within the disk_write() function. Required at _FS_READONLY == 0. |
GET_SECTOR_COUNT | Returns number of available sectors on the drive into the DWORD variable pointed by buff. This command is used by only f_mkfs() and f_fdisk() function to determine the volume/partition size to be created. Required at _USE_MKFS == 1 or _MULTI_PARTITION == 1. |
GET_SECTOR_SIZE | Returns sector size of the media into the WORD variable pointed by buff. Valid return values of this command are 512, 1024, 2048 or 4096. This command is required at variable sector size configuration, _MAX_SS > _MIN_SS. Never used at fixed sector size configuration, _MAX_SS == _MIN_SS, and it must work at that sector size. |
GET_BLOCK_SIZE | Returns erase block size of the flash memory in unit of sector into the DWORD variable pointed by buff. The allowable value is from 1 to 32768 in power of 2. Return 1 if the erase block size is unknown or disk media. This command is used by only f_mkfs() function and it attempts to align data area to the erase block boundary. Required at _USE_MKFS == 1. |
CTRL_ERASE_SECTOR | Informs device that the data on the block of sectors specified by a DWORD array {<start sector>, <end sector>} pointed by buff is no longer needed and may be erased. The device would force erased the memory block. This is a command similar to Trim command of ATA device. When this feature is not supported or not a flash memory media, nothing to do for this command. The FatFs does not check the result code and the file function is not affected even if the sectors ware not erased well. This command is called on removing a cluster chain and f_mkfs() function. Required at _USE_ERASE == 1. |
FatFs never uses any device dependent command and user defined command. Following table shows an example of non-standard commands usable for some applications.
+Command | Description |
---|---|
CTRL_FORMAT | Create a physical format on the media. If buff is not null, it is pointer to the call-back function for progress notification. |
CTRL_POWER_IDLE | Put the device idle state. STA_NOINIT in status flag may not be set if the device would go active state by generic read/write function. |
CTRL_POWER_OFF | Put the device off state. Shut-down the power to the device and deinitialize the device interface if needed. STA_NOINIT in status flag must be set. The device goes active state by disk_initialize() function. |
CTRL_LOCK | Lock media eject mechanism. |
CTRL_UNLOCK | Unlock media eject mechanism. |
CTRL_EJECT | Eject media cartridge. STA_NOINIT and STA_NODISK in status flag are set after the function succeeded. |
MMC_GET_TYPE | Get card type. The type flags, bit0:MMCv3, bit1:SDv1, bit2:SDv2+ and bit3:LBA, is stored to a BYTE variable pointed by buff. (MMC/SDC specific command) |
MMC_GET_CSD | Get CSD register into a 16-byte buffer pointed by buff. (MMC/SDC specific command) |
MMC_GET_CID | Get CID register into a 16-byte buffer pointed by buff. (MMC/SDC specific command) |
MMC_GET_OCR | Get OCR register into a 4-byte buffer pointed by buff. (MMC/SDC specific command) |
MMC_GET_SDSTAT | Get SDSTATUS register into a 64-byte buffer pointed by buff. (SDC specific command) |
ATA_GET_REV | Get the revision string into a 16-byte buffer pointed by buff. (ATA/CFC specific command) |
ATA_GET_MODEL | Get the model string into a 40-byte buffer pointed by buff. (ATA/CFC specific command) |
ATA_GET_SN | Get the serial number string into a 20-byte buffer pointed by buff. (ATA/CFC specific command) |
The disk_read function reads sector(s) from the storage device.
++DRESULT disk_read ( + BYTE pdrv, /* [IN] Physical drive number */ + BYTE* buff, /* [OUT] Pointer to the read data buffer */ + DWORD sector, /* [IN] Start sector number */ + UINT count /* [IN] Number of sectros to read */ +); ++
The memory address specified by buff is not that always aligned to word boundary because the type of argument is defined as BYTE*. The misaligned read/write request can occure at direct transfer. If the bus architecture, especially DMA controller, does not allow misaligned memory access, it should be solved in this function. There are some workarounds described below to avoid this issue.
+Generally, a multiple sector transfer request must not be split into single sector transactions to the storage device, or you will not get good read throughput.
+The disk_status function returns the current disk status.
++DSTATUS disk_status ( + BYTE pdrv /* [IN] Physical drive number */ +); ++
The disk status is returned in combination of following flags. FatFs refers only STA_NOINIT and STA_PROTECT.
+The disk_write writes sector(s) to the storage device.
++DRESULT disk_write ( + BYTE drv, /* [IN] Physical drive number */ + const BYTE* buff, /* [IN] Pointer to the data to be written */ + DWORD sector, /* [IN] Sector number to write from */ + UINT count /* [IN] Number of sectors to write */ +); ++
The specified memory address is not that always aligned to word boundary because the type of pointer is defined as BYTE*. For more information, refer to the description of disk_read() function.
+Generally, a multiple sector transfer request must not be split into single sector transactions to the storage device, or you will never get good write throughput.
+FatFs expects delayed write feature of the disk functions. The write operation to the media need not to be completed due to write operation is in progress or only stored it into the cache buffer when return from this function. But data on the buff is invalid after return from this function. The write completion request is done by CTRL_SYNC command of disk_ioctl() function. Therefore, if delayed write feature is implemented, the write throughput may be improved.
+Application program MUST NOT call this function, or FAT structure on the volume can be collapsed.
+This function is not needed when _FS_READONLY == 1.
+The f_eof function tests for end-of-file on a file.
++int f_eof ( + FIL* fp /* [IN] File object */ +); ++
The f_eof() function returns a non-zero value if the read/write pointer has reached end of the file; otherwise it returns a zero.
+In this revision, this function is implemented as a macro.
+
+#define f_eof(fp) (((fp)->fptr) == ((fp)->fsize) ? 1 : 0)
+
+Always available.
+The f_error tests for an error on a file.
++int f_error ( + FIL* fp /* [IN] File object */ +); ++
Returns a non-zero value if a hard error has occured; otherwise it returns a zero.
+In this revision, this function is implemented as a macro.
+
+#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
+
+Always available.
+The get_fattime function gets current time.
++DWORD get_fattime (void); ++
Currnet time is returned with packed into a DWORD value. The bit field is as follows:
+The get_fattime() function shall return any valid time even if the system does not support a real time clock. If a zero is returned, the file will not have a valid timestamp.
+This function is not needed when _FS_READONLY == 1.
+The f_fdisk fucntion divides a physical drive.
++FRESULT f_fdisk ( + BYTE pdrv, /* [IN] Physical drive number */ + const DWORD part[], /* [IN] Partition size */ + void* work /* [IN] Work area */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_NOT_READY, +FR_WRITE_PROTECTED, +FR_INVALID_PARAMETER +
+The f_fdisk() function creates a partition table into the MBR of the physical drive. The partitioning rule is in generic FDISK format, so that it can create upto four primary partitions. Logical volumes in the extended partition is not supported. The part[] with four items specifies how to divide the physical drive. The first item specifies the size of first primary partition and fourth item specifies the fourth primary partition. If the value is less than or equal to 100, it specifies percentage of the partition in the entire disk space. If it is larger than 100, it specifies the partition size in unit of sector.
+Available when _FS_READOLNY == 0, _USE_MKFS == 1 and _MULTI_PARTITION == 1.
++ /* Volume management table defined by user (required when _MULTI_PARTITION == 1) */ + + PARTITION VolToPart[] = { + {0, 1}, /* Logical drive 0 ==> Physical drive 0, 1st partition */ + {0, 2}, /* Logical drive 1 ==> Physical drive 0, 2nd partition */ + {1, 0} /* Logical drive 2 ==> Physical drive 1, auto detection */ + }; ++
+ /* Initialize a brand-new disk drive mapped to physical drive 0 */ + + FATFS fs; + DWORD plist[] = {50, 50, 0, 0}; /* Divide drive into two partitions */ + BYTE work[_MAX_SS]; + + f_fdisk(0, plist, work); /* Divide physical drive 0 */ + + f_mount(&fs, "0:", 0); /* Register work area to the logical drive 0 */ + f_mkfs("0:", 0, 0); /* Create FAT volume on the logical drive 0. 2nd argument is ignored. */ + f_mount(0, "0:", 0); /* Unregister work area from the logical drive 0 */ + + f_mount(&fs, "1:", 0); /* Register a work area to the logical drive 1 */ + f_mkfs("1:", 0, 0); /* Create FAT volume on the logical drive 1. 2nd argument is ignored. */ + f_mount(0, "1:", 0); /* Unregister work area from the logical drive 1 */ + ++
The format of path name on the FatFs module is similer to the filename specs of DOS/Windos as follows:
+"[drive:][/]directory/file"+
The FatFs module supports long file name (LFN) and 8.3 format file name (SFN). The LFN can be used when LFN feature is enabled (_USE_LFN > 0). The sub directories are separated with a \ or / in the same way as DOS/Windows API. Duplicated separators are skipped and ignored. Only a difference is that the logical drive is specified in a numeral with a colon. When drive number is omitted, the drive number is assumed as default drive (drive 0 or current drive).
+Control characters ('\0' to '\x1F') are recognized as end of the path name. Leading/embedded spaces in the path name are valid as a part of the name at LFN configuration but they are recognized as end of the path name at non-LFN configuration. Trailing spaces and dots are ignored.
+In default configuration (_FS_RPATH == 0), it does not have a concept of current directory like OS oriented file system. All objects on the volume are always specified in full path name that follows from the root directory. Dot directory names are not allowed. Heading separator is ignored and it can be exist or omitted. The default drive is fixed to drive 0.
+When relative path feature is enabled (_FS_RPATH == 1), specified path is followed from the root directory if a heading separator is exist. If not, it is followed from the current directory of the drive set with f_chdir function. Dot names are also allowed for the path name. The default drive is the current drive set with f_chdrive function.
+Path name | _FS_RPATH == 0 | _FS_RPATH == 1 |
file.txt | A file in the root directory of the drive 0 | A file in the current directory of the current drive |
/file.txt | A file in the root directory of the drive 0 | A file in the root directory of the current drive |
The root directory of the drive 0 | The current directory of the current drive | |
/ | The root directory of the drive 0 | The root directory of the current drive |
2: | The root directory of the drive 2 | The current directory of the drive 2 |
2:/ | The root directory of the drive 2 | The root directory of the drive 2 |
2:file.txt | A file in the root directory of the drive 2 | A file in the current directory of the drive 2 |
../file.txt | Invalid name | A file in the parent directory |
. | Invalid name | This directory |
.. | Invalid name | Parent directory of the current directory |
dir1/.. | Invalid name | The current directory |
/.. | Invalid name | The root directory (sticks the top level) |
When option _STR_VOLUME_ID is specified, also pre-defined strings can be used as drive identifier in the path name instead of a numeral.
+The path names are input/output in either ANSI/OEM code (SBCS/DBCS) or Unicode depends on the configuration options. The type of arguments which specify the file names are defined as TCHAR. It is an alias of char in default. The code set used to the file name string is ANSI/OEM specifid by _CODE_PAGE. When _LFN_UNICODE is set to 1, the type of the TCHAR is switched to WCHAR to support Unicode (UTF-16 encoding). In this case, the LFN feature is fully supported and the Unicode specific characters, such as ✝☪✡☸☭, can also be used for the path name. It also affects data types and encoding of the string I/O functions. To define literal strings, _T(s) and _TEXT(s) macro are available to select either ANSI/OEM or Unicode automatically. The code shown below is an example to define the literal strings.
++ f_open(fp, "filename.txt", FA_READ); /* ANSI/OEM string */ + f_open(fp, L"filename.txt", FA_READ); /* Unicode string */ + f_open(fp, _T("filename.txt"), FA_READ); /* Changed by configuration */ ++
The FatFs module needs dynamic work area called file system object for each volume (logical drive). It is registered to the FatFs module by f_mount() function. By default, each logical drive is bound to the physical drive with the same drive number and an FAT volume on the drive is serched by auto detect feature. It loads boot sectors and checks it if it is an FAT boot sector in order of sector 0 as SFD format, 1st partition, 2nd partition, 3rd partition and 4th partition as FDISK format.
+When _MULTI_PARTITION == 1 is specified by configuration option, each individual logical drive is bound to the partition on the physical drive specified by volume management table. The volume management table must be defined by user to resolve relationship between logical drives and partitions. Following code is an example of a volume management table.
++Example: Logical drive 0-2 are tied to three pri-partitions on the physical drive 0 (fixed disk) + Logical drive 3 is tied to an FAT volume on the physical drive 1 (removable disk) + +PARTITION VolToPart[] = { + {0, 1}, /* Logical drive 0 ==> Physical drive 0, 1st partition */ + {0, 2}, /* Logical drive 1 ==> Physical drive 0, 2nd partition */ + {0, 3}, /* Logical drive 2 ==> Physical drive 0, 3rd partition */ + {1, 0} /* Logical drive 3 ==> Physical drive 1 (auto detection) */ +}; + + ++
There are some considerations on using _MULTI_PARTITION configuration.
+The f_forward function reads the file data and forward it to the data streaming device.
++FRESULT f_forward ( + FIL* fp, /* [IN] File object */ + UINT (*func)(const BYTE*,UINT), /* [IN] Data streaming function */ + UINT btf, /* [IN] Number of bytes to forward */ + UINT* bf /* [OUT] Number of bytes forwarded */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The f_forward() function reads the data from the file and forward it to the outgoing stream without data buffer. This is suitable for small memory system because it does not require any data buffer at application module. The file pointer of the file object increases in number of bytes forwarded. In case of *bf is less than btf without error, it means the requested bytes could not be transferred due to end of file or stream goes busy during data transfer.
+Available when _USE_FORWARD == 1 and _FS_TINY == 1.
++/*------------------------------------------------------------------------*/ +/* Sample code of data transfer function to be called back from f_forward */ +/*------------------------------------------------------------------------*/ + +UINT out_stream ( /* Returns number of bytes sent or stream status */ + const BYTE *p, /* Pointer to the data block to be sent */ + UINT btf /* >0: Transfer call (Number of bytes to be sent). 0: Sense call */ +) +{ + UINT cnt = 0; + + + if (btf == 0) { /* Sense call */ + /* Return stream status (0: Busy, 1: Ready) */ + /* When once it returned ready to sense call, it must accept a byte at least */ + /* at subsequent transfer call, or f_forward will fail with FR_INT_ERR. */ + if (FIFO_READY) cnt = 1; + } + else { /* Transfer call */ + do { /* Repeat while there is any data to be sent and the stream is ready */ + FIFO_PORT = *p++; + cnt++; + } while (cnt < btf && FIFO_READY); + } + + return cnt; +} + + +/*------------------------------------------------------------------------*/ +/* Sample code using f_forward function */ +/*------------------------------------------------------------------------*/ + +FRESULT play_file ( + char *fn /* Pointer to the audio file name to be played */ +) +{ + FRESULT rc; + FIL fil; + UINT dmy; + + /* Open the audio file in read only mode */ + rc = f_open(&fil, fn, FA_READ); + if (rc) return rc; + + /* Repeat until the file pointer reaches end of the file */ + while (rc == FR_OK && fil.fptr < fil.fsize) { + + /* any other processes... */ + + /* Fill output stream periodicaly or on-demand */ + rc = f_forward(&fil, out_stream, 1000, &dmy); + } + + /* Close the file and return */ + f_close(&fil); + return rc; +} ++
The f_getcwd function retrieves the current directory.
++FRESULT f_getcwd ( + TCHAR* buff, /* [OUT] Buffer to return path name */ + UINT len /* [IN] The length of the buffer */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+The f_getcwd() function retrieves full path name of the current directory of the current drive. When _VOLUMES is larger than 1, a logical drive number is added to top of the path name.
+Available when _FS_RPATH == 2.
+The f_getfree function gets number of the free clusters on the volume.
++FRESULT f_getfree ( + const TCHAR* path, /* [IN] Logical drive number */ + DWORD* nclst, /* [OUT] Number of free clusters */ + FATFS** fatfs /* [OUT] Corresponding file system object */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT +
+The f_getfree() function gets number of free clusters on the volume. The member csize in the file system object indicates number of sectors per cluster, so that the free space in unit of sector can be calcurated with this information. When FSINFO structure on the FAT32 volume is not in sync, this function can return an incorrect free cluster count. To avoid this problem, FatFs can be forced full FAT scan by _FS_NOFSINFO option.
+Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
++ FATFS *fs; + DWORD fre_clust, fre_sect, tot_sect; + + + /* Get volume information and free clusters of drive 1 */ + res = f_getfree("1:", &fre_clust, &fs); + if (res) die(res); + + /* Get total sectors and free sectors */ + tot_sect = (fs->n_fatent - 2) * fs->csize; + fre_sect = fre_clust * fs->csize; + + /* Print the free space (assuming 512 bytes/sector) */ + printf("%10lu KiB total drive space.\n%10lu KiB available.\n", + tot_sect / 2, fre_sect / 2); ++
The f_getlabel function returns volume label and volume serial number of a drive.
++FRESULT f_getlabel ( + const TCHAR* path, /* [IN] Drive number */ + TCHAR* label, /* [OUT] Volume label */ + DWORD* vsn /* [OUT] Volume serial number */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT +
+Available when _USE_LABEL == 1.
++ char str[12]; + + /* Get volume label of the default drive */ + f_getlabel("", str, 0); + + /* Get volume label of the drive 2 */ + f_getlabel("2:", str, 0); ++
The f_gets reads a string from the file.
++TCHAR* f_gets ( + TCHAR* buff, /* [OUT] Read buffer */ + int len, /* [IN] Size of the read buffer */ + FIL* fp /* [IN] File object */ +); ++
When the function succeeded, buff will be returuned.
+The f_gets() function is a wrapper function of f_read() function. The read operation continues until a '\n' is stored, reached end of the file or the buffer is filled with len - 1 characters. The read string is terminated with a '\0'. When no character to read or any error occured during read operation, it returns a null pointer. The status of EOF and error can be examined with f_eof() and f_error() macros.
+When FatFs is configured to Unicode API (_LFN_UNICODE == 1), data types on the srting fuctions, f_putc(), f_puts(), f_printf() and f_gets(), is also switched to Unicode. The character encoding on the file to be read/written via those functions is selected by _STRF_ENCODE option.
+Available when _USE_STRFUNC is 1 or 2. When it is set to 2, '\r's contained in the file are stripped out.
+The f_lseek function moves the file read/write pointer of an open file object. It can also be used to expand the file size (cluster pre-allocation).
+ ++FRESULT f_lseek ( + FIL* fp, /* [IN] File object */ + DWORD ofs /* [IN] File read/write pointer */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The f_lseek() function moves the file read/write pointer of an open file. The offset can be specified in only origin from top of the file. When an offset beyond the file size is specified at write mode, the file size is expanded to the specified offset. The file data in the expanded area is undefined because no data is written to the file. This is suitable to pre-allocate a cluster chain quickly, for fast write operation. After the f_lseek() function succeeded, the current read/write pointer should be checked in order to make sure the read/write pointer has been moved correctry. In case of the current read/write pointer is not the expected value, either of followings has been occured.
+Fast seek feature is enabled when _USE_FASTSEEK is set to 1 and the member cltbl in the file object is not NULL. This feature enables fast backward/long seek operations without FAT access by using CLMT (cluster link map table). The fast seek feature is also applied to f_read()/f_write() function, however, the file size cannot be expanded by f_write()/f_lseek() function.
+The CLMT must be created in the user defined DWORD array prior to use the fast seek feature. To create the CLMT, set address of the DWORD array to the member cltbl in the file object, set the array size in unit of items into the first item and call the f_lseek() function with ofs = CREATE_LINKMAP. After the function succeeded and CLMT is created, no FAT access is occured at subsequent f_read()/f_write()/f_lseek() function to the file. If the function failed with FR_NOT_ENOUGH_CORE, the given array size is insufficient for the file and number of items required is returned into the first item of the array. The required array size is (number of fragments + 1) * 2 items. For example, when the file is fragmented in 5, 12 items will be required for the CLMT.
+Available when _FS_MINIMIZE <= 2.
++ /* Open file */ + fp = malloc(sizeof (FIL)); + res = f_open(fp, "file.dat", FA_READ|FA_WRITE); + if (res) ... + + /* Move to offset of 5000 from top of the file */ + res = f_lseek(fp, 5000); + + /* Move to end of the file to append data */ + res = f_lseek(fp, f_size(fp)); + + /* Forward 3000 bytes */ + res = f_lseek(fp, f_tell(fp) + 3000); + + /* Rewind 2000 bytes (take care on wraparound) */ + res = f_lseek(fp, f_tell(fp) - 2000); ++
+/* Cluster pre-allocation (to prevent buffer overrun on streaming write) */ + + res = f_open(fp, recfile, FA_CREATE_NEW | FA_WRITE); /* Create a file */ + + res = f_lseek(fp, PRE_SIZE); /* Expand file size (cluster pre-allocation) */ + if (res || f_tell(fp) != PRE_SIZE) ... /* Check if the file has been expanded */ + + res = f_lseek(fp, DATA_START); /* Record data stream WITHOUT cluster allocation delay */ + ... /* DATA_START and write block size should be aligned to sector boundary */ + + res = f_truncate(fp); /* Truncate unused area */ + res = f_lseek(fp, 0); /* Put file header */ + ... + + res = f_close(fp); ++
+/* Using fast seek feature */ + + DWORD clmt[SZ_TBL]; /* Cluster link map table buffer */ + + res = f_lseek(fp, ofs1); /* This is normal seek (cltbl is nulled on file open) */ + + fp->cltbl = clmt; /* Enable fast seek feature (cltbl != NULL) */ + clmt[0] = SZ_TBL; /* Set table size */ + res = f_lseek(fp, CREATE_LINKMAP); /* Create CLMT */ + ... + + res = f_lseek(fp, ofs2); /* This is fast seek */ ++
The f_mkdir function creates a new directory.
++FRESULT f_mkdir ( + const TCHAR* path /* [IN] Directory name */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_EXIST, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+This function creates a new directory.
+Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
++ res = f_mkdir("sub1"); + if (res) die(res); + res = f_mkdir("sub1/sub2"); + if (res) die(res); + res = f_mkdir("sub1/sub2/sub3"); + if (res) die(res); ++
The f_mkfs fucntion creates an FAT file system on the logical drive.
++FRESULT f_mkfs ( + const TCHAR* path, /* [IN] Logical drive number */ + BYTE sfd, /* [IN] Partitioning rule */ + UINT au /* [IN] Size of the allocation unit */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_NOT_READY, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_MKFS_ABORTED, +FR_INVALID_PARAMETER +
+The f_mkfs() function creates an FAT volume on the logical drive. When FDISK format is specified, a primary partition occupies the entire disk space is created and then an FAT volume is created on the partition. When SFD format is specified, the FAT volume starts from the first sector of the physical drive.
+If the logical drive has been bound to any partition (1-4) by multiple partition feature (_MULTI_PARTITION), the FAT volume is created into the specified partition. In this case, the second argument sfd is ignored. The physical drive must have been partitioned with f_fdisk() function or any other partitioning tool prior to use this function.
+Note that there are two partitioning rules, FDISK and SFD. The FDISK partitioning is usually used for harddisk, MMC, SDC, CFC and U Disk. It can divide a physical drive into one or more partitions with a partition table on the MBR. However Windows does not support multiple partition on the removable media. The SFD is non-partitioned method. The FAT volume starts from the first sector on the physical drive without partition table. It is usually used for floppy disk, Microdrive, optical disk and super-floppy media.
+The FAT sub-type, FAT12/FAT16/FAT32, is determined by number of clusters on the volume and nothing else, according to the FAT specification issued by Microsoft. Thus which FAT sub-type is selected, is depends on the volume size and the specified cluster size. The cluster size affects performance of the file system and large cluster increases the performance.
+When the number of clusters gets near the FAT sub-type boundaries, the function can fail with FR_MKFS_ABORTED.
+Available when _FS_READOLNY == 0 and _USE_MKFS == 1.
+The f_mount fucntion registers/unregisters file system object to the FatFs module.
++FRESULT f_mount ( + FATFS* fs, /* [IN] File system object */ + const TCHAR* path, /* [IN] Logical drive number */ + BYTE opt /* [IN] Initialization option */ +); ++
+FR_OK, +FR_INVALID_DRIVE, +FR_DISK_ERR, +FR_NOT_READY, +FR_NO_FILESYSTEM +
+The f_mount() function registers/unregisters a file system object used for the logical drive to the FatFs module as follows:
+The file system object is the work area needed for each logical drive. It must be given to the logical drive with this function prior to use any other file functions except for f_fdisk() function. To unregister a work area, specify a NULL to the fs, and then the work area can be discarded.
+If forced mount is not specified, this function always succeeds regardless of the physical drive status due to delayed mount feature. It only clears (de-initializes) the given work area and registers its address to the internal table. No activity of the physical drive in this function. It can also be used to force de-initialized the registered work area of a logical drive. The volume mount processes, initialize the corresponding physical drive, find the FAT volume in it and initialize the work area, is performed in the subsequent file access functions when either or both of following condition is true.
+If the function with forced mount failed, it means that the file system object is registered but the volume is currently not available. Mount process will also be attempted in subsequent file access functions.
+If implementation of the disk I/O layer lacks media change detection, application program needs to perform a f_mount() after each media change to force cleared the file system object.
+Always available.
+The f_open function creates a file object to be used to access the file.
++FRESULT f_open ( + FIL* fp, /* [OUT] Pointer to the file object structure */ + const TCHAR* path, /* [IN] File name */ + BYTE mode /* [IN] Mode flags */ +); ++
Value | Description |
---|---|
FA_READ | Specifies read access to the object. Data can be read from the file. Combine with FA_WRITE for read-write access. |
FA_WRITE | Specifies write access to the object. Data can be written to the file. Combine with FA_READ for read-write access. |
FA_OPEN_EXISTING | Opens the file. The function fails if the file is not existing. (Default) |
FA_OPEN_ALWAYS | Opens the file if it is existing. If not, a new file is created. +To append data to the file, use f_lseek() function after file open in this method. |
FA_CREATE_NEW | Creates a new file. The function fails with FR_EXIST if the file is existing. |
FA_CREATE_ALWAYS | Creates a new file. If the file is existing, it will be truncated and overwritten. |
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_EXIST, +FR_INVALID_OBJECT, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED, +FR_NOT_ENOUGH_CORE, +FR_TOO_MANY_OPEN_FILES +
+After f_open() function succeeded, the file object is valid. The file object is used for subsequent read/write functions to identify the file. To close an open file, use f_close() function. If the file is modified and not closed properly, the file data will be collapsed.
+If duplicated file open is needed, read here carefully. However duplicated open of a file with write mode flag is always prohibited.
+Before using any file function, a work area (file system object) must be registered to the logical drive with f_mount() function. All API functions except for f_fdisk() function can work after this procedure.
+Always available. The mode flags, FA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW and FA_OPEN_ALWAYS, are not available when _FS_READONLY == 1.
++/* Read a text file and display it */ + +FATFS FatFs; /* Work area (file system object) for logical drive */ + +int main (void) +{ + FIL fil; /* File object */ + char line[82]; /* Line buffer */ + FRESULT fr; /* FatFs return code */ + + + /* Register work area to the default drive */ + f_mount(&FatFs, "", 0); + + /* Open a text file */ + fr = f_open(&fil, "message.txt", FA_READ); + if (fr) return (int)fr; + + /* Read all lines and display it */ + while (f_gets(line, sizeof line, &fil)) + printf(line); + + /* Close the file */ + f_close(&fil); + + return 0; +} ++
+/* Copy a file "file.bin" on the drive 1 to drive 0 */ + +int main (void) +{ + FATFS fs[2]; /* Work area (file system object) for logical drives */ + FIL fsrc, fdst; /* File objects */ + BYTE buffer[4096]; /* File copy buffer */ + FRESULT fr; /* FatFs function common result code */ + UINT br, bw; /* File read/write count */ + + + /* Register work area for each logical drive */ + f_mount(&fs[0], "0:", 0); + f_mount(&fs[1], "1:", 0); + + /* Open source file on the drive 1 */ + fr = f_open(&fsrc, "1:file.bin", FA_OPEN_EXISTING | FA_READ); + if (fr) return (int)fr; + + /* Create destination file on the drive 0 */ + fr = f_open(&fdst, "0:file.bin", FA_CREATE_ALWAYS | FA_WRITE); + if (fr) return (int)fr; + + /* Copy source to destination */ + for (;;) { + fr = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of source file */ + if (fr || br == 0) break; /* error or eof */ + fr = f_write(&fdst, buffer, br, &bw); /* Write it to the destination file */ + if (fr || bw < br) break; /* error or disk full */ + } + + /* Close open files */ + f_close(&fsrc); + f_close(&fdst); + + /* Unregister work area prior to discard it */ + f_mount(NULL, "0:", 0); + f_mount(NULL, "1:", 0); + + return (int)fr; +} ++
The f_opendir function opens a directory.
++FRESULT f_opendir ( + DIR* dp, /* [OUT] Pointer to the directory object structure */ + const TCHAR* path /* [IN] Directory name */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_PATH, +FR_INVALID_NAME, +FR_INVALID_OBJECT, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE, +FR_TOO_MANY_OPEN_FILES +
+The f_opendir() function opens an exsisting directory and creates a directory object for subsequent f_readdir() function.
+Available when _FS_MINIMIZE <= 1.
+The f_printf function writes formatted string to the file.
++int f_printf ( + FIL* fp, /* [IN] File object */ + const TCHAR* fmt, /* [IN] Format stirng */ + ... +); ++
When the function succeeded, it returns number of characters written. When the function failed due to disk full or any error, an EOF (-1) will be returned.
+The f_printf() is a wrapper function of f_write(). The format control directive is a sub-set of standard library shown as follos:
+Available when _FS_READONLY == 0 and _USE_STRFUNC is 1 or 2. When it is set to 2, '\n's contained in the output are converted to '\r'+'\n'.
+When FatFs is configured to Unicode API (_LFN_UNICODE == 1), data types on the srting fuctions, f_putc(), f_puts(), f_printf() and f_gets(), is also switched to Unicode. The character encoding on the file to be read/written via those functions is selected by _STRF_ENCODE option.
++ f_printf(&fil, "%d", 1234); /* "1234" */ + f_printf(&fil, "%6d,%3d%%", -200, 5); /* " -200, 5%" */ + f_printf(&fil, "%ld", 12345L); /* "12345" */ + f_printf(&fil, "%06d", 25); /* "000025" */ + f_printf(&fil, "%06d", -25); /* "000-25" */ + f_printf(&fil, "%-6d", 25); /* "25 " */ + f_printf(&fil, "%u", -1); /* "65535" or "4294967295" */ + f_printf(&fil, "%04x", 0xAB3); /* "0ab3" */ + f_printf(&fil, "%08LX", 0x123ABCL); /* "00123ABC" */ + f_printf(&fil, "%016b", 0x550F); /* "0101010100001111" */ + f_printf(&fil, "%s", "String"); /* "String" */ + f_printf(&fil, "%8s", "abc"); /* " abc" */ + f_printf(&fil, "%-8s", "abc"); /* "abc " */ + f_printf(&fil, "%c", 'a'); /* "a" */ + f_printf(&fil, "%f", 10.0); /* f_printf lacks floating point support */ ++
The f_putc funciton puts a character to the file.
++int f_putc ( + TCHAR chr, /* [IN] A character to put */ + FIL* fp /* [IN] File object */ +); ++
When the character was written successfuly, it returns number of characters written. When the function failed due to disk full or any error, an EOF (-1) will be returned.
+When FatFs is configured to Unicode API (_LFN_UNICODE == 1), character encoding on the string fuctions, f_putc(), f_puts(), f_printf() and f_gets(), is also switched to Unicode. The character encoding on the file to be read/written via those functions is selected by _STRF_ENCODE option.
+The f_putc() function is a wrapper function of f_write() function.
+Available when _FS_READONLY == 0 and _USE_STRFUNC is 1 or 2. When it is set to 2, a '\n' is converted to '\r'+'\n'.
+The f_puts function writes a string to the file.
++int f_puts ( + const TCHAR* str, /* [IN] String */ + FIL* fp /* [IN] File object */ +); ++
When the function succeeded, it returns number of characters written. When the write operation is aborted due to disk full or any error, an EOF (-1) will be returned.
+When FatFs is configured to Unicode API (_LFN_UNICODE == 1), character encoding on the srting fuctions, f_putc(), f_puts(), f_printf() and f_gets(), is also switched to Unicode. The character encoding on the file to be read/written via those functions is selected by _STRF_ENCODE option.
+The f_puts() function is a wrapper function of f_write() function.
+Available when _FS_READONLY == 0 and _USE_STRFUNC is 1 or 2. When it is set to 2, '\n's contained in the string are converted to '\r'+'\n'.
+On the FatFs API, most of file functions return common result code as enum type FRESULT. When a function succeeded, it returns zero, otherwise returns non-zero value that indicates type of error.
+ +The f_read function reads data from a file.
++FRESULT f_read ( + FIL* fp, /* [IN] File object */ + void* buff, /* [OUT] Buffer to store read data */ + UINT btr, /* [IN] Number of bytes to read */ + UINT* br /* [OUT] Number of bytes read */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The file read/write pointer of the file object advances number of bytes read. After the function succeeded, *br should be checked to detect end of the file. In case of *br is less than btr, it means the read/write pointer reached end of the file during read operation.
+Always available.
+The f_readdir function reads directory entries.
++FRESULT f_readdir ( + DIR* dp, /* [IN] Directory object */ + FILINFO* fno /* [OUT] File information structure */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+The f_readdir() function reads directory items, file and directory, in sequence. All items in the directory can be read by calling f_readdir() function repeatedly. When relative path feature is enabled (_FS_RPATH >= 1), dot entries ("." and "..") are not filtered out and they will appear in the read items. When all directory items have been read and no item to read, a null string is returned into the fname[] without any error. When a null pointer is given to the fno, the read index of the directory object is rewinded.
+When LFN feature is enabled, lfname and lfsize in the file information structure must be initialized with valid value prior to use it. The lfname is a pointer to the LFN read buffer. The lfsize is size of the LFN read buffer in unit of TCHAR. If the LFN is not needed, set a null pointer to the lfname and the LFN is not returned. A null string will be returned into the LFN read buffer in case of following conditions.
+When the directory item has no LFN information, lower case characters can be contained in the fname[].
+Available when _FS_MINIMIZE <= 1.
++FRESULT scan_files ( + char* path /* Start node to be scanned (also used as work area) */ +) +{ + FRESULT res; + FILINFO fno; + DIR dir; + int i; + char *fn; /* This function is assuming non-Unicode cfg. */ +#if _USE_LFN + static char lfn[_MAX_LFN + 1]; /* Buffer to store the LFN */ + fno.lfname = lfn; + fno.lfsize = sizeof lfn; +#endif + + + res = f_opendir(&dir, path); /* Open the directory */ + if (res == FR_OK) { + i = strlen(path); + for (;;) { + res = f_readdir(&dir, &fno); /* Read a directory item */ + if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ + if (fno.fname[0] == '.') continue; /* Ignore dot entry */ +#if _USE_LFN + fn = *fno.lfname ? fno.lfname : fno.fname; +#else + fn = fno.fname; +#endif + if (fno.fattrib & AM_DIR) { /* It is a directory */ + sprintf(&path[i], "/%s", fn); + res = scan_files(path); + if (res != FR_OK) break; + path[i] = 0; + } else { /* It is a file. */ + printf("%s/%s\n", path, fn); + } + } + f_closedir(&dir) + } + + return res; +} ++
Renames a file or sub-directory.
++FRESULT f_rename ( + const TCHAR* old_name, /* [IN] Old object name */ + const TCHAR* new_name /* [IN] New object name */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_EXIST, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED, +FR_NOT_ENOUGH_CORE +
+Renames a file or sub-directory and can also move it to other directory within the same logical drive. Do not rename open objects or directry table can be broken.
+Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
++ /* Rename an object */ + f_rename("oldname.txt", "newname.txt"); + + /* Rename and move an object to other directory */ + f_rename("oldname.txt", "dir1/newname.txt"); ++
The DIR structure is used for the work area to read a directory by f_oepndir()/f_readdir() function. Application program must not modify any member in this structure.
++typedef struct { + FATFS* fs; /* Pointer to the owner file system object */ + WORD id; /* Owner file system mount ID */ + WORD index; /* Index of directory entry to start to search next */ + DWORD sclust; /* Table start cluster (0:Root directory) */ + DWORD clust; /* Current cluster */ + DWORD sect; /* Current sector */ + BYTE* dir; /* Pointer to the current SFN entry in the win[] */ + BYTE* fn; /* Pointer to the SFN buffer (in/out) {file[8],ext[3],status[1]} */ +#if _FS_LOCK + UINT lockid; /* Sub-directory lock ID (0:Root directory) */ +#endif +#if _USE_LFN + WCHAR* lfn; /* Pointer to the LFN buffer (in/out) */ + WORD lfn_idx; /* Index of the LFN entris (0xFFFF:No LFN) */ +#endif +} DIR; ++
The f_setlabel function sets/removes the label of a volume.
++FRESULT f_setlabel ( + const TCHAR* label /* [IN] Volume label to be set */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_NAME, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT +
+When the string has a drive number, the volume label will be set to the volume specified by the drive number. If not, the label will be set to the default drive. If the given string is a null-string, the volume label on the volume will be removed. The format of the volume label is similar to the short file name but there are some differences shown below:
+Available when _FS_READONLY == 0 and _USE_LABEL == 1.
++ /* Set volume label to the default drive */ + f_setlabel("DATA DISK"); + + /* Set volume label to the drive 2 */ + f_setlabel("2:DISK 3 OF 4"); + + /* Remove volume label of the drive 2 */ + f_setlabel("2:"); ++
The FATFS structure (file system object) holds dynamic work area of individual logical drives. It is given by application program and registerd/unregisterd to the FatFs module with f_mount() function. Initialization is done on first API call after f_mount() function or media change. Application program must not modify any member in this structure.
++typedef struct { + BYTE fs_type; /* FAT sub-type (0:Not mounted) */ + BYTE drv; /* Physical drive number */ + BYTE csize; /* Sectors per cluster (1,2,4,...,128) */ + BYTE n_fats; /* Number of FAT copies (1,2) */ + BYTE wflag; /* win[] flag (b0:win[] is dirty) */ + BYTE fsi_flag; /* FSINFO flags (b7:Disabled, b0:Dirty) */ + WORD id; /* File system mount ID */ + WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ +#if _MAX_SS != _MIN_SS + WORD ssize; /* Sector size (512,1024,2048 or 4096) */ +#endif +#if _FS_REENTRANT + _SYNC_t sobj; /* Identifier of sync object */ +#endif +#if !_FS_READONLY + DWORD last_clust; /* FSINFO: Last allocated cluster */ + DWORD free_clust; /* FSINFO: Number of free clusters */ +#endif +#if _FS_RPATH + DWORD cdir; /* Current directory start cluster (0:root) */ +#endif + DWORD n_fatent; /* Number of FAT entries (== Number of clusters + 2) */ + DWORD fsize; /* Sectors per FAT */ + DWORD volbase; /* Volume start sector */ + DWORD fatbase; /* FAT area start sector */ + DWORD dirbase; /* Root directory area start sector (FAT32: Cluster#) */ + DWORD database; /* Data area start sector */ + DWORD winsect; /* Current sector appearing in the win[] */ + BYTE win[_MAX_SS]; /* Disk access window for directory, FAT (and file data at tiny cfg) */ +} FATFS; ++
The FIL structure (file object) holds state of an open file. It is created by f_open() function and discarded by f_close() function. Application program must not modify any member in this structure except for cltbl. Note that a sector buffer is defined in this structure at non-tiny configuration, so that the FIL structures should not be defined as auto variable.
+ ++typedef struct { + FATFS* fs; /* Pointer to the owner file system object */ + WORD id; /* Owner file system mount ID */ + BYTE flag; /* File object status flags */ + BYTE err; /* Abort flag (error code) */ + DWORD fptr; /* File read/write pointer (Byte offset origin from top of the file) */ + DWORD fsize; /* File size in unit of byte */ + DWORD sclust; /* File start cluster */ + DWORD clust; /* Current cluster */ + DWORD dsect; /* Current data sector */ +#if !_FS_READONLY + DWORD dir_sect; /* Sector containing the directory entry */ + BYTE* dir_ptr; /* Ponter to the directory entry in the window */ +#endif +#if _USE_FASTSEEK + DWORD* cltbl; /* Pointer to the cluster link map table (Nulled on file open) */ +#endif +#if _FS_LOCK + UINT lockid; /* Fle lock ID */ +#endif +#if !_FS_TINY + BYTE buf[_MAX_SS]; /* File private data transfer buffer */ +#endif +} FIL; ++ +
The FILINFO structure holds a file information returned by f_readdir() and f_stat() function.
++typedef struct { + DWORD fsize; /* File size */ + WORD fdate; /* Last modified date */ + WORD ftime; /* Last modified time */ + BYTE fattrib; /* Attribute */ + TCHAR fname[13]; /* Short file name (8.3 format) */ +#if _USE_LFN + TCHAR* lfname; /* Pointer to the LFN buffer */ + int lfsize; /* Size of the LFN buffer in unit of TCHAR */ +#endif +} FILINFO; ++
The f_size function gets the size of a file.
++DWORD f_size ( + FIL* fp /* [IN] File object */ +); ++
Returns the size of the file in unit of byte.
+In this revision, the f_size() function is implemented as a macro.
+
+#define f_size(fp) ((fp)->fsize)
+
+Always available.
+The f_stat function checks the existence of a file or sub-directory.
++FRESULT f_stat ( + const TCHAR* path, /* [IN] Object name */ + FILINFO* fno /* [OUT] FILINFO structure */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+The f_stat() function checks the existence of a file or sub-directory. If not exist, the function returns with FR_NO_FILE. If exist, the function returns with FR_OK and the informations of the object, file size, timestamp, attribute and SFN, are stored to the file information structure. For details of the file information, refer to the FILINFO structure and f_readdir() function.
+Available when _FS_MINIMIZE == 0.
+The f_sync function flushes the cached information of a writing file.
++FRESULT f_sync ( + FIL* fp /* [IN] File object */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The f_sync() function performs the same process as f_close() function but the file is left opened and can continue read/write/seek operations to the file. This is suitable for the applications that open files for a long time in write mode, such as data logger. Performing f_sync() function of periodic or immediataly after f_write() function can minimize the risk of data loss due to a sudden blackout or an unintentional media removal. For more information, refer to application note.
+However there is no sense in f_sync() function immediataly before f_close() function because it performs f_sync() function in it. In other words, the differnce between those functions is that the file object is invalidated or not.
+Available when _FS_READONLY == 0.
+The f_tell function gets the current read/write pointer of a file.
++DWORD f_tell ( + FIL* fp /* [IN] File object */ +); ++
Returns current read/write pointer of the file.
+In this revision, the f_tell() function is implemented as a macro.
+
+#define f_tell(fp) ((fp)->fptr)
+
+Always available.
+The f_truncate function truncates the file size.
++FRESULT f_truncate ( + FIL* fp /* [IN] File object */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The f_truncate() function truncates the file size to the current file read/write pointer. This function has no effect if the file read/write pointer is already pointing end of the file.
+Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
+The f_unlink function removes a file or sub-directory.
++FRESULT f_unlink ( + const TCHAR* path /* [IN] Object name */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_EXIST, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED, +FR_NOT_ENOUGH_CORE +
++If condition of the object to be removed is applicable to the following terms, the function will be rejected.
Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
+The f_utime function changes the timestamp of a file or sub-directory.
++FRESULT f_utime ( + const TCHAR* path, /* [IN] Object name */ + const FILINFO* fno /* [IN] Time and data to be set */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+The f_utime() function changes the timestamp of a file or sub-directory
+
+FRESULT set_timestamp (
+ char *obj, /* Pointer to the file name */
+ int year,
+ int month,
+ int mday,
+ int hour,
+ int min,
+ int sec
+)
+{
+ FILINFO fno;
+
+ fno.fdate = (WORD)(((year - 1980) * 512U) | month * 32U | mday);
+ fno.ftime = (WORD)(hour * 2048U | min * 32U | sec / 2U);
+
+ return f_utime(obj, &fno);
+}
+
+Available when _FS_READONLY == 0 and _FS_MINIMIZE == 0.
+The f_write writes data to a file.
++FRESULT f_write ( + FIL* fp, /* [IN] Pointer to the file object structure */ + const void* buff, /* [IN] Pointer to the data to be written */ + UINT btw, /* [IN] Number of bytes to write */ + UINT* bw /* [OUT] Pointer to the variable to return number of bytes written */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+The read/write pointer of the file object advances number of bytes written. After the function succeeded, *bw should be checked to detect the disk full. In case of *bw is less than btw, it means the volume got full during the write operation. The function can take a time when the volume is full or close to full.
+Available when _FS_READONLY == 0.
+FatFsモジュールは移植性に関して次の点を前提としています。
+下に示す依存関係図は、FatFsモジュール利用の組み込みシステムにおける代表的な構成を示します。
+ +(a) FatFs用に書かれたディスク・モジュールがある場合は、そのまま追加するだけです。 (b) しかし、多くの既存のディスク・モジュールはそのAPIをFatFsに合わせるため、グルー関数が必要になるでしょう。
+ + +必要なのはFatFsモジュールの要求するディスク関数を用意することだけで、それ以外にすることはありません。既に動作しているディスク・モジュールがあるなら、そのAPIをFatFsに合わせるかグルー関数を介してつなぐだけで済みますが、無い場合はほかから移植するか最初から書くかする必要があります。定義されている全ての関数が常に必要なわけではありません。例えば、リード・オンリー構成では書き込み系関数は必要ありません。次の表に構成オプションと要求される関数の対応を示します。
+必要な関数 | 必要となる条件 | 備考 |
---|---|---|
disk_status disk_initialize disk_read | 常時 | ffsample.zip (サンプル) その他web上に多数 |
disk_write get_fattime disk_ioctl (CTRL_SYNC) | _FS_READONLY == 0 | |
disk_ioctl (GET_SECTOR_COUNT) disk_ioctl (GET_BLOCK_SIZE) | _USE_MKFS == 1 | |
disk_ioctl (GET_SECTOR_SIZE) | _MAX_SS != _MIN_SS | |
disk_ioctl (CTRL_ERASE_SECTOR) | _USE_ERASE == 1 | |
ff_convert ff_wtoupper | _USE_LFN >= 1 | option/unicode.c |
ff_cre_syncobj ff_rel_grant ff_req_grant ff_del_syncobj | _FS_REENTRANT == 1 | option/syscall.c (サンプル) |
ff_mem_alloc ff_mem_free | _USE_LFN == 3 |
次の表にいくつかのターゲットにおけるメモリ使用量の例を示します。テスト時の構成オプションはその下の通りです。数値の単位はバイトで、Vはボリューム数、Fは同時オープン・ファイル数を示します。コンパイラの最適化オプションはコード・サイズとしています。
+ARM7 32bit | ARM7 Thumb | CM3 Thumb-2 | AVR | H8/300H | PIC24 | RL78 | V850ES | SH-2A | RX600 | IA-32 | |
---|---|---|---|---|---|---|---|---|---|---|---|
Compiler | GCC | GCC | GCC | GCC | CH38 | C30 | CC78K0R | CA850 | SHC | RXC | VC6 |
_WORD_ACCESS | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 1 |
text (Full, R/W) | 10675 | 7171 | 6617 | 13355 | 10940 | 11722 | 13262 | 8113 | 9048 | 6032 | 7952 |
text (Min, R/W) | 6727 | 4631 | 4331 | 8569 | 7262 | 7720 | 9088 | 5287 | 5800 | 3948 | 5183 |
text (Full, R/O) | 4731 | 3147 | 2889 | 6235 | 5170 | 5497 | 6482 | 3833 | 3972 | 2862 | 3719 |
text (Min, R/O) | 3559 | 2485 | 2295 | 4575 | 4064 | 4240 | 5019 | 2993 | 3104 | 2214 | 2889 |
bss | V*4 + 2 | V*4 + 2 | V*4 + 2 | V*2 + 2 | V*4 + 2 | V*2 + 2 | V*2 + 2 | V*4 + 2 | V*4 + 2 | V*4 + 2 | V*4 + 2 |
Work area (_FS_TINY == 0) | V*560 + F*550 | V*560 + F*550 | V*560 + F*550 | V*560 + F*544 | V*560 + F*550 | V*560 + F*544 | V*560 + F*544 | V*560 + F*544 | V*560 + F*550 | V*560 + F*550 | V*560 + F*550 |
Work area (_FS_TINY == 1) | V*560 + F*36 | V*560 + F*36 | V*560 + F*36 | V*560 + F*32 | V*560 + F*36 | V*560 + F*32 | V*560 + F*32 | V*560 + F*36 | V*560 + F*36 | V*560 + F*36 | V*560 + F*36 |
+FatFs R0.10a options: +_FS_READONLY 0 (R/W) or 1 (R/O) +_FS_MINIMIZE 0 (Full function) or 3 (Minimized function) +_USE_STRFUNC 0 (Disable string functions) +_USE_MKFS 0 (Disable f_mkfs function) +_USE_FORWARD 0 (Disable f_forward function) +_USE_FASTSEEK 0 (Disable fast seek feature) +_CODE_PAGE 932 (Japanese Shift-JIS) +_USE_LFN 0 (Disable LFN feature) +_MAX_SS 512 (Fixed sector size) +_FS_RPATH 0 (Disable relative path feature) +_FS_LABEL 0 (Disable volume label functions) +_VOLUMES V (Number of logical drives to be used) +_MULTI_PARTITION 0 (Single partition per drive) +_FS_REENTRANT 0 (Disable thread safe) +_FS_LOCK 0 (Disable file lock control) ++
次の表は構成オプションの設定値によりどの機能が削除されるかを示します。
+Function | _FS_MINIMIZE | _FS_READONLY | _USE_STRFUNC | _FS_RPATH | _FS_LABEL | _USE_MKFS | _USE_FORWARD | _MULTI_PARTITION | |||||||||||
0 | 1 | 2 | 3 | 0 | 1 | 0 | 1/2 | 0 | 1 | 2 | 0 | 1 | 0 | 1 | 0 | 1 | 0/1 | 2 | |
f_mount | |||||||||||||||||||
f_open | |||||||||||||||||||
f_close | |||||||||||||||||||
f_read | |||||||||||||||||||
f_write | x | ||||||||||||||||||
f_sync | x | ||||||||||||||||||
f_lseek | x | ||||||||||||||||||
f_opendir | x | x | |||||||||||||||||
f_closedir | x | x | |||||||||||||||||
f_readdir | x | x | |||||||||||||||||
f_stat | x | x | x | ||||||||||||||||
f_getfree | x | x | x | x | |||||||||||||||
f_truncate | x | x | x | x | |||||||||||||||
f_unlink | x | x | x | x | |||||||||||||||
f_mkdir | x | x | x | x | |||||||||||||||
f_chmod | x | x | x | x | |||||||||||||||
f_utime | x | x | x | x | |||||||||||||||
f_rename | x | x | x | x | |||||||||||||||
f_chdir | x | ||||||||||||||||||
f_chdrive | x | ||||||||||||||||||
f_getcwd | x | x | |||||||||||||||||
f_getlabel | x | ||||||||||||||||||
f_setlabel | x | x | |||||||||||||||||
f_forward | x | ||||||||||||||||||
f_mkfs | x | x | |||||||||||||||||
f_fdisk | x | x | x | ||||||||||||||||
f_putc | x | x | |||||||||||||||||
f_puts | x | x | |||||||||||||||||
f_printf | x | x | |||||||||||||||||
f_gets | x |
FatFsモジュールは、長いファイル名(LFN)をサポートします。ファイルに付けられた2つの異なる名前(短いファル名と長いファイル名)は、f_readdir()を除くファイル操作関数において透過です。デフォルト構成では、LFN機能はOFFになっています。LFN機能を有効にするには、_USE_LFNを1,2または3に設定し、option/unicode.cをプロジェクトに追加します。LFN機能は、加えてある程度のワーク・エリア(LFN操作バッファ)を必要とします。バッファ長は使用できるメモリに応じて_MAX_LFNオプションで構成されることができます。LFNの長さは最大255文字に達するので、LFN完全対応のためには_MAX_LFNは255に設定されるべきです。与えられたファイル名に対してバッファ長が不足した場合、ファイル関数はFR_INVALID_NAMEで失敗します。
+ファイル関数に再入を行う条件の下でLFN機能を使用する場合は、_USE_LFNは2または3に設定されなければなりません。この場合、ファイル関数はワーク・エリアを動的に確保(スタックまたはヒープ)します。バッファ・サイズは、(_MAX_LFN + 1) * 2バイトになるので、スタック等のサイズはそれを考慮した十分なサイズでなければなりません。
+コードページ | コードサイズ[bytes] |
---|---|
SBCS | +3721 |
932(Shift-JIS) | +62609 |
936(GBK) | +177797 |
949(Korean) | +139857 |
950(Big5) | +111497 |
LFN機能の上手な使い方は、それを使わないということです。実際、組み込み用途ではLFN機能がどうしても必要になるということはほとんど無いはずです。LFNを有効にすると、選択されたコード・ページに応じてモジュール・サイズが増大されます。右の表に各コード・ページにおけるLFNを有効にしたときのモジュール・サイズの違いを示します。特に、CJK地域では数万の文字が使われていますが、不幸なことにそれは巨大なOEM-Unicode相互変換テーブルを要求し、モジュール・サイズは劇的に増大されます。その結果、それらのコード・ページにおいてLFNを有効にしたFatFsモジュールは、AVRを含む殆どの8ビット・マイコンにインプリメントされることができません。
+LFN機能のハードルはそれだけではありません。マイクロソフト社はFATファイル・システムについていくつかの特許を保有しています。いずれもLFN機能の実装に関するもので、その利用に対して$0.25/unitのライセンス料を要求しています。このため、商用製品でLFN機能を利用するときは、最終仕向地によってはライセンスが必要になります。最近のFAT32ドライバの多くはLFN機能を含んでいるため、それらの使用に当たってライセンスが必要になりますが、FatFsではLFN機能を構成オプションで任意にON/OFFできるため、無効にしてライセンス問題を回避することもできます。
+CP932(Shift_JIS)でかつ非LFN構成のときは、拡張文字の小文字(2バイト英字・キリル文字・ギリシャ文字)に対して大文字変換を行わず、小文字のままSFNエントリに記録・検索されます(日本語MSDOS仕様)。このため、非LFN構成で全角小文字を含むファイルを作成すると、NT系Windowsでそのファイルを開けなくなります。LFN構成では大文字変換を行います(NT系Windows仕様)。
+FatFs API上におけるファイル名等の文字列データの入出力は、デフォルトではANSI/OEMコードで行われますが、これをUnicode(UTF-16)に切り替えることもできます(_LFN_UNICODEオプションで設定)。つまり、これはFatFsがLFN機能に完全対応していることを意味します。Unicodeのファイル名に関する詳細は、ファイル名を参照してください。
+互いに異なるボリュームに対するファイル操作はリエントラントで、常に同時平行に動作できます。同じボリュームに対してはデフォルトではリエントラントではありませんが、_FS_REENTRANTオプションでリエントラント(スレッド・セーフ)にすることはできます。この場合、OS依存の同期オブジェクト操作関数ff_cre_syncobj(), ff_del_syncobj(), ff_req_grant(), ff_rel_grant()もまたプロジェクトに追加されなければなりません。サンプル・コードと解説はoption/syncobj.cにあります。
+この場合、あるタスクがボリュームを使用中に他のタスクからそのボリュームに対するファイル関数が呼び出されると、そのアクセスは先のタスクがファイル関数を抜けるまでブロックされます。もし、待ち時間が_TIMEOUTで指定された期間を越すと、その関数はFR_TIMEOUTでアボートします。いくつかのRTOSではタイムアウト機能はサポートされないかも知れません。
+ひとつの例外がf_mount(), f_mkfs(), f_fdisk()にあります。これらの関数は同じボリューム(または関連する物理ドライブ)に対してリエントラントではありません。これらの関数を使用するときは、アプリケーション・レベルで排他制御しなければなりません。
+注: このセクションはFatFsモジュールそれ自体のリエントランシーについて説明しています。その下位のディスクI/Oモジュールのリエントランシーに関しては何の前提もありません。
+FatFsモジュールではデフォルトでは多重アクセス制御機能をサポートしていません。ファイルに対する多重アクセスは、そのアクセス・モードによって制限されます。一つのファイルに対する多重オープンは、それらが全てリード・モードのときに限って許可されます。書き込みモードを含む多重オープン、また開かれているファイルに対するリネームや削除を行ってはなりません。さもないと、そのボリュームのFAT構造が破壊される可能性があります。
+_FS_LOCKに1以上の値(値は同時に管理できるファイル数)をセットすることで多重アクセス制御機能が有効になり、ファイル単位のアクセス制御を自動で行うこともできます。この場合、上記のルールを破ったオープン・リネーム・削除を試みると、その関数はFR_LOCKEDで失敗します。また、_FS_LOCKを越える数のファイルやサブ・ディレクトリを同時にオープンしようとすると、FR_TOO_MANY_OPEN_FILESで失敗します。
+小規模な組込システムでのファイルの読み書きにおける効率の良いアクセスのため、アプリケーション・プログラマはFatFsモジュールの中でどのような処理が行われているか考慮すべきです。ストレージ上のデータはf_read()により次のシーケンスで転送されます。
+図1. セクタ・ミスアラインド・リード (ショート)
+
+
図2. セクタ・ミスアラインド・リード (ロング)
+
+
図3. セクタ・アラインド・リード
+
+
ファイルI/Oバッファはセクタの一部のデータを読み書きするためのセクタ・バッファを意味します。セクタ・バッファは、それぞれのファイル・オブジェクト内のプライベート・セクタ・バッファまたはファイル・システム・オブジェクト内の共有セクタ・バッファのどちらかです。バッファ構成オプションの_FS_TINYは、データ転送にどちらを使うかを決定します。タイニー・バッファ(1)が選択されるとデータ・メモリの消費はそれぞれのファイル・オブジェクトで512バイト減少されます。この場合、FatFsモジュールはファイル・データの転送とFAT/ディレクトリ・アクセスにファイル・システム・オブジェクト内のセクタ・バッファだけを使用します。タイニー・バッファの欠点は、セクタ・バッファにキャッシュされたFATデータがファイル・データの転送により失われ、クラスタ境界の毎にリロードされなければならないことです。でも、悪くない性能と少ないメモリ消費の視点から多くのアプリケーションに適するでしょう。
+図1はセクタの一部のデータがファイルI/Oバッファを経由で転送されることを示します。図2に示される長いデータの転送では、転送データの中間の1セクタまたはそれ以上のセクタにまたがる転送データがアプリケーション・バッファに直接転送されています。図3は転送データ全体がセクタ境界にアライメントされている場合を示しています。この場合、ファイルI/Oバッファは使用されません。直接転送においては最大の範囲のセクタがdisk_read()で一度に読み込まれますが、クラスタ境界を越えるマルチ・セクタ転送はそれが隣接であっても行われません。
+このように、セクタにアライメントしたファイルの読み書きへの配慮はバッファ経由のデータ転送を避け、読み書き性能は改善されるでしょう。その効果に加え、タイニー構成でキャッシュされたFATデータがファイル・データの転送によりフラッシュされず、非タイニー構成と同じ性能を小さなメモリ・フットプリントで達成できます。
+HDDなどのディスク・メディアとは異なり、SDCやCFCなどのフラッシュ・メモリ・メディアの性能を引き出すには、その特性を意識した制御が必要になります。
+フラッシュ・メモリ・メディアの書き込み速度はシングル・セクタ書き込みの時に最も低いものになり、一回のトランザクションで転送されるセクタ数が大きくなるほど書き込み速度は向上します。この効果はバス速度が高速になるほど顕著で、10倍以上の差が現れることも珍しくありません。テスト結果は、マルチ・セクタ書き込み(W:16K, 32 sectors)がシングル・セクタ書き込み(W:100, 1 sector)よりどの程度速いかを明確に示しています。大容量メディアほどシングル・セクタ書き込みが遅くなる点もまた重要です。書き込みトランザクションの回数はまた、メディアの寿命にも影響してきます。このため、アプリケーションはなるべく大きなブロック(クラスタ・サイズまたは2の累乗セクタ境界にアライメントした)で読み書きを行う必要があります。もちろん、アプリケーションからメディアに至る全てのレイヤがマルチ・セクタ転送に対応していないと意味がありません。残念ながら、既存のオープン・ソースのドライバの多くはマルチ・セクタ転送に未対応です。なお、FatFsモジュールおよびサンプル・ドライバはマルチ・セクタ転送に対応しています。
+通常のファイル消去では、記録されたデータに対して何らかの制御が行われるわけではなく、単にFAT上に未使用クラスタとして記録されているだけです。このため、ファイルが消去されたあともそれらは有効なメモリ・ブロックとしてフラッシュ・メモリ上に残ります。そこで、ファイルを消去するとき、占有していたデータ・セクタを明示的に消去(つまり未使用ブロックにする)することにより、メディア内の空きブロックを増やすことができます。これにより、次にそのブロックに書き込むときの消去動作が無くなり、書き込み性能が向上する可能性があります。また、ウェアレベリングに使えるブロックが増え、メディアの耐久性も向上するかも知れません。この機能を有効にするには、構成オプションの_USE_ERASEに1を設定します。これはフラッシュ・メモリ・メディアの内部動作に期待した制御なので、効果があるとは限りません。また、ファイル消去の時間が延びることも考慮に入れるべきです。
+ストレージ上のFAT構造を操作している途中で、停電、不正なメディアの取り外し、回復不能なデータ・エラー等の障害が発生すると、処理が中途半端な状態で中断され、その結果としてFATボリュームの構造が破壊される可能性があります。次にFatFsモジュールにおけるクリチカル・セクションと、その間の障害により起きうるエラーの状態を示します。
+赤で示したセクションを実行中に障害が発生した場合、クロス・リンクが発生して操作対象のファイル・ディレクトリが失われる可能性があります。黄色で示したセクションを実行中に障害が発生した場合、つぎのうちいずれかまたは複数の結果が生じる可能性があります。
+いずれも書き込み中や操作の対象でないファイルには影響はありません。これらのクリチカル・セクションは、ファイルを書き込みモードで開いている時間を最小限にするか、f_sync()を適宜使用することで図5のようにリスクを最小化することができます。
+FatFs APIの拡張的使用例です。有用なコードがあった場合は、随時追加していきます。。
+ +ソース・ファイルのヘッダにライセンス条件が記述されているので、利用の際はそれに従うこと。英語を読めない方のために以下に日本語訳を示しておきます。
+/*----------------------------------------------------------------------------/ +/ FatFs - FAT file system module R0.10b (C)ChaN, 2014 +/-----------------------------------------------------------------------------/ +/ FatFsモジュールは、小規模な組み込みシステム向けの汎用FATファイルシステム・ +/ モジュールです。これはフリー・ソフトウェアとして、教育・研究・開発のために +/ 以下のライセンス・ポリシーの下で公開されています。 +/ +/ Copyright (C) 2014, ChaN, all right reserved. +/ +/ * FatFsモジュールはフリー・ソフトウェアであり、また無保証です。 +/ * 用途に制限はありません。あなたの責任の下において、個人的・非営利的な +/ ものから商用製品の開発に及ぶ目的に使用・改変・再配布することができます。 +/ * ソース・コードを再配布するときは、上記の著作権表示を保持しなければなりません。 +/ +/-----------------------------------------------------------------------------/+
要するにFatFsはタダで自由に使えるということです。ソース・コードを再配布するときは、このブロックをそのまま保持しておくこと。このようにFatFsはBSDライクなライセンスとしていますが、一つ大きな違いがあります。特に組み込み用途での利用価値を高めるため、バイナリ形式(ソース・コードを含まない形式全て)での再配布については、条件は設けていません。その場合は、FatFsおよびそのライセンス文書についてはドキュメントに明記してもしなくてもかまいません。これは、一条項BSDライセンスと等価ということです。もちろんGNU GPLプロジェクトとも共存可能です。何らかの変更を加えて再配布する際は、矛盾しない他のライセンス(GNU GPLや修正BSDライセンスなど)に変更することも可能です。
+JgEfBNgύX܂B
++FRESULT f_chdir ( + const TCHAR* path /* [IN] fBNgւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_NOT_ENOUGH_CORE +
+e{[̃JgEfBNgύX܂BJgEfBNǵÃ{[̃}Eg삪sꂽƂA[gEfBNgɏݒ肳܂BJgEfBNǵAt@CEVXeEIuWFNgɕێ邽߁Ã{[gpSẴ^XNɑĉe^܂B
+_FS_RPATH == 1̂ƂɎgp\ƂȂ܂B
++ /* JgEhCũJgEfBNgύX ([gdir1) */ + f_chdir("/dir1"); + + /* hCu2̃JgEfBNgύX (efBNg) */ + f_chdir("2:.."); ++
JgEhCuύX܂B
++FRESULT f_chdrive ( + const TCHAR* path /* [IN] ΏۃhCuw肵܂ */ +); ++
+FR_OK, +FR_INVALID_DRIVE +
+JgEhCuύX܂BVXeN̏l̓hCu0łB̐ݒFatFsW[̐ÓIϐɋL^邽߁ASẴ^XNɑĉe^܂B
+_FS_RPATH == 1ŁA_VOLUMES > 1̂ƂɎgp\ƂȂ܂B
+t@C܂̓TuEfBNg̑ύX܂B
++FRESULT f_chmod ( + const TCHAR* path, /* [IN] IuWFNgւ̃|C^ */ + BYTE attr, /* [IN] ݒl */ + BYTE mask /* [IN] ύX}XN */ +); ++
l | Ӗ |
---|---|
AM_RDO | [hEI[ |
AM_ARC | A[JCu |
AM_SYS | VXe |
AM_HID | qhD |
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_WRITE_PROTECTED, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED, +FR_NOT_ENOUGH_CORE +
+_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂Ƃgp\łB
+
+ /* [hI[ZbgAA[JCuNAȂ͕ύXȂ */
+ f_chmod("file.txt", AM_RDO, AM_RDO | AM_ARC);
+
+t@C܂B
++FRESULT f_close ( + FIL* fp /* [IN] t@CEIuWFNgւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+t@C܂B炩݂̏̍sꂽt@C̏ꍇALbVꂽ([h/CgEobt@̃f[^AύXꂽFATfBNg)̓fBXNɏ߂܂BIƁÃt@CEIuWFNg͖ɂȂÃł܂B
+t@CEIuWFNgǂݏop[hŁA_FS_LOCKIvVIĂȂꍇ́At@CɃt@CEIuWFNgj邱Ƃł܂BA͏̌݊̓_Ő͂܂B
+SĂ̍\Ŏgp\łB
+fBNg܂B
++FRESULT f_closedir ( + DIR* dp /* [IN] fBNgEIuWFNgւ̃|C^ */ +); ++
+FR_OK, +FR_INT_ERR, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+fBNg܂BIƁÃfBNgEIuWFNg͖ɂȂÃł܂B
+_FS_LOCKIvVIĂȂꍇ́Ȁs킸ɃfBNgEIuWFNgj邱Ƃł܂BA͏̌݊̓_Ő͂܂B
+_FS_MINIMIZE <= 1̂Ƃgp\ɂȂ܂B
+Xg[WEfoCX܂B
++DSTATUS disk_initialize ( + BYTE pdrv /* [IN] hCuԍ */ +); ++
͖̊߂lƂăfBXNEXe[^XԂ܂BfBXNEXe[^X̏ڍׂɊւĂdisk_status()QƂĂB
+Xg[WEfoCXAf[^̓ǂݏȂǑSĂ̓삪\ȏԂɂ܂BƁA߂lSTA_NOINITtONA܂B
+AvP[V͂̊ĂяoĂ͂Ȃ܂BȂƁAFAT{[j\܂BG[ɂďKvȂƂ́Af_mount()gpĂBFatFsW[́A}EgɂAKvɉĂ̊Ăяo܂B
+ʓIȃf[^ǂݏȊÕXg[WEfoCX̂ɑlXȐs܂B
++DRESULT disk_ioctl ( + BYTE pdrv, /* [IN] hCuԍ */ + BYTE cmd, /* [IN] R}h */ + void* buff /* [I/O] f[^nobt@ */ +); ++
Xg[WEfoCX̎ނɂT|[gR}h͈قȂ܂AFatFsW[̂́A̔ėpR}ĥݎgpÃfoCXɈˑ͍s܂B
+R}h | |
---|---|
CTRL_SYNC | Xg[WEfoCX̃f[^ݏ܂BCgEobNELbVȂǂ݂ꍇ́A܂ĂȂf[^݂܂BfBAւ݂̏ꂼdisk_write()̓Ŋꍇ́ÃR}hɑĂ邱Ƃ͂܂B |
GET_SECTOR_COUNT | buff̎wDWORD^ϐɃhCȗZN^Ԃ܂Bf_mkfs()f_fdisk()ĂяoA쐬{[̃TCY肷邽߂Ɏgp܂B |
GET_SECTOR_SIZE | buff̎wWORD^ϐɃhCũZN^ETCYԂ܂BLl512A1024A2048܂4096łBZN^ETCYŒ\(_MAX_SS ==_MIN_SS)̂Ƃ͂̃R}h͎g邱Ƃ͂ȂAɂ̃ZN^ETCYœ삵ȂȂ܂B |
GET_BLOCK_SIZE | buff̎wDWORD^ϐɃtbVȄubNETCY(ZN^P)Ԃ܂B132768͈̔͂2̗ݏ̒lłȂȂ܂Bsȏꍇ܂̓tbVEȊÕfBAł1Ԃ܂Bf_mkfs()ł̂ݎgpA{[̃f[^̈͂̋EɃACg܂B |
CTRL_ERASE_SECTOR | tbVË̗̖IBbuff̎wDWORD^zɂ͏̈ {JnZN^,IZN^} w肵ČĂяo܂B_USE_ERASE1̂ƂANX^ƂɌĂяo܂B́AATAR}hEZbgTrimR}hƓŁA̋@\T|[gȂꍇ͉Kv͂܂B܂A߂l̓`FbNꂸASɍsȂƂĂFatFs̓ɂ͉e܂B |
FatFŝ̓foCXˑR}h[U`R}h͈؎gp܂AAvP[V牽炩̃foCX䂪sƕ֗ȂƂ܂BAvP[VŕWȊO̐䂪KvȂƂ́AKvɉă[U`R}hljėpƂ悢ł傤BɃR}h̗܂B
+R}h | |
---|---|
CTRL_FORMAT | fBA̕tH[}bgs܂BbuffNULLłȂƂAis\̂߂̃R[obÑAhX܂B |
CTRL_POWER_IDLE | foCXAChԂɂ܂Bʏ̓ǂݏvŃANeBuԂɖ߂ȂASTA_NOINITtOZbgKv͂܂B |
CTRL_POWER_OFF | foCXVbg_EԂɂ܂BSTA_NOINIT̓Zbg܂BfoCXdisk_initialize()ŃANeBuԂɖ߂܂B |
CTRL_LOCK | [Uɂ郁fBA̎o֎~܂B |
CTRL_UNLOCK | [Uɂ郁fBA̎o܂B |
CTRL_EJECT | fBAro܂BASTA_NOINITSTA_NODISKtO̓Zbg܂B |
MMC_GET_TYPE | J[hE^CvtO(b0:MMCv3, b1:SDv1, b2:SDv2+, b3:LBA)buff̎BYTEϐɓǂݏo܂B(MMC/SDJ[hp) |
MMC_GET_CSD | CSDWX^̓ebuff̎16oCg̃obt@ɓǂݏo܂B(MMC/SDJ[hp) |
MMC_GET_CID | CIDWX^̓ebuff̎16oCg̃obt@ɓǂݏo܂B(MMC/SDJ[hp) |
MMC_GET_OCR | OCRWX^̓ebuff̎4oCg̃obt@ɓǂݏo܂B(MMC/SDJ[hp) |
MMC_GET_SDSTAT | SD STATUSWX^̓ebuff̎64oCg̃obt@ɓǂݏo܂B(SDJ[hp) |
ATA_GET_REV | rWER[hbuff̎16oCg̃obt@ɓǂݏo܂B(ATA/CFJ[hp) |
ATA_GET_MODEL | fER[hbuff̎40oCg̃obt@ɓǂݏo܂B(ATA/CFJ[hp) |
ATA_GET_SN | VAԍbuff̎20oCg̃obt@ɓǂݏo܂B(ATA/CFJ[hp) |
[hEI[\ŁAZN^ETCYŒ\̂Ƃ́Å͕KvƂ܂B
+Xg[WEfoCXf[^ǂݏo܂B
++DRESULT disk_read ( + BYTE pdrv, /* [IN] hCuԍ */ + BYTE* buff, /* [OUT] ǂݏoobt@ւ̃|C^ */ + DWORD sector, /* [IN] ǂݏoJnZN^ԍ */ + UINT count /* [IN] ǂݏoZN^ */ +); ++
buffBYTE^Ȃ̂ŁAw肳AhXɃ[hEACgĂƂ͌܂BACgEAhXւ̓]́Aړ]ɂĔ邱Ƃ܂BAn[hEFA̐ł̂悤ȓ]s\ȂƂ́Åœi]Ȃǂĉ邩A܂͕ʂ̕@őΉȂȂ܂Bɂ̑Ή@܂(ꂩOK)B
+ʓIɁAZN^̓]v́AXg[WEfoCXɑĉ\Ȍ}`EZN^]ȂȂ܂B̃VOEZN^ǂݏoɕꂽꍇAX[vbgቺ邱Ƃ܂B
+Xg[WEfoCX̏Ԃ擾܂B
++DSTATUS disk_status ( + BYTE pdrv /* [IN] hCuԍ */ +); ++
Xg[WEfoCX̏Ԃ̃tȎgݍ킹lŕԂ܂B
+Xg[WEfoCXɃf[^݂܂B
++DRESULT disk_write ( + BYTE pdrv, /* [IN] hCuԍ */ + const BYTE* buff, /* [IN] ރf[^ւ̃|C^ */ + DWORD sector, /* [IN] ݊JnZN^ԍ */ + UINT count /* [IN] ރZN^ */ +); ++
buffɎw肳AhXɃ[hEACgĂƂ͌܂Bڍׂ́Adisk_read()̉QƂĂB
+ʓIɁAZN^̓]v́AfoCXɑĉ\Ȍ}`EZN^]ȂȂ܂B̃VOEZN^݂ɕꂽꍇAX[vbgቺ邱Ƃ܂B
+FatFs̓fBXNx@\Ƃz肵Ă܂B̊߂ƂAfoCXݒƂLbVɏ܂ꂽȂǁAKf[^݂̏ĂKv͂܂BAbuff̃f[^́Å߂ƖƂȂ܂B̗݊v́Adisk_ioctl()CTRL_SYNCR}hɂčs܂B̂悤Ȓx@\ꂽꍇAX[vbgɌコ邱Ƃł܂B
+AvP[V͂̊ĂяoĂ͂Ȃ܂BȂƁAFAT{[j\܂B
+[hEI[\(_FS_READONLY == 1)ł͕͂̊KvƂ܂B
+[h/CgE|C^t@CI[ɒBĂ邩ǂׂ܂B.
++int f_eof ( + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
[h/CgE|C^t@CI[ɂǂݏof[^Ȃꍇ́A0ȊO̒lԂ܂BȊO̎0Ԃ܂B
+f_eof́ArWł̓}NƂĎĂ܂B
+
+#define f_eof(fp) (((fp)->fptr) == ((fp)->fsize) ? 1 : 0)
+
+Ɏgp\B
+G[̗Lׂ܂B
++int f_error ( + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
t@C쒆̃G[ɂďfĂꍇ́A0ȊO̒lԂ܂BȊO̎0Ԃ܂B
+f_erroŕArWł̓}NƂĎĂ܂B
+
+#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
+
+Ɏgp\B
+ݎ擾܂B
++DWORD get_fattime (void); ++
݂̃[JE^CDWORDlɃpbNĕԂ܂BrbgEtB[h͎Ɏ悤ɂȂ܂B
+RTCT|[gȂVXełA_~[Ƃĉ炩̓tƂėLȒlԂׂłB0ȂǂԂꍇÃt@C̃^CX^v͖ɂȂ܂B
+[hEI[\(_FS_READONLY == 1)ł͕͂̊KvƂ܂B
+hCu܂B
++FRESULT f_fdisk ( + BYTE pdrv, /* [IN] hCuԍ */ + const DWORD part[], /* [IN] }bvEe[u */ + void* work /* [-] [NGA */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_NOT_READY, +FR_WRITE_PROTECTED, +FR_INVALID_PARAMETER +
+̊́Aw肳ꂽhCuMBRɋe[u쐬܂B敪͈ʓIFDISK`ōs߁Aő4̊{쐬邱Ƃł܂Bgɂ͑ΉĂ܂B}bve[upart[]ɂ̓hCuǂ̂悤ɕ邩w肵ēn܂B̔z4̍ڂ琬A擪̍ڂ1Ԗڂ́AŌ̍ڂ4Ԗڂ̋̃TCY܂Bl100ȉ̏ꍇAhCȗeʂɑ銄p[ZgPʂŎw肵܂B100l̏ꍇ̓ZN^̒ڎwɂȂ܂B
+_FS_READOLNY == 0 _USE_MKFS == 1 _MULTI_PARTITION == 1 ̂Ƃgp\łB
++ /* [U`̃{[Ǘe[u (_MULTI_PARTITION == 1 ̂ƂKv) */ + + PARTITION VolToPart[] = { + {0, 1}, /* _hCu 0 ==> hCu 0, 1 */ + {0, 2}, /* _hCu 1 ==> hCu 0, 2 */ + {1, 0} /* _hCu 2 ==> hCu 1, o */ + }; ++
+ /* VhCu(0)̏ */ + + FATFS fs; + DWORD plist[] = {50, 50, 0, 0}; /* 2 */ + BYTE work[_MAX_SS]; + + f_fdisk(0, plist, work); /* hCu 0 ̕ */ + + f_mount(&fs "0:", 0); + f_mkfs("0:", 0, 0); /* _hCu 0 ̃tH[}bg. ͖. */ + f_mount(0, "0:", 0); + + f_mount(&fs, "1:", 0); + f_mkfs("1:", 0, 0); /* _hCu 0 ̃tH[}bg. ͖. */ + f_mount(0, "1:", 0); + ++
FatFsモジュールでのファイル、ディレクトリ、ドライブの指定方法はDOS/Windows APIとほぼ同じです。パス名のフォーマットは次の通りです。
+"[論理ドライブ番号:][/]ディレクトリ名/ファイル名"+
FatFsモジュールは長いファイル名(LFN)および8.3形式ファイル名(SFN)に対応しています。LFNは、(_USE_LFN > 0)のとき使用可能になります。ディレクトリ・セパレータにはDOS/Windows APIと同じく'/'と'\'を使用します。連続したセパレータは無視され1個として扱われます。唯一の違いは、論理ドライブの指定だけです。論理ドライブ番号は、'0'~'9'の一文字の数字とコロンで指定し、省略した場合はデフォルト・ドライブ(0またはカレント・ドライブ)が選択されます。
+ヌル文字や制御文字('\0'~'\x1F')は、パス名の終端として認識されます。パス名に先行あるいは中に含まれるスペースは、LFN構成では名前の一部として有効ですが、非LFN構成ではスペースはパス名の終端として認識されます。
+標準構成(_FS_RPATH == 0)のときは、全てのオブジェクトがルート・ディレクトリから辿る絶対パスで指定されます。OS指向なカレント・ディレクトリという概念は無く、またドット・ディレクトリ("."や"..")は使用できません。パス名先頭のセパレータは無視されます。デフォルト・ドライブ番号は常に0になります。
+相対パスを有効(_FS_RPATH == 1)にしたときは、先行するセパレータの有無によって検索開始ディレクトリが変わり、セパレータがある場合はルート・ディレクトリから、無い場合はf_chdir()で設定されるカレント・ディレクトリからになります。またパス名にドット・ディレクトリが使用できます。デフォルト・ドライブ番号はf_chdrive()で設定された値となります。
+パス名の例 | _FS_RPATH == 0 | _FS_RPATH == 1 |
file.txt | ドライブ0のルート・ディレクトリ下のファイル | カレント・ドライブのカレント・ディレクトリ下のファイル |
/file.txt | ドライブ0のルート・ディレクトリ下のファイル | カレント・ドライブのルート・ディレクトリ下のファイル |
ドライブ0のルート・ディレクトリ | カレント・ドライブのカレント・ディレクトリ | |
2: | ドライブ2のルート・ディレクトリ | ドライブ2のカレント・ディレクトリ |
2:file1.txt | ドライブ2のルート・ディレクトリ下のファイル | ドライブ2のカレント・ディレクトリ下のファイル |
2:/ | ドライブ2のルート・ディレクトリ | ドライブ2のルート・ディレクトリ |
../file.txt | 無効 | 親ディレクトリ下のファイル |
. | 無効 | このディレクトリ |
.. | 無効 | カレント・ディレクトリの親ディレクトリ |
dir1/.. | 無効 | カレント・ディレクトリ |
/.. | 無効 | ルート・ディレクトリ(その上は辿れない) |
また、_STR_VOLUME_IDオプションを有効にすることでドライブ番号の識別には数字のほか、任意の文字列を使用することも可能になります。
+ファイル関数の入出力のうちファイル名やパス名を指定する引数の型は、TCHARで定義されていますが、これは通常はcharのエリアスになっています。そして、_CODE_PAGEで指定されるANSI/OEMコード(SBCSまたはDBCS)の文字列として扱われます。ファイル名入出力をUnicodeとする構成(_LFN_UNICODE == 1)にしたときは、TCHARはワイド文字(WCHAR, unsigned short)に切り替わり、パス名の入出力にUnicodeを使用するようになります。これによりLFN規格に完全対応となり、ANSI/OEMコードにない文字(たとえば ✝☪✡☸☭など)も使用できます。この設定は文字列入出力関数のデータ型とファイル上のエンコーディングにも影響を与えます。リテラル文字列を定義するとき、次に示すように_T(s)および_TEXT(s)マクロを使ってANSI/OEMとUnicodeを自動切り替えすることができます。
++ f_open(fp, "filename.txt", FA_READ); /* ANSI/OEM専用コード */ + f_open(fp, L"filename.txt", FA_READ); /* Unicode専用コード */ + f_open(fp, _T("filename.txt"), FA_READ); /* 両用コード(自動切り替え) */ ++
デフォルトの構成では、それぞれの論理ドライブは同じ番号の物理ドライブに1:1で結びつけられていて、自動検出機能によりその物理ドライブ上の一つのFATボリュームがマウントされます。FATボリュームの検出は、セクタ0、第一区画~第四区画の順に行われます。
+_MULTI_PARTITIONに1を指定すると、それぞれの論理ドライブに対して個別に物理ドライブ番号と区画を指定できるようになります。この構成では、論理ドライブと区画の対応を解決するためのテーブルを次に示すように定義する必要があります。
++例:論理ドライブ0~2を物理ドライブ0(非リムーバブル)の3つの基本区画に割り当て、 + 論理ドライブ3を物理ドライブ1(リムーバブル)に割り当てる場合。 + +PARTITION VolToPart[] = { + {0, 1}, /* 論理ドライブ 0 ==> 物理ドライブ 0, 第1区画 */ + {0, 2}, /* 論理ドライブ 1 ==> 物理ドライブ 0, 第2区画 */ + {0, 3}, /* 論理ドライブ 2 ==> 物理ドライブ 0, 第3区画 */ + {1, 0} /* 論理ドライブ 3 ==> 物理ドライブ 1, 自動検出 */ +}; + + ++
複数区画指定を使用する場合、次の点に注意しなければなりません。 +
t@Cf[^ǂݏoAMXg[ɒړ]܂B
++FRESULT f_forward ( + FIL* fp, /* [IN] t@CEIuWFNg\ */ + UINT (*func)(const BYTE*,UINT), /* [IN] f[^] */ + UINT btf, /* [IN] ]oCg */ + UINT* bf /* [OUT] ]ꂽoCg */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_DENIED, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+t@C̃f[^obt@ɓǂݏoɑMXg[ɒړ]܂BAvP[VŃf[^Eobt@KvƂȂ̂ŁǍꂽŗLłB[h/CgE|C^͓]ꂽoCgi݂܂Bw肳ꂽoCg̓]Ƀt@C̏I[ɒBꍇMXg[rW[ɂȂꍇA*bfbtfȂ܂B
+_USE_FORWARD == 1ŁA_FS_TINY == 1̂ƂɎgp\łB
++/*-----------------------------------------------------------------------*/ +/* f_forwardĂf[^M̗ */ +/*-----------------------------------------------------------------------*/ + +UINT out_stream ( /* ߂l: ]ꂽoCg܂̓Xg[̏ */ + const BYTE *p, /* ]f[^w|C^ */ + UINT btf /* >0: ]s(oCg). 0: Xg[̏Ԃׂ */ +) +{ + UINT cnt = 0; + + + if (btf == 0) { /* ZXv */ + /* Xg[̏ԂԂ (0: rW[, 1: fB) */ + /* UAfBԂA]vŏȂƂ1oCg */ + /* ]Ȃ f_forward FR_INT_ERR ƂȂB */ + if (FIFO_READY) cnt = 1; + } + else { /* ]v */ + do { /* SẴoCg]邩AXg[rW[ɂȂ܂ŌJԂ */ + FIFO_PORT = *p++; + cnt++; + } while (cnt < btf && FIFO_READY); + } + + return cnt; +} + + +/*-----------------------------------------------------------------------*/ +/* f_forward̎gp */ +/*-----------------------------------------------------------------------*/ + +FRESULT play_file ( + char *fn /* ĐI[fBIEt@Cw|C^ */ +) +{ + FRESULT rc; + FIL fil; + UINT dmy; + + /* t@Cǂݏo[hŊJ */ + rc = f_open(&fil, fn, FA_READ); + if (rc) return rc; + + /* SẴf[^]邩G[܂ő */ + while (rc == FR_OK && fil.fptr < fil.fsize) { + + /* ق̏... */ + + /* I܂͗vɉăf[^Xg[ɑo */ + rc = f_forward(&fil, out_stream, 1000, &dmy); + } + + /* t@CĖ߂ */ + f_close(&fil); + return rc; +} ++
JgEfBNg܂B
++FRESULT f_getcwd ( + TCHAR* buff, /* [OUT] obt@ */ + UINT len /* [IN] obt@ETCY */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+JgEhCũJgEfBNg̃tEpX擾܂B_VOLUMES2ȏ̂Ƃ́A_hCuԍ̕tꂽpXƂȂ܂B
+_FS_RPATH == 2̂Ƃgp\łB
+{[̋̈̃TCY擾܂B
++FRESULT f_getfree ( + const TCHAR* path, /* [IN] ΏۃhCuw肵܂ */ + DWORD* nclst, /* [OUT] NX^i[ϐւ̃|C^ */ + FATFS** fatfs /* [OUT] t@CEVXeEIuWFNgw|C^ւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT +
+_hCű̈̃TCYNX^PʂŎ擾܂BԂꂽt@CEVXeEIuWFNgcsizeoNX^̃ZN^Ă̂ŁAɃZN^Pʂ̋TCYvZł܂BFAT32{[ɂẮAFSINFȌۂ̋NX^ƓĂȂꍇAsmȒlԂ\܂B̖邽߁A_FS_NOFSINFOIvVŃ}Eg͕̏KtFATXL悤ɍ\邱Ƃł܂B
+_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂Ƃgp\łB
++ FATFS *fs; + DWORD fre_clust, fre_sect, tot_sect; + + + /* hCu1̃{[ƋNX^ */ + res = f_getfree("1:", &fre_clust, &fs); + if (res) die(res); + + /* SZN^ƋZN^vZ */ + tot_sect = (fs->n_fatent - 2) * fs->csize; + fre_sect = fre_clust * fs->csize; + + /* {[Ŝ̃TCYƋ̃TCY\ (512oCg/ZN^Ɖ) */ + printf("%10lu KiB total drive space.\n%10lu KiB available.\n", + tot_sect / 2, fre_sect / 2); ++
{[Ex擾܂B
++FRESULT f_getlabel ( + const TCHAR* path, /* [IN] ΏۃhCu */ + TCHAR* label, /* [OUT] {[i[obt@ */ + DWORD* vsn /* [OUT] {[EVAԍi[ϐ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT +
+_USE_LABEL == 1̂ƂɎgp\łB
++ char str[12]; + + /* ftHgEhCũ{[ */ + f_getlabel("", str, 0); + + /* hCu2̃{[ */ + f_getlabel("2:", str, 0); ++
t@C當ǂݏo܂B
++TCHAR* f_gets ( + TCHAR* buff, /* [OUT] obt@ */ + int len, /* [IN] obt@̃TCY */ + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
buffԂ܂B
+̊f_read()̃bp[łBǂݏóAŏ'\n'ǂݍނAt@CI[ɒB邩Alen - 1ǂݏo܂ő܂Bǂݍ܂ꂽ̏I[ɂ'\0't܂BɃt@CI[1ǂݍ܂ȂƂA܂͉炩̃G[Ƃ͎͊skE|C^Ԃ܂Bt@CI[G[f_eof(),f_error()}NŒׂ܂B
+Unicode API\(_LFN_UNICODE == 1)IĂƂ́AbuffUTF-16ɂȂ܂At@C̃GR[h́A_STRF_ENCODEIvVőIł܂BȊO͖̎ϊ(1oCg/1)œǂݏo܂B
+_USE_STRFUNC1܂2̂Ƃgp\łB2̂Ƃ́At@CɊ܂܂'\r'菜ăobt@ɓǂݍ܂܂B
+t@C̃[h/CgE|C^ړ܂B܂AV[N@\gpɂCLMT(q)̍쐬ɂgp܂B
++FRESULT f_lseek ( + FIL* fp, /* [IN] t@CEIuWFNg\̂ւ̃|C^ */ + DWORD ofs /* [IN] ړItZbg */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+t@C̃[h/CgE|C^(ɓǂݏoE݂oCg̃ItZbg)ړ܂BItZbǧ_̓t@C擪łB݃[hŃt@CETCY傫Ȓlw肷ƁA܂Ńt@CETCYgAgꂽ̃f[^͖`ƂȂ܂Bf[^xɏ݂Ƃ́A\߂̊ŕKvȃTCY܂Ńt@CETCYgĂƗǂł傤Bf_lseek()IƂ́A[h/CgE|C^ړ`FbNׂłB[h/CgE|C^w菬Ƃ́Ǎl܂B
+_USE_FASTSEEK1ŁAt@CEIuWFNgcltbloNULLȊO(f_open()NULLɐݒ肳)̂ƂAV[NE[hɂȂ܂B̓t@C̃NX^zu(CLMT)ɕێĂƂɂAFATɃANZX邱ƂȂV[NOEV[Nɍs@\łBV[NE[h́Af_read()/f_wtite()̓ɂKp܂BV[NE[hłf_wtite()/f_lseek()ɂt@CETCY̊g͂ł܂B
+V[NsOɁACLMT쐬ĂKv܂B쐬ɂ́A܂CLMTi[obt@(DWORD^z)Acltbloɂ̃|C^Zbg܂BāAz̐擪vfɂ̔z̃TCY(vf)Af_lseek()ofsCREATE_LINKMAPw肵ČĂяo܂BCLMT쐬Aȍ~f_read()/f_write()/f_lseek()łFATւ̃ANZX͔܂BFR_NOT_ENOUGH_COREŎsƂ͔zTCYsŁA擪vfɂ͎ۂɕKvƂȂvfԂ܂BKvȗvf́A(t@C̕ + 1) * 2 łBƂAt@C5̃tOgɕfĂƂɕKvȗvf́A12ƂȂ܂B
+_FS_MINIMIZE < 3̂Ƃgp\łB
++ /* t@CJ */ + fp = malloc(sizeof (FIL)); + res = f_open(fp, "file.dat", FA_READ|FA_WRITE); + if (res) ... + + /* t@CEItZbg5000ֈړ */ + res = f_lseek(fp, 5000); + + /* t@CI[ֈړ(t@CNjL̏) */ + res = f_lseek(fp, f_size(fp)); + + /* 3000oCgi߂ */ + res = f_lseek(fp, f_tell(fp) + 3000); + + /* 2000oCg߂ (bvAEhɒ) */ + res = f_lseek(fp, f_tell(fp) - 2000); ++
+/* NX^s蓖 (Xg[~OECg̃obt@EI[o[h~) */ + + res = f_open(fp, "record.wav", FA_CREATE_NEW | FA_WRITE); /* t@C쐬 */ + + res = f_lseek(fp, MAX_SIZE); /* \ȃNX^̐s蓖 */ + if (res || f_tell(fp) != PRE_SIZE) ... /* t@Cgꂽ`FbN */ + + res = f_lseek(fp, DATA_START); /* f[^EXg[̋L^(AP[VfBC) */ + ... + + res = f_truncate(fp); /* sv̈̐̂ */ + res = f_lseek(fp, 0); /* wb_̋L^ */ + ... + + res = f_close(fp); ++
+/* V[N@\g */ + + DWORD clmt[SZ_TBL]; /* NE}bvEe[ui[obt@ */ + + res = f_lseek(fp, ofs1); /* ʏV[N (I[vAcltblNULLɏ) */ + + fp->cltbl = clmt; /* V[N@\̗L */ + clmt[0] = SZ_TBL; /* 擪vfɔzvfZbg */ + res = f_lseek(fp, CREATE_LINKMAP); /* CLMT̍쐬 */ + ... + + res = f_lseek(fp, ofs2); /* ȍ~Af_read/f_write/f_lseekFATANZX͔Ȃ */ ++
fBNg쐬܂B
++FRESULT f_mkdir ( + const TCHAR* path /* [IN] 쐬fBNgւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_EXIST, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+̃fBNg쐬܂B
+_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂Ƃgp\łB
++ res = f_mkdir("sub1"); + if (res) die(res); + res = f_mkdir("sub1/sub2"); + if (res) die(res); + res = f_mkdir("sub1/sub2/sub3"); + if (res) die(res); ++
_hCuFAT{[쐬(tH[}bg)܂B
++FRESULT f_mkfs ( + const TCHAR* path, /* [IN] _hCuԍ */ + BYTE sfd, /* [IN] 쐬@ */ + UINT au /* [IN] NXE^TCY */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_NOT_READY, +FR_NOT_ENABLED, +FR_MKFS_ABORTED, +FR_INVALID_PARAMETER +
+hCuFAT{[쐬܂BFDISK`w肳ꂽꍇ́AhCuŜ߂{(p[e[V)쐬A̒FAT{[쐬܂BSFD`ł́AFAT{[hCu̐擪ZN^x^ō쐬܂B
+tH[}bg_hCu}`Ep[e[V@\(_MULTI_PARTITION)ɂē̋(1`4)ɌѕtĂꍇ́A̋̒FAT{[쐬܂Bsfd͖A܂Ή镨hCu͂ɐ旧Af_fdisk()܂͑̃c[œKɋݒ肳ĂKv܂B
+p[e[V`ɂ́AFDISK`SFD`̓ʂ肠܂BFDISK`́An[hfBXNAMMCASDCACFCAU DiskȂǂŕWIɎgp܂BFDISK`ł͈̕hCuɈ܂͕̋쐬邱Ƃł܂BǗMBR(hCu̐擪ZN^)ɋL^܂BSFD`͒Pɉ̕sȂ`ŁA{[͕hCu̐擪ZN^Jn܂BSFD`́Atbs[EfBXNA}CNhCuAwfBXNAт̑X[p[Etbs[EfBAŕWIɎgpĂ܂B
+FAT^Cv(FAT12/FAT16/FAT32)́Ã{[NX^ɂĂ̂錈܂[FATdl]ɂȂĂāAȊO̗v͂܂BāAǂFAT^CvɂȂ邩̓{[ETCYƃNX^ETCYɈˑ܂BNX^ETCY͑傫قǐ\オ܂B
+NX^FAT^Cv̋Eɋ߂ȂƂ́AFR_MKFS_ABORTEDŊs\܂B
+_FS_READONLY == 0ŁA_USE_MKFS == 1̂Ƃgp\łB
+_hCuɃt@CEVXeEIuWFNgo^E܂B
++FRESULT f_mount ( + FATFS* fs, /* [IN] t@CEVXeEIuWFNg */ + const TCHAR* path, /* [IN] _hCuԍ */ + BYTE opt /* [IN] IvV */ +); ++
+FR_OK, +FR_INVALID_DRIVE, +FR_DISK_ERR, +FR_NOT_READY, +FR_NO_FILESYSTEM +
+FatFsW[ł́Aꂼ̘_hCut@CEVXeEIuWFNgƂ[NEGAKvłB̊͘_hCuɃt@CEVXeEIuWFNgo^薕肵܂B炩̃t@CgpOɁÅł̘_hCũt@CEVXeEIuWFNg^ĂȂȂ܂BfsɃkE|C^w肷ƁA̘_hCũt@CEVXeEIuWFNg̓o^͖邾łBo^ꂽt@CEVXeEIuWFNg͉̃ł܂BΏۂ̘_hCuɊJĂt@CfBNgꍇA͑SĖɂȂ܂B͎̊̓̂悤ȏɍs܂B
+opt0w肷ƁA}Eg(hCȕAFAT{[̌ABPB͂t@CEVXeEIuWFNg)͍sꂸA͕hCȕԂɊւ炸ɐ܂Bł͉ʃCւ̃ANZX͔Aw肳ꂽt@CEVXeEIuWFNgNA()ÃAhXzɓo^邾łBPɓo^ς݂̃t@CEVXeEIuWFNgNAړIɂg܂BāAă{[ւ̃ANZXsꂽƂ̂ꂩł^̏ꍇ́Aۂ̃}Eg삪s܂B
+opt1w肷ƁAo^ɑă}Eg삪s܂B}EgɎsƑΉG[Ԃ܂Ao^͗LȂ̂ő{[ւ̃ANZXōĂу}Eg삪s܂B
+
ʃC̎チfBǍoT|[gȂ(disk_status()ɔfȂ)Ƃ́AAvP[V̓fBǍケ̊Ńt@CEVXeEIuWFNgIɃNAčă}Egɍs悤ɂKv܂B
+SĂ̍\Ŏgp\łB
+t@CI[v܂͍쐬܂B
++FRESULT f_open ( + FIL* fp, /* [OUT] ̃t@CEIuWFNg\̂ւ̃|C^ */ + const TCHAR* path, /* [IN] t@Cւ̃|C^ */ + BYTE mode /* [IN] [htO */ +); ++
l | Ӗ |
---|---|
FA_READ | ǂݏo[hŊJ܂BǂݏꍇFA_WRITEƋɎw肵܂B |
FA_WRITE | ݃[hŊJ܂BǂݏꍇFA_READƋɎw肵܂B |
FA_OPEN_EXISTING | ̃t@CJ܂Bt@CƂ̓G[ɂȂ܂B(ftHg) |
FA_OPEN_ALWAYS | ̃t@CJ܂Bt@CƂ̓t@C쐬܂BNjL̏ꍇ́A̕@ŃI[vAf_lseek()Ńt@C̍ŌɈړĂB |
FA_CREATE_NEW | t@C쐬܂B̃t@Cꍇ́AFR_EXISTŎs܂B |
FA_CREATE_ALWAYS | t@C쐬܂B̃t@Cꍇ́ATCY0ɂĂJ܂B |
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_EXIST, +FR_INVALID_OBJECT, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED, +FR_NOT_ENOUGH_CORE, +FR_TOO_MANY_OPEN_FILES +
+̃t@CJAVt@C쐬܂Bƃt@CEIuWFNg쐬Aȍ~̃t@CɑANZXɎgp܂Bt@CƂ́Af_close()gp܂B炩̕ύXsꂽt@Č㐳ȂꍇÃt@Cjꍇ܂B
+ɊJĂt@CJKvꍇ́AdANZXQƂĂBÃt@Cɑ鏑݃[h܂ޏdI[v͏ɋ֎~łB
+t@CEANZXJnOɁAf_mount()gĂꂼ̘_hCuɃ[NEGA(t@CEVXeEIuWFNg)^Kv܂B̏̌A̘_hCuɑđSẴt@Cg悤ɂȂ܂B
+SĂ̍\Ŏgp\łB_FS_READONLY == 1̂Ƃ́AFA_WRITE, FA_CREATE_ALWAYS, FA_CREATE_NEW, FA_OPEN_ALWAYS̊etO̓T|[g܂B
++/* eLXgt@Cǂݏoĕ\ */ + +FATFS FatFs; /* _hCũ[NEGA(t@CEVXeEIuWFNg) */ + +int main (void) +{ + FIL fil; /* t@CEIuWFNg */ + char line[82]; /* sobt@ */ + FRESULT fr; /* ߂l */ + + + /* ftHgEhCuɃ[NGA^ */ + f_mount(&FatFs, "", 0); + + /* eLXgEt@CJ */ + fr = f_open(&fil, "message.txt", FA_READ); + if (fr) return (int)fr; + + /* 1sǂݏoĕ\ */ + while (f_gets(line, sizeof line, &fil)) + printf(line); + + /* t@C */ + f_close(&fil); + + return 0; +} ++
+/* hCu1̃t@C "file.bin" hCu0փRs[ */ + +int main (void) +{ + FATFS fs[2]; /* _hCũ[NGA(t@CEVXeEIuWFNg) */ + FIL fsrc, fdst; /* t@CEIuWFNg */ + BYTE buffer[4096]; /* File copy buffer */ + FRESULT fr; /* FatFs function common result code */ + UINT br, bw; /* File R/W count */ + + /* hCu0,1Ƀ[NEGA^ */ + f_mount(&fs[0], "0:", 0); + f_mount(&fs[1], "1:", 0); + + /* hCu1̃Rs[t@CJ */ + res = f_open(&fsrc, "1:file.dat", FA_OPEN_EXISTING | FA_READ); + if (fr) return (int)fr; + + /* hCu0ɃRs[t@C쐬 */ + res = f_open(&fdst, "0:file.dat", FA_CREATE_ALWAYS | FA_WRITE); + if (fr) return (int)fr; + + /* Rs[Rs[Ƀf[^] */ + for (;;) { + res = f_read(&fsrc, buffer, sizeof buffer, &br); /* Rs[炩ǂݏo */ + if (res || br == 0) break; /* G[t@CI[ */ + res = f_write(&fdst, buffer, br, &bw); /* Rs[ɏ */ + if (res || bw < br) break; /* G[fBXNt */ + } + + /* SẴt@C */ + f_close(&fsrc); + f_close(&fdst); + + /* [NEGAJ */ + f_mount(NULL, "0:", 0); + f_mount(NULL, "1:", 0); + + return (int)fr; +} ++
fBNgJ܂B
++FRESULT f_opendir ( + DIR* dp, /* [OUT] fBNgEuWFNg\̂ւ̃|C^ */ + const TCHAR* path /* [IN] fBNgւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_PATH, +FR_INVALID_NAME, +FR_INVALID_OBJECT, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE, +FR_TOO_MANY_OPEN_FILES +
+ +fBNgJ܂BIA쐬ꂽDIR\̂gẴfBNg̍ڂǂݏo܂B
+_FS_MINIMIZE <= 1̂Ƃgp\ɂȂ܂B
+t@Cɏ݂܂B
++int f_printf ( + FIL* fp, /* [IN] t@CEIuWFNg */ + const TCHAR* fmt, /* [IN] 䕶 */ + ... +); ++
ɏ܂ƁA܂ꂽԂ܂BfBXNt܂͂̑G[ɂ萳ɏ܂ȂƂ́A͎sEOF (-1)Ԃ܂B
+̊́Af_putc()f_puts()̃bp[łB@\CWCũTuZbgƂȂĂāA䕶͎Ɏ̂gp\łB
+_FS_READONLY == 0ŁA_USE_STRFUNC1܂2̂Ƃgp\ɂȂ܂B2̎́Ao͂Ɋ܂܂'\n''\r'+'\n'ɓWJăt@Cɏ܂܂B
+APIUnicodeI(_LFN_UNICODE1)ĂƂ́AfmtUnicodeɂȂ܂At@C̃GR[h́A_STRF_ENCODEIvVőIł܂BȊO͖̎ϊ(1oCg/1)ŏ݂܂B
++ f_printf(&fil, "%d", 1234); /* "1234" */ + f_printf(&fil, "%6d,%3d%%", -200, 5); /* " -200, 5%" */ + f_printf(&fil, "%ld", 12345L); /* "12345" */ + f_printf(&fil, "%06d", 25); /* "000025" */ + f_printf(&fil, "%06d", -25); /* "000-25" */ + f_printf(&fil, "%-6d", 25); /* "25 " */ + f_printf(&fil, "%u", -1); /* "65535" or "4294967295" */ + f_printf(&fil, "%04x", 0xAB3); /* "0ab3" */ + f_printf(&fil, "%08LX", 0x123ABCL); /* "00123ABC" */ + f_printf(&fil, "%016b", 0x550F); /* "0101010100001111" */ + f_printf(&fil, "%s", "String"); /* "String" */ + f_printf(&fil, "%8s", "abc"); /* " abc" */ + f_printf(&fil, "%-8s", "abc"); /* "abc " */ + f_printf(&fil, "%c", 'a'); /* "a" */ + f_printf(&fil, "%f", 10.0); /* _͖T|[g */ ++
t@Cɕ݂܂B
++int f_putc ( + TCHAR chr, /* [IN] ޕ */ + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
ɏ܂ƏԂ܂BfBXNt܂̓G[ɂ菑܂ȂƂEOF (-1)Ԃ܂B
+APIUnicodeI(_LFN_UNICODE1)ĂƂ́AchrUTF-16ɂȂ܂At@C̃GR[h́A_STRF_ENCODEIvVőIł܂BȊO͖̎ϊ(1oCg/1)ŏ݂܂B
+1t@Cɏ݂܂B̊f_write()̃bp[łB
+_FS_READONLY == 0ŁA_USE_STRFUNC 1܂ 2̂Ƃgp\łB2w肷ƁA'\n''\r'+'\n'ɓWJăt@Cɏ܂܂B
+t@Cɕ݂܂B
++int f_puts ( + const TCHAR* str, /* [IN] ޕ */ + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
ɏ܂ƁA܂ꂽԂ܂BfBXNt܂̓G[ɂ菑݂fꂽƂEOF (-1)Ԃ܂B
+APIUnicodeI(_LFN_UNICODE1)ĂƂ́AstrUTF-16ɂȂ܂At@C̃GR[h́A_STRF_ENCODEIvVőIł܂BȊO͖̎ϊ(1oCg/1)ŏ݂܂B
+t@Cɏ݂܂B̊f_write()̃bp[łB
+_FS_READONLY == 0ŁA_USE_STRFUNC1܂2̂Ƃgp\łB2w肷ƁAɊ܂܂'\n''\r'+'\n'ɓWJăt@Cɏ܂܂B
+FatFsのAPIでは、一部の関数を除き結果に応じた共通のリザルト・コード(FRESULT型(enum))を返します。関数が成功した場合は0を返します。失敗した場合は0以外の値を返し、値はエラーの種類を示します。
+ +t@Cf[^ǂݏo܂B
++FRESULT f_read ( + FIL* fp, /* [IN] t@CEIuWFNg\ */ + void* buff, /* [OUT] ǂݏof[^i[obt@ */ + UINT btr, /* [IN] ǂݏooCg */ + UINT* br /* [OUT] ǂݏoꂽoCg */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_DENIED, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+ǂݍ݊JnʒúÃ݂[h/CgE|C^ɂȂ܂B[h/CgE|C^͓ǂݍ܂ꂽoCgi݂܂BÍA*br̒l`FbNׂłB*brbtrƂ́AǂݍݒɃt@C̏I[ɒBƂĂ܂B
+SĂ̍\Ŏgp\łB
+fBNgڂǂݏo܂B
++FRESULT f_readdir ( + DIR* dp, /* [IN] fBNgEuWFNg\̂ւ̃|C^ */ + FILINFO* fno /* [OUT] t@C\̂ւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+fBNg̍(t@CƃfBNg)ǂݏo܂B̊JԂs邱Ƃɂ肻̃fBNg̑SĂ̍ڂǂݏoƂł܂Bt@C̏ڍׂɂĂ FILINFO\̂QƂĂBSĂ̍ڂǂݏoAǂݏoڂƂ́Afname[]oɃkԂ܂BfnoɃkE|C^w肷ƁÃfBNg̃[hECfbNX擪Ɋ߂܂B܂Å͎Ɏ悤Ɋ֘A\IvVɂ蓮삪ς܂B
+hbgEGg("."A"..")́ApXLȂƂ(_FS_RPATH >= 1)ɂ̂ݏo͂Ɍ܂B
+LFN@\LȎ́Å̌Ăяoɐ旧FILINFO\̂lfnamelfsizeLȒlŏĂȂȂ܂BlfnameLFNi[obt@ŁAlfsize͂̃obt@̗vfłBLFNǂݏoKvȂƂ́AlfnameɃkE|C^ZbgĂB̏ɈłYꍇ́ALFNi[obt@ɃkԂ܂B
+܂AfBNgڂLFN݂Ȃꍇ́Afname[]ɉp܂܂ꍇ܂B
+_FS_MINIMIZE <= 1̂ƂɎgp\łB
++FRESULT scan_files ( + char* path /* Jnm[h ([NGAƂĂgp) */ +) +{ + FRESULT res; + FILINFO fno; + DIR dir; + int i; + char *fn; /* Unicode\z */ +#if _USE_LFN + static char lfn[_MAX_LFN + 1]; + fno.lfname = lfn; + fno.lfsize = sizeof lfn; +#endif + + + res = f_opendir(&dir, path); /* fBNgJ */ + if (res == FR_OK) { + i = strlen(path); + for (;;) { + res = f_readdir(&dir, &fno); /* fBNgڂ1ǂݏo */ + if (res != FR_OK || fno.fname[0] == 0) break; /* G[܂͍ږ̂Ƃ͔ */ + if (fno.fname[0] == '.') continue; /* hbgGg͖ */ +#if _USE_LFN + fn = *fno.lfname ? fno.lfname : fno.fname; +#else + fn = fno.fname; +#endif + if (fno.fattrib & AM_DIR) { /* fBNg */ + sprintf(&path[i], "/%s", fn); + res = scan_files(path); + if (res != FR_OK) break; + path[i] = 0; + } else { /* t@C */ + printf("%s/%s\n", path, fn); + } + } + f_closedir(&dir); + } + + return res; +} ++
t@C܂̓TuEfBNg̖O̕ύX܂͈ړ܂B
++FRESULT f_rename ( + const TCHAR* old_name, /* [IN] ÂIuWFNg */ + const TCHAR* new_name /* [IN] VIuWFNg */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_EXIST, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED +
+t@C܂̓TuEfBNg̖OύX܂B܂Aɕʂ̃fBNgւ̈ړ\łAقȂhCuւ̈ړ͂ł܂BJĂIuWFNgɑĎgpĂ͂Ȃ܂B
+_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂ƂɎgp\łB
++ /* t@C܂̓TufBNg̖OύX */ + f_rename("oldname.txt", "newname.txt"); + + /* t@C܂̓TufBNg̖O̕ύXƕʂ̃fBNgւ̈ړ */ + f_rename("oldname.txt", "dir1/newname.txt"); ++
DIR\̂́Af_opendir(), f_readdir()̃[NEGAƂĎgp܂BAvP[V́A̍\̂̃oĂ͂Ȃ܂B
++typedef struct { + FATFS* fs; /* et@CEVXeEIuWFNgւ̃|C^ */ + WORD id; /* et@CEVXeEIuWFNg̃}EgID */ + WORD index; /* ɌJnfBNgECfbNXԍ */ + DWORD sclust; /* e[uJnNX^ (0:[g) */ + DWORD clust; /* ݂̃NX^ԍ */ + DWORD sect; /* ݂̃ZN^ԍ */ + BYTE* dir; /* ݂SFNGgւ̃|C^ */ + BYTE* fn; /* SFNobt@ւ̃|C^ (in/out) {file[8],ext[3],status[1]} */ +#if _FS_LOCK + UINT lockid; /* bNID */ +#endif +#if _USE_LFN + WCHAR* lfn; /* LFNobt@ւ̃|C^ (in/out) */ + WORD lfn_idx; /* LFNGg̐擪CfbNX (0xFFFF:) */ +#endif +} DIR; ++
{[Ƀ{[Exݒ肵܂B
++FRESULT f_setlabel ( + const TCHAR* label /* [IN] ݒ肷{[Exւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_NAME, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT +
+̐擪ɃhCuԍ܂ޏꍇ́A̘_hCuɑĐݒ肳܂B܂܂Ȃꍇ́AftHgEhCuɐݒ肳܂B{[Ex폜Ƃ́Akw肵܂B{[Ex̃tH[}bǵAt@C(SFN)ƂقړłA̓_قȂ܂B
+_FS_READONLY == 0ŁA_USE_LABEL == 1̂ƂɎgp\łB
++ /* ftHgEhCuɃ{[Exݒ */ + f_setlabel("DATA DISK"); + + /* hCu2Ƀ{[Exݒ */ + f_setlabel("2:DISK 3 OF 4"); + + /* hCu2̃{[Ex폜 */ + f_setlabel("2:"); ++
FATFS\(t@CEVXeEIuWFNg)́AX̘_hCũ_Ci~bNE[NEGAێAf_mount()FatFsW[ɓo^܂Bs^C~ÓAf_mount()(}Egw)̎s܂̓fBǍ̍ŏ̃t@CEANZX̎łBAvP[V́A̍\̂̃oĂ͂Ȃ܂B
+ ++typedef struct { + BYTE fs_type; /* FAT^Cv */ + BYTE drv; /* hCuԍ */ + BYTE csize; /* NX^̃ZN^ (1,2,4,8,...,128)*/ + BYTE n_fats; /* FAT̑d (1,2) */ + BYTE wflag; /* win[]_[eBEtO */ + BYTE fsi_flag; /* FSINFOtO (b7:Disabled, b0:Dirty)*/ + WORD id; /* t@CEVXeE}EgID */ + WORD n_rootdir; /* [gEfBNg̃Gg (FAT12/16) */ +#if _MAX_SS != _MIN_SS + WORD ssize; /* ZN^ETCY (512, 1024, 2048 or 4096) */ +#endif +#if _FS_REENTRANT + _SYNC_t sobj; /* IuWFNgID */ +#endif +#if !_FS_READONLY + DWORD last_clust; /* FSINFO: ŌɊ蓖ĂꂽNX^ԍ */ + DWORD free_clust; /* FSINFO: NX^ */ +#endif +#if _FS_RPATH + DWORD cdir; /* JgEfBNg̃NX^ (0:[g) */ +#endif + DWORD n_fatent; /* FATGg (NX^ + 2) */ + DWORD fsize; /* FAT 1̃ZN^ */ + DWORD volbase; /* {[JnZN^ */ + DWORD fatbase; /* FAT̈JnZN^ */ + DWORD dirbase; /* [gEfBNg̈JnZN^(NX^) */ + DWORD database; /* f[^̈JnZN^ */ + DWORD winsect; /* win[]ɌĂZN^ԍ */ + BYTE win[_MAX_SS]; /* fBXNEANZXEEBhE */ +} FATFS; ++
FIL\(t@CEIuWFNg)́Af_open()ŏAȌセ̃t@C̏Ԃێ܂B܂Af_close()Ńt@CƖ܂BAvP[V́A̍\̂̃oĂ͂Ȃ܂(cltbl͗O)B^Cj[\ł́AɃZN^Eobt@mۂ̂ŁALTCYɒӂKvłB
+ ++typedef struct { + FATFS* fs; /* et@CEVXeEIuWFNgւ̃|C^ */ + WORD id; /* et@CEVXeEIuWFNg̃}EgID */ + BYTE flag; /* t@CEXe[^XEtO */ + BYTE err; /* G[ftO */ + DWORD fptr; /* t@Cǂݏ|C^ (t@C擪̃oCgEItZbg) */ + DWORD fsize; /* t@CETCY(oCgP) */ + DWORD sclust; /* t@CJnNX^ԍ (0:蓖Ė) */ + DWORD clust; /* ݂̃NX^ */ + DWORD dsect; /* ݂̃f[^EZN^ */ +#if !_FS_READONLY + DWORD dir_sect; /* ̃t@C̃fBNgEGĝZN^ */ + BYTE* dir_ptr; /* ̃t@C̃fBNgւ̃|C^ */ +#endif +#if _USE_FASTSEEK + DWORD* cltbl; /* t@C̃NX^ENւ̃|C^ (I[vNULLZbg) */ +#endif +#if _FS_LOCK + UINT lockid; /* t@CEbNID */ +#endif +#if !_FS_TINY + BYTE buf[_MAX_SS]; /* t@CEvCx[gEf[^]obt@ */ +#endif +} FIL; ++
FILINFO\̂́Af_stat(), f_readdir()ŕԂt@Cێ܂B
++typedef struct { + DWORD fsize; /* t@CETCY */ + WORD fdate; /* ŌɍXVꂽt */ + WORD ftime; /* ŌɍXVꂽ */ + BYTE fattrib; /* Agr[g */ + TCHAR fname[13]; /* Zt@C (8.3tH[}bg) */ +#if _USE_LFN + TCHAR* lfname; /* t@C̃obt@ւ̃|C^ */ + int lfsize; /* t@C̃obt@̃TCY [] */ +#endif +} FILINFO; ++
t@C̃TCY擾܂B
++DWORD f_size ( + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
oCgPʂ̃t@CETCYԂ܂B
+f_sizéArWł̓}NƂĎĂ܂B
+
+#define f_size(fp) ((fp)->fsize)
+
+Ɏgp\B
+t@C܂̓TuEfBNgׁ݂̑A܂̏擾܂B
++FRESULT f_stat ( + const TCHAR* path, /* [IN] IuWFNgւ̃|C^ */ + FILINFO* fno /* [OUT] t@C\̂ւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+w肳ꂽt@C܂̓TuEfBNgׂ݂̑܂B݂Ȃꍇ́AFR_NO_FILEA܂B݂ꍇFR_OKAAt@C\̂ɂւ(TCYA^CX^vAђZt@C)XgA܂B
+_FS_MINIMIZE == 0̂ƂɎgp\łB
+ݒ̃t@C̃LbVꂽtbV܂B
++FRESULT f_sync ( + FIL* fp /* [IN] t@CEIuWFNg\̂ւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+̊f_close()Ɠs܂At@C͈Jꂽ܂܂ɂȂAǂݏsł܂BMOȂǁA݃[hŒԃt@CJĂAvP[VɂāAI܂̗͋ǂƂł̊gp邱ƂɂAsӂ̓dffBA̎Oɂ莸f[^ŏɂ邱Ƃł܂B̔wiɂẮAAvP[VEm[gQƂĂB
+ۂ̂ƂAf_close()ł͂̊Ăяot@CEIuWFNgĂ邾Ȃ̂ŁAf_close()Of_sync()uƂ͖ӖłB
+_FS_READONLY == 0̂ƂɎgp\łB
+݂̃[h/CgE|C^擾܂B
++DWORD f_tell ( + FIL* fp /* [IN] t@CEIuWFNg */ +); ++
݂̃[h/CgE|C^(t@C擪̃oCgPʂ̃ItZbg)Ԃ܂B
+f_telĺArWł̓}NƂĎĂ܂B
+
+#define f_tell(fp) ((fp)->fptr)
+
+Ɏgp\B
+t@Cl߂܂B
++FRESULT f_truncate ( + FIL* fp /* [IN] t@CEIuWFNgւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_DENIED, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+t@C݂̒̃[h/CgE|C^ɐl߂܂B[h/CgE|C^Ƀt@C̏I[wĂƂ́Å͉̌ʂ܂B
+_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂ƂɎgp\łB
+t@C܂̓TuEfBNg폜܂B
++FRESULT f_unlink ( + const TCHAR* path /* [IN] IuWFNgւ̃|C^ */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_DENIED, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_LOCKED, +FR_NOT_ENOUGH_CORE +
+폜Ώۂ̃IuWFNg̏ɓĂ͂܂ꍇÃANZX͋ۂ͎s܂B +
_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂ƂɎgp\łB
+t@C܂̓TuEfBNg̃^CX^vύX܂B
++FRESULT f_utime ( + const TCHAR* path, /* [IN] IuWFNgւ̃|C^ */ + const FILINFO* fno /* [IN] ݒ肷t */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_NO_FILE, +FR_NO_PATH, +FR_INVALID_NAME, +FR_WRITE_PROTECTED, +FR_INVALID_DRIVE, +FR_NOT_ENABLED, +FR_NO_FILESYSTEM, +FR_TIMEOUT, +FR_NOT_ENOUGH_CORE +
+IuWFNg̃^CX^vύX܂B
+
+FRESULT set_timestamp (
+ char *obj, /* t@Cւ̃|C^ */
+ int year,
+ int month,
+ int mday,
+ int hour,
+ int min,
+ int sec
+)
+{
+ FILINFO fno;
+
+ fno.fdate = (WORD)(((year - 1980) * 512U) | month * 32U | mday);
+ fno.ftime = (WORD)(hour * 2048U | min * 32U | sec / 2U);
+
+ return f_utime(obj, &fno);
+}
+
+_FS_READONLY == 0ŁA_FS_MINIMIZE == 0̂ƂɎgp\łB
+t@CɃf[^݂܂B
++FRESULT f_write ( + FIL* fp, /* [IN] t@CEIuWFNg */ + const void* buff, /* [IN] ݃f[^ */ + UINT btw, /* [IN] ރoCg */ + UINT* bw /* [OUT] ܂ꂽoCg */ +); ++
+FR_OK, +FR_DISK_ERR, +FR_INT_ERR, +FR_NOT_READY, +FR_DENIED, +FR_INVALID_OBJECT, +FR_TIMEOUT +
+݊JnʒúA[h/CgE|C^̈ʒuɂȂ܂B[h/CgE|C^͎ۂɏ܂ꂽoCgi݂܂BIAvoCg܂ꂽǂ*bw`FbNׂłB*bwbtw菬Ƃ́AfBXNEtӖ܂BfBXNEtĂƂ܂͂ɋ߂Ƃ́A䂪A܂ŎԂꍇ܂B
+_FS_READONLY == 0̂ƂɎgp\łB
+