fatfs v0.03a Dec 11, 2006:

- Improved cluster scan algolithm to write files fast.
- Fixed f_mkdir() creates incorrect directory on FAT32.
This commit is contained in:
savelij13 2025-09-11 08:59:00 +03:00
parent 9a43e8b517
commit f76dc3a97f
24 changed files with 225 additions and 175 deletions

View File

@ -39,25 +39,25 @@
<div class="para"> <div class="para">
<h3>Memory Usage</h3> <h3>Memory Usage</h3>
<p>There are some configuration options to eliminate unused functions to reduce module size. Following tables are memory consumption in unit of byte on avr-gcc.</p> <p>There are some configuration options to eliminate unused functions to reduce module size. Following tables are the memory consumption in unit of byte on avr-gcc.</p>
<div class="lset"> <div class="lset">
<table class="lst2"> <table class="lst2">
<caption>FatFs</caption> <caption>FatFs</caption>
<tr><th>Section</th><th>Std cfg.</th><th>Min cfg.</th></tr> <tr><th>Section</th><th>Std cfg.</th><th>Min cfg.</th></tr>
<tr><td>Program (R/W cfg.)</td><td>8554</td><td>6020</td></tr> <tr><td>Program (R/W cfg.)</td><td>8526</td><td>6042</td></tr>
<tr><td>Program (R/O cfg.)</td><td>4170</td><td>3400</td></tr> <tr><td>Program (R/O cfg.)</td><td>4172</td><td>3402</td></tr>
<tr><td>Static Work Area</td><td colspan="2">2</td> <tr><td>Static Work Area</td><td colspan="2">2</td>
<tr><td>Dynamic Work Area</td><td colspan="2">544 + 540 * &lt;files&gt;</td> <tr><td>Dynamic Work Area</td><td colspan="2">548 + 540 * &lt;files&gt;</td>
</table> </table>
</div> </div>
<table class="lst2"> <table class="lst2">
<caption>Tiny-FatFs</caption> <caption>Tiny-FatFs</caption>
<tr><th>Section</th><th>Std cfg.</th><th>Min cfg.</th></tr> <tr><th>Section</th><th>Std cfg.</th><th>Min cfg.</th></tr>
<tr><td>Program (R/W cfg.)</td><td>7112</td><td>4836</td></tr> <tr><td>Program (R/W cfg.)</td><td>7138</td><td>4862</td></tr>
<tr><td>Program (R/O cfg.)</td><td>3538</td><td>2774</td></tr> <tr><td>Program (R/O cfg.)</td><td>3542</td><td>2778</td></tr>
<tr><td>Static Work Area</td><td colspan="2">2</td></tr> <tr><td>Static Work Area</td><td colspan="2">2</td></tr>
<tr><td>Dynamic Work Area</td><td colspan="2">538 + 24 * &lt;files&gt;</td></tr> <tr><td>Dynamic Work Area</td><td colspan="2">540 + 24 * &lt;files&gt;</td></tr>
</table> </table>
</div> </div>
@ -87,7 +87,7 @@
<div class="para"> <div class="para">
<h3>Disk I/O Interface</h3> <h3>Disk I/O Interface</h3>
<p>Since the FatFs/Tiny-FatFs module is completely separated from disk I/O layer, it requires following functions to lower layer to read/write physical disk and to get current time.</p> <p>Since the FatFs/Tiny-FatFs module is completely separated from disk I/O layer, it requires following functions to lower layer to read/write physical disk and to get current time. These functions must be provided by user.</p>
<ul> <ul>
<li><a href="en/dinit.html">disk_initialize</a> - Initialize disk drive</li> <li><a href="en/dinit.html">disk_initialize</a> - Initialize disk drive</li>
<li><a href="en/dstat.html">disk_status</a> - Get disk status</li> <li><a href="en/dstat.html">disk_status</a> - Get disk status</li>
@ -103,13 +103,11 @@
<p>The FatFs/Tiny-FatFs module is a free software and is opened for education, research and development. You can use, modify and/or republish it for personal, non-profit or profit use without any restriction under your responsibility.</p> <p>The FatFs/Tiny-FatFs module is a free software and is opened for education, research and development. You can use, modify and/or republish it for personal, non-profit or profit use without any restriction under your responsibility.</p>
<br> <br>
<ul> <ul>
<li><a href="ff003.zip">FatFs/Tiny-FatFs R0.03 Source Files</a> <span class="mfd">Sep. 22, 2006</span></li>
<li><a href="rwtest.png">Benchmark</a> (Test Board: ATmega64/9.2MHz with <a href="rw_mmc.jpeg">MMC</a>/<a href="rw_ata.jpeg">HDD</a>/<a href="rw_cfc.jpeg">CFC</a>)</li> <li><a href="rwtest.png">Benchmark</a> (Test Board: ATmega64/9.2MHz with <a href="rw_mmc.jpeg">MMC</a>/<a href="rw_ata.jpeg">HDD</a>/<a href="rw_cfc.jpeg">CFC</a>)</li>
<li><a href="http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx">FAT32 Specification</a> (Microsoft)</li> <li><a href="http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx">FAT32 Specification</a> (Microsoft)</li>
<li><a href="http://elm-chan.org/docs/mmc/mmc_e.html">How to Use an MMC</a></li> <li><a href="../../docs/mmc/mmc_e.html">How to Use an MMC</a></li>
</ul> </ul>
</div> </div>
</body> </body>
</html> </html>

View File

@ -44,19 +44,19 @@
<table class="lst2"> <table class="lst2">
<caption>FatFs</caption> <caption>FatFs</caption>
<tr><th>セクション</th><th>標準</th><th>最小</th></tr> <tr><th>セクション</th><th>標準</th><th>最小</th></tr>
<tr><td>プログラム(R/W構成)</td><td>8554</td><td>6020</td></tr> <tr><td>プログラム(R/W構成)</td><td>8526</td><td>6042</td></tr>
<tr><td>プログラム(R/O構成)</td><td>4170</td><td>3400</td></tr> <tr><td>プログラム(R/O構成)</td><td>4172</td><td>3402</td></tr>
<tr><td>静的ワークエリア</td><td colspan="2">2</td> <tr><td>静的ワークエリア</td><td colspan="2">2</td>
<tr><td>動的ワークエリア</td><td colspan="2">544 + 540 * &lt;files&gt;</td> <tr><td>動的ワークエリア</td><td colspan="2">548 + 540 * &lt;files&gt;</td>
</table> </table>
</div> </div>
<table class="lst2"> <table class="lst2">
<caption>Tiny-FatFs</caption> <caption>Tiny-FatFs</caption>
<tr><th>セクション</th><th>標準</th><th>最小</th></tr> <tr><th>セクション</th><th>標準</th><th>最小</th></tr>
<tr><td>プログラム(R/W構成)</td><td>7112</td><td>4836</td></tr> <tr><td>プログラム(R/W構成)</td><td>7138</td><td>4862</td></tr>
<tr><td>プログラム(R/O構成)</td><td>3538</td><td>2774</td></tr> <tr><td>プログラム(R/O構成)</td><td>3542</td><td>2778</td></tr>
<tr><td>静的ワークエリア</td><td colspan="2">2</td></tr> <tr><td>静的ワークエリア</td><td colspan="2">2</td></tr>
<tr><td>動的ワークエリア</td><td colspan="2">538 + 24 * &lt;files&gt;</td></tr> <tr><td>動的ワークエリア</td><td colspan="2">540 + 24 * &lt;files&gt;</td></tr>
</table> </table>
</div> </div>
@ -99,16 +99,14 @@
<div class="para"> <div class="para">
<h3>資料</h3> <h3>資料</h3>
<p>FatFs/Tiny-FatFsモジュールはフリーウェアとして教育・研究・開発用に公開しています。どのような利用目的個人・非商用・商用でも使用・改変・配布について一切の制限はありませんが、全て利用者の責任の下での利用とします。</p> <p>FatFs/Tiny-FatFsモジュールはフリーソフトウェアとして教育・研究・開発用に公開しています。どのような利用目的(個人・非商用・商用)でも使用・改変・配布について一切の制限はありませんが、全て利用者の責任の下での利用とします。</p>
<br> <br>
<ul> <ul>
<li><a href="ff003.zip">FatFs/Tiny-FatFs R0.03 ソースファイル</a> <span class="mfd">2006. 9. 22</span></li>
<li><a href="rwtest.png">パフォーマンス・テスト</a>テストボードATmega64/9.2MHz with <a href="rw_mmc.jpeg">MMC</a>/<a href="rw_ata.jpeg">HDD</a>/<a href="rw_cfc.jpeg">CFC</a></li> <li><a href="rwtest.png">パフォーマンス・テスト</a>テストボードATmega64/9.2MHz with <a href="rw_mmc.jpeg">MMC</a>/<a href="rw_ata.jpeg">HDD</a>/<a href="rw_cfc.jpeg">CFC</a></li>
<li><a href="http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx">FAT32仕様書</a> (Microsoft)</li> <li><a href="http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx">FAT32仕様書</a> (Microsoft)</li>
<li><a href="http://elm-chan.org/docs/mmc/mmc.html">MMCの使いかた</a></li> <li><a href="../../docs/mmc/mmc.html">MMCの使いかた</a></li>
</ul> </ul>
</div> </div>
</body> </body>
</html> </html>

View File

@ -79,7 +79,7 @@ FRESULT f_chmod (
<div class="para"> <div class="para">
<h4>Example</h4> <h4>Example</h4>
<pre> <pre>
// Set read-only flag, clear archive flag and others are not changed. // Set read-only flag, clear archive flag and others are left unchanged.
f_chmod("file.txt", AR_RDO, AR_RDO | AR_ARC); f_chmod("file.txt", AR_RDO, AR_RDO | AR_ARC);
</pre> </pre>
</div> </div>

View File

@ -46,7 +46,7 @@ FRESULT f_close (
<div class="para"> <div class="para">
<h4>Description</h4> <h4>Description</h4>
<p>Function f_close closes a file. When the function succeeded, the file object structure can be discarded.</p> <p>Function f_close closes a file. If any data has been written, the cached information of the file is written back to the disk. When the function succeeded, the file object is no longer valid and structure can be discarded.</p>
</div> </div>

View File

@ -39,7 +39,7 @@ FRESULT f_mkdir (
<dt>FR_INVALID_NAME</dt> <dt>FR_INVALID_NAME</dt>
<dd>The path name is invalid.</dd> <dd>The path name is invalid.</dd>
<dt>FR_DENIED</dt> <dt>FR_DENIED</dt>
<dd>The function was denied due to any of following reasons: any file or directory that has same name is existing, cannot be created due to the directory table or disk full.</dd> <dd>The function was denied due to any of following reasons: any file or directory that has same name is existing, cannot be created due to directory table or disk is full.</dd>
<dt>FR_NOT_READY</dt> <dt>FR_NOT_READY</dt>
<dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd> <dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>
<dt>FR_WRITE_PROTECTED</dt> <dt>FR_WRITE_PROTECTED</dt>

View File

@ -37,8 +37,8 @@ FRESULT f_mountdrv (void);
<div class="para"> <div class="para">
<h4>Description</h4> <h4>Description</h4>
<p>The f_mountdrv forces the partition mounted (initializes <tt>FATFS</tt> structure). This function should not be used except for recover FR_INCORRECT_DISK_CHANGE error. The file system is initialized automatically in accordance with the necessity when any file function is called.</p> <p>The f_mountdrv forces the partition mounted (initializes <tt>FATFS</tt> structure). The file system is initialized automatically in accordance with the necessity when any file function is called. This function should not be used except for recovering <tt>FR_INCORRECT_DISK_CHANGE</tt> error. Using this function, while any file is opened, can destroy the file system.</p>
<p>To terminate use of the FatFs module, close all files and then the <tt>FATFS</tt> structure can be discarded. In this function, following process is executed.</p><br> <p>In this function, following processes are executed.</p><br>
<ul> <ul>
<li>Initializes lower level layer (disk I/O module).</li> <li>Initializes lower level layer (disk I/O module).</li>
<li>Searches a FAT boot record in order of sector 0 (as SFD format) and first primary partition (as FDISK format).</li> <li>Searches a FAT boot record in order of sector 0 (as SFD format) and first primary partition (as FDISK format).</li>

View File

@ -35,9 +35,9 @@ FRESULT f_open (
<tr><th>Value</th><th>Description</th></tr> <tr><th>Value</th><th>Description</th></tr>
<tr><td>FA_READ</td><td>Specifies read access to the object. Data can be read from the file.<br>Combine with FA_WRITE for read-write access.</td></tr> <tr><td>FA_READ</td><td>Specifies read access to the object. Data can be read from the file.<br>Combine with FA_WRITE for read-write access.</td></tr>
<tr><td>FA_WRITE</td><td>Specifies write access to the object. Data can be written to the file.<br>Combine with FA_READ for read-write access.</td></tr> <tr><td>FA_WRITE</td><td>Specifies write access to the object. Data can be written to the file.<br>Combine with FA_READ for read-write access.</td></tr>
<tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file does not exist.</td></tr> <tr><td>FA_OPEN_EXISTING</td><td>Opens the file. The function fails if the file is not existing.</td></tr>
<tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file exists, it is truncated and overwritten.</td></tr> <tr><td>FA_CREATE_ALWAYS</td><td>Creates a new file. If the file is existing, it is truncated and overwritten.</td></tr>
<tr><td>FA_OPEN_ALWAYS</td><td>Opens the file, if it exists. If the file does not exist, the function creates the file.</td></tr> <tr><td>FA_OPEN_ALWAYS</td><td>Opens the file, if it is existing. If not, the function creates the new file.</td></tr>
</table> </table>
</dd> </dd>
</dl> </dl>
@ -75,7 +75,7 @@ FRESULT f_open (
<div class="para"> <div class="para">
<h4>Description</h4> <h4>Description</h4>
<p>To start to use the FatFs module, prepare a work area (<tt>FATFS</tt> structure), clear it and set its address to the global pointer 'FatFs' to allocate the work area to the FatFs module. Then the FatFs module can work.</p> <p>To start FatFs module, prepare a work area (<tt>FATFS</tt> structure), fill it with zero and set its address to the global variable <em>FatFs</em> to give the work area to the FatFs module. All file functions can work after the initialization. <a href="mountdrv.html">f_mountdrv()</a> can also be used instead of the zero filling, however, the physical drive must be ready at that time. To terminate use of the FatFs module, close all files, clear <em>FatFs</em> and then the work area can be discarded.</p>
<p>Flags <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS</tt> are not supported in read-only configuration.</p> <p>Flags <tt>FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS</tt> are not supported in read-only configuration.</p>
</div> </div>
@ -92,9 +92,9 @@ void main ()
WORD br, bw; // File R/W count WORD br, bw; // File R/W count
// Give a work area to FatFs module (activate module) // Give a work area to FatFs module (activate FatFs module)
memset(&fs, 0, sizeof(FATFS));
FatFs = &fs; FatFs = &fs;
memset(&fs, 0, sizeof(FATFS)); // Clear work area. f_mountdrv() can also be used instead.
// Open source file // Open source file
res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ); res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ);

View File

@ -64,16 +64,17 @@ FRESULT f_rename (
<div class="para"> <div class="para">
<h4>Description</h4> <h4>Description</h4>
<p>Rename a file or directory and can move it to other directory. This function is not supported in read-only configuration or minimization level of &gt;= 1.</p> <p>Rename a file or directory and can move it to other directory. This function is not supported in read-only configuration or minimization level of &gt;= 1.</p>
<p>Note: In this revision, moving any directory to other directory collapses the file system.</p>
</div> </div>
<div class="para"> <div class="para">
<h4>Example</h4> <h4>Example</h4>
<pre> <pre>
// Rename an object // Rename file or directory
f_rename("oldname.txt", "newname.txt"); f_rename("oldname.txt", "newname.txt");
// Rename and move to other directory simultaneously // Rename and move file to other directory simultaneously
f_rename("oldname.txt", "dir1/newname.txt"); f_rename("oldname.txt", "dir1/newname.txt");
</pre> </pre>
</div> </div>

View File

@ -16,40 +16,42 @@
<h4>FatFs</h4> <h4>FatFs</h4>
<pre> <pre>
typedef struct _FATFS { typedef struct _FATFS {
BYTE fs_type; // FAT type BYTE fs_type; /* FAT type */
BYTE files; // Number of files currently opend BYTE files; /* Number of files currently opend */
BYTE sects_clust; // Sectors per cluster BYTE sects_clust; /* Sectors per cluster */
BYTE n_fats; // Number of FAT copies BYTE n_fats; /* Number of FAT copies */
WORD n_rootdir; // Number of root directory entry (always 0 in FAT32) WORD n_rootdir; /* Number of root directory entry */
BYTE winflag; // win[] dirty flag (1:must be written back) BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE pad1; BYTE pad1;
DWORD sects_fat; // Sectors per fat DWORD winsect; /* Current sector appearing in the win[] */
DWORD max_clust; // Maximum cluster# + 1 DWORD sects_fat; /* Sectors per fat */
DWORD fatbase; // FAT start sector DWORD max_clust; /* Maximum cluster# + 1 */
DWORD dirbase; // Root directory start sector (cluster# for FAT32) DWORD fatbase; /* FAT start sector */
DWORD database; // Data start sector DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */
DWORD winsect; // Current sector# appearing in the win[] DWORD database; /* Data start sector */
BYTE win[512]; // Disk access window for directory and FAT DWORD last_clust; /* Last allocated cluster */
BYTE win[512]; /* Disk access window for Directory/FAT */
} FATFS; } FATFS;
</pre> </pre>
<h4>Tiny-FatFs</h4> <h4>Tiny-FatFs</h4>
<pre> <pre>
typedef struct _FATFS { typedef struct _FATFS {
BYTE fs_type; // FAT type BYTE fs_type; /* FAT type */
BYTE files; // Number of files currently opend BYTE files; /* Number of files currently opend */
BYTE sects_clust; // Sectors per cluster BYTE sects_clust; /* Sectors per cluster */
BYTE n_fats; // Number of FAT copies BYTE n_fats; /* Number of FAT copies */
WORD n_rootdir; // Number of root directory entry WORD n_rootdir; /* Number of root directory entry */
BYTE winflag; // win[] dirty flag (1:must be written back) BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE pad1; BYTE pad1;
WORD sects_fat; // Sectors per fat DWORD winsect; /* Current sector appearing in the win[] */
WORD max_clust; // Maximum cluster# + 1 WORD sects_fat; /* Sectors per fat */
DWORD fatbase; // FAT start sector WORD max_clust; /* Maximum cluster# + 1 */
DWORD dirbase; // Root directory start sector DWORD fatbase; /* FAT start sector */
DWORD database; // Data start sector DWORD dirbase; /* Root directory start sector */
DWORD winsect; // Current sector# appearing in the win[] DWORD database; /* Data start sector */
BYTE win[512]; // Disk access window for directory, FAT and file WORD last_clust; /* Last allocated cluster */
BYTE win[512]; /* Disk access window for Directory/FAT/File */
} FATFS; } FATFS;
</pre> </pre>
</div> </div>

View File

@ -46,7 +46,7 @@ FRESULT f_sync (
<div class="para"> <div class="para">
<h4>Description</h4> <h4>Description</h4>
<p>The f_sync() writes back the cached information of the file being written. This performs the same function as f_close() but the file is left opened and can continue file read/write operations to the file. This is suitable for applications that open files for a long time in writing mode, such as data logger. Performing f_sync() of periodic or immediataly after f_write() can minimize risk of data loss due to sudden blackout or unintentional disk removal. This function is not supported in read-only configuration.</p> <p>The function f_sync() performs the same process as f_close() but the file is left opened and can continue file read/write operations to the file. This is suitable for applications that open files for a long time in writing mode, such as data logger. Performing f_sync() of periodic or immediataly after f_write() can minimize risk of data loss due to sudden blackout or unintentional disk removal. This function is not supported in read-only configuration.</p>
</div> </div>

View File

@ -45,7 +45,7 @@ FRESULT f_unlink (
<dt>FR_NOT_READY</dt> <dt>FR_NOT_READY</dt>
<dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd> <dd>The disk drive cannot work due to no medium in the drive or any other reason.</dd>
<dt>FR_WRITE_PROTECTED</dt> <dt>FR_WRITE_PROTECTED</dt>
<dd>Write mode open or creation under the medium is write protected.</dd> <dd>The medium is write protected.</dd>
<dt>FR_RW_ERROR</dt> <dt>FR_RW_ERROR</dt>
<dd>Any error occured in low level disk I/O.</dd> <dd>Any error occured in low level disk I/O.</dd>
<dt>FR_INCORRECT_DISK_CHANGE</dt> <dt>FR_INCORRECT_DISK_CHANGE</dt>

View File

@ -31,10 +31,10 @@ FRESULT f_chmod (
<dd>設定する属性。指定可能な属性は次の通りで、これらの組み合わせで指定します。指定されなかった属性は解除されます。<br> <dd>設定する属性。指定可能な属性は次の通りで、これらの組み合わせで指定します。指定されなかった属性は解除されます。<br>
<table class="lst"> <table class="lst">
<tr><th></th><th>意味</th></td> <tr><th></th><th>意味</th></td>
<tr><td>AR_RDO</td><td>リードオンリー</td></td> <tr><td>AM_RDO</td><td>リードオンリー</td></td>
<tr><td>AR_ARC</td><td>アーカイブ</td></td> <tr><td>AM_ARC</td><td>アーカイブ</td></td>
<tr><td>AR_SYS</td><td>システム</td></td> <tr><td>AM_SYS</td><td>システム</td></td>
<tr><td>AR_HID</td><td>ヒドゥン</td></td> <tr><td>AM_HID</td><td>ヒドゥン</td></td>
</table> </table>
</dd> </dd>
<dt>AttributeMask</dt> <dt>AttributeMask</dt>
@ -79,8 +79,8 @@ FRESULT f_chmod (
<div class="para"> <div class="para">
<h4>使用例</h4> <h4>使用例</h4>
<pre> <pre>
// Set read-only flag , clear archive flag and others are not changed. // Set read-only flag , clear archive flag and others are left unchanged.
f_chmod("/file.txt", AR_RDO, AR_RDO | AR_ARC); f_chmod("file.txt", AM_RDO, AM_RDO | AM_ARC);
</pre> </pre>
</div> </div>

View File

@ -24,7 +24,7 @@ FRESULT f_close (
<h4>パラメータ</h4> <h4>パラメータ</h4>
<dl class="par"> <dl class="par">
<dt>FileObject</dt> <dt>FileObject</dt>
<dd>閉じるファイルオブジェクト構造体へのポインタを指定します。</dd> <dd>閉じようとすファイルのファイルオブジェクト構造体へのポインタを指定します。</dd>
</dl> </dl>
</div> </div>
@ -46,7 +46,7 @@ FRESULT f_close (
<div class="para"> <div class="para">
<h4>解説</h4> <h4>解説</h4>
<p>ファイルを閉じます。関数が正常終了したら、そのファイルオブジェクト構造体を破棄できます。</p> <p>ファイルを閉じます。書き込みの行われたファイルの場合、キャッシュされた状態R/Wバッファ上のデータ、変更されたFATやディレクトリ項目はディスクに書き戻されます。関数が正常終了すると、そのファイルオブジェクト構造体は無効になり、そのメモリも解放できます。</p>
</div> </div>

View File

@ -24,7 +24,7 @@ FRESULT f_mkdir (
<h4>パラメータ</h4> <h4>パラメータ</h4>
<dl class="par"> <dl class="par">
<dt>DirName</dt> <dt>DirName</dt>
<dd>作成するディレクトリのフルパス名が入った<tt>'\0'</tt>で終わる文字列へのポインタを指定します。ディレクトリセパレータには<tt>'/'</tt>を使用します。FatFsモジュールにはカレントディレクトリの概念がないので、パスはルートディレクトリから辿る絶対パスとなります。文字列先頭のスペースはスキップされます。パス先頭の<tt>'/'</tt>はあってもなくてもかまいません。リードオンリー構成および _FS_MINIMIZE &gt;= 1 ではこの関数はサポートされません。</dd> <dd>作成するディレクトリのフルパス名が入った<tt>'\0'</tt>で終わる文字列へのポインタを指定します。ディレクトリセパレータには<tt>'/'</tt>を使用します。FatFsモジュールにはカレントディレクトリの概念がないので、パスはルートディレクトリから辿る絶対パスとなります。文字列先頭のスペースはスキップされます。パス先頭の<tt>'/'</tt>はあってもなくてもかまいません。</dd>
</dl> </dl>
</div> </div>

View File

@ -38,7 +38,7 @@ FRESULT f_mountdrv (void);
<div class="para"> <div class="para">
<h4>解説</h4> <h4>解説</h4>
<p>ファイルシステムを強制的に初期化マウントします。FatFsモジュールではマウント動作はファイル関数呼び出し時に必要に応じて内部で行われるので、通常はこの関数を使用すべきではありません。自動マウント動作中に回復不能エラーたとえば<tt>FR_INCORRECT_DISK_CHANGE</tt>)が発生した場合、全てのファイル関数が使えなくなるので、そのときはこの関数で再マウントして回復することができます。</p> <p>ファイルシステムを強制的に初期化マウントします。FatFsモジュールではマウント動作はファイル関数呼び出し時に必要に応じて内部で行われるので、通常はこの関数を使用すべきではありません。自動マウント動作中に回復不能エラーたとえば<tt>FR_INCORRECT_DISK_CHANGE</tt>)が発生した場合、全てのファイル関数が使えなくなるので、そのときはこの関数で再マウントして回復することができます。</p>
<p>FatFsモジュールの使用を終了するには、全てのファイルを閉じたあとグローバル変数<tt>FatFs</tt>をクリアします。その後、FatFsモジュールのワークエリアは解放できます。f_mountdrv関数内では次の処理が行われます。</p> <p>f_mountdrv関数内では次の処理が行われます。</p>
<br> <br>
<ul> <ul>
<li>下位レイヤディスクI/Oモジュールの初期化。</li> <li>下位レイヤディスクI/Oモジュールの初期化。</li>

View File

@ -15,7 +15,7 @@
<p>ファイルをオープンまたは作成します。</p> <p>ファイルをオープンまたは作成します。</p>
<pre> <pre>
FRESULT f_open ( FRESULT f_open (
FIL* <em>FileObject</em>, // 作成するファイルオブジェクト構造体へのポインタ FIL* <em>FileObject</em>, // 空のファイルオブジェクト構造体へのポインタ
const char* <em>FileName</em>, // ファイルのフルパス名へのポインタ const char* <em>FileName</em>, // ファイルのフルパス名へのポインタ
BYTE <em>ModeFlags</em> // モードフラグ BYTE <em>ModeFlags</em> // モードフラグ
); );
@ -75,7 +75,7 @@ FRESULT f_open (
<div class="para"> <div class="para">
<h4>解説</h4> <h4>解説</h4>
<p>FatFsモジュールの使用を開始するにはまず、FatFsモジュールにワークエリア(構造体<tt>FATFS</tt>を割り当てます。確保したワークエリアを0で初期化したあと、FatFsモジュールのグローバル変数<em>FatFs</em>にそのアドレスを代入するだけでモジュールは動作可能状態になり、ファイル関数が使えるようになります。</p> <p>FatFsモジュールの使用を開始するにはまず、FatFsモジュールのグローバル変数<em>FatFs</em>にゼロクリアしたワークエリア(<tt>FATFS</tt>構造体)のアドレスを代入します。このようにしてFatFsモジュールにワークエリアを割り当てるとモジュールは動作可能状態になり、ファイル関数が使えるようになります。ゼロクリアの代わりに<a href="mountdrv.html">f_mountdrv()</a>でもOKですが、少なくともその時点で物理ドライブが動作可能になっていなければなりません。FatFsモジュールの使用を終了するには、全てのファイルを閉じたあと<em>FatFs</em>をクリアします。その後、ワークエリアは破棄できます。</p>
<p>リードオンリー構成では、<tt>FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS</tt>の各フラグはサポートされません。</p> <p>リードオンリー構成では、<tt>FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS</tt>の各フラグはサポートされません。</p>
</div> </div>
@ -93,8 +93,8 @@ void main ()
// FatFsのワークエリアを確保する // FatFsのワークエリアを確保する
memset(&fs, 0, sizeof(FATFS));
FatFs = &fs; FatFs = &fs;
memset(&fs, 0, sizeof(FATFS)); // ワークエリアの初期化。代わりに f_mountdrv() でもよい
// ソース・ファイルを開く // ソース・ファイルを開く
res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ); res = f_open(&fsrc, "srcfile.dat", FA_OPEN_EXISTING | FA_READ);

View File

@ -11,7 +11,7 @@
<body> <body>
<div class="para"> <div class="para">
<h2>f_chmod</h2> <h2>f_rename</h2>
<p>ファイルまたはディレクトリの名前の変更または移動。</p> <p>ファイルまたはディレクトリの名前の変更または移動。</p>
<pre> <pre>
FRESULT f_rename ( FRESULT f_rename (
@ -64,6 +64,7 @@ FRESULT f_rename (
<div class="para"> <div class="para">
<h4>解説</h4> <h4>解説</h4>
<p>ファイルまたはディレクトリの名前を変更します。別のディレクトリへの移動も可能です。リードオンリー構成および<tt>_FS_MINIMIZE &gt;= 1</tt>ではこの関数はサポートされません。</p> <p>ファイルまたはディレクトリの名前を変更します。別のディレクトリへの移動も可能です。リードオンリー構成および<tt>_FS_MINIMIZE &gt;= 1</tt>ではこの関数はサポートされません。</p>
<p>※現リビジョンでは、ディレクトリを別のディレクトリに移動するとファイルシステムが壊れます。</p>
</div> </div>

View File

@ -16,40 +16,42 @@
<h4>FatFs</h4> <h4>FatFs</h4>
<pre> <pre>
typedef struct _FATFS { typedef struct _FATFS {
BYTE fs_type; // FAT type BYTE fs_type; /* FAT type */
BYTE files; // Number of files currently opend BYTE files; /* Number of files currently opend */
BYTE sects_clust; // Sectors per cluster BYTE sects_clust; /* Sectors per cluster */
BYTE n_fats; // Number of FAT copies BYTE n_fats; /* Number of FAT copies */
WORD n_rootdir; // Number of root directory entry WORD n_rootdir; /* Number of root directory entry */
BYTE winflag; // win[] dirty flag (1:must be written back) BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE pad1; BYTE pad1;
DWORD sects_fat; // Sectors per fat DWORD winsect; /* Current sector appearing in the win[] */
DWORD max_clust; // Maximum cluster# + 1 DWORD sects_fat; /* Sectors per fat */
DWORD fatbase; // FAT start sector DWORD max_clust; /* Maximum cluster# + 1 */
DWORD dirbase; // Root directory start sector (cluster# for FAT32) DWORD fatbase; /* FAT start sector */
DWORD database; // Data start sector DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */
DWORD winsect; // Current sector# appearing in the win[] DWORD database; /* Data start sector */
BYTE win[512]; // Disk access window for Directory/FAT area DWORD last_clust; /* Last allocated cluster */
BYTE win[512]; /* Disk access window for Directory/FAT */
} FATFS; } FATFS;
</pre> </pre>
<h4>Tiny-FatFs</h4> <h4>Tiny-FatFs</h4>
<pre> <pre>
typedef struct _FATFS { typedef struct _FATFS {
BYTE fs_type; // FAT type BYTE fs_type; /* FAT type */
BYTE files; // Number of files currently opend BYTE files; /* Number of files currently opend */
BYTE sects_clust; // Sectors per cluster BYTE sects_clust; /* Sectors per cluster */
BYTE n_fats; // Number of FAT copies BYTE n_fats; /* Number of FAT copies */
WORD n_rootdir; // Number of root directory entry WORD n_rootdir; /* Number of root directory entry */
BYTE winflag; // win[] dirty flag (1:must be written back) BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE pad1; BYTE pad1;
WORD sects_fat; // Sectors per fat DWORD winsect; /* Current sector appearing in the win[] */
WORD max_clust; // Maximum cluster# + 1 WORD sects_fat; /* Sectors per fat */
DWORD fatbase; // FAT start sector WORD max_clust; /* Maximum cluster# + 1 */
DWORD dirbase; // Root directory start sector (cluster# for FAT32) DWORD fatbase; /* FAT start sector */
DWORD database; // Data start sector DWORD dirbase; /* Root directory start sector */
DWORD winsect; // Current sector# appearing in the win[] DWORD database; /* Data start sector */
BYTE win[512]; // Disk access window WORD last_clust; /* Last allocated cluster */
BYTE win[512]; /* Disk access window for Directory/FAT/File */
} FATFS; } FATFS;
</pre> </pre>
</div> </div>

View File

@ -46,7 +46,7 @@ FRESULT f_sync (
<div class="para"> <div class="para">
<h4>解説</h4> <h4>解説</h4>
<p>書き込み中のファイルのそこまでの状態R/Wバッファ上のデータ、変更されたFATやディレクトリ項目をディスクに書き込みます。この関数はファイルクローズのプロセスを実行しますが、ファイルは引き続き開かれたままになり、読み書きを続行できます。ロギングなど、書き込みモードで長時間ファイルが開かれているアプリケーションにおいて、定期的または区切りの良いところでsyncすることにより、不意の電源断やメディアの取り外しにより失われるデータを最小限にます。</p> <p>この関数は<tt>f_close()</tt>と同じ処理を実行しますが、ファイルは引き続き開かれたままになり、読み書きを続行できます。ロギングなど、書き込みモードで長時間ファイルが開かれているアプリケーションにおいて、定期的または区切りの良いところでsyncすることにより、不意の電源断やメディアの取り外しにより失われるデータを最小限にすることができます。</p>
<p>リードオンリー構成ではこの関数はサポートされません。</p> <p>リードオンリー構成ではこの関数はサポートされません。</p>
</div> </div>

View File

@ -1,4 +1,4 @@
FatFs/Tiny-FatFs Module Source Files R0.03 (C)ChaN, 2006 FatFs/Tiny-FatFs Module Source Files R0.03a (C)ChaN, 2006
FILES FILES
@ -40,7 +40,8 @@ CONFIGURATION OPTIONS
When application program does not require any write function, _FS_READONLY When application program does not require any write function, _FS_READONLY
can be defined to eliminate writing code to reduce module size. This is can be defined to eliminate writing code to reduce module size. This is
UNDEFINED in default. UNDEFINED in default. This setting should be reflected to configurations for
low level disk function if available.
_FS_MINIMIZE _FS_MINIMIZE
@ -83,8 +84,8 @@ AGREEMENTS
The FatFs/Tiny-FatFs module is a free software and there is no warranty. The FatFs/Tiny-FatFs module is a free software and there is no warranty.
The FatFs/Tiny-FatFs module is opened for education, reserch and development. The FatFs/Tiny-FatFs module is opened for education, reserch and development.
You can use, modify and/or republish it for personal, non-profit or profit You can use and/or modify it for personal, non-profit or profit use without
use without any restriction under your responsibility. any restriction under your responsibility.
@ -97,3 +98,5 @@ REVISION HISTORY
Jun 10, 2006 R0.02a Added a configuration option _FS_MINIMUM. Jun 10, 2006 R0.02a Added a configuration option _FS_MINIMUM.
Sep 22, 2006 R0.03 Added f_rename(). Sep 22, 2006 R0.03 Added f_rename().
Changed option _FS_MINIMUM to _FS_MINIMIZE. Changed option _FS_MINIMUM to _FS_MINIMIZE.
Dec 11, 2006 R0.03a Improved cluster scan algolithm to write files fast.
Fixed f_mkdir() creates incorrect directory on FAT32.

View File

@ -1,10 +1,17 @@
/*--------------------------------------------------------------------------/ /*--------------------------------------------------------------------------/
/ FatFs - FAT file system module R0.03 (C)ChaN, 2006 / FatFs - FAT file system module R0.03a (C)ChaN, 2006
/---------------------------------------------------------------------------/ /---------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to / FatFs module is an experimenal project to implement FAT file system to
/ cheap microcontrollers. This is a free software and is opened for education, / cheap microcontrollers. This is a free software and is opened for education,
/ research and development. You can use, modify and/or republish it for / research and development under license policy of following trems.
/ non-profit or profit use without any restriction under your responsibility. /
/ Copyright (C) 2006, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------/ /---------------------------------------------------------------------------/
/ Feb 26, 2006 R0.00 Prototype. / Feb 26, 2006 R0.00 Prototype.
/ Apr 29, 2006 R0.01 First stable version. / Apr 29, 2006 R0.01 First stable version.
@ -13,6 +20,8 @@
/ Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM). / Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM).
/ Sep 22, 2006 R0.03 Added f_rename(). / Sep 22, 2006 R0.03 Added f_rename().
/ Changed option _FS_MINIMUM to _FS_MINIMIZE. / Changed option _FS_MINIMUM to _FS_MINIMIZE.
/ Dec 11, 2006 R0.03a Improved cluster scan algolithm to write files fast.
/ Fixed f_mkdir() creates incorrect directory on FAT32.
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#include <string.h> #include <string.h>
@ -94,7 +103,7 @@ DWORD get_cluster (
case FS_FAT32 : case FS_FAT32 :
if (!move_window(fatsect + clust / 128)) break; if (!move_window(fatsect + clust / 128)) break;
return LD_DWORD(&(fs->win[((WORD)clust * 4) % 512])); return LD_DWORD(&(fs->win[((WORD)clust * 4) % 512])) & 0x0FFFFFFF;
} }
} }
return 1; /* There is no cluster information, or an error occured */ return 1; /* There is no cluster information, or an error occured */
@ -185,34 +194,36 @@ DWORD create_chain (
DWORD clust /* Cluster# to stretch, 0 means create new */ DWORD clust /* Cluster# to stretch, 0 means create new */
) )
{ {
DWORD ncl, ccl, mcl = FatFs->max_clust; DWORD cstat, ncl, scl, mcl;
FATFS *fs = FatFs;
mcl = fs->max_clust;
if (clust == 0) { /* Create new chain */ if (clust == 0) { /* Create new chain */
ncl = 1; scl = fs->last_clust; /* Get last allocated cluster */
do { if (scl < 2 || scl >= mcl) scl = 1;
ncl++; /* Check next cluster */
if (ncl >= mcl) return 0; /* No free custer was found */
ccl = get_cluster(ncl); /* Get the cluster status */
if (ccl == 1) return 0; /* Any error occured */
} while (ccl); /* Repeat until find a free cluster */
} }
else { /* Stretch existing chain */ else { /* Stretch existing chain */
ncl = get_cluster(clust); /* Check the cluster status */ cstat = get_cluster(clust); /* Check the cluster status */
if (ncl < 2) return 0; /* It is an invalid cluster */ if (cstat < 2) return 0; /* It is an invalid cluster */
if (ncl < mcl) return ncl; /* It is already followed by next cluster */ if (cstat < mcl) return cstat; /* It is already followed by next cluster */
ncl = clust; /* Search free cluster */ scl = clust;
do {
ncl++; /* Check next cluster */
if (ncl >= mcl) ncl = 2; /* Wrap around */
if (ncl == clust) return 0; /* No free custer was found */
ccl = get_cluster(ncl); /* Get the cluster status */
if (ccl == 1) return 0; /* Any error occured */
} while (ccl); /* Repeat until find a free cluster */
} }
ncl = scl; /* Scan start cluster */
do {
ncl++; /* Next cluster */
if (ncl >= mcl) { /* Wrap around */
ncl = 2;
if (scl == 1) return 0; /* No free custer was found */
}
if (ncl == scl) return 0; /* No free custer was found */
cstat = get_cluster(ncl); /* Get the cluster status */
if (cstat == 1) return 0; /* Any error occured */
} while (cstat); /* Repeat until find a free cluster */
if (!put_cluster(ncl, 0xFFFFFFFF)) return 0; /* Mark the new cluster "in use" */ if (!put_cluster(ncl, 0x0FFFFFFF)) return 0; /* Mark the new cluster "in use" */
if (clust && !put_cluster(clust, ncl)) return 0; /* Link it to previous one if needed */ if (clust && !put_cluster(clust, ncl)) return 0; /* Link it to previous one if needed */
fs->last_clust = ncl;
return ncl; /* Return new cluster number */ return ncl; /* Return new cluster number */
} }
@ -249,7 +260,8 @@ BYTE check_fs (
static const char fatsign[] = "FAT12FAT16FAT32"; static const char fatsign[] = "FAT12FAT16FAT32";
FATFS *fs = FatFs; FATFS *fs = FatFs;
/* Determines FAT type by signature string but this is not correct */ /* Determines FAT type by signature string but this is not correct.
For further information, refer to fatgen103.doc from Microsoft. */
memset(fs->win, 0, 512); memset(fs->win, 0, 512);
if (disk_read(fs->win, sect, 1) == RES_OK) { /* Load boot record */ if (disk_read(fs->win, sect, 1) == RES_OK) { /* Load boot record */
if (LD_WORD(&(fs->win[510])) == 0xAA55) { /* Is it valid? */ if (LD_WORD(&(fs->win[510])) == 0xAA55) { /* Is it valid? */
@ -1151,7 +1163,7 @@ FRESULT f_mkdir (
{ {
FRESULT res; FRESULT res;
BYTE *dir, *w, n; BYTE *dir, *w, n;
DWORD sect, dsect, dclust, tim; DWORD sect, dsect, dclust, pclust, tim;
DIR dirscan; DIR dirscan;
char fn[8+3+1]; char fn[8+3+1];
FATFS *fs = FatFs; FATFS *fs = FatFs;
@ -1184,11 +1196,13 @@ FRESULT f_mkdir (
*(w+11) = AM_DIR; *(w+11) = AM_DIR;
tim = get_fattime(); tim = get_fattime();
ST_DWORD(w+22, tim); ST_DWORD(w+22, tim);
memcpy(w+32, w, 32); *(w+33) = '.';
ST_WORD(w+26, dclust); ST_WORD(w+26, dclust);
ST_WORD(w+20, dclust >> 16); ST_WORD(w+20, dclust >> 16);
ST_WORD(w+32+26, dirscan.sclust); memcpy(w+32, w, 32); *(w+33) = '.';
ST_WORD(w+32+20, dirscan.sclust >> 16); pclust = dirscan.sclust;
if (fs->fs_type == FS_FAT32 && pclust == fs->dirbase) pclust = 0;
ST_WORD(w+32+26, pclust);
ST_WORD(w+32+20, pclust >> 16);
fs->winflag = 1; fs->winflag = 1;
if (!move_window(sect)) return FR_RW_ERROR; if (!move_window(sect)) return FR_RW_ERROR;

View File

@ -1,14 +1,23 @@
/*--------------------------------------------------------------------------/ /*--------------------------------------------------------------------------/
/ FatFs - FAT file system module include file R0.03 (C)ChaN, 2006 / FatFs - FAT file system module include file R0.03a (C)ChaN, 2006
/---------------------------------------------------------------------------/ /---------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to / FatFs module is an experimenal project to implement FAT file system to
/ cheap microcontrollers. This is a free software and is opened for education, / cheap microcontrollers. This is a free software and is opened for education,
/ research and development. You can use, modify and/or republish it for / research and development under license policy of following trems.
/ non-profit or profit use without any restriction under your responsibility. /
/ Copyright (C) 2006, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#ifndef _FATFS #ifndef _FATFS
#error Don't forget to change these options.
//#define _BYTE_ACC //#define _BYTE_ACC
/* The _BYTE_ACC enables byte-by-byte access for multi-byte variables. This /* The _BYTE_ACC enables byte-by-byte access for multi-byte variables. This
/ MUST be defined when multi-byte variable is stored in big-endian and/or / MUST be defined when multi-byte variable is stored in big-endian and/or
@ -45,12 +54,13 @@ typedef struct _FATFS {
WORD n_rootdir; /* Number of root directory entry */ WORD n_rootdir; /* Number of root directory entry */
BYTE winflag; /* win[] dirty flag (1:must be written back) */ BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE pad1; BYTE pad1;
DWORD winsect; /* Current sector appearing in the win[] */
DWORD sects_fat; /* Sectors per fat */ DWORD sects_fat; /* Sectors per fat */
DWORD max_clust; /* Maximum cluster# + 1 */ DWORD max_clust; /* Maximum cluster# + 1 */
DWORD fatbase; /* FAT start sector */ DWORD fatbase; /* FAT start sector */
DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */
DWORD database; /* Data start sector */ DWORD database; /* Data start sector */
DWORD winsect; /* Current sector appearing in the win[] */ DWORD last_clust; /* Last allocated cluster */
BYTE win[512]; /* Disk access window for Directory/FAT */ BYTE win[512]; /* Disk access window for Directory/FAT */
} FATFS; } FATFS;

View File

@ -1,10 +1,17 @@
/*--------------------------------------------------------------------------/ /*--------------------------------------------------------------------------/
/ FatFs - FAT file system module R0.03 (C)ChaN, 2006 / FatFs - FAT file system module R0.03a (C)ChaN, 2006
/---------------------------------------------------------------------------/ /---------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to / FatFs module is an experimenal project to implement FAT file system to
/ cheap microcontrollers. This is a free software and is opened for education, / cheap microcontrollers. This is a free software and is opened for education,
/ research and development. You can use, modify and/or republish it for / research and development under license policy of following trems.
/ non-profit or profit use without any restriction under your responsibility. /
/ Copyright (C) 2006, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------/ /---------------------------------------------------------------------------/
/ Feb 26, 2006 R0.00 Prototype. / Feb 26, 2006 R0.00 Prototype.
/ Apr 29, 2006 R0.01 First stable version. / Apr 29, 2006 R0.01 First stable version.
@ -13,6 +20,7 @@
/ Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM). / Jun 10, 2006 R0.02a Added a configuration option (_FS_MINIMUM).
/ Sep 22, 2006 R0.03 Added f_rename(). / Sep 22, 2006 R0.03 Added f_rename().
/ Changed option _FS_MINIMUM to _FS_MINIMIZE. / Changed option _FS_MINIMUM to _FS_MINIMIZE.
/ Dec 09, 2006 R0.03a Improved cluster scan algolithm to write files fast.
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#include <string.h> #include <string.h>
@ -170,34 +178,36 @@ WORD create_chain (
WORD clust /* Cluster# to stretch, 0 means create new */ WORD clust /* Cluster# to stretch, 0 means create new */
) )
{ {
WORD ncl, ccl, mcl = FatFs->max_clust; WORD cstat, ncl, scl, mcl;
FATFS *fs = FatFs;
mcl = fs->max_clust;
if (clust == 0) { /* Create new chain */ if (clust == 0) { /* Create new chain */
ncl = 1; scl = fs->last_clust; /* Get last allocated cluster */
do { if (scl < 2 || scl >= mcl) scl = 1;
ncl++; /* Check next cluster */
if (ncl >= mcl) return 0; /* No free custer was found */
ccl = get_cluster(ncl); /* Get the cluster status */
if (ccl == 1) return 0; /* Any error occured */
} while (ccl); /* Repeat until find a free cluster */
} }
else { /* Stretch existing chain */ else { /* Stretch existing chain */
ncl = get_cluster(clust); /* Check the cluster status */ cstat = get_cluster(clust); /* Check the cluster status */
if (ncl < 2) return 0; /* It is an invalid cluster */ if (cstat < 2) return 0; /* It is an invalid cluster */
if (ncl < mcl) return ncl; /* It is already followed by next cluster */ if (cstat < mcl) return cstat; /* It is already followed by next cluster */
ncl = clust; /* Search free cluster */ scl = clust;
do {
ncl++; /* Check next cluster */
if (ncl >= mcl) ncl = 2; /* Wrap around */
if (ncl == clust) return 0; /* No free custer was found */
ccl = get_cluster(ncl); /* Get the cluster status */
if (ccl == 1) return 0; /* Any error occured */
} while (ccl); /* Repeat until find a free cluster */
} }
ncl = scl; /* Scan start cluster */
do {
ncl++; /* Next cluster */
if (ncl >= mcl) { /* Wrap around */
ncl = 2;
if (scl == 1) return 0; /* No free custer was found */
}
if (ncl == scl) return 0; /* No free custer was found */
cstat = get_cluster(ncl); /* Get the cluster status */
if (cstat == 1) return 0; /* Any error occured */
} while (cstat); /* Repeat until find a free cluster */
if (!put_cluster(ncl, 0xFFFF)) return 0; /* Mark the new cluster "in use" */ if (!put_cluster(ncl, 0xFFFF)) return 0; /* Mark the new cluster "in use" */
if (clust && !put_cluster(clust, ncl)) return 0; /* Link it to previous one if needed */ if (clust && !put_cluster(clust, ncl)) return 0; /* Link it to previous one if needed */
fs->last_clust = ncl;
return ncl; /* Return new cluster number */ return ncl; /* Return new cluster number */
} }
@ -234,7 +244,8 @@ BYTE check_fs (
static const char fatsign[] = "FAT12FAT16"; static const char fatsign[] = "FAT12FAT16";
FATFS *fs = FatFs; FATFS *fs = FatFs;
/* Determines FAT type by signature string but this is not correct */ /* Determines FAT type by signature string but this is not correct.
For further information, refer to fatgen103.doc from Microsoft. */
memset(fs->win, 0, 512); memset(fs->win, 0, 512);
if (disk_read(fs->win, sect, 1) == RES_OK) { /* Load boot record */ if (disk_read(fs->win, sect, 1) == RES_OK) { /* Load boot record */
if (LD_WORD(&(fs->win[510])) == 0xAA55) { /* Is it valid? */ if (LD_WORD(&(fs->win[510])) == 0xAA55) { /* Is it valid? */

View File

@ -1,14 +1,23 @@
/*--------------------------------------------------------------------------/ /*--------------------------------------------------------------------------/
/ Tiny-FatFs - FAT file system module include file R0.03 (C)ChaN, 2006 / Tiny-FatFs - FAT file system module include file R0.03a (C)ChaN, 2006
/---------------------------------------------------------------------------/ /---------------------------------------------------------------------------/
/ FatFs module is an experimenal project to implement FAT file system to / FatFs module is an experimenal project to implement FAT file system to
/ cheap microcontrollers. This is a free software and is opened for education, / cheap microcontrollers. This is a free software and is opened for education,
/ research and development. You can use, modify and/or republish it for / research and development under license policy of following trems.
/ non-profit or profit use without any restriction under your responsibility. /
/ Copyright (C) 2006, ChaN, all right reserved.
/
/ * The FatFs module is a free software and there is no warranty.
/ * You can use, modify and/or redistribute it for personal, non-profit or
/ profit use without any restriction under your responsibility.
/ * Redistributions of source code must retain the above copyright notice.
/
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#ifndef _FATFS #ifndef _FATFS
#error Don't forget to change these options.
//#define _BYTE_ACC //#define _BYTE_ACC
/* The _BYTE_ACC enables byte-by-byte access for multi-byte variables. This /* The _BYTE_ACC enables byte-by-byte access for multi-byte variables. This
/ MUST be defined when multi-byte variable is stored in big-endian and/or / MUST be defined when multi-byte variable is stored in big-endian and/or
@ -45,12 +54,13 @@ typedef struct _FATFS {
WORD n_rootdir; /* Number of root directory entry */ WORD n_rootdir; /* Number of root directory entry */
BYTE winflag; /* win[] dirty flag (1:must be written back) */ BYTE winflag; /* win[] dirty flag (1:must be written back) */
BYTE pad1; BYTE pad1;
DWORD winsect; /* Current sector appearing in the win[] */
WORD sects_fat; /* Sectors per fat */ WORD sects_fat; /* Sectors per fat */
WORD max_clust; /* Maximum cluster# + 1 */ WORD max_clust; /* Maximum cluster# + 1 */
DWORD fatbase; /* FAT start sector */ DWORD fatbase; /* FAT start sector */
DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ DWORD dirbase; /* Root directory start sector */
DWORD database; /* Data start sector */ DWORD database; /* Data start sector */
DWORD winsect; /* Current sector appearing in the win[] */ WORD last_clust; /* Last allocated cluster */
BYTE win[512]; /* Disk access window for Directory/FAT/File */ BYTE win[512]; /* Disk access window for Directory/FAT/File */
} FATFS; } FATFS;