FatFS是一个为小型嵌入式系统设计的通用FAT(File Allocation Table)文件系统模块。FatFs 的编写遵循ANSI C,并且完全与磁盘I/O层分开。因此,它独立(不依赖)于硬件架构。它可以被嵌入到低成本的微控制器中,如AVR, 8051, PIC, ARM, Z80, 68K 等等,而不需要做任何修改。
FatFs 模块为应用程序提供了下列函数,这些函数描述了FatFs能对FAT卷执行哪些操作。
在FatFs模块上注册/注销一个工作区(文件系统对象)
FRESULT f_mount <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 逻辑驱动器号 */</span></span> FATFS<span class="sy0"><span style="color:#339933;">*</span></span> FileSystemObject <span class="coMULTI"><span style="color:#808080;">/* 工作区指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
FileSystemObject
FR_OK (0)
FR_INVALID_DRIVE
f_mount函数在FatFs模块上注册/注销一个工作区。 在使用任何其他文件函数之前,必须使用该函数为每个卷注册一个工作区。要注销一个工作区,只要指定FileSystemObject为NULL即可,然后该工作区可以被丢弃。
该函数只初始化给定的工作区,以及将该工作区的地址注册到内部表中,不访问磁盘I/O层。卷装入过程是在f_mount函数后或存储介质改变后的第一次文件访问时完成的。
创建/打开一个用于访问文件的文件对象
FRESULT f_open <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 空白文件对象结构指针 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> FileName<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件名指针 */</span></span> BYTE ModeFlags <span class="coMULTI"><span style="color:#808080;">/* 模式标志 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
FileName
ModeFlags
模式 | 描述 |
---|---|
FA_READ | 指定读访问对象。可以从文件中读取数据。 与FA_WRITE结合可以进行读写访问。 |
FA_WRITE | 指定写访问对象。可以向文件中写入数据。 与FA_READ结合可以进行读写访问。 |
FA_OPEN_EXISTING | 打开文件。如果文件不存在,则打开失败。(默认) |
FA_OPEN_ALWAYS | 如果文件存在,则打开;否则,创建一个新文件。 |
FA_CREATE_NEW | 创建一个新文件。如果文件已存在,则创建失败。 |
FA_CREATE_ALWAYS | 创建一个新文件。如果文件已存在,则它将被截断并覆盖。 |
FR_OK (0)
FR_NO_FILE
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_EXIST
FR_DENIED
FR_NOT_READY
FR_WRITE_PROTECTED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
如果函数成功,则创建一个文件对象。该文件对象被后续的读/写函数用来访问文件。如果想要关闭一个打开的文件对象,则使用f_close函数。如果不关闭修改后的文件,那么文件可能会崩溃。
在使用任何文件函数之前,必须使用f_mount函数为驱动器注册一个工作区。只有这样,其他文件函数才能正常工作。
<span class="kw4"><span style="color:#993333;">void</span></span> main <span class="br0"><span style="color:#009900;">(</span></span><span class="kw4"><span style="color:#993333;">void</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> FATFS fs<span class="br0"><span style="color:#009900;">[</span></span><span class="nu0"><span style="color:#0000dd;">2</span></span><span class="br0"><span style="color:#009900;">]</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 逻辑驱动器的工作区(文件系统对象) */</span></span> FIL fsrc<span class="sy0"><span style="color:#339933;">,</span></span> fdst<span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件对象 */</span></span> BYTE buffer<span class="br0"><span style="color:#009900;">[</span></span><span class="nu0"><span style="color:#0000dd;">4096</span></span><span class="br0"><span style="color:#009900;">]</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件拷贝缓冲区 */</span></span> FRESULT res<span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* FatFs 函数公共结果代码 */</span></span> UINT br<span class="sy0"><span style="color:#339933;">,</span></span> bw<span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件读/写字节计数 */</span></span> <span class="coMULTI"><span style="color:#808080;">/* 为逻辑驱动器注册工作区 */</span></span> f_mount<span class="br0"><span style="color:#009900;">(</span></span><span class="nu0"><span style="color:#0000dd;">0</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>fs<span class="br0"><span style="color:#009900;">[</span></span><span class="nu0"><span style="color:#0000dd;">0</span></span><span style="color:#009900;"><span class="br0">]</span><span class="br0">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> f_mount<span class="br0"><span style="color:#009900;">(</span></span><span class="nu0"><span style="color:#0000dd;">1</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>fs<span class="br0"><span style="color:#009900;">[</span></span><span class="nu0"><span style="color:#0000dd;">1</span></span><span style="color:#009900;"><span class="br0">]</span><span class="br0">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 打开驱动器 1 上的源文件 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_open<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fsrc<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"1:srcfile.dat"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> FA_OPEN_EXISTING <span class="sy0"><span style="color:#339933;">|</span></span> FA_READ<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span> die<span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 在驱动器 0 上创建目标文件 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_open<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fdst<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"0:dstfile.dat"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> FA_CREATE_ALWAYS <span class="sy0"><span style="color:#339933;">|</span></span> FA_WRITE<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span> die<span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 拷贝源文件到目标文件 */</span></span> <span class="kw1"><span style="color:#b1b100;">for</span></span> <span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">;;</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_read<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fsrc<span class="sy0"><span style="color:#339933;">,</span></span> buffer<span class="sy0"><span style="color:#339933;">,</span></span> <span class="kw4"><span style="color:#993333;">sizeof</span></span><span class="br0"><span style="color:#009900;">(</span></span>buffer<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>br<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res <span class="sy0"><span style="color:#339933;">||</span></span> br <span class="sy0"><span style="color:#339933;">==</span></span> <span class="nu0"><span style="color:#0000dd;">0</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="kw2"><strong>break</strong></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件结束错误 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_write<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fdst<span class="sy0"><span style="color:#339933;">,</span></span> buffer<span class="sy0"><span style="color:#339933;">,</span></span> br<span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>bw<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res <span class="sy0"><span style="color:#339933;">||</span></span> bw <span class="sy0"><span style="color:#339933;"><</span></span> br<span class="br0"><span style="color:#009900;">)</span></span> <span class="kw2"><strong>break</strong></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 磁盘满错误 */</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="coMULTI"><span style="color:#808080;">/* 关闭打开的文件 */</span></span> f_close<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fsrc<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> f_close<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fdst<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 注销工作区(在废弃前) */</span></span> f_mount<span class="br0"><span style="color:#009900;">(</span></span><span class="nu0"><span style="color:#0000dd;">0</span></span><span class="sy0"><span style="color:#339933;">,</span></span> NULL<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> f_mount<span class="br0"><span style="color:#009900;">(</span></span><span class="nu0"><span style="color:#0000dd;">1</span></span><span class="sy0"><span style="color:#339933;">,</span></span> NULL<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span>
关闭一个打开的文件
FRESULT f_close <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject <span class="coMULTI"><span style="color:#808080;">/* 文件对象结构的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
FR_OK (0) 文件对象已被成功关闭。 >FR_DISK_ERR 由于底层磁盘I/O函数中的错误,而导致该函数失败。 FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT
f_close函数关闭一个打开的文件对象。无论向文件写入任何数据,文件的缓存信息都将被写回到磁盘。该函数成功后,文件对象不再有效,并且可以被丢弃。如果文件对象是在只读模式下打开的,不需要使用该函数,也能被丢弃。
从一个文件读取数据
FRESULT f_read <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件对象结构的指针 */</span></span> <span class="kw4"><span style="color:#993333;">void</span></span><span class="sy0"><span style="color:#339933;">*</span></span> Buffer<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 存储读取数据的缓冲区的指针 */</span></span> UINT ByteToRead<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 要读取的字节数 */</span></span> UINT<span class="sy0"><span style="color:#339933;">*</span></span> ByteRead <span class="coMULTI"><span style="color:#808080;">/* 返回已读取字节数变量的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
Buffer
ByteToRead
ByteRead
FR_OK (0)
FR_DENIED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT 文件对象无效。
文件对象中的读/写指针以已读取字节数增加。该函数成功后,应该检查 *ByteRead 来检测文件是否结束。在读操作过程中,一旦 *ByteRead < ByteToRead ,则读/写指针到达了文件结束位置。
写入数据到一个文件
FRESULT f_write <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件对象结构的指针 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> <span class="kw4"><span style="color:#993333;">void</span></span><span class="sy0"><span style="color:#339933;">*</span></span> Buffer<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 存储写入数据的缓冲区的指针 */</span></span> UINT ByteToWrite<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 要写入的字节数 */</span></span> UINT<span class="sy0"><span style="color:#339933;">*</span></span> ByteWritten <span class="coMULTI"><span style="color:#808080;">/* 返回已写入字节数变量的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
Buffer
ByteToRead
ByteRead
FR_OK (0)
FR_DENIED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT 文件对象无效。
文件对象中的读/写指针以已写入字节数增加。该函数成功后,应该检查 *ByteWritten 来检测磁盘是否已满。在写操作过程中,一旦 *ByteWritten < *ByteToWritten ,则意味着该卷已满。
移动一个打开的文件对象的文件读/写指针。也可以被用来扩展文件大小(簇预分配)。
FRESULT f_lseek <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件对象结构指针 */</span></span> DWORD Offset <span class="coMULTI"><span style="color:#808080;">/* 文件字节偏移 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
Offset
FR_OK (0)
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT 文件对象无效。
f_lseek函数当FS_MINIMIZE <= 2时可用。
offset只能被指定为相对于文件起始处的字节数。当在写模式下指定了一个超过文件大小的offset时,文件的大小将被扩展,并且该扩展的区域中的数据是未定义的。这适用于为快速写操作迅速地创建一个大的文件。f_lseek函数成功后,为了确保读/写指针已被正确地移动,必须检查文件对象中的成员fptr。如果fptr不是所期望的值,则发生了下列情况之一。
<span class="coMULTI"><span style="color:#808080;">/* 移动文件读/写指针到相对于文件起始处偏移为5000字节处 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> <span class="nu0"><span style="color:#0000dd;">5000</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 移动文件读/写指针到文件结束处,以便添加数据 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> file<span class="sy0"><span style="color:#339933;">-></span></span>fsize<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 向前3000字节 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> file<span class="sy0"><span style="color:#339933;">-></span></span>fptr <span class="sy0"><span style="color:#339933;">+</span></span> <span class="nu0"><span style="color:#0000dd;">3000</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 向后(倒带)2000字节(注意溢出) */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> file<span class="sy0"><span style="color:#339933;">-></span></span>fptr <span class="sy0"><span style="color:#339933;">-</span></span> <span class="nu0"><span style="color:#0000dd;">2000</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 簇预分配(为了防止在流写时缓冲区上溢 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_open<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> recfile<span class="sy0"><span style="color:#339933;">,</span></span> FA_CREATE_NEW <span class="sy0"><span style="color:#339933;">|</span></span> FA_WRITE<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 创建一个文件 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> PRE_SIZE<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 预分配簇 */</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res <span class="sy0"><span style="color:#339933;">||</span></span> file<span class="sy0"><span style="color:#339933;">-></span></span>fptr <span class="sy0"><span style="color:#339933;">!=</span></span> PRE_SIZE<span class="br0"><span style="color:#009900;">)</span></span> ... <span class="coMULTI"><span style="color:#808080;">/* 检查文件大小是否已被正确扩展 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> DATA_START<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 没有簇分配延迟地记录数据流 */</span></span> ... <span class="me1"><span style="color:#202020;">res</span></span> <span class="sy0"><span style="color:#339933;">=</span></span> f_truncate<span class="br0"><span style="color:#009900;">(</span></span>file<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 截断未使用的区域 */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_lseek<span class="br0"><span style="color:#009900;">(</span></span>file<span class="sy0"><span style="color:#339933;">,</span></span> <span class="nu0"><span style="color:#0000dd;">0</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 移动到文件起始处 */</span></span> ... <span class="me1"><span style="color:#202020;">res</span></span> <span class="sy0"><span style="color:#339933;">=</span></span> f_close<span class="br0"><span style="color:#009900;">(</span></span>file<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
截断文件大小
FRESULT f_truncate <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject <span class="coMULTI"><span style="color:#808080;">/* 文件对象结构指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
FR_OK (0
FR_DENIED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT 文件对象无效。
f_truncate函数当_FS_READONLY == 0 并且 _FS_MINIMIZE == 0时可用。
f_truncate函数截断文件到当前的文件读/写指针。当文件读/写指针已经指向文件结束时,该函数不起作用。
冲洗一个写文件的缓存信息
FRESULT f_sync <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject <span class="coMULTI"><span style="color:#808080;">/* 文件对象结构的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
FR_OK (0)
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT 文件对象无效。
f_sync函数当_FS_READONLY == 0时可用。
f_sync函数和f_close函数执行同样的过程,但是文件仍处于打开状态,并且可以继续对文件执行读/写/移动指针操作。这适用于以写模式长时间打开文件,比如数据记录器。定期的或f_write后立即执行f_sync可以将由于突然断电或移去磁盘而导致数据丢失的风险最小化。在f_close前立即执行f_sync没有作用,因为在f_close中执行了f_sync。换句话说,这两个函数的差异就是文件对象是不是无效的。
打开一个目录
FRESULT f_opendir <span class="br0"><span style="color:#009900;">(</span></span> DIR<span class="sy0"><span style="color:#339933;">*</span></span> DirObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 空白目录对象结构的指针 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> DirName <span class="coMULTI"><span style="color:#808080;">/* 目录名的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
DirObject
DirName
FR_OK (0)
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_NOT_READY
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_opendir函数当_FS_MINIMIZE <= 1时可用。
f_opendir函数打开一个已存在的目录,并为后续的调用创建一个目录对象。该目录对象结构可以在任何时候不经任何步骤而被丢弃。
读取目录项
FRESULT f_readdir <span class="br0"><span style="color:#009900;">(</span></span> DIR<span class="sy0"><span style="color:#339933;">*</span></span> DirObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 指向打开的目录对象结构的指针 */</span></span> FILINFO<span class="sy0"><span style="color:#339933;">*</span></span> FileInfo <span class="coMULTI"><span style="color:#808080;">/* 指向文件信息结构的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
DirObject
FileInfo
FR_OK (0)
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT 文件对象无效。
f_readdir函数当_FS_MINIMIZE <= 1时可用。
f_readdir函数顺序读取目录项。目录中的所有项可以通过重复调用f_readdir函数被读取。当所有目录项已被读取并且没有项要读取时,该函数没有任何错误地返回一个空字符串到f_name[]成员中。当FileInfo给定一个空指针时,目录对象的读索引将被回绕。
当LFN功能被使能时,在使用f_readdir函数之前,文件信息结构中的lfname和lfsize必须被初始化为有效数值。lfname是一个返回长文件名的字符串缓冲区指针。lfsize是以字符为单位的字符串缓冲区的大小。如果读缓冲区或LFN工作缓冲区的大小(对于LFN)不足,或者对象没有LFN,则一个空字符串将被返回到LFN读缓冲区。如果LFN包含任何不能被转换为OEM代码的字符,则一个空字符串将被返回,但是这不是Unicode API配置的情况。当lfname是一个空字符串时,没有LFN的任何数据被返回。当对象没有LFN时,任何小型大写字母可以被包含在SFN中。
当相对路径功能被使能(_FS_RPATH == 1)时,"."和".."目录项不会被过滤掉,并且它将出现在读目录项中。
FRESULT scan_files <span class="br0"><span style="color:#009900;">(</span></span><span class="kw4"><span style="color:#993333;">char</span></span><span class="sy0"><span style="color:#339933;">*</span></span> path<span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> FRESULT res<span class="sy0"><span style="color:#339933;">;</span></span> FILINFO fno<span class="sy0"><span style="color:#339933;">;</span></span> DIR dir<span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw4"><span style="color:#993333;">int</span></span> i<span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw4"><span style="color:#993333;">char</span></span> <span class="sy0"><span style="color:#339933;">*</span></span>fn<span class="sy0"><span style="color:#339933;">;</span></span> <span class="co2"><span style="color:#339933;">#if _USE_LFN</span></span> <span class="kw4"><span style="color:#993333;">static</span></span> <span class="kw4"><span style="color:#993333;">char</span></span> lfn<span class="br0"><span style="color:#009900;">[</span></span>_MAX_LFN <span class="sy0"><span style="color:#339933;">*</span></span> <span class="br0"><span style="color:#009900;">(</span></span>_DF1S <span class="sy0"><span style="color:#339933;">?</span></span> <span class="nu0"><span style="color:#0000dd;">2</span></span> <span class="sy0"><span style="color:#339933;">:</span></span> <span class="nu0"><span style="color:#0000dd;">1</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="sy0"><span style="color:#339933;">+</span></span> <span class="nu0"><span style="color:#0000dd;">1</span></span><span class="br0"><span style="color:#009900;">]</span></span><span class="sy0"><span style="color:#339933;">;</span></span> fno.<span class="me1"><span style="color:#202020;">lfname</span></span> <span class="sy0"><span style="color:#339933;">=</span></span> lfn<span class="sy0"><span style="color:#339933;">;</span></span> fno.<span class="me1"><span style="color:#202020;">lfsize</span></span> <span class="sy0"><span style="color:#339933;">=</span></span> <span class="kw4"><span style="color:#993333;">sizeof</span></span><span class="br0"><span style="color:#009900;">(</span></span>lfn<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="co2"><span style="color:#339933;">#endif</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_opendir<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>dir<span class="sy0"><span style="color:#339933;">,</span></span> path<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res <span class="sy0"><span style="color:#339933;">==</span></span> FR_OK<span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> i <span class="sy0"><span style="color:#339933;">=</span></span> strlen<span class="br0"><span style="color:#009900;">(</span></span>path<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">for</span></span> <span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">;;</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_readdir<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>dir<span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>fno<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res <span class="sy0"><span style="color:#339933;">!=</span></span> FR_OK <span class="sy0"><span style="color:#339933;">||</span></span> fno.<span class="me1"><span style="color:#202020;">fname</span></span><span class="br0"><span style="color:#009900;">[</span></span><span class="nu0"><span style="color:#0000dd;">0</span></span><span class="br0"><span style="color:#009900;">]</span></span> <span class="sy0"><span style="color:#339933;">==</span></span> <span class="nu0"><span style="color:#0000dd;">0</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="kw2"><strong>break</strong></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>fno.<span class="me1"><span style="color:#202020;">fname</span></span><span class="br0"><span style="color:#009900;">[</span></span><span class="nu0"><span style="color:#0000dd;">0</span></span><span class="br0"><span style="color:#009900;">]</span></span> <span class="sy0"><span style="color:#339933;">==</span></span> <span class="st0"><span style="color:#ff0000;">'.'</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="kw1"><span style="color:#b1b100;">continue</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="co2"><span style="color:#339933;">#if _USE_LFN</span></span> fn <span class="sy0"><span style="color:#339933;">=</span></span> <span class="sy0"><span style="color:#339933;">*</span></span>fno.<span class="me1"><span style="color:#202020;">lfname</span></span> <span class="sy0"><span style="color:#339933;">?</span></span> fno.<span class="me1"><span style="color:#202020;">lfname</span></span> <span class="sy0"><span style="color:#339933;">:</span></span> fno.<span class="me1"><span style="color:#202020;">fname</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="co2"><span style="color:#339933;">#else</span></span> fn <span class="sy0"><span style="color:#339933;">=</span></span> fno.<span class="me1"><span style="color:#202020;">fname</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="co2"><span style="color:#339933;">#endif</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>fno.<span class="me1"><span style="color:#202020;">fattrib</span></span> <span class="sy0"><span style="color:#339933;">&</span></span> AM_DIR<span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> sprintf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>path<span class="br0"><span style="color:#009900;">[</span></span>i<span class="br0"><span style="color:#009900;">]</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"/%s"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> fn<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> scan_files<span class="br0"><span style="color:#009900;">(</span></span>path<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res <span class="sy0"><span style="color:#339933;">!=</span></span> FR_OK<span class="br0"><span style="color:#009900;">)</span></span> <span class="kw2"><strong>break</strong></span><span class="sy0"><span style="color:#339933;">;</span></span> path<span class="br0"><span style="color:#009900;">[</span></span>i<span class="br0"><span style="color:#009900;">]</span></span> <span class="sy0"><span style="color:#339933;">=</span></span> <span class="nu0"><span style="color:#0000dd;">0</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="kw1"><span style="color:#b1b100;">else</span></span> <span class="br0"><span style="color:#009900;">{</span></span> <span class="kw3"><span style="color:#000066;">printf</span></span><span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"%s/%s</span><span class="es1"><strong><span style="color:#000099;">\n</span></strong></span><span style="color:#ff0000;">"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> path<span class="sy0"><span style="color:#339933;">,</span></span> fn<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="kw1"><span style="color:#b1b100;">return</span></span> res<span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span>
获取空闲簇的数目
FRESULT f_getfree <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> Path<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 驱动器的根目录 */</span></span> DWORD<span class="sy0"><span style="color:#339933;">*</span></span> Clusters<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 存储空闲簇数目变量的指针 */</span></span> FATFS<span class="sy0"><span style="color:#339933;">**</span></span> FileSystemObject <span class="coMULTI"><span style="color:#808080;">/* 文件系统对象指针的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Path
Clusters
FileSystemObject
FR_OK (0)
FR_INVALID_DRIVE
FR_NOT_READY
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_getfree函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时有效。
f_getfree函数获取驱动器上空闲簇的数目。文件系统对象中的成员csize是每簇中的扇区数,因此,以扇区为单位的空闲空间可以被计算出来。当FAT32卷上的FSInfo结构不同步时,该函数返回一个错误的空闲簇计数。
FATFS <span class="sy0"><span style="color:#339933;">*</span></span>fs<span class="sy0"><span style="color:#339933;">;</span></span> DWORD fre_clust<span class="sy0"><span style="color:#339933;">,</span></span> fre_sect<span class="sy0"><span style="color:#339933;">,</span></span> tot_sect<span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* Get drive information and free clusters */</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_getfree<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"/"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>fre_clust<span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>fs<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span> die<span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* Get total sectors and free sectors */</span></span> tot_sect <span class="sy0"><span style="color:#339933;">=</span></span> <span class="br0"><span style="color:#009900;">(</span></span>fs<span class="sy0"><span style="color:#339933;">-></span></span>max_clust <span class="sy0"><span style="color:#339933;">-</span></span> <span class="nu0"><span style="color:#0000dd;">2</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="sy0"><span style="color:#339933;">*</span></span> fs<span class="sy0"><span style="color:#339933;">-></span></span>csize<span class="sy0"><span style="color:#339933;">;</span></span> fre_sect <span class="sy0"><span style="color:#339933;">=</span></span> fre_clust <span class="sy0"><span style="color:#339933;">*</span></span> fs<span class="sy0"><span style="color:#339933;">-></span></span>csize<span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* Print free space in unit of KB (assuming 512B/sector) */</span></span> <span class="kw3"><span style="color:#000066;">printf</span></span><span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"%lu KB total drive space.</span><span class="es1"><strong><span style="color:#000099;">\n</span></strong></span><span style="color:#ff0000;">"</span></span> <span class="st0"><span style="color:#ff0000;">"%lu KB available.</span><span class="es1"><strong><span style="color:#000099;">\n</span></strong></span><span style="color:#ff0000;">"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> fre_sect <span class="sy0"><span style="color:#339933;">/</span></span> <span class="nu0"><span style="color:#0000dd;">2</span></span><span class="sy0"><span style="color:#339933;">,</span></span> tot_sect <span class="sy0"><span style="color:#339933;">/</span></span> <span class="nu0"><span style="color:#0000dd;">2</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
获取文件状态
FRESULT f_stat <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> FileName<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件名或目录名的指针 */</span></span> FILINFO<span class="sy0"><span style="color:#339933;">*</span></span> FileInfo <span class="coMULTI"><span style="color:#808080;">/* FILINFO结构的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileName
FileInfo
FR_OK (0)
FR_NO_FILE
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_NOT_READY
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_stat函数当_FS_MINIMIZE == 0时可用。
f_stat函数获取一个文件或目录的信息。信息的详情,请参考FILINFO结构和f_readdir函数。
创建一个目录
FRESULT f_mkdir <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> DirName <span class="coMULTI"><span style="color:#808080;">/* 目录名的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
DirName
FR_OK (0)
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_DENIED
FR_EXIST
FR_NOT_READY
FR_WRITE_PROTECTED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_mkdir函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_mkdir函数创建一个新目录。
res <span class="sy0"><span style="color:#339933;">=</span></span> f_mkdir<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"sub1"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span> die<span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_mkdir<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"sub1/sub2"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span> die<span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> res <span class="sy0"><span style="color:#339933;">=</span></span> f_mkdir<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"sub1/sub2/sub3"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span> die<span class="br0"><span style="color:#009900;">(</span></span>res<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
移除一个对象
FRESULT f_unlink <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> FileName <span class="coMULTI"><span style="color:#808080;">/* 对象名的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileName
FR_OK (0)
FR_NO_FILE
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_DENIED
FR_NOT_READY
FR_WRITE_PROTECTED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_unlink函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_unlink函数移除一个对象。不要移除打开的对象或当前目录。
修改一个文件或目录的属性。
FRESULT f_chmod <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> FileName<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件或目录的指针 */</span></span> BYTE Attribute<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 属性标志 */</span></span> BYTE AttributeMask <span class="coMULTI"><span style="color:#808080;">/* 属性掩码 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileName
Attribute
属性 | 描述 |
---|---|
AM_RDO | 只读 |
AM_ARC | 存档 |
AM_SYS | 系统 |
AM_HID | 隐藏 |
AttributeMask
FR_OK (0)
FR_NO_FILE
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_NOT_READY
FR_WRITE_PROTECTED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_chmod函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_chmod函数修改一个文件或目录的属性。
<span class="co1"><span style="color:#666666;">// 设置只读标志,清除存档标志,其他不变</span></span> f_chmod<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"file.txt"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> AR_RDO<span class="sy0"><span style="color:#339933;">,</span></span> AR_RDO <span class="sy0"><span style="color:#339933;">|</span></span> AR_ARC<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
f_utime函数修改一个文件或目录的时间戳。
FRESULT f_utime <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> FileName<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件或目录路径的指针 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> FILINFO<span class="sy0"><span style="color:#339933;">*</span></span> TimeDate <span class="coMULTI"><span style="color:#808080;">/* 待设置的时间和日期 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileName
TimeDate
FR_OK (0)
FR_NO_FILE
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_NOT_READY
FR_WRITE_PROTECTED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_utime函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_utime函数修改一个文件或目录的时间戳。
重命名一个对象。
FRESULT f_rename <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> OldName<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 原对象名的指针 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> NewName <span class="coMULTI"><span style="color:#808080;">/* 新对象名的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
OldName
NewName
FR_OK (0)
FR_NO_FILE
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_NOT_READY
FR_EXIST
FR_DENIED
FR_WRITE_PROTECTED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_rename函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_rename函数重命名一个对象,并且也可以将对象移动到其他目录。逻辑驱动器号由原名决定,新名不能包含一个逻辑驱动器号。不要重命名打开的对象。
<span class="coMULTI"><span style="color:#808080;">/* 重命名一个对象 */</span></span> f_rename<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"oldname.txt"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"newname.txt"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 重命名并且移动一个对象到另一个目录 */</span></span> f_rename<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"oldname.txt"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"dir1/newname.txt"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
在驱动器上创建一个文件系统
FRESULT f_mkfs <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 逻辑驱动器号 */</span></span> BYTE PartitioningRule<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 分区规则 */</span></span> WORD AllocSize <span class="coMULTI"><span style="color:#808080;">/* 分配单元大小 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
PartitioningRule
AllocSize
FR_OK (0)
FR_INVALID_DRIVE
FR_NOT_READY
FR_WRITE_PROTECTED
FR_NOT_ENABLED
FR_DISK_ERR
FR_MKFS_ABORTED
f_mkfs函数当_FS_READOLNY == 0并且_USE_MKFS == 1时可用。
f_mkfs函数在驱动器中创建一个FAT文件系统。对于可移动媒介,有两种分区规则:FDISK和SFD,通过参数PartitioningRule选择。FDISK格式在大多数情况下被推荐使用。该函数当前不支持多分区,因此,物理驱动器上已存在的分区将被删除,并且重新创建一个占据全部磁盘空间的新分区。
根据Microsoft发布的FAT规范,FAT分类:FAT12/FAT16/FAT32,由驱动器上的簇数决定。因此,选择哪种FAT分类,取决于卷大小和指定的簇大小。簇大小影响文件系统的性能,并且大簇会提高性能。
读取文件数据并将其转发到数据流设备。
FRESULT f_forward <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件对象 */</span></span> UINT <span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">*</span></span>Func<span style="color:#009900;"><span class="br0">)</span><span class="br0">(</span></span><span class="kw4"><span style="color:#993333;">const</span></span> BYTE<span class="sy0"><span style="color:#339933;">*,</span></span>UINT<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 数据流函数 */</span></span> UINT ByteToFwd<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 要转发的字节数 */</span></span> UINT<span class="sy0"><span style="color:#339933;">*</span></span> ByteFwd <span class="coMULTI"><span style="color:#808080;">/* 已转发的字节数 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
Func
ByteToFwd
ByteFwd
FR_OK (0)
FR_DENIED
FR_DISK_ERR
FR_INT_ERR
FR_NOT_READY
FR_INVALID_OBJECT
f_forward函数当_USE_FORWARD == 1并且_FS_TINY == 1时可用。
f_forward函数从文件中读取数据并将数据转发到输出流,而不使用数据缓冲区。这适用于小存储系统,因为它在应用模块中不需要任何数据缓冲区。文件对象的文件指针以转发的字节数增加。如果*ByteFwd < ByteToFwd并且没有错误,则意味着由于文件结束或在数据传输过程中流忙,请求的字节不能被传输。
<span class="coMULTI"><span style="color:#808080;">/*-----------------------------------------------------------------------*/</span></span> <span class="coMULTI"><span style="color:#808080;">/* 示例代码:数据传输函数,将被f_forward函数调用 */</span></span> <span class="coMULTI"><span style="color:#808080;">/*-----------------------------------------------------------------------*/</span></span> UINT out_stream <span class="br0"><span style="color:#009900;">(</span></span> <span class="coMULTI"><span style="color:#808080;">/* 返回已发送字节数或流状态 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> BYTE <span class="sy0"><span style="color:#339933;">*</span></span>p<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 将被发送的数据块的指针 */</span></span> UINT btf <span class="coMULTI"><span style="color:#808080;">/* >0: 传输调用(将被发送的字节数)。0: 检测调用 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> UINT cnt <span class="sy0"><span style="color:#339933;">=</span></span> <span class="nu0"><span style="color:#0000dd;">0</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>btf <span class="sy0"><span style="color:#339933;">==</span></span> <span class="nu0"><span style="color:#0000dd;">0</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> <span class="coMULTI"><span style="color:#808080;">/* 检测调用 */</span></span> <span class="coMULTI"><span style="color:#808080;">/* 返回流状态(0: 忙,1: 就绪) */</span></span> <span class="coMULTI"><span style="color:#808080;">/* 当检测调用时,一旦它返回就绪,那么在后续的传输调用时,它必须接收至少一个字节,或者f_forward将以FR_INT_ERROR而失败。 */</span></span> <span class="kw1"><span style="color:#b1b100;">if</span></span> <span class="br0"><span style="color:#009900;">(</span></span>FIFO_READY<span class="br0"><span style="color:#009900;">)</span></span> cnt <span class="sy0"><span style="color:#339933;">=</span></span> <span class="nu0"><span style="color:#0000dd;">1</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="kw1"><span style="color:#b1b100;">else</span></span> <span class="br0"><span style="color:#009900;">{</span></span> <span class="coMULTI"><span style="color:#808080;">/* 传输调用 */</span></span> <span class="kw1"><span style="color:#b1b100;">do</span></span> <span class="br0"><span style="color:#009900;">{</span></span> <span class="coMULTI"><span style="color:#808080;">/* 当有数据要发送并且流就绪时重复 */</span></span> FIFO_PORT <span class="sy0"><span style="color:#339933;">=</span></span> <span class="sy0"><span style="color:#339933;">*</span></span>p<span class="sy0"><span style="color:#339933;">++;</span></span> cnt<span class="sy0"><span style="color:#339933;">++;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="kw1"><span style="color:#b1b100;">while</span></span> <span class="br0"><span style="color:#009900;">(</span></span>cnt <span class="sy0"><span style="color:#339933;"><</span></span> btf <span class="sy0"><span style="color:#339933;">&&</span></span> FIFO_READY<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="kw1"><span style="color:#b1b100;">return</span></span> cnt<span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="coMULTI"><span style="color:#808080;">/*-----------------------------------------------------------------------*/</span></span> <span class="coMULTI"><span style="color:#808080;">/* 示例代码:使用f_forward函数 */</span></span> <span class="coMULTI"><span style="color:#808080;">/*-----------------------------------------------------------------------*/</span></span> FRESULT play_file <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">char</span></span> <span class="sy0"><span style="color:#339933;">*</span></span>fn <span class="coMULTI"><span style="color:#808080;">/* 待播放的音频文件名的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> FRESULT rc<span class="sy0"><span style="color:#339933;">;</span></span> FIL fil<span class="sy0"><span style="color:#339933;">;</span></span> UINT dmy<span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 以只读模式打开音频文件 */</span></span> rc <span class="sy0"><span style="color:#339933;">=</span></span> f_open<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> fn<span class="sy0"><span style="color:#339933;">,</span></span> FA_READ<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* 重复,直到文件指针到达文件结束位置 */</span></span> <span class="kw1"><span style="color:#b1b100;">while</span></span> <span class="br0"><span style="color:#009900;">(</span></span>rc <span class="sy0"><span style="color:#339933;">==</span></span> FR_OK <span class="sy0"><span style="color:#339933;">&&</span></span> fil.<span class="me1"><span style="color:#202020;">fptr</span></span> <span class="sy0"><span style="color:#339933;"><</span></span> fil.<span class="me1"><span style="color:#202020;">fsize</span></span><span class="br0"><span style="color:#009900;">)</span></span> <span class="br0"><span style="color:#009900;">{</span></span> <span class="coMULTI"><span style="color:#808080;">/* 任何其他处理... */</span></span> <span class="coMULTI"><span style="color:#808080;">/* 定期或请求式填充输出流 */</span></span> rc <span class="sy0"><span style="color:#339933;">=</span></span> f_forward<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> out_stream<span class="sy0"><span style="color:#339933;">,</span></span> <span class="nu0"><span style="color:#0000dd;">1000</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">&</span></span>dmy<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span> <span class="coMULTI"><span style="color:#808080;">/* 该只读的音频文件对象不需要关闭就可以被丢弃 */</span></span> <span class="kw1"><span style="color:#b1b100;">return</span></span> rc<span class="sy0"><span style="color:#339933;">;</span></span> <span class="br0"><span style="color:#009900;">}</span></span>
f_chdir函数改变一个驱动器的当前目录。
FRESULT f_chdir <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> XCHAR<span class="sy0"><span style="color:#339933;">*</span></span> Path <span class="coMULTI"><span style="color:#808080;">/* 路径名的指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Path
FR_OK (0)
FR_NO_PATH
FR_INVALID_NAME
FR_INVALID_DRIVE
FR_NOT_READY
FR_DISK_ERR
FR_INT_ERR
FR_NOT_ENABLED
FR_NO_FILESYSTEM
f_chdir函数当_FS_RPATH == 1时可用。
f_chdir函数改变一个逻辑驱动器的当前目录。当一个逻辑驱动器被自动挂载时,它的当前目录被初始化为根目录。注意:当前目录被保存在每个文件系统对象中,因此它也影响使用同一逻辑驱动器的其它任务。
<span class="co1"><span style="color:#666666;">// 改变当前驱动器的当前目录(根目录下的dir1)</span></span> f_chdir<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"/dir1"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="co1"><span style="color:#666666;">// 改变驱动器2的当前目录(父目录)</span></span> f_chdir<span class="br0"><span style="color:#009900;">(</span></span><span class="st0"><span style="color:#ff0000;">"2:.."</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
f_chdrive函数改变当前驱动器。
FRESULT f_chdrive <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive <span class="coMULTI"><span style="color:#808080;">/* 逻辑驱动器号 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
FR_OK (0)
FR_INVALID_DRIVE
f_chdrive函数当_FS_RPATH == 1时可用。
f_chdrive函数改变当前驱动器。当前驱动器号初始值为0,注意:当前驱动器被保存为一个静态变量,因此它也影响使用文件函数的其它任务。
f_gets从文件中读取一个字符串。
<span class="kw4"><span style="color:#993333;">char</span></span><span class="sy0"><span style="color:#339933;">*</span></span> f_gets <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">char</span></span><span class="sy0"><span style="color:#339933;">*</span></span> Str<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 读缓冲区 */</span></span> <span class="kw4"><span style="color:#993333;">int</span></span> Size<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 读缓冲区大小 */</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject <span class="coMULTI"><span style="color:#808080;">/* 文件对象 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Str
Size
FileObject
当函数成功后,Str将被返回。
f_gets函数当_USE_STRFUNC == 1或者_USE_STRFUNC == 2时可用。如果_USE_STRFUNC == 2,文件中包含的'\r'则被去除。
f_gets函数是f_read的一个封装函数。当读取到'\n'、文件结束或缓冲区被填冲了Size - 1个字符时,读操作结束。读取的字符串以'\0'结束。当文件结束或读操作中发生了任何错误,f_gets()返回一个空字符串。可以使用宏f_eof()和f_error()检查EOF和错误状态。
f_putc函数向文件中写入一个字符。
<span class="kw4"><span style="color:#993333;">int</span></span> f_putc <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">int</span></span> Chr<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 字符 */</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject <span class="coMULTI"><span style="color:#808080;">/* 文件对象 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Chr
FileObject
当字符被成功地写入后,函数返回该字符。由于磁盘满或任何错误而导致函数失败,将返回EOF。
f_putc函数当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,字符'\n'被转换为"\r\n"写入文件中。
f_putc函数是f_write的一个封装函数。
f_puts函数向文件中写入一个字符串。
<span class="kw4"><span style="color:#993333;">int</span></span> f_puts <span class="br0"><span style="color:#009900;">(</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> <span class="kw4"><span style="color:#993333;">char</span></span><span class="sy0"><span style="color:#339933;">*</span></span> Str<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 字符串指针 */</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject <span class="coMULTI"><span style="color:#808080;">/* 文件对象指针 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Str
FileObject
函数成功后,将返回写入的字符数。由于磁盘满或任何错误而导致函数失败,将返回EOF。
f_puts()当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,字符串中的'\n'被转换为"\r\n"写入文件中。
f_puts()是f_putc()的一个封装函数。
f_printf函数向文件中写入一个格式化字符串。
<span class="kw4"><span style="color:#993333;">int</span></span> f_printf <span class="br0"><span style="color:#009900;">(</span></span> FIL<span class="sy0"><span style="color:#339933;">*</span></span> FileObject<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 文件对象指针 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> <span class="kw4"><span style="color:#993333;">char</span></span><span class="sy0"><span style="color:#339933;">*</span></span> Foramt<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 格式化字符串指针 */</span></span> ... <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
FileObject
Format
...
函数成功后,将返回写入的字符数。由于磁盘满或任何错误而导致函数失败,将返回EOF。
f_printf函数当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,包含在格式化字符串中的'\n'将被转换成"\r\n"写入文件中。
f_printf函数是f_putc和f_puts的一个封装函数。如下所示,格式控制符是标准库的一个子集:
类型:c s d u X
大小:l
标志:0
f_printf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"%6d"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="sy0"><span style="color:#339933;">-</span></span><span class="nu0"><span style="color:#0000dd;">200</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* " -200" */</span></span> f_printf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"%02u"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="nu0"><span style="color:#0000dd;">5</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* "05" */</span></span> f_printf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"%ld"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="nu0"><span style="color:#0000dd;">12345678L</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* "12345678" */</span></span> f_printf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"%08lX"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> 1194684UL<span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* "00123ABC" */</span></span> f_printf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"%s"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"String"</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* "String" */</span></span> f_printf<span class="br0"><span style="color:#009900;">(</span></span><span class="sy0"><span style="color:#339933;">&</span></span>fil<span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">"%c"</span></span><span class="sy0"><span style="color:#339933;">,</span></span> <span class="st0"><span style="color:#ff0000;">'a'</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span> <span class="coMULTI"><span style="color:#808080;">/* "a" */</span></span>
由于FatFs模块完全与磁盘I/O层分开,因此底层磁盘I/O需要下列函数去读/写物理磁盘以及获取当前时间。由于底层磁盘I/O模块并不是FatFs的一部分,因此它必须由用户提供。
初始化磁盘驱动器
DSTATUS disk_initialize <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive <span class="coMULTI"><span style="color:#808080;">/* 物理驱动器号 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
disk_initialize函数返回一个磁盘状态作为结果。磁盘状态的详情,参考disk_status函数。
disk_initialize函数初始化一个物理驱动器。函数成功后,返回值中的STA_NOINIT标志被清除。
disk_initialize函数被FatFs模块在卷挂载过程中调用,去管理存储介质的改变。当FatFs模块起作用时,或卷上的FAT结构可以被瓦解时,应用程序不能调用该函数。可以使用f_mount函数去重新初始化文件系统。
获取当前磁盘的状态
DSTATUS disk_status <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive <span class="coMULTI"><span style="color:#808080;">/* 物理驱动器号*/</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
磁盘状态,是下列标志的组合: STA_NOINIT
STA_PROTECTED
从磁盘驱动器中读取扇区
DRESULT disk_read <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 物理驱动器号 */</span></span> BYTE<span class="sy0"><span style="color:#339933;">*</span></span> Buffer<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 读取数据缓冲区的指针 */</span></span> DWORD SectorNumber<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 起始扇区号 */</span></span> BYTE SectorCount <span class="coMULTI"><span style="color:#808080;">/* 要读取的扇区数 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
Buffer
SectorCount
RES_OK (0)
RES_ERROR
RES_PARERR
RES_NOTRDY
向磁盘驱动器中写入扇区
DRESULT disk_write <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 物理驱动器号 */</span></span> <span class="kw4"><span style="color:#993333;">const</span></span> BYTE<span class="sy0"><span style="color:#339933;">*</span></span> Buffer<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 写入数据缓冲区的指针(可能未对齐) */</span></span> DWORD SectorNumber<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 起始扇区号 */</span></span> BYTE SectorCount <span class="coMULTI"><span style="color:#808080;">/* 要写入的扇区数 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
Buffer
SectorNumber
SectorCount
RES_OK (0)
RES_ERROR
RES_WRPRT
RES_PARERR
RES_NOTRDY
控制设备特定的功能以及磁盘读写以外的其它功能。
DRESULT disk_ioctl <span class="br0"><span style="color:#009900;">(</span></span> BYTE Drive<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 驱动器号 */</span></span> BYTE Command<span class="sy0"><span style="color:#339933;">,</span></span> <span class="coMULTI"><span style="color:#808080;">/* 控制命令代码 */</span></span> <span class="kw4"><span style="color:#993333;">void</span></span><span class="sy0"><span style="color:#339933;">*</span></span> Buffer <span class="coMULTI"><span style="color:#808080;">/* 数据传输缓冲区 */</span></span> <span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
Drive
Command
Buffer
RES_OK (0)
RES_ERROR
RES_PARERR
RES_NOTRDY
FatFs模块只使用下述与设备无关的命令,没有使用任何设备相关功能。
命令 | 描述 |
---|---|
CTRL_SYNC | 确保磁盘驱动器已经完成等待写过程。当磁盘I/O模块有一个写回高速缓存时,立即冲洗脏扇区。在只读配置中,不需要该命令。 |
GET_SECTOR_SIZE | 返回驱动器的扇区大小赋给Buffer指向的WORD变量。在单个扇区大小配置中(_MAX_SS 为 512),不需要该命令。 |
GET_SECTOR_COUNT | 返回总扇区数赋给Buffer指向的DWORD变量。只在f_mkfs函数中,使用了该命令。 |
GET_BLOCK_SIZE | 返回以扇区为单位的存储阵列的擦除块大小赋给Buffer指向的DWORD变量。当擦除块大小未知或是磁盘设备时,返回1。只在f_mkfs函数中,使用了该命令。 |
获取当前时间
DWORD get_fattime <span class="br0"><span style="color:#009900;">(</span></span><span class="kw4"><span style="color:#993333;">void</span></span><span class="br0"><span style="color:#009900;">)</span></span><span class="sy0"><span style="color:#339933;">;</span></span>
返回的当前时间被打包进一个DWORD数值。各位域定义如下:
bit31:25
bit24:21
bit20:16
bit15:11
bit10:5
bit4:0
get_fattime函数必须返回任何有效的时间,即使系统不支持实时时钟。如果返回一个0,则文件将没有一个有效的时间。在只读配置中,不需要此函数。
http://blog.csdn.net/qsycn/article/details/9226403