Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

FatFs Module Application Note

FatFs 模块应用手册
Considerations on porting to various platform

移植到各种平台的考虑

The FatFs module is assuming following terms on portability.

FatFs 模块的移植性前提是满足以下条件。

 ANSI C
The FatFs module is a middleware that written in ANSI C. There is no
platform dependence, so long as the compiler is in compliance with ANSI C.
However it handles the system portable FAT structures. You must take the
endian into consideration. This setting is defined in ff.h (tff.h). It must be
changed for your platform first or the compiler will abort with an error.

FatFs 模块是一个用 ANSI C 写的中间层软件。它没有平台限制,只要编


译器兼容 ANSI C 标准。无论它怎么的可移植,但你必须处更好好字节
的大小端问题。它在 ff.h(tff.h)文件中设置。你必须先设置好,否则你的
编译器可能会出错。

 Size of char/short/long are 8/16/32-bit and int is 16 or 32 bit.


These correspondence are defined in integer.h. This will not be a problem on
most compilers. When any conflict of the definitions between the integer.h
and existing definitions is occured, you must resolve the confrict with care.
 char/short/long 分别是 8/16/32 位的,int 是 16 或 32 位的。
这些相关的设置都在 integer.h 中。这些对于常用的编译器往往不是问题。当这
些设置与你现有的设置或定义产生冲突时,你必须小心把它解决之。

Memory Usage (R0.06)

储存器使用信息(R0.06 版本)

These are the memory usage on some target systems. The memory sizes are in unit of
byte, D means number of logical drives and F means number of open files. All
samples are optimezed in code size.

这是在一些目标系中的储存器使用信息。储存器大小的单位是字节,D 的意思
是逻辑盘的数量,F 的意思是打开文件的数量。所有范例都是以代码优化的。

MSP43 TLCS-
AVR H8/300H PIC V850ES
0 870/C
gcc(WinAV CC870
Compiler CH38 gcc(C30) CL430 CA850 SH
R) C
_MCU_ENDIAN 1 2 2 2 1 1 2
FatFs Code (Full,
9280 9348 9387 6728 753
R/W)
FatFs Code (Min,
5814 5798 5913 4094 474
R/W)
FatFs Code (Full,
4402 4236 4371 3054 347
R/O)
FatFs Code (Min,
3066 3158 3213 2172 263
R/O)
FatFs Work
D*2+2 D*4+2 D*2+2 D*4+2 D*4
(Static)
FatFs Work D*554+F*5 D*554+F*5 D*554+F*5 D*554+F*5 D*5
(Dynamic) 44 50 44 50 50
Tiny-FatFs Code
7628 7668 7560 7108 9501 5978 664
(Full, R/W)
Tiny-FatFs Code
4684 4820 4698 4390 6322 3678 423
(Min, R/W)
Tiny-FatFs Code
3634 3600 3618 3382 4554 2722 307
(Full, R/O)
Tiny-FatFs Code
2524 2700 2601 2398 3390 1862 230
(Min, R/O)
Tiny-FatFs Wrok
4 6 4 4 4 6 6
(Static)
Tiny-FatFs Work 544+F* 544+F*
544+F*28 544+F*32 544+F*28 544+F*32 544
(Dynamic) 28 28

FatFs vs. Tiny-FatFs

FatFs 与 Tiny-FatFs 对比
For most applications, such as portable audio and data logger, Tiny-FatFs is the best
choice. However because the Tiny-FatFs does not support FAT32 in default, there is a
limitation that can handle only tiny storage upto 2GB(4GB in FAT64). The FAT32
support can be added by _USE_FAT32 option with an additional code size. The FatFs
is suitable for fast multiple files access, and for multiple drive system.

对于大多数的应用,例如手持的语音或数据记录设备,Tiny-FatFs 是最佳选择。
然而,因为 Tiny-FatFs 默认不支持 FAT32,所以有只能处理 2GB(4GB in
FAT64)容量。FAT32 的支持可以通过加入_USE_FAT32 选项加入,同时代码占
用量增加。FatFs 适合快速多文件访问,和多磁盘系统。

FAT
Memory Size
Type
<= 64MB FAT12
128MB - 2GB FAT16
>= 4GB FAT32

Rignt table shows the correspondence between memory size and FAT type for SD
memroy card and they are shipped with this format. The data area is justified to the
erase block boundary and the memory card works with the best performance. For that
reason, the memory card should not be reformated with PC. When cluster size or FAT
type is changed, the write performance can be decreased.

表格中指出的容量与对应的 FAT 格式,这是 SD 卡出厂默认格式。这样的数据


容量适合储存卡的擦写并且是最佳性能的。所以,储存卡不要在 PC 中重新格
式化。当簇大小或 FAT 类型的改变,会减低卡的写入性能。

Performance effective file access

性能有效的文件访问

For good performance on reading/writing files on the small embedded system,


application program should consider what process is done in the FatFs module. The
file data on the disk is transferred by f_read function in these sequence.

为了满足小型嵌入式系统的读写文件的性能,FatFs 模块中的应用程序会考虑怎
样处理数据。磁盘上的文件数据会被函数 f_read 以下图的方法传送。

Figure 1. Sector miss-aligned read (short)


Figure 2. Sector miss-aligned read (long)

Figure 3. Sector aligned read

The file I/O buffer means a sector buffer to read/write a partial data on the sector. On
the FatFs, member buffer[] in the file object is used. On the Tiny-FatFs, member
win[] in the file system object is used.

文件 I/O buffer 是指读写扇区的扇区缓存。在 FatFs 中,文件结构体中的成员变


量 buffer[]就是这个缓存。在 Tiny-FatFs 中,文件系统结构体中的 win[]变量就是
扇区缓存。

Tiny-FatFs processes all file data transfer and access to the FAT/directory with only
one sector buffer, so that FAT sector cached into the buffer is lost and it must
reloaded at every cluster boundary. FatFs has a FAT/directory buffer separated from
file I/O buffer, the frequency of FAT accesses is only 1/341, 1/256 or 1/128 (when
cluster is contiguous) compared to Tiny-FatFs. Thus the Tiny-FatFs is sacrificing its
performance in compensation for very small memory footprint.

Tiny-FatFs 在传输文件数据和访问 FAT 或目录时都只用一个扇区缓存,所以


FAT 扇区缓存在第个簇边界时都要重新载入。FatFs 则是目录缓存与文件 I/O 缓
存分开的,所以相对于 Tiny-FatFs 只有 1/341,1/256 或者 1/128 的 FAT 访问频率。
显然,Tiny-FatFs 是以牺牲性能换取非常小的代码量。

Figure 1 shows that partial sector data is transferred via the file I/O buffer. At long
data transfer shown in Figure 2, middle of transfer data that aligned to sector
boundary is transferred into memory directly. Figure 3 shows that entier transfer data
is aligned to the sector boundary. In this case, file I/O buffer is not used. At the
unbuffered transfer, maximum extent of sectors are read with disk_read function at a
time but it never across cluster boundary even if it is contiguous.

Figure 1 指出不够一个扇区数据通过 I/O 缓存传输的. Figure 2 则表时是大量数据


传输时情形,中间满足一个扇区对齐的数据直接传输到内存(不经过 I/O
buffer)。Figure 3 则是整个要传输的数据都是扇区对齐的。这种情况下不会使
用 I/O 缓存。在这种无缓存传输中,disk_read 函数每次会最大程度读取扇区数
目,但不会跨越簇边界,就算是连续的也不会。

Therefore taking effort to sector aligned read/write accesss eliminates memcpy and
the read/write performance will be improved. Besides the effect, cached FAT sector is
not flushed during read/write access on the Tiny-FatFs, so that it can achieve same
performance as FatFs and its small memory footprint simultanesously.

所以,尽量使读写扇区对齐,可以消除 memcpy 函数的时间浪费,增强读写性


能。由于这个效果,Tiny-FatFs 的 FAT 的扇区缓存在读写时不能被清除,这样
它才能同时达到 FatFs 性能和非常小的代码量。

Critical section

临界区

When write operation to the FAT file system is interrupted due to any accidental
failure, such as sudden blackout, incorrect disk removal and unrecoverable data error,
the FAT structure can be destroyed. Following images shows the critical section on
the FatFs module.

当在 FAT 文件系统写操作时,由于一些意外的错误中断,例如突然断电,错误
地移除磁盘和不可恢复的数据错误,FAT 结构都会被破坏。下图展示了 FatFs
模块的临界区域。

Figure 4. Long critical section


图 4.长的临界区

Figure 5. Minimized critical section


图 5.把临界区减到最小
An interruption in the red section can cause a cross link; as a result, the file/directory
being changed will be lost. There is one or more possibility listed below when an
interruption in the yellow section is occured.

在红色区域执行时被打断,会产生交叉连接;结果就是,被改名的文件或者目
录丢失。以下列出在黄色区域执行时被打断时会可能发生的情况。

 File data being rewrited is collapted.


 A file being appended returns initial state.
 A file created as new is gone.
 A file created as new or in overwritten remains with length of zero.
 Efficiency of disk use gets worse due to lost chain.
 重写的文件数据将产生错误。(collapted 是什么意思?是不是拼错了)
 被告附加内容的文件返回到初如状态。
 新建文件丢失。
 新建的文件或者被告覆盖的文件长度变为 0。
 由于失去连接,磁盘的使用效率变差。

Each case does not affect the files that not in write operation. To minimize risk of data
loss, the critical section can be minimized like shown in Figure 5 by minimizing the
time that file is opened in write mode and using f_sync function properly.

这些情况对于没有在写操作的文件是没有影响的。为了使数据丢失减到最低,
应该像图 5 那样在文件打开进行写操作期间正确使用 f_sync 函数使临界区减到
最小,从而减少临界区时间。

Problems and Ideas

存在的问题与想法

 Abstraction of file object


Currently the file object structure is held by application layer. This is not a
problem for Tiny-FatFs, it consumes the task stack only 26 bytes per file. But
FatFs requires 550 bytes per file. When managed the file objects in file handle,
stack consumption at application module will able to be reduced. In this case,
the work area for file objects is managed in FatFs module. This has a
disadvantage that direct access to the file object structure cannot be used and
additional file functions, such as feof and ftell, will be needed.
 抽象的文件对象
目前,该文件对象的结构是由应用层处理的。对于 Tiny-FatFs 来说不是
一个问题,每个文件只消耗任务堆栈的仅仅 26 个字节。但 FatFs 需要每
个文件 550 字节。 当以文件句柄管理文件时,应用模块的堆栈消耗将能
够降低。在这种情况下,文件对象的工作区将会被 FatFs 模块管理 。这
样做了弱点是不能直接访问文件对象,并且需要增加文件函数,例如
feof 和 ftell。
 Efficient sector buffer management
The FatFs module has only one sector buffer per logical drive. There is an
ineffciency on random file access with many files simultanesously. When
additional memory for the sector buffer is available, the file access
performance will able to be improved with an advanced cache mechanism.
 高效的扇区缓存管理
FatFs 模块只为每个逻辑盘分配一个扇区缓存。这对于同时随机存取多个
文件效率低下。当在允许增加扇区缓存的情况下,使用更先进的缓存机
制能有效提高文件访问性能。
 Long file name
There is an extended feature to handle long file name (LFN) up to 255
characters in addition to 8.3 format file name on the FAT file system. To
support this feature, 512 byte string buffer for file name and Unicode - Local
code mutual conversion table which occupies 256KB is required. Therefore
memory consumption of code and work area will be increased drastically. The
FatFs module currently does not support this feature. The LFN on the FAT file
system is a patent of Microsoft. When use it on the commercial products, you
have to be licensed.
 长文件名
FAT 文件系统 有一个扩展功能,除了 8.3 文件名格式外还能够处理多达
255 个字符的长文件名。要支持这个特性,要增加 512 字节文件名缓存和
256KB Unicode-本地码互换表。因此内存和代码量会大幅增加。所以
FatFs 模块目前不支持长文件名。FAT 文件系统的长文件名(LNF)是微软
公司的专利。在商业用途时你必须得到授权。
 Porting to RTOS
When use FatFs module from only one task, no consideration is needed.
However when make access to a logical drive from two or more tasks
simultanesously, any exclusion control will be required. The FatFs module is
also ported to a free RTOS based on μITRON by TOPPERS Project.
 移植到实时操作系统
当使用 FatFs 模块只有一个任务,没有考虑 RTOS 的必要。然而,当同时
对两个或以上任务访问逻辑盘时,任何排斥的控制将是需要的。FatFs 模
块也被移植到了基于由 TOPPERS Project 维护的 μITRON 的免费实时操
作系统上。

These are the problems and ideas on current revision of FatFs module. However the
main target of FatFs module is 8 bit microcontrollers. These extensions requires much
resource and the FatFs will unable to be ported to the 8 bit system. This may be the
most serious problem on future plan.

这些是目前 FatFs 版本存在的问题与想法。然而 FatFs 模块的目标系统是 8 位机


的。这些扩展需要很多资源,使得 FatFs 不能再被移植到 8 位系统上。这可能是
今后计划的最重要的问题。

racinglam@gmail.com rax@2009-02-02

You might also like