From 238747ef97b5881989299b2a98e6a404137d8d31 Mon Sep 17 00:00:00 2001 From: savelij13 Date: Thu, 11 Sep 2025 08:53:18 +0300 Subject: [PATCH] fatfs v0.02 Jun 01, 2006: - Added FAT12. Removed unbuffered mode. - Fixed a problem on small (<32M) patition. --- doc/00index_e.html | 22 +++--- doc/00index_j.html | 16 ++--- doc/css_e.css | 1 + doc/css_j.css | 1 + doc/en/chmod.html | 3 +- doc/en/close.html | 3 +- doc/en/dinit.html | 1 + doc/en/dread.html | 1 + doc/en/dstat.html | 1 + doc/en/dwrite.html | 1 + doc/en/fattime.html | 1 + doc/en/getfree.html | 3 +- doc/en/lseek.html | 15 ++--- doc/en/mkdir.html | 3 +- doc/en/mountdrv.html | 1 + doc/en/open.html | 35 +++++----- doc/en/opendir.html | 5 +- doc/en/read.html | 5 +- doc/en/readdir.html | 1 + doc/en/sdir.html | 1 + doc/en/sfatfs.html | 1 + doc/en/sfile.html | 5 +- doc/en/sfileinfo.html | 1 + doc/en/stat.html | 3 +- doc/en/sync.html | 5 +- doc/en/unlink.html | 3 +- doc/en/write.html | 5 +- doc/ja/chmod.html | 1 + doc/ja/close.html | 1 + doc/ja/dinit.html | 1 + doc/ja/dread.html | 1 + doc/ja/dstat.html | 1 + doc/ja/dwrite.html | 1 + doc/ja/fattime.html | 1 + doc/ja/getfree.html | 1 + doc/ja/lseek.html | 15 ++--- doc/ja/mkdir.html | 1 + doc/ja/mountdrv.html | 1 + doc/ja/open.html | 44 +++++------- doc/ja/opendir.html | 1 + doc/ja/read.html | 5 +- doc/ja/readdir.html | 1 + doc/ja/sdir.html | 1 + doc/ja/sfatfs.html | 1 + doc/ja/sfile.html | 5 +- doc/ja/sfileinfo.html | 1 + doc/ja/stat.html | 1 + doc/ja/sync.html | 1 + doc/ja/unlink.html | 1 + doc/ja/write.html | 7 +- src/00readme.txt | 14 ++-- src/diskio.h | 2 +- src/ff.c | 152 ++++++++++++++++++++++++++---------------- src/ff.h | 120 ++++++++++++++++----------------- src/tff.c | 120 +++++++++++++++++++++------------ src/tff.h | 115 ++++++++++++++++---------------- 56 files changed, 424 insertions(+), 336 deletions(-) diff --git a/doc/00index_e.html b/doc/00index_e.html index 7ca5d00..3541cda 100644 --- a/doc/00index_e.html +++ b/doc/00index_e.html @@ -5,7 +5,7 @@ -FatFs - FAT Files System Module +ELM - Generic FAT Files System Module @@ -14,35 +14,35 @@
-

FatFs module is an experimental project to implement a FAT file system to small embdded system. Because the module is written in compliance with ANSI C, it can be used for most 8/16 bit microcontrollers, such as PIC, AVR, H8, Z80 and etc..., without any modification. To use the FatFs module, low level disk I/O functions for each recording media must be provided by user. I created two modules in different configurations in consideration of various use. For read only applications, wriiting codes can also be eliminated to reduce the code size.

+

FatFs module is an experimental project to implement FAT file system to small embdded systems. The FatFs module is written in compliance with ANSI C, therefore it is independent of hardware architecture. It can be implemented to most 8/16 small microcontrollers, such as PIC, AVR, H8, Z80 and etc..., without any changes. I created two modules in different configurations in consideration of various use. For read only applications, writing codes can also be eliminated to reduce the module size. To use the FatFs module, low level disk I/O functions for each media must be provided by user.

Features of FatFs Module

Features of Tiny-FatFs Module

@@ -71,7 +71,7 @@

Disk I/O Interface

-

FatFs module and Tiny-FatFs module require following functions to lower layer to read/write to physical disk and to get current time.

+

FatFs module and Tiny-FatFs module require following functions to lower layer to read/write physical disk and to get current time.

@@ -83,7 +83,7 @@

資料

-

FatFsモジュールとTiny-FatFsモジュールは教育・研究・開発用に公開しています。どのような目的(個人・非商用・商用)の利用でも使用・改変・配布について一切の制限はありませんが、全て利用者の責任の下での利用とします。

+

FatFsモジュールとTiny-FatFsモジュールは教育・研究・開発用に公開しています。どのような利用目的(個人・非商用・商用)でも使用・改変・配布について一切の制限はありませんが、全て利用者の責任の下での利用とします。


  • パフォーマンス・テスト(テストボード:ATmega64/9.2MHz with MMC/HDD/CFC
  • diff --git a/doc/css_e.css b/doc/css_e.css index 7a926ef..e3ef1f3 100644 --- a/doc/css_e.css +++ b/doc/css_e.css @@ -30,6 +30,7 @@ a.imglnk img {border: 1px solid;} .iequ {white-space: nowrap; font-weight: bold;} .clr {clear: both;} .it {font-style: italic;} +.mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap} h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;} p.hdd {float: right; text-align: right; margin-top: 0.5em;} diff --git a/doc/css_j.css b/doc/css_j.css index 8299ab3..fe7ad6c 100644 --- a/doc/css_j.css +++ b/doc/css_j.css @@ -33,6 +33,7 @@ a.imglnk img {border: 1px solid;} .iequ {white-space: nowrap; font-weight: bold;} .clr {clear: both;} .it {font-style: italic;} +.mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap} h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;} p.hdd {float: right; text-align: right; margin-top: 0.5em;} diff --git a/doc/en/chmod.html b/doc/en/chmod.html index b715378..989a341 100644 --- a/doc/en/chmod.html +++ b/doc/en/chmod.html @@ -3,6 +3,7 @@ + FatFs - f_chmod @@ -60,7 +61,7 @@ FRESULT f_chmod (
    FR_RW_ERROR
    Any error occured in low level disk I/O.
    FR_INCORRECT_DISK_CHANGE
    -
    Incorrect disk removal, such as a medium change during any file opend, has been occured.
    +
    Incorrect disk removal, such as a medium change during any file is opend, has been occured.
    FR_NOT_ENABLED
    FatFs module has not been enabled.
    FR_NO_FILESYSTEM
    diff --git a/doc/en/close.html b/doc/en/close.html index f0ce408..737700d 100644 --- a/doc/en/close.html +++ b/doc/en/close.html @@ -3,6 +3,7 @@ + FatFs - f_close @@ -36,7 +37,7 @@ FRESULT f_close (
    FR_RW_ERROR
    Any error occured in low level disk I/O.
    FR_INCORRECT_DISK_CHANGE
    -
    Incorrect disk removal, such as a medium change during any file opend, has been occured.
    +
    Incorrect disk removal, such as a medium change during any file is opend, has been occured.
    FR_NOT_ENABLED
    FatFs module is not enabled.
    diff --git a/doc/en/dinit.html b/doc/en/dinit.html index 74ff0e4..cb8a231 100644 --- a/doc/en/dinit.html +++ b/doc/en/dinit.html @@ -3,6 +3,7 @@ + FatFs - disk_initialize diff --git a/doc/en/dread.html b/doc/en/dread.html index 2eaf97b..6a071a9 100644 --- a/doc/en/dread.html +++ b/doc/en/dread.html @@ -3,6 +3,7 @@ + FatFs - disk_read diff --git a/doc/en/dstat.html b/doc/en/dstat.html index 576da2d..f84ea3a 100644 --- a/doc/en/dstat.html +++ b/doc/en/dstat.html @@ -3,6 +3,7 @@ + FatFs - disk_status diff --git a/doc/en/dwrite.html b/doc/en/dwrite.html index ab044bb..a7ce75e 100644 --- a/doc/en/dwrite.html +++ b/doc/en/dwrite.html @@ -3,6 +3,7 @@ + FatFs - disk_write diff --git a/doc/en/fattime.html b/doc/en/fattime.html index faf7dcc..938fd7a 100644 --- a/doc/en/fattime.html +++ b/doc/en/fattime.html @@ -3,6 +3,7 @@ + FatFs - get_fattime diff --git a/doc/en/getfree.html b/doc/en/getfree.html index 60826f7..99d0058 100644 --- a/doc/en/getfree.html +++ b/doc/en/getfree.html @@ -3,6 +3,7 @@ + FatFs - f_getfree @@ -38,7 +39,7 @@ FRESULT f_getfree (
    FR_RW_ERROR
    Any error occured in low level disk I/O.
    FR_INCORRECT_DISK_CHANGE
    -
    Incorrect disk removal, such as a medium change during any file opend, has been occured.
    +
    Incorrect disk removal, such as a medium change during any file is opend, has been occured.
    FR_NOT_ENABLED
    FatFs module has not been enabled.
    FR_NO_FILESYSTEM
    diff --git a/doc/en/lseek.html b/doc/en/lseek.html index 8754204..1081c53 100644 --- a/doc/en/lseek.html +++ b/doc/en/lseek.html @@ -3,6 +3,7 @@ + FatFs - f_lseek @@ -36,12 +37,10 @@ FRESULT f_lseek (
    FR_OK
    The function succeeded.
    -
    FR_ALIGN_ERROR
    -
    Out of alignment. In unbuffered mode, specified offset must be aligned on 512 byte boundary.
    FR_RW_ERROR
    Any error occured in low level disk I/O.
    FR_INCORRECT_DISK_CHANGE
    -
    Incorrect disk removal, such as a medium change during any file opend, has been occured.
    +
    Incorrect disk removal, such as a medium change during any file is opend, has been occured.
    FR_NOT_ENABLED
    FatFs module has not been enabled.
    @@ -59,15 +58,15 @@ FRESULT f_lseek (
         // Move to offset of 5000 from top of file.
         res = f_lseek(&file, 5000);
    -    if (res) die(res);
     
    -    // Move to 3000 bytes front of current offset.
    +    // Forward 3000 bytes
         res = f_lseek(&file, file.fptr + 3000);
    -    if (res) die(res);
     
    -    // Move to 2000 bytes back of current offset.
    +    // Rewind 2000 bytes
         res = f_lseek(&file, file.fptr - 2000);
    -    if (res) die(res);
    +
    +    // Move to end of file
    +    res = f_lseek(&file, 0xFFFFFFFF);
     
diff --git a/doc/en/mkdir.html b/doc/en/mkdir.html index 72242ce..cb7c808 100644 --- a/doc/en/mkdir.html +++ b/doc/en/mkdir.html @@ -3,6 +3,7 @@ + FatFs - f_mkdir @@ -46,7 +47,7 @@ FRESULT f_mkdir (
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_INCORRECT_DISK_CHANGE
-
Incorrect disk removal, such as a medium change during any file opend, has been occured.
+
Incorrect disk removal, such as a medium change during any file is opend, has been occured.
FR_NOT_ENABLED
FatFs module has not been enabled.
FR_NO_FILESYSTEM
diff --git a/doc/en/mountdrv.html b/doc/en/mountdrv.html index acc96b8..cfa8bc8 100644 --- a/doc/en/mountdrv.html +++ b/doc/en/mountdrv.html @@ -3,6 +3,7 @@ + FatFs - f_mountdrv diff --git a/doc/en/open.html b/doc/en/open.html index e64490e..d04410e 100644 --- a/doc/en/open.html +++ b/doc/en/open.html @@ -3,6 +3,7 @@ + FatFs - f_open @@ -25,19 +26,18 @@ FRESULT f_open (

Parameters

FileObject
-
Pointer to the blank file object structure to be initialized. After the f_open function succeeded, the file can be accessed with the file object structure until it is closed. The member buffer in the structure must be initialized with the address of a 512 bytes file read/write buffer before or immediataly after the f_open function. The initialization is not needed when FA_UNBUFFERED flag has been specified.
+
Pointer to the blank file object structure to be initialized. After the f_open() succeeded, the file can be accessed with the file object structure until it is closed.
FileName
Pointer to a null-terminated string specifies the full-path file name to create or open. The directory separator is '/'. Because the FatFs module does not have a concept of current directory, a full-path name that followed from the root directory must be used. Leading space charactors are skipped if exist and heading '/' can be exist or omitted.
ModeFlags
Specifies the type of access and open method for the file. It is specified by a combination of following flags.
- - - - - - - + + + + + +
ValueDescription
FA_READSpecifies read access to the object. Data can be read from the file.
Combine with FA_WRITE for read-write access.
FA_WRITESpecifies write access to the object. Data can be written to the file.
Combine with FA_READ for read-write access.
FA_UNBUFFEREDThis is for only FatFs module. When not specified, the file can be read/written in
stream I/O via the file read/write buffer pointed by member 'buffer' in the file
object. When specified, file read/write buffer is not used and number of bytes
to read/write must be integer multiple of 512.
FA_OPEN_EXISTINGOpens the file. The function fails if the file does not exist.
FA_CREATE_ALWAYSCreates a new file. If the file exists, it is truncated and overwritten.
FA_OPEN_ALWAYSOpens the file, if it exists. If the file does not exist, the function creates the file.
ValueDescription
FA_READSpecifies read access to the object. Data can be read from the file.
Combine with FA_WRITE for read-write access.
FA_WRITESpecifies write access to the object. Data can be written to the file.
Combine with FA_READ for read-write access.
FA_OPEN_EXISTINGOpens the file. The function fails if the file does not exist.
FA_CREATE_ALWAYSCreates a new file. If the file exists, it is truncated and overwritten.
FA_OPEN_ALWAYSOpens the file, if it exists. If the file does not exist, the function creates the file.
@@ -64,7 +64,7 @@ FRESULT f_open (
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_INCORRECT_DISK_CHANGE
-
Incorrect disk removal, such as a medium change during any file opend, has been occured.
+
Incorrect disk removal, such as a medium change during any file is opend, has been occured.
FR_NOT_ENABLED
FatFs module has not been enabled.
FR_NO_FILESYSTEM
@@ -75,46 +75,41 @@ FRESULT f_open (

Description

-

To start to use the FatFs module, prepare a work area (FATFS structure), clear the structure and store the address to the global variable 'FatFs' to allocate the work area to the FatFs module. Then the FatFs module can work.

-

Flags FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS are not supported in read-only configuration. Flag FA_UNBUFFERED is not supported in Tiny-FatFs.

+

To start to use the FatFs module, prepare a work area (FATFS 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.

+

Flags FA_WRITE, FA_CREATE_ALWAYS, FA_OPEN_ALWAYS are not supported in read-only configuration.

-

Sample Code

+

Example (File Copy)

 void main ()
 {
     FATFS fs;            // FatFs work area
     FIL fsrc, fdst;      // file structures
-    BYTE fbuff[512*2];   // file r/w buffers (not required for Tiny-FatFs)
     BYTE buffer[4096];   // file copy buffer
     FRESULT res;         // FatFs function common result code
     WORD br, bw;         // File R/W count
 
 
-    // Activate FatFs module
+    // Give a work area for FatFs (activate module)
     memset(&fs, 0, sizeof(FATFS));
     FatFs = &fs;
 
     // Open source file
-    fsrc.buffer = fbuff+0;	// (not required for Tiny-FatFs)
     res = f_open(&fsrc, "/srcfile.dat", FA_OPEN_EXISTING | FA_READ);
     if (res) die(res);
 
     // Create destination file
-    fdst.buffer = fbuff+512;	// (not required for Tiny-FatFs)
     res = f_open(&fdst, "/dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
     if (res) die(res);
 
     // Copy source to destination
     for (;;) {
         res = f_read(&fsrc, buffer, sizeof(buffer), &br);
-        if (res) die(res);
-        if (br == 0) break;
+        if (res || br == 0) break;
         res = f_write(&fdst, buffer, br, &bw);
-        if (res) die(res);
-        if (bw < br) break;
+        if (res || bw < br) break;
     }
 
     // Close all files
diff --git a/doc/en/opendir.html b/doc/en/opendir.html
index d0ca017..99b0646 100644
--- a/doc/en/opendir.html
+++ b/doc/en/opendir.html
@@ -3,6 +3,7 @@
 
 
 
+
 
 FatFs - f_opendir
 
@@ -35,7 +36,7 @@ FRESULT f_opendir (
 

Return Values

FR_OK
-
The function succeeded. The DirObject structure is used for subsequent calls to read the directory items.
+
The function succeeded. The DirObject structure is used for subsequent calls to read the directory entries.
FR_NOPATH
Could not find the path.
FR_INVALID_NAME
@@ -45,7 +46,7 @@ FRESULT f_opendir (
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_INCORRECT_DISK_CHANGE
-
Incorrect disk removal, such as a medium change during any file opend, has been occured.
+
Incorrect disk removal, such as a medium change during any file is opend, has been occured.
FR_NOT_ENABLED
FatFs module has not been enabled.
FR_NO_FILESYSTEM
diff --git a/doc/en/read.html b/doc/en/read.html index e5e091b..6abcb49 100644 --- a/doc/en/read.html +++ b/doc/en/read.html @@ -3,6 +3,7 @@ + FatFs - f_read @@ -44,8 +45,6 @@ FRESULT f_read (
The function succeeded.
FR_DENIED
The function denied due to the file has been opened in write only mode.
-
FR_ALIGN_ERROR
-
The file has been opened in unbufferred mode but unaligned access was detected.
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_NOT_READY
@@ -58,7 +57,7 @@ FRESULT f_read (

Description

-

The read/write pointer increases in number of bytes read. The ByteRead will be smaller than ByteToRead when the read pointer reached to end of the file or alignment error occured during the read operation. In unbufferred mode, last fractional bytes cannot be read due to FR_ALIGN_ERROR.

+

The read/write pointer increases in number of bytes read. The ByteRead will be smaller than ByteToRead when the read pointer reached to end of the file or alignment error occured during the read operation.

diff --git a/doc/en/readdir.html b/doc/en/readdir.html index 37995ad..ad476bf 100644 --- a/doc/en/readdir.html +++ b/doc/en/readdir.html @@ -3,6 +3,7 @@ + FatFs - f_readdir diff --git a/doc/en/sdir.html b/doc/en/sdir.html index ee15cab..537930e 100644 --- a/doc/en/sdir.html +++ b/doc/en/sdir.html @@ -3,6 +3,7 @@ + FatFs - DIR diff --git a/doc/en/sfatfs.html b/doc/en/sfatfs.html index b4baae7..7379a3a 100644 --- a/doc/en/sfatfs.html +++ b/doc/en/sfatfs.html @@ -3,6 +3,7 @@ + FatFs - FATFS diff --git a/doc/en/sfile.html b/doc/en/sfile.html index 271fc9e..121d4f9 100644 --- a/doc/en/sfile.html +++ b/doc/en/sfile.html @@ -3,6 +3,7 @@ + FatFs - FIL @@ -11,7 +12,7 @@

FIL

-

The FIL structure holds state of a file and it is allocated by an application program. Only buffer member can be initialized by the application program.

+

The FIL structure holds state of a file and it is allocated by an application program. There is no member that can be changed by the application program.

FatFs

@@ -23,9 +24,9 @@ typedef struct _FIL {
     DWORD   curr_sect;      // Current sector
     DWORD   dir_sect;       // Sector# containing the directory entry
     BYTE*   dir_ptr;        // Ponter to the directory entry in the window
-    BYTE*   buffer;         // Pointer to 512 byte file R/W buffer
     BYTE    flag;           // File status flags
     BYTE    sect_clust;     // Left sectors in current cluster
+    BYTE    buffer[512];    // File R/W buffer
 } FIL;
 
diff --git a/doc/en/sfileinfo.html b/doc/en/sfileinfo.html index 6508fab..0d18636 100644 --- a/doc/en/sfileinfo.html +++ b/doc/en/sfileinfo.html @@ -3,6 +3,7 @@ + FatFs - FILINFO diff --git a/doc/en/stat.html b/doc/en/stat.html index fcaa85d..310aba3 100644 --- a/doc/en/stat.html +++ b/doc/en/stat.html @@ -3,6 +3,7 @@ + FatFs - f_stat @@ -45,7 +46,7 @@ FRESULT f_stat (
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_INCORRECT_DISK_CHANGE
-
Incorrect disk removal, such as a medium change during any file opend, has been occured.
+
Incorrect disk removal, such as a medium change during any file is opend, has been occured.
FR_NOT_ENABLED
FatFs module has not been enabled.
FR_NO_FILESYSTEM
diff --git a/doc/en/sync.html b/doc/en/sync.html index 5ea8bad..b5aa731 100644 --- a/doc/en/sync.html +++ b/doc/en/sync.html @@ -3,6 +3,7 @@ + FatFs - f_sync @@ -36,7 +37,7 @@ FRESULT f_sync (
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_INCORRECT_DISK_CHANGE
-
Incorrect disk removal, such as a medium change during any file opend, has been occured.
+
Incorrect disk removal, such as a medium change during any file is opend, has been occured.
FR_NOT_ENABLED
FatFs module has not been enabled.
@@ -45,7 +46,7 @@ FRESULT f_sync (

Description

-

The f_sync writes back the cached information of a file being written into the disk. This is the same function as f_close but the file is left opened and can continue the file access. This is suitable for an application of data logger that opens a file for long time in writing mode. The f_sync of periodic or immediataly after f_wriete can minimize the lost data due to an unintentional black out or disk removal. This function is not supported in read-only configuration.

+

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.

diff --git a/doc/en/unlink.html b/doc/en/unlink.html index 4e386c3..aa6b7dc 100644 --- a/doc/en/unlink.html +++ b/doc/en/unlink.html @@ -3,6 +3,7 @@ + FatFs - f_unlink @@ -48,7 +49,7 @@ FRESULT f_unlink (
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_INCORRECT_DISK_CHANGE
-
Incorrect disk removal, such as a medium change during any file opend, has been occured.
+
Incorrect disk removal, such as a medium change during any file is opend, has been occured.
FR_NOT_ENABLED
FatFs module has not been enabled.
FR_NO_FILESYSTEM
diff --git a/doc/en/write.html b/doc/en/write.html index 54307a0..bc63f88 100644 --- a/doc/en/write.html +++ b/doc/en/write.html @@ -3,6 +3,7 @@ + FatFs - f_write @@ -44,8 +45,6 @@ FRESULT f_write (
The function succeeded.
FR_DENIED
The function denied due to the file has been opened in read only mode.
-
FR_ALIGN_ERROR
-
The file has been opened in unbufferred mode but unaligned access was detected.
FR_RW_ERROR
Any error occured in low level disk I/O.
FR_NOT_READY
@@ -58,7 +57,7 @@ FRESULT f_write (

Description

-

The read/write pointer increases in number of bytes written. The ByteWritten will be smaller than ByteToWrite when disk full or alignment error occured during write function. This function is not supported in read only configuration.

+

The read/write pointer increases in number of bytes written. The ByteWritten will be smaller than ByteToWrite when disk gets full during write function. This function is not supported in read only configuration.

diff --git a/doc/ja/chmod.html b/doc/ja/chmod.html index 32a89e7..8a48b4d 100644 --- a/doc/ja/chmod.html +++ b/doc/ja/chmod.html @@ -3,6 +3,7 @@ + FatFs - f_chmod diff --git a/doc/ja/close.html b/doc/ja/close.html index 90458d9..6497239 100644 --- a/doc/ja/close.html +++ b/doc/ja/close.html @@ -3,6 +3,7 @@ + FatFs - f_close diff --git a/doc/ja/dinit.html b/doc/ja/dinit.html index 1cf4c80..6bf5abc 100644 --- a/doc/ja/dinit.html +++ b/doc/ja/dinit.html @@ -3,6 +3,7 @@ + FatFs - disk_initialize diff --git a/doc/ja/dread.html b/doc/ja/dread.html index 846bb09..07cb187 100644 --- a/doc/ja/dread.html +++ b/doc/ja/dread.html @@ -3,6 +3,7 @@ + FatFs - disk_read diff --git a/doc/ja/dstat.html b/doc/ja/dstat.html index f1c20fe..56a5fd3 100644 --- a/doc/ja/dstat.html +++ b/doc/ja/dstat.html @@ -3,6 +3,7 @@ + FatFs - disk_status diff --git a/doc/ja/dwrite.html b/doc/ja/dwrite.html index 1792aaa..8324ce6 100644 --- a/doc/ja/dwrite.html +++ b/doc/ja/dwrite.html @@ -3,6 +3,7 @@ + FatFs - disk_write diff --git a/doc/ja/fattime.html b/doc/ja/fattime.html index fcd49ac..14da2fb 100644 --- a/doc/ja/fattime.html +++ b/doc/ja/fattime.html @@ -3,6 +3,7 @@ + FatFs - get_fattime diff --git a/doc/ja/getfree.html b/doc/ja/getfree.html index f5066c9..4eefcee 100644 --- a/doc/ja/getfree.html +++ b/doc/ja/getfree.html @@ -3,6 +3,7 @@ + FatFs - f_getfree diff --git a/doc/ja/lseek.html b/doc/ja/lseek.html index b8b3373..b7cb67e 100644 --- a/doc/ja/lseek.html +++ b/doc/ja/lseek.html @@ -3,6 +3,7 @@ + FatFs - f_lseek @@ -36,8 +37,6 @@ FRESULT f_lseek (
FR_OK
正常終了。
-
FR_ALIGN_ERROR
-
アンバッファモードで開いたファイルで、512の整数倍でないオフセットを指定した。
FR_RW_ERROR
ディスクアクセスでエラーが発生した。
FR_INCORRECT_DISK_CHANGE
@@ -57,17 +56,17 @@ FRESULT f_lseek (

使用例

-    // Move to offset of 5000 from top of file.
+    // ファイルオフセット 5000 へ移動
     res = f_lseek(&file, 5000);
-    if (res) die(res);
 
-    // Move to 3000 bytes front of current offset.
+    // 3000バイト進める
     res = f_lseek(&file, file.fptr + 3000);
-    if (res) die(res);
 
-    // Move to 2000 bytes back of current offset.
+    // 2000バイト戻す
     res = f_lseek(&file, file.fptr - 2000);
-    if (res) die(res);
+
+    // ファイル末尾 + 1 へ移動
+    res = f_lseek(&file, 0xFFFFFFFF);
 
diff --git a/doc/ja/mkdir.html b/doc/ja/mkdir.html index dd67dff..3384212 100644 --- a/doc/ja/mkdir.html +++ b/doc/ja/mkdir.html @@ -3,6 +3,7 @@ + FatFs - f_mkdir diff --git a/doc/ja/mountdrv.html b/doc/ja/mountdrv.html index 2bd37be..b344422 100644 --- a/doc/ja/mountdrv.html +++ b/doc/ja/mountdrv.html @@ -3,6 +3,7 @@ + FatFs - f_mountdrv diff --git a/doc/ja/open.html b/doc/ja/open.html index cd95f5d..72fdd5d 100644 --- a/doc/ja/open.html +++ b/doc/ja/open.html @@ -3,6 +3,7 @@ + FatFs - f_open @@ -25,22 +26,18 @@ FRESULT f_open (

パラメータ

FileObject
-
新しく作成するファイルオブジェクト構造体へのポインタを指定します。以降、このファイルを閉じるまでそのファイルオブジェクトを使用してファイル操作をします。構造体メンバのbufferは、オープン前または直後にそのファイルで使用するR/Wバッファへのアドレスで初期化しておく必要があります(FA_UNBUFFEREDを指定した場合はバッファは不要)。
+
新しく作成するファイルオブジェクト構造体へのポインタを指定します。以降、そのファイルを閉じるまでこのファイルオブジェクトを使用してファイル操作をします。
FileName
作成する (またはオープンする) ファイルのフルパス名が入った'\0'で終わる文字列へのポインタを指定します。ディレクトリセパレータには'/'を使用します。FatFsモジュールにはカレントディレクトリの概念がないので、パスはルートディレクトリから辿る絶対パスとなります。文字列先頭のスペースはスキップされます。パス先頭の'/'はあってもなくてもかまいません。
ModeFlags
ファイルのアクセス方法やオープン方法を決めるフラグです。このパラメータには次の組み合わせを指定します。
- - - - - - + + + + +
意味
FA_READ読み出しモードでオープンします。読み書きする場合はFA_WRITEと共に指定します。
FA_WRITE書き込みモードでオープンします。読み書きする場合はFA_READと共に指定します。
FA_UNBUFFERED -FatFsでのみ指定可。指定するとファイルR/Wバッファを使用せず、メモリを節約できます。
-read/write/seekは512の整数倍単位でなければなりません。指定しない場合は、ファイル
-R/Wバッファ(ファイルオブジェクト構造体メンバのbufferで指定)を使用。
FA_OPEN_EXISTING既存のファイルを開きます。ファイルが無いときはエラーになります。
FA_CREATE_ALWAYSファイルを作成します。既存のファイルがある場合は、サイズを0にしてから開きます。
FA_OPEN_ALWAYS既存のファイルを開きます。ファイルが無いときはファイルを作成します。
FA_READ読み出しモードでオープンします。読み書きする場合はFA_WRITEと共に指定します。
FA_WRITE書き込みモードでオープンします。読み書きする場合はFA_READと共に指定します。
FA_OPEN_EXISTING既存のファイルを開きます。ファイルが無いときはエラーになります。
FA_CREATE_ALWAYSファイルを作成します。既存のファイルがある場合は、サイズを0にしてから開きます。
FA_OPEN_ALWAYS既存のファイルを開きます。ファイルが無いときはファイルを作成します。
@@ -84,47 +81,42 @@ R/W
-

使用例

+

使用例(ファイルコピー)

 void main ()
 {
-    FATFS fs;            // FatFs work area
-    FIL fsrc, fdst;      // file structures
-    BYTE fbuff[512*2];   // file r/w buffers (not required for Tiny-FatFs)
+    FATFS fs;            // FatFsワークエリア
+    FIL fsrc, fdst;      // ファイルオブジェクト
     BYTE buffer[4096];   // file copy buffer
     FRESULT res;         // FatFs function common result code
     WORD br, bw;         // File R/W count
 
 
-    // Activate FatFs module
+    // FatFsのワークエリアを確保する
     memset(&fs, 0, sizeof(FATFS));
     FatFs = &fs;
 
-    // Open source file
-    fsrc.buffer = fbuff+0;	// (not required for Tiny-FatFs)
+    // ソース・ファイルを開く
     res = f_open(&fsrc, "/srcfile.dat", FA_OPEN_EXISTING | FA_READ);
     if (res) die(res);
 
-    // Create destination file
-    fdst.buffer = fbuff+512;	// (not required for Tiny-FatFs)
+    // デスティネーション・ファイルを作成する
     res = f_open(&fdst, "/dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
     if (res) die(res);
 
-    // Copy source to destination
+    // ソースからデスティネーションにコピーする
     for (;;) {
         res = f_read(&fsrc, buffer, sizeof(buffer), &br);
-        if (res) die(res);
-        if (br == 0) break;
+        if (res || br == 0) break;
         res = f_write(&fdst, buffer, br, &bw);
-        if (res) die(res);
-        if (bw < br) break;
+        if (res || bw < br) break;
     }
 
-    // Close all files
+    // 全てのファイルを閉じる
     f_close(&fsrc);
     f_close(&fdst);
 
-    // Deactivate FatFs module
+    // FatFsのワークエリアを開放する
     FatFs = NULL;
 }
 
diff --git a/doc/ja/opendir.html b/doc/ja/opendir.html index 72610c8..8ac0f63 100644 --- a/doc/ja/opendir.html +++ b/doc/ja/opendir.html @@ -3,6 +3,7 @@ + FatFs - f_opendir diff --git a/doc/ja/read.html b/doc/ja/read.html index d745285..6b58c75 100644 --- a/doc/ja/read.html +++ b/doc/ja/read.html @@ -3,6 +3,7 @@ + FatFs - f_read @@ -44,8 +45,6 @@ FRESULT f_read (
正常終了。
FR_DENIED
書き込み専用モードで開いたファイルから読み込もうとした。
-
FR_ALIGN_ERROR
-
アンバッファモードで開かれたファイルで、512の整数倍でないアクセスを検出した。
FR_RW_ERROR
ディスクアクセスでエラーが発生した。
FR_NOT_READY
@@ -58,7 +57,7 @@ FRESULT f_read (

解説

-

読み込み開始位置は、現在のファイルR/Wポインタからになります。ファイルR/Wポインタは読み込まれたバイト数だけ進みます。読み込み中にファイルの終端に達すると、*ByteReadByteToReadよりも小さくなります。アンバッファモードでは最後の端数バイトを残してFR_ALIGN_ERRORになります。

+

読み込み開始位置は、現在のファイルR/Wポインタからになります。ファイルR/Wポインタは読み込まれたバイト数だけ進みます。読み込み中にファイルの終端に達すると、*ByteReadByteToReadよりも小さくなります。

diff --git a/doc/ja/readdir.html b/doc/ja/readdir.html index a9c73cb..8da9e56 100644 --- a/doc/ja/readdir.html +++ b/doc/ja/readdir.html @@ -3,6 +3,7 @@ + FatFs - f_readdir diff --git a/doc/ja/sdir.html b/doc/ja/sdir.html index ee07752..e2168dd 100644 --- a/doc/ja/sdir.html +++ b/doc/ja/sdir.html @@ -3,6 +3,7 @@ + FatFs - DIR diff --git a/doc/ja/sfatfs.html b/doc/ja/sfatfs.html index d08b8f0..4a89c80 100644 --- a/doc/ja/sfatfs.html +++ b/doc/ja/sfatfs.html @@ -3,6 +3,7 @@ + FatFs - FATFS diff --git a/doc/ja/sfile.html b/doc/ja/sfile.html index c2f42a1..478c18f 100644 --- a/doc/ja/sfile.html +++ b/doc/ja/sfile.html @@ -3,6 +3,7 @@ + FatFs - FIL @@ -11,7 +12,7 @@

FIL

-

FIL構造体は、1個で1ファイルの状態を保持し、アプリケーション側で確保・管理されます。アプリケーションから変更可能なメンバはbufferのみです。

+

FIL構造体は、個々のファイルの状態を保持し、アプリケーション側で確保・管理されます。アプリケーションから変更可能なメンバはありません。

FatFs

 typedef struct _FIL {
@@ -22,9 +23,9 @@ typedef struct _FIL {
     DWORD   curr_sect;      // Current sector
     DWORD   dir_sect;       // Sector# containing the directory entry
     BYTE*   dir_ptr;        // Ponter to the directory entry in the window
-    BYTE*   buffer;         // Pointer to 512 byte file R/W buffer
     BYTE    flag;           // File status flags
     BYTE    sect_clust;     // Left sectors in current cluster
+    BYTE    buffer[512];    // File R/W buffer
 } FIL;
 
diff --git a/doc/ja/sfileinfo.html b/doc/ja/sfileinfo.html index bf3261f..91fd104 100644 --- a/doc/ja/sfileinfo.html +++ b/doc/ja/sfileinfo.html @@ -3,6 +3,7 @@ + FatFs - FILINFO diff --git a/doc/ja/stat.html b/doc/ja/stat.html index 18ee293..8e6d2c2 100644 --- a/doc/ja/stat.html +++ b/doc/ja/stat.html @@ -3,6 +3,7 @@ + FatFs - f_stat diff --git a/doc/ja/sync.html b/doc/ja/sync.html index 8c1f423..22b2be2 100644 --- a/doc/ja/sync.html +++ b/doc/ja/sync.html @@ -3,6 +3,7 @@ + FatFs - f_sync diff --git a/doc/ja/unlink.html b/doc/ja/unlink.html index 33af9db..c830195 100644 --- a/doc/ja/unlink.html +++ b/doc/ja/unlink.html @@ -3,6 +3,7 @@ + FatFs - f_unlink diff --git a/doc/ja/write.html b/doc/ja/write.html index d7339c6..cf2b567 100644 --- a/doc/ja/write.html +++ b/doc/ja/write.html @@ -3,6 +3,7 @@ + FatFs - f_write @@ -32,7 +33,7 @@ FRESULT f_write (
ByteToWrite
書き込むバイト数を指定します。
ByteWritten
-
実際に書き込まれたバイト数を格納する変数を指すポインタを指定します。
+
書き込まれたバイト数を格納する変数を指すポインタを指定します。
@@ -44,8 +45,6 @@ FRESULT f_write (
正常終了。
FR_DENIED
読み込み専用モードで開いたファイルに書き込もうとした。
-
FR_ALIGN_ERROR
-
アンバッファモードで開かれたファイルで、512の整数倍でないアクセスを検出した。
FR_RW_ERROR
ディスクアクセスでエラーが発生した。
FR_NOT_READY
@@ -58,7 +57,7 @@ FRESULT f_write (

解説

-

書き込み開始位置は、ファイルR/Wポインタの現在位置からになります。ファイルR/Wポインタは実際に書き込まれたバイト数だけ進みます。書き込み中にディスクが一杯になった、またはFR_ALIGN_ERRORが帰ったときは、*ByteWrittenByteToWriteよりも小さくなります。リードオンリー構成ではこの関数はサポートされません。

+

書き込み開始位置は、ファイルR/Wポインタの現在位置からになります。ファイルR/Wポインタは実際に書き込まれたバイト数だけ進みます。書き込み中にディスクが一杯になったときは、*ByteWrittenByteToWriteよりも小さくなります。リードオンリー構成ではこの関数はサポートされません。

diff --git a/src/00readme.txt b/src/00readme.txt index 4c2637c..960e31f 100644 --- a/src/00readme.txt +++ b/src/00readme.txt @@ -1,12 +1,12 @@ -FatFs/Tiny-FatFs Module Source Files R0.01 (C)ChaN, 2006 +FatFs/Tiny-FatFs Module Source Files R0.02 (C)ChaN, 2006 Files ff.h Common include file for FatFs and application module. - ff.c FatFs module + ff.c FatFs module. tff.h Common include file for Tiny-FatFs and application module. - tff.c Tiny-FatFs module + tff.c Tiny-FatFs module. diskio.h Common include file for (Tiny-)FatFs and disk I/O module. integer.h Alternative type definitions for unsigned integers. @@ -20,7 +20,7 @@ Files Configuration Options There are several configuration options for various envilonment and requirement. - The configuration options are in include files ff.h and tff.h. + The configuration options are in include files, ff.h and tff.h. #define _BYTE_ACC @@ -34,7 +34,7 @@ Configuration Options #define _FS_READONLY - When application program does not require any write access, _FS_READONLY can be + When application program does not require any write function, _FS_READONLY can be defined to eliminate writing code to reduce module size. @@ -50,7 +50,7 @@ Agreements 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. You can use, modify and republish it for personal, non-profit or profit use - without any limitation under your responsibility. + without any limitation under your responsibility. @@ -58,3 +58,5 @@ Revision History Feb 26, 2006 R0.00 Prototype Apr 29, 2006 R0.01 First stable version + Jun 01, 2006 R0.02 Added FAT12. Removed unbuffered mode. + Fixed a problem on small (<32M) patition. diff --git a/src/diskio.h b/src/diskio.h index 2bea802..14f07ea 100644 --- a/src/diskio.h +++ b/src/diskio.h @@ -1,5 +1,5 @@ /*----------------------------------------------------------------------- -/ Low level disk interface modlue include file R0.01 (C)ChaN, 2006 +/ Low level disk interface modlue include file R0.02 (C)ChaN, 2006 /-----------------------------------------------------------------------*/ #ifndef _DISKIF diff --git a/src/ff.c b/src/ff.c index 110efb3..c7cf3d6 100644 --- a/src/ff.c +++ b/src/ff.c @@ -1,13 +1,15 @@ /*--------------------------------------------------------------------------/ -/ FatFs - FAT file system module R0.01 (C)ChaN, 2006 +/ FatFs - FAT file system module R0.02 (C)ChaN, 2006 /---------------------------------------------------------------------------/ / FatFs module is an experimenal project to implement FAT file system to / cheap microcontrollers. This is opened for education, reserch and / development. You can use, modify and republish it for non-profit or profit / use without any limitation under your responsibility. /---------------------------------------------------------------------------/ -/ Feb 26, 2006 R0.00 Prototype -/ Apr 29, 2006 R0.01 First stable version +/ Feb 26, 2006 R0.00 Prototype. +/ Apr 29, 2006 R0.01 First stable version. +/ Jun 01, 2006 R0.02 Added FAT12. Removed unbuffered mode. +/ Fixed a problem on small (<32M) patition. /---------------------------------------------------------------------------*/ #include @@ -59,34 +61,46 @@ BOOL move_window ( -/*---------------------*/ -/* Get Cluster State */ +/*----------------------*/ +/* Get a Cluster Status */ static DWORD get_cluster ( DWORD clust /* Cluster# to get the link information */ ) { + WORD wc, bc; + DWORD fatsect; FATFS *fs = FatFs; if ((clust >= 2) && (clust < fs->max_clust)) { /* Valid cluster# */ + fatsect = fs->fatbase; switch (fs->fs_type) { + case FS_FAT12 : + bc = (WORD)clust * 3 / 2; + if (!move_window(fatsect + bc / 512)) break; + wc = fs->win[bc % 512]; bc++; + if (!move_window(fatsect + bc / 512)) break; + wc |= (WORD)fs->win[bc % 512] << 8; + return (clust & 1) ? (wc >> 4) : (wc & 0xFFF); + case FS_FAT16 : - if (!move_window(clust / 256 + fs->fatbase)) break; - return LD_WORD(&(fs->win[((WORD)clust * 2) & 511])); + if (!move_window(fatsect + clust / 256)) break; + return LD_WORD(&(fs->win[((WORD)clust * 2) % 512])); + case FS_FAT32 : - if (!move_window(clust / 128 + fs->fatbase)) break; - return LD_DWORD(&(fs->win[((WORD)clust * 4) & 511])); + if (!move_window(fatsect + clust / 128)) break; + return LD_DWORD(&(fs->win[((WORD)clust * 4) % 512])); } } - return 1; /* Return with 1 means failed */ + return 1; /* Return with 1 means function failed */ } /*--------------------------*/ -/* Change a Cluster State */ +/* Change a Cluster Status */ #ifndef _FS_READONLY static @@ -95,18 +109,35 @@ BOOL put_cluster ( DWORD val /* New value to mark the cluster */ ) { + WORD bc; + BYTE *p; + DWORD fatsect; FATFS *fs = FatFs; + fatsect = fs->fatbase; switch (fs->fs_type) { + case FS_FAT12 : + bc = (WORD)clust * 3 / 2; + if (!move_window(fatsect + bc / 512)) return FALSE; + p = &fs->win[bc % 512]; + *p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; + fs->winflag = 1; bc++; + if (!move_window(fatsect + bc / 512)) return FALSE; + p = &fs->win[bc % 512]; + *p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); + break; + case FS_FAT16 : - if (!move_window(clust / 256 + fs->fatbase)) return FALSE; - ST_WORD(&(fs->win[((WORD)clust * 2) & 511]), (WORD)val); + if (!move_window(fatsect + clust / 256)) return FALSE; + ST_WORD(&(fs->win[((WORD)clust * 2) % 512]), (WORD)val); break; + case FS_FAT32 : - if (!move_window(clust / 128 + fs->fatbase)) return FALSE; - ST_DWORD(&(fs->win[((WORD)clust * 4) & 511]), val); + if (!move_window(fatsect + clust / 128)) return FALSE; + ST_DWORD(&(fs->win[((WORD)clust * 4) % 512]), val); break; + default : return FALSE; } @@ -209,7 +240,7 @@ BYTE check_fs ( DWORD sect /* Sector# to check if it is a FAT boot record or not */ ) { - static const char fatsign[] = "FAT16FAT32"; + static const char fatsign[] = "FAT12FAT16FAT32"; FATFS *fs = FatFs; @@ -217,8 +248,10 @@ BYTE check_fs ( if (disk_read(fs->win, sect, 1) == RES_OK) { /* Load boot record */ if (LD_WORD(&(fs->win[510])) == 0xAA55) { /* Is it valid? */ if (!memcmp(&(fs->win[0x36]), &fatsign[0], 5)) + return FS_FAT12; + if (!memcmp(&(fs->win[0x36]), &fatsign[5], 5)) return FS_FAT16; - if (!memcmp(&(fs->win[0x52]), &fatsign[5], 5) && (fs->win[0x28] == 0)) + if (!memcmp(&(fs->win[0x52]), &fatsign[10], 5) && (fs->win[0x28] == 0)) return FS_FAT32; } } @@ -511,7 +544,7 @@ FRESULT check_mounted () FRESULT f_mountdrv () { BYTE fat; - DWORD sect, fatend; + DWORD sect, fatend, maxsect; FATFS *fs = FatFs; @@ -526,7 +559,7 @@ FRESULT f_mountdrv () /* Search FAT partition */ fat = check_fs(sect = 0); /* Check sector 0 as an SFD format */ if (!fat) { /* Not a FAT boot record, it will be an FDISK format */ - /* Check a pri-partition listed in top of the partition table */ + /* Check a partition listed in top of the partition table */ if (fs->win[0x1C2]) { /* Is the partition existing? */ sect = LD_DWORD(&(fs->win[0x1C6])); /* Partition offset in LBA */ fat = check_fs(sect); /* Check the partition */ @@ -545,14 +578,15 @@ FRESULT f_mountdrv () fatend = fs->sects_fat * fs->n_fats + fs->fatbase; if (fat == FS_FAT32) { - fs->dirbase = LD_DWORD(&(fs->win[0x2C])); /* Directory start cluster */ - fs->database = fatend; /* Data start sector (physical) */ + fs->dirbase = LD_DWORD(&(fs->win[0x2C])); /* FAT32: Directory start cluster */ + fs->database = fatend; /* FAT32: Data start sector (physical) */ } else { fs->dirbase = fatend; /* Directory start sector (physical) */ fs->database = fs->n_rootdir / 16 + fatend; /* Data start sector (physical) */ } - fs->max_clust = /* Maximum cluster number */ - (LD_DWORD(&(fs->win[0x20])) - fs->database + sect) / fs->sects_clust + 2; + maxsect = LD_DWORD(&(fs->win[0x20])); /* Calculate maximum cluster number */ + if (!maxsect) maxsect = LD_WORD(&(fs->win[0x13])); + fs->max_clust = (maxsect - fs->database + sect) / fs->sects_clust + 2; return FR_OK; } @@ -567,32 +601,39 @@ FRESULT f_getfree ( ) { DWORD n, clust, sect; - BYTE m, *ptr, fat; + BYTE fat, f, *p; FRESULT res; FATFS *fs = FatFs; if ((res = check_mounted()) != FR_OK) return res; - fat = fs->fs_type; /* Count number of free clusters */ - n = m = clust = 0; - ptr = NULL; - sect = fs->fatbase; - do { - if (m == 0) { - if (!move_window(sect++)) return FR_RW_ERROR; - ptr = fs->win; - } - if (fat == FS_FAT32) { - if (LD_DWORD(ptr) == 0) n++; - ptr += 4; m += 2; - } else { - if (LD_WORD(ptr) == 0) n++; - ptr += 2; m++; - } - clust++; - } while (clust < fs->max_clust); + fat = fs->fs_type; + n = 0; + if (fat == FS_FAT12) { + clust = 2; + do { + if ((WORD)get_cluster(clust) == 0) n++; + } while (++clust < fs->max_clust); + } else { + clust = fs->max_clust; + sect = fs->fatbase; + f = 0; p = 0; + do { + if (!f) { + if (!move_window(sect++)) return FR_RW_ERROR; + p = fs->win; + } + if (fat == FS_FAT16) { + if (LD_WORD(p) == 0) n++; + p += 2; f += 1; + } else { + if (LD_DWORD(p) == 0) n++; + p += 4; f += 2; + } + } while (--clust); + } *nclust = n; return FR_OK; @@ -669,9 +710,9 @@ FRESULT f_open ( #endif #ifdef _FS_READONLY - fp->flag = mode & (FA_UNBUFFERED|FA_READ); + fp->flag = mode & FA_READ; #else - fp->flag = mode & (FA_UNBUFFERED|FA_WRITE|FA_READ); + fp->flag = mode & (FA_WRITE|FA_READ); fp->dir_sect = fs->winsect; /* Pointer to the directory entry */ fp->dir_ptr = dir; #endif @@ -711,7 +752,7 @@ FRESULT f_read ( for ( ; btr; /* Repeat until all data transferred */ buff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) { - if ((fp->fptr & 511) == 0) { /* On the sector boundary */ + if ((fp->fptr % 512) == 0) { /* On the sector boundary */ if (--(fp->sect_clust)) { /* Decrement sector counter */ sect = fp->curr_sect + 1; /* Next sector */ } else { /* Next cluster */ @@ -736,14 +777,12 @@ FRESULT f_read ( fp->curr_sect += cc - 1; rcnt = cc * 512; continue; } - if (fp->flag & FA_UNBUFFERED) /* Reject unaligned access when unbuffered mode */ - return FR_ALIGN_ERROR; if (disk_read(fp->buffer, sect, 1) != RES_OK) /* Load the sector into file I/O buffer */ goto fr_error; } - rcnt = 512 - (fp->fptr & 511); /* Copy fractional bytes from file I/O buffer */ + rcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes from file I/O buffer */ if (rcnt > btr) rcnt = btr; - memcpy(buff, &fp->buffer[fp->fptr & 511], rcnt); + memcpy(buff, &fp->buffer[fp->fptr % 512], rcnt); } return FR_OK; @@ -781,7 +820,7 @@ FRESULT f_write ( for ( ; btw; /* Repeat until all data transferred */ buff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) { - if ((fp->fptr & 511) == 0) { /* On the sector boundary */ + if ((fp->fptr % 512) == 0) { /* On the sector boundary */ if (--(fp->sect_clust)) { /* Decrement sector counter */ sect = fp->curr_sect + 1; /* Next sector */ } else { /* Next cluster */ @@ -810,15 +849,13 @@ FRESULT f_write ( fp->curr_sect += cc - 1; wcnt = cc * 512; continue; } - if (fp->flag & FA_UNBUFFERED) /* Reject unalighend access when unbuffered mode */ - return FR_ALIGN_ERROR; if ((fp->fptr < fp->fsize) && /* Fill sector buffer with file data if needed */ (disk_read(fp->buffer, sect, 1) != RES_OK)) goto fw_error; } - wcnt = 512 - (fp->fptr & 511); /* Copy fractional bytes to file I/O buffer */ + wcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes to file I/O buffer */ if (wcnt > btw) wcnt = btw; - memcpy(&fp->buffer[fp->fptr & 511], buff, wcnt); + memcpy(&fp->buffer[fp->fptr % 512], buff, wcnt); fp->flag |= FA__DIRTY; } @@ -857,7 +894,6 @@ FRESULT f_lseek ( } #endif if (ofs > fp->fsize) ofs = fp->fsize; /* Clip offset by file size */ - if ((ofs & 511) && (fp->flag & FA_UNBUFFERED)) return FR_ALIGN_ERROR; fp->fptr = ofs; fp->sect_clust = 1; /* Re-initialize file pointer */ /* Seek file pinter if needed */ @@ -872,7 +908,7 @@ FRESULT f_lseek ( if ((clust < 2) || (clust >= fs->max_clust)) goto fk_error; fp->curr_clust = clust; fp->curr_sect = clust2sect(clust) + sc - fp->sect_clust; /* Current sector */ - if (fp->fptr & 511) { /* Load currnet sector if needed */ + if (fp->fptr % 512) { /* Load currnet sector if needed */ if (disk_read(fp->buffer, fp->curr_sect, 1) != RES_OK) goto fk_error; } @@ -993,9 +1029,9 @@ FRESULT f_unlink ( } while (next_dir_entry(&dirscan)); } - if (!remove_chain(dclust)) return FR_RW_ERROR; /* Remove the cluster chain */ - if (!move_window(dsect)) return FR_RW_ERROR; /* Mark the directory entry deleted */ + if (!move_window(dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */ *dir = 0xE5; fs->winflag = 1; + if (!remove_chain(dclust)) return FR_RW_ERROR; /* Remove the cluster chain */ if (!move_window(0)) return FR_RW_ERROR; return FR_OK; diff --git a/src/ff.h b/src/ff.h index 663377d..53ad297 100644 --- a/src/ff.h +++ b/src/ff.h @@ -5,9 +5,6 @@ / cheap microcontrollers. This is opened for education, reserch and / development. You can use it for non-profit or profit use without any / limitation under your responsibility. -/---------------------------------------------------------------------------/ -/ Feb 26, 2006 R0.00 Prototype -/ Apr 29, 2006 R0.01 First stable version /---------------------------------------------------------------------------*/ #ifndef _FATFS @@ -32,56 +29,56 @@ typedef unsigned char FRESULT; /* File system object structure */ typedef struct _FATFS { - BYTE fs_type; // FAT type - BYTE files; // Number of files currently opend - BYTE sects_clust; // Sectors per cluster - BYTE n_fats; // Number of FAT copies - WORD n_rootdir; // Number of root directory entry - BYTE winflag; // win[] dirty flag (1:must be written back) + BYTE fs_type; /* FAT type */ + BYTE files; /* Number of files currently opend */ + BYTE sects_clust; /* Sectors per cluster */ + BYTE n_fats; /* Number of FAT copies */ + WORD n_rootdir; /* Number of root directory entry */ + BYTE winflag; /* win[] dirty flag (1:must be written back) */ BYTE pad1; - DWORD sects_fat; // Sectors per fat - DWORD max_clust; // Maximum cluster# + 1 - DWORD fatbase; // FAT start sector - DWORD dirbase; // Root directory start sector (cluster# for FAT32) - DWORD database; // Data start sector - DWORD winsect; // Current sector appearing in the win[] - BYTE win[512]; // Disk access window for Directory/FAT area + DWORD sects_fat; /* Sectors per fat */ + DWORD max_clust; /* Maximum cluster# + 1 */ + DWORD fatbase; /* FAT start sector */ + DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ + DWORD database; /* Data start sector */ + DWORD winsect; /* Current sector appearing in the win[] */ + BYTE win[512]; /* Disk access window for Directory/FAT area */ } FATFS; /* Directory object structure */ typedef struct _DIR { - DWORD sclust; // Start cluster - DWORD clust; // Current cluster - DWORD sect; // Current sector - WORD index; // Current index + DWORD sclust; /* Start cluster */ + DWORD clust; /* Current cluster */ + DWORD sect; /* Current sector */ + WORD index; /* Current index */ } DIR; /* File object structure */ typedef struct _FIL { - DWORD fptr; // File R/W pointer - DWORD fsize; // File size - DWORD org_clust; // File start cluster - DWORD curr_clust; // Current cluster - DWORD curr_sect; // Current sector + DWORD fptr; /* File R/W pointer */ + DWORD fsize; /* File size */ + DWORD org_clust; /* File start cluster */ + DWORD curr_clust; /* Current cluster */ + DWORD curr_sect; /* Current sector */ #ifndef _FS_READONLY - DWORD dir_sect; // Sector containing the directory entry - BYTE* dir_ptr; // Ponter to the directory entry in the window + DWORD dir_sect; /* Sector containing the directory entry */ + BYTE* dir_ptr; /* Ponter to the directory entry in the window */ #endif - BYTE* buffer; // Pointer to 512 byte file R/W buffer - BYTE flag; // File status flags - BYTE sect_clust; // Left sectors in cluster + BYTE flag; /* File status flags */ + BYTE sect_clust; /* Left sectors in cluster */ + BYTE buffer[512]; /* File R/W buffer */ } FIL; /* File status structure */ typedef struct _FILINFO { - DWORD fsize; // Size - WORD fdate; // Date - WORD ftime; // Time - BYTE fattrib; // Attribute - char fname[8+1+3+1]; // Name (8.3 format) + DWORD fsize; /* Size */ + WORD fdate; /* Date */ + WORD ftime; /* Time */ + BYTE fattrib; /* Attribute */ + char fname[8+1+3+1]; /* Name (8.3 format) */ } FILINFO; @@ -89,31 +86,31 @@ typedef struct _FILINFO { /*-----------------------------------------------------*/ /* FatFs module application interface */ -extern FATFS *FatFs; // Pointer to active file system object +extern FATFS *FatFs; /* Pointer to active file system object */ -FRESULT f_open (FIL*, const char*, BYTE); // Open or create a file -FRESULT f_read (FIL*, BYTE*, WORD, WORD*); // Read file -FRESULT f_close (FIL*); // Close file -FRESULT f_lseek (FIL*, DWORD); // Seek file pointer -FRESULT f_opendir (DIR*, const char*); // Initialize to read a directory -FRESULT f_readdir (DIR*, FILINFO*); // Read a directory item -FRESULT f_stat (const char*, FILINFO*); // Get file status -FRESULT f_getfree (DWORD*); // Get number of free clusters -FRESULT f_mountdrv (); // Force initialized the file system +FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ +FRESULT f_read (FIL*, BYTE*, WORD, WORD*); /* Read file */ +FRESULT f_close (FIL*); /* Close file */ +FRESULT f_lseek (FIL*, DWORD); /* Seek file pointer */ +FRESULT f_opendir (DIR*, const char*); /* Initialize to read a directory */ +FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */ +FRESULT f_stat (const char*, FILINFO*); /* Get file status */ +FRESULT f_getfree (DWORD*); /* Get number of free clusters */ +FRESULT f_mountdrv (); /* Force initialized the file system */ #ifndef _FS_READONLY -FRESULT f_write (FIL*, const BYTE*, WORD, WORD*); // Write file -FRESULT f_sync (FIL*); // Flush cached information of a writing file -FRESULT f_unlink (const char*); // Delete a file or directory -FRESULT f_mkdir (const char*); // Create a directory -FRESULT f_chmod (const char*, BYTE, BYTE); // Change file attriburte +FRESULT f_write (FIL*, const BYTE*, WORD, WORD*); /* Write file */ +FRESULT f_sync (FIL*); /* Flush cached information of a writing file */ +FRESULT f_unlink (const char*); /* Delete a file or directory */ +FRESULT f_mkdir (const char*); /* Create a directory */ +FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file attriburte */ #endif /* User defined function to give a current time to fatfs module */ #ifndef _FS_READONLY -DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) - // 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) +DWORD get_fattime(); /* 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) */ + /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ #endif @@ -128,7 +125,6 @@ DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day #define FR_DENIED 5 #define FR_DISK_FULL 6 #define FR_RW_ERROR 7 -#define FR_ALIGN_ERROR 8 #define FR_INCORRECT_DISK_CHANGE 9 #define FR_WRITE_PROTECTED 10 #define FR_NOT_ENABLED 11 @@ -138,7 +134,6 @@ DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day /* File access control and file status flags */ #define FA_READ 0x01 -#define FA_UNBUFFERED 0x04 #define FA_OPEN_EXISTING 0x00 #ifndef _FS_READONLY #define FA_WRITE 0x02 @@ -152,18 +147,19 @@ DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day /* FAT type signature (fs_type) */ -#define FS_FAT16 1 -#define FS_FAT32 2 +#define FS_FAT12 1 +#define FS_FAT16 2 +#define FS_FAT32 3 /* File attribute mask for directory entry */ -#define AM_RDO 0x01 // Read Only -#define AM_HID 0x02 // Hidden -#define AM_SYS 0x04 // System -#define AM_VOL 0x08 // Volume Label -#define AM_DIR 0x10 // Directory -#define AM_ARC 0x20 // Archive +#define AM_RDO 0x01 /* Read Only */ +#define AM_HID 0x02 /* Hidden */ +#define AM_SYS 0x04 /* System */ +#define AM_VOL 0x08 /* Volume Label */ +#define AM_DIR 0x10 /* Directory */ +#define AM_ARC 0x20 /* Archive */ diff --git a/src/tff.c b/src/tff.c index e6c4fb9..a9b1600 100644 --- a/src/tff.c +++ b/src/tff.c @@ -1,5 +1,5 @@ /*--------------------------------------------------------------------------/ -/ Tiny FatFs - FAT file system module R0.01 (C)ChaN, 2006 +/ Tiny FatFs - FAT file system module R0.02 (C)ChaN, 2006 /---------------------------------------------------------------------------/ / FatFs module is an experimenal project to implement FAT file system to / cheap microcontrollers. This is opened for education, reserch and @@ -8,6 +8,8 @@ /---------------------------------------------------------------------------/ / Feb 26, 2006 R0.00 Prototype / Apr 29, 2006 R0.01 First stable version +/ Jun 01, 2006 R0.02 Added FAT12. Removed unbuffered mode. +/ Fixed a problem on small (<32M) patition. /---------------------------------------------------------------------------*/ @@ -60,28 +62,38 @@ BOOL move_window ( -/*---------------------*/ -/* Get a Cluster State */ +/*----------------------*/ +/* Get a Cluster Status */ static -DWORD get_cluster (WORD clust) /* Cluster# to get the link information */ +WORD get_cluster (WORD clust) /* Cluster# to get the link information */ { + WORD wc, bc; + DWORD fatsect; FATFS *fs = FatFs; if ((clust >= 2) && (clust < fs->max_clust)) { /* Valid cluster# */ - if (fs->fs_type == FS_FAT16) { - if (move_window(clust / 256 + fs->fatbase)) - return LD_WORD(&(fs->win[(clust * 2) & 511])); + fatsect = fs->fatbase; + if (fs->fs_type == FS_FAT12) { + bc = clust * 3 / 2; + if (!move_window(fatsect + bc / 512)) return 1; + wc = fs->win[bc % 512]; bc++; + if (!move_window(fatsect + bc / 512)) return 1; + wc |= (WORD)fs->win[bc % 512] << 8; + return (clust & 1) ? (wc >> 4) : (wc & 0xFFF); + } else { + if (!move_window(fatsect + clust / 256)) return 1; + return LD_WORD(&(fs->win[(clust * 2) % 512])); } } - return 1; /* Return with 1 means failed */ + return 1; /* Return with 1 means function failed */ } -/*------------------------*/ -/* Change a Cluster State */ +/*-------------------------*/ +/* Change a Cluster Status */ #ifndef _FS_READONLY static @@ -90,12 +102,26 @@ BOOL put_cluster ( WORD val /* New value to mark the cluster */ ) { + WORD bc; + BYTE *p; + DWORD fatsect; FATFS *fs = FatFs; - if (fs->fs_type != FS_FAT16) return FALSE; - if (!move_window(clust / 256 + fs->fatbase)) return FALSE; - ST_WORD(&(fs->win[((WORD)clust * 2) & 511]), (WORD)val); + fatsect = fs->fatbase; + if (fs->fs_type == FS_FAT12) { + bc = clust * 3 / 2; + if (!move_window(fatsect + bc / 512)) return FALSE; + p = &fs->win[bc % 512]; + *p = (clust & 1) ? ((*p & 0x0F) | ((BYTE)val << 4)) : (BYTE)val; + fs->winflag = 1; bc++; + if (!move_window(fatsect + bc / 512)) return FALSE; + p = &fs->win[bc % 512]; + *p = (clust & 1) ? (BYTE)(val >> 4) : ((*p & 0xF0) | ((BYTE)(val >> 8) & 0x0F)); + } else { + if (!move_window(fatsect + clust / 256)) return FALSE; + ST_WORD(&(fs->win[((WORD)clust * 2) % 512]), (WORD)val); + } fs->winflag = 1; return TRUE; @@ -196,7 +222,7 @@ BYTE check_fs ( DWORD sect /* Sector# to check if it is a FAT boot record or not */ ) { - static const char fatsign[] = "FAT16"; + static const char fatsign[] = "FAT12FAT16"; FATFS *fs = FatFs; @@ -204,6 +230,8 @@ BYTE check_fs ( if (disk_read(fs->win, sect, 1) == RES_OK) { /* Load boot record */ if (LD_WORD(&(fs->win[510])) == 0xAA55) { /* Is it valid? */ if (!memcmp(&(fs->win[0x36]), &fatsign[0], 5)) + return FS_FAT12; + if (!memcmp(&(fs->win[0x36]), &fatsign[5], 5)) return FS_FAT16; } } @@ -492,7 +520,7 @@ FRESULT check_mounted () FRESULT f_mountdrv () { BYTE fat; - DWORD sect, fatend; + DWORD sect, fatend, maxsect; FATFS *fs = FatFs; @@ -524,10 +552,12 @@ FRESULT f_mountdrv () fs->n_rootdir = LD_WORD(&(fs->win[0x11])); /* Nmuber of root directory entries */ fatend = fs->sects_fat * fs->n_fats + fs->fatbase; - fs->dirbase = fatend; /* Directory start sector (physical) */ - fs->database = fs->n_rootdir / 16 + fatend; /* Data start sector (physical) */ - fs->max_clust = /* Maximum cluster number */ - (LD_DWORD(&(fs->win[0x20])) - fs->database + sect) / fs->sects_clust + 2; + fs->dirbase = fatend; /* Directory start sector (physical) */ + fs->database = fs->n_rootdir / 16 + fatend; /* Data start sector (physical) */ + + maxsect = LD_DWORD(&(fs->win[0x20])); /* Calculate maximum cluster number */ + if (!maxsect) maxsect = LD_WORD(&(fs->win[0x13])); + fs->max_clust = (maxsect - fs->database + sect) / fs->sects_clust + 2; fs->files = 0; return FR_OK; @@ -542,29 +572,35 @@ FRESULT f_getfree ( DWORD *nclust /* Pointer to the double word to return number of free clusters */ ) { - WORD clust; DWORD n, sect; - BYTE m, *ptr, fat; + WORD clust; + BYTE f, *p; FRESULT res; FATFS *fs = FatFs; if ((res = check_mounted()) != FR_OK) return res; - fat = fs->fs_type; /* Count number of free clusters */ - n = clust = m = 0; - ptr = NULL; - sect = fs->fatbase; - do { - if (m == 0) { - if (!move_window(sect++)) return FR_RW_ERROR; - ptr = fs->win; - } - if (LD_WORD(ptr) == 0) n++; - ptr += 2; m++; - clust++; - } while (clust < fs->max_clust); + n = 0; + if (fs->fs_type == FS_FAT12) { + clust = 2; + do { + if (get_cluster(clust) == 0) n++; + } while (++clust < fs->max_clust); + } else { + clust = fs->max_clust; + sect = fs->fatbase; + f = 0; p = 0; + do { + if (!f) { + if (!move_window(sect++)) return FR_RW_ERROR; + p = fs->win; + } + if (LD_WORD(p) == 0) n++; + p += 2; f += 1; + } while (--clust); + } *nclust = n; return FR_OK; @@ -682,7 +718,7 @@ FRESULT f_read ( for ( ; btr; /* Repeat until all data transferred */ buff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) { - if ((fp->fptr & 511) == 0) { /* On the sector boundary */ + if ((fp->fptr % 512) == 0) { /* On the sector boundary */ if (--(fp->sect_clust)) { /* Decrement sector counter */ sect = fp->curr_sect + 1; /* Next sector */ } else { /* Next cluster */ @@ -703,9 +739,9 @@ FRESULT f_read ( } } if (!move_window(fp->curr_sect)) goto fr_error; /* Move sector window */ - rcnt = 512 - (fp->fptr & 511); /* Copy fractional bytes from sector window */ + rcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes from sector window */ if (rcnt > btr) rcnt = btr; - memcpy(buff, &fs->win[fp->fptr & 511], rcnt); + memcpy(buff, &fs->win[fp->fptr % 512], rcnt); } return FR_OK; @@ -743,7 +779,7 @@ FRESULT f_write ( for ( ; btw; /* Repeat until all data transferred */ buff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) { - if ((fp->fptr & 511) == 0) { /* On the sector boundary */ + if ((fp->fptr % 512) == 0) { /* On the sector boundary */ if (--(fp->sect_clust)) { /* Decrement sector counter */ sect = fp->curr_sect + 1; /* Next sector */ } else { /* Next cluster */ @@ -774,9 +810,9 @@ FRESULT f_write ( } } if (!move_window(fp->curr_sect)) goto fw_error; /* Move sector window */ - wcnt = 512 - (fp->fptr & 511); /* Copy fractional bytes bytes to sector window */ + wcnt = 512 - (fp->fptr % 512); /* Copy fractional bytes bytes to sector window */ if (wcnt > btw) wcnt = btw; - memcpy(&fs->win[fp->fptr & 511], buff, wcnt); + memcpy(&fs->win[fp->fptr % 512], buff, wcnt); fs->winflag = 1; } @@ -934,9 +970,9 @@ FRESULT f_unlink ( } while (next_dir_entry(&dirscan)); } - if (!remove_chain(dclust)) return FR_RW_ERROR; /* Remove the cluster chain */ - if (!move_window(dsect)) return FR_RW_ERROR; /* Mark the directory entry deleted */ + if (!move_window(dsect)) return FR_RW_ERROR; /* Mark the directory entry 'deleted' */ *dir = 0xE5; fs->winflag = 1; + if (!remove_chain(dclust)) return FR_RW_ERROR; /* Remove the cluster chain */ if (!move_window(0)) return FR_RW_ERROR; return FR_OK; diff --git a/src/tff.h b/src/tff.h index b1d8597..e0b1f8a 100644 --- a/src/tff.h +++ b/src/tff.h @@ -5,9 +5,6 @@ / cheap microcontrollers. This is opened for education, reserch and / development. You can use it for non-profit or profit use without any / limitation under your responsibility. -/---------------------------------------------------------------------------/ -/ Feb 26, 2006 R0.00 Prototype -/ Apr 29, 2006 R0.01 First stable version /---------------------------------------------------------------------------*/ #ifndef _FATFS @@ -32,86 +29,86 @@ typedef unsigned char FRESULT; /* File system object */ typedef struct _FATFS { - BYTE fs_type; // FAT type - BYTE files; // Number of files currently opend - BYTE sects_clust; // Sectors per cluster - BYTE n_fats; // Number of FAT copies - WORD n_rootdir; // Number of root directory entry - BYTE winflag; // win[] dirty flag (1:must be written back) + BYTE fs_type; /* FAT type */ + BYTE files; /* Number of files currently opend */ + BYTE sects_clust; /* Sectors per cluster */ + BYTE n_fats; /* Number of FAT copies */ + WORD n_rootdir; /* Number of root directory entry */ + BYTE winflag; /* win[] dirty flag (1:must be written back) */ BYTE pad1; - WORD sects_fat; // Sectors per fat - WORD max_clust; // Maximum cluster# + 1 - DWORD fatbase; // FAT start sector - DWORD dirbase; // Root directory start sector (cluster# for FAT32) - DWORD database; // Data start sector - DWORD winsect; // Current sector appearing in the win[] - BYTE win[512]; // Disk access window + WORD sects_fat; /* Sectors per fat */ + WORD max_clust; /* Maximum cluster# + 1 */ + DWORD fatbase; /* FAT start sector */ + DWORD dirbase; /* Root directory start sector (cluster# for FAT32) */ + DWORD database; /* Data start sector */ + DWORD winsect; /* Current sector appearing in the win[] */ + BYTE win[512]; /* Disk access window */ } FATFS; /* Directory scan object */ typedef struct _DIR { - WORD sclust; // Start cluster - WORD clust; // Current cluster - DWORD sect; // Current sector - WORD index; // Current index + WORD sclust; /* Start cluster */ + WORD clust; /* Current cluster */ + DWORD sect; /* Current sector */ + WORD index; /* Current index */ } DIR; /* File object */ typedef struct _FIL { - DWORD fptr; // File Pointer - DWORD fsize; // File Size - WORD org_clust; // File start cluster - WORD curr_clust; // Current cluster - DWORD curr_sect; // Current sector + DWORD fptr; /* File Pointer */ + DWORD fsize; /* File Size */ + WORD org_clust; /* File start cluster */ + WORD curr_clust; /* Current cluster */ + DWORD curr_sect; /* Current sector */ #ifndef _FS_READONLY - DWORD dir_sect; // Sector containing the directory entry - BYTE* dir_ptr; // Ponter to the directory entry in the window + DWORD dir_sect; /* Sector containing the directory entry */ + BYTE* dir_ptr; /* Ponter to the directory entry in the window */ #endif - BYTE flag; // File status flags - BYTE sect_clust; // Left sectors in cluster + BYTE flag; /* File status flags */ + BYTE sect_clust; /* Left sectors in cluster */ } FIL; /* File status structure */ typedef struct _FILINFO { - DWORD fsize; // Size - WORD fdate; // Date - WORD ftime; // Time - BYTE fattrib; // Attribute - char fname[8+1+3+1]; // File/Directory name (8.3 format) + DWORD fsize; /* Size */ + WORD fdate; /* Date */ + WORD ftime; /* Time */ + BYTE fattrib; /* Attribute */ + char fname[8+1+3+1]; /* File/Directory name (8.3 format) */ } FILINFO; /* fatfs module application interface */ -extern FATFS *FatFs; // Pointer to active file system object +extern FATFS *FatFs; /* Pointer to active file system object */ -FRESULT f_open (FIL*, const char*, BYTE); // Open or create a file -FRESULT f_read (FIL*, BYTE*, WORD, WORD*); // Read file -FRESULT f_close (FIL*); // Close file -FRESULT f_lseek (FIL*, DWORD); // Seek file pointer -FRESULT f_opendir (DIR*, const char*); // Initialize to read a directory -FRESULT f_readdir (DIR*, FILINFO*); // Read a directory item -FRESULT f_stat (const char*, FILINFO*); // Get file status -FRESULT f_getfree (DWORD*); // Get number of free clusters -FRESULT f_mountdrv (); // Force initialized the file system +FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */ +FRESULT f_read (FIL*, BYTE*, WORD, WORD*); /* Read file */ +FRESULT f_close (FIL*); /* Close file */ +FRESULT f_lseek (FIL*, DWORD); /* Seek file pointer */ +FRESULT f_opendir (DIR*, const char*); /* Initialize to read a directory */ +FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */ +FRESULT f_stat (const char*, FILINFO*); /* Get file status */ +FRESULT f_getfree (DWORD*); /* Get number of free clusters */ +FRESULT f_mountdrv (); /* Force initialized the file system */ #ifndef _FS_READONLY -FRESULT f_write (FIL*, const BYTE*, WORD, WORD*); // Write file -FRESULT f_sync (FIL*); // Flush cached information of a writing file -FRESULT f_unlink (const char*); // Delete a file or directory -FRESULT f_mkdir (const char*); // Create a directory -FRESULT f_chmod (const char*, BYTE, BYTE); // Change file attriburte +FRESULT f_write (FIL*, const BYTE*, WORD, WORD*); /* Write file */ +FRESULT f_sync (FIL*); /* Flush cached information of a writing file */ +FRESULT f_unlink (const char*); /* Delete a file or directory */ +FRESULT f_mkdir (const char*); /* Create a directory */ +FRESULT f_chmod (const char*, BYTE, BYTE); /* Change file attriburte */ #endif /* User defined function to give a current time to fatfs module */ #ifndef _FS_READONLY -DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) - // 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) +DWORD get_fattime(); /* 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day(1-31) */ + /* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */ #endif @@ -126,7 +123,6 @@ DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day #define FR_DENIED 5 #define FR_DISK_FULL 6 #define FR_RW_ERROR 7 -#define FR_ALIGN_ERROR 8 #define FR_INCORRECT_DISK_CHANGE 9 #define FR_WRITE_PROTECTED 10 #define FR_NOT_ENABLED 11 @@ -148,17 +144,18 @@ DWORD get_fattime(); // 31-25: Year(0-127 +1980), 24-21: Month(1-12), 20-16: Day /* FAT type signature (fs_type) */ -#define FS_FAT16 1 +#define FS_FAT12 1 +#define FS_FAT16 2 /* File attribute mask for directory entry */ -#define AM_RDO 0x01 // Read Only -#define AM_HID 0x02 // Hidden -#define AM_SYS 0x04 // System -#define AM_VOL 0x08 // Volume Label -#define AM_DIR 0x10 // Directory -#define AM_ARC 0x20 // Archive +#define AM_RDO 0x01 /* Read Only */ +#define AM_HID 0x02 /* Hidden */ +#define AM_SYS 0x04 /* System */ +#define AM_VOL 0x08 /* Volume Label */ +#define AM_DIR 0x10 /* Directory */ +#define AM_ARC 0x20 /* Archive */