FatFs函数详解

来源:https://www.cnblogs.com/Danhuise/

disk_initialize:

/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */

DSTATUS disk_initialize (
    BYTE drv                /* Physical drive nmuber (0..) */
)
{
    SD_Error  Status;
    /* Supports only single drive */
    if (drv)
    {
        return STA_NOINIT;
    }
/*-------------------------- SD Init ----------------------------- */
  Status = SD_Init();
    if (Status!=SD_OK )
    {
        return STA_NOINIT;
    }
    else
    {
        return RES_OK;
    }

}

函数功能:初始化磁盘驱动器

描述:

disk_initialize函数初始化一个物理驱动器。函数成功后,返回值中的STA_NOINIT标志被清除。

disk_initialize函数被FatFs模块在卷挂载过程中调用,去管理存储介质的改变。当FatFs模块起作用时,或卷上的FAT结构可以被瓦解时,应用程序不能调用该函数。可以使用f_mount函数去重新初始化文件系统。

disk_status:

/*-----------------------------------------------------------------------*/
/* Return Disk Status                                                    */

DSTATUS disk_status (
    BYTE drv        /* Physical drive nmuber (0..) */
)
{
    return RES_OK;
}

函数功能:获取当前磁盘的状态

disk_read:

/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */

DRESULT disk_read (
    BYTE drv,        /* Physical drive nmuber (0..) */
    BYTE *buff,        /* Data buffer to store read data */
    DWORD sector,    /* Sector address (LBA) */
    BYTE count        /* Number of sectors to read (1..255) */
)
{
    return RES_OK;
}

函数功能:从磁盘驱动器中读取扇区

disk_write:

/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */

#if _READONLY == 0
DRESULT disk_write (
    BYTE drv,            /* Physical drive nmuber (0..) */
    const BYTE *buff,    /* Data to be written */
    DWORD sector,        /* Sector address (LBA) */
    BYTE count            /* Number of sectors to write (1..255) */
)
{
    return RES_OK;
}
#endif /* _READONLY */

函数功能:向磁盘驱动器中写入扇区

disk_ioctl:

/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */

DRESULT disk_ioctl (
    BYTE drv,        /* Physical drive nmuber (0..) */
    BYTE ctrl,        /* Control code */
    void *buff        /* Buffer to send/receive control data */
)
{
    return RES_OK;
}

get_fattime:

/*-----------------------------------------------------------------------*/
/* Get current time                                                      */
/*-----------------------------------------------------------------------*/
DWORD get_fattime(void)
{

     return 0;

}

函数功能:获取当前时间

描述:get_fattime函数必须返回任何有效的时间,即使系统不支持实时时钟。如果返回一个0,则文件将没有一个有效的时间。在只读配置中,不需要此函数。

f_gets:

/*-----------------------------------------------------------------------*/
/* Get a string from the file                                            */
/*-----------------------------------------------------------------------*/
TCHAR* f_gets (
    TCHAR* buff,    /* Pointer to the string buffer to read */
    int len,        /* Size of string buffer (characters) */
    FIL* fil        /* Pointer to the file object */
)
{
    int n = 0;
    TCHAR c, *p = buff;
    BYTE s[2];
    UINT rc;


    while (n < len - 1) {            /* Read bytes until buffer gets filled */
        f_read(fil, s, 1, &rc);
        if (rc != 1) break;            /* Break on EOF or error */
        c = s[0];
#if _LFN_UNICODE                    /* Read a character in UTF-8 encoding */
        if (c >= 0x80) {
            if (c < 0xC0) continue;    /* Skip stray trailer */
            if (c < 0xE0) {            /* Two-byte sequense */
                f_read(fil, s, 1, &rc);
                if (rc != 1) break;
                c = ((c & 0x1F) << 6) | (s[0] & 0x3F);
                if (c < 0x80) c = '?';
            } else {
                if (c < 0xF0) {        /* Three-byte sequense */
                    f_read(fil, s, 2, &rc);
                    if (rc != 2) break;
                    c = (c << 12) | ((s[0] & 0x3F) << 6) | (s[1] & 0x3F);
                    if (c < 0x800) c = '?';
                } else {            /* Reject four-byte sequense */
                    c = '?';
                }
            }
        }
#endif
#if _USE_STRFUNC >= 2
        if (c == '\r') continue;    /* Strip '\r' */
#endif
        *p++ = c;
        n++;
        if (c == '\n') break;        /* Break on EOL */
    }
    *p = 0;
    return n ? buff : 0;            /* When no data read (eof or error), return with error. */
}

函数功能:f_gets从文件中读取一个字符串。

描述:

f_gets函数当_USE_STRFUNC == 1或者_USE_STRFUNC == 2时可用。如果_USE_STRFUNC == 2,文件中包含的’\r’则被去除。
f_gets函数是f_read的一个封装函数。当读取到’\n’、文件结束或缓冲区被填冲了Size - 1个字符时,读操作结束。读取的字符串以’\0’结束。当文件结束或读操作中发生了任何错误,f_gets()返回一个空字符串。可以使用宏f_eof()和f_error()检查EOF和错误状态。

f_putc:

/*-----------------------------------------------------------------------*/
/* Put a character to the file                                           */
/*-----------------------------------------------------------------------*/
int f_putc (
    TCHAR c,    /* A character to be output */
    FIL* fil    /* Pointer to the file object */
)
{
    UINT bw, btw;
    BYTE s[3];


#if _USE_STRFUNC >= 2
    if (c == '\n') f_putc ('\r', fil);    /* LF -> CRLF conversion */
#endif

#if _LFN_UNICODE    /* Write the character in UTF-8 encoding */
    if (c < 0x80) {            /* 7-bit */
        s[0] = (BYTE)c;
        btw = 1;
    } else {
        if (c < 0x800) {    /* 11-bit */
            s[0] = (BYTE)(0xC0 | (c >> 6));
            s[1] = (BYTE)(0x80 | (c & 0x3F));
            btw = 2;
        } else {            /* 16-bit */
            s[0] = (BYTE)(0xE0 | (c >> 12));
            s[1] = (BYTE)(0x80 | ((c >> 6) & 0x3F));
            s[2] = (BYTE)(0x80 | (c & 0x3F));
            btw = 3;
        }
    }
#else                /* Write the character without conversion */
    s[0] = (BYTE)c;
    btw = 1;
#endif
    f_write(fil, s, btw, &bw);        /* Write the char to the file */
    return (bw == btw) ? 1 : EOF;    /* Return the result */
}

函数功能:f_putc函数向文件中写入一个字符。

描述:

f_putc函数当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,字符’\n’被转换为"\r\n"写入文件中。
f_putc函数是f_write的一个封装函数。

f_puts:

/*-----------------------------------------------------------------------*/
/* Put a string to the file                                              */
/*-----------------------------------------------------------------------*/
int f_puts (
    const TCHAR* str,    /* Pointer to the string to be output */
    FIL* fil            /* Pointer to the file object */
)
{
    int n;


    for (n = 0; *str; str++, n++) {
        if (f_putc(*str, fil) == EOF) return EOF;
    }
    return n;
}

函数功能:f_puts函数向文件中写入一个字符串。

描述:

f_puts()当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,字符串中的’\n’被转换为"\r\n"写入文件中。
f_puts()是f_putc()的一个封装函数。

f_printf:

/*-----------------------------------------------------------------------*/
/* Put a formatted string to the file                                    */
/*-----------------------------------------------------------------------*/
int f_printf (
    FIL* fil,            /* Pointer to the file object */
    const TCHAR* str,    /* Pointer to the format string */
    ...                    /* Optional arguments... */
)
{
    va_list arp;
    BYTE f, r;
    UINT i, j, w;
    ULONG v;
    TCHAR c, d, s[16], *p;
    int res, chc, cc;


    va_start(arp, str);

    for (cc = res = 0; cc != EOF; res += cc) {
        c = *str++;
        if (c == 0) break;            /* End of string */
        if (c != '%') {                /* Non escape character */
            cc = f_putc(c, fil);
            if (cc != EOF) cc = 1;
            continue;
        }
        w = f = 0;
        c = *str++;
        if (c == '0') {                /* Flag: '0' padding */
            f = 1; c = *str++;
        } else {
            if (c == '-') {            /* Flag: left justified */
                f = 2; c = *str++;
            }
        }
        while (IsDigit(c)) {        /* Precision */
            w = w * 10 + c - '0';
            c = *str++;
        }
        if (c == 'l' || c == 'L') {    /* Prefix: Size is long int */
            f |= 4; c = *str++;
        }
        if (!c) break;
        d = c;
        if (IsLower(d)) d -= 0x20;
        switch (d) {                /* Type is... */
        case 'S' :                    /* String */
            p = va_arg(arp, TCHAR*);
            for (j = 0; p[j]; j++) ;
            chc = 0;
            if (!(f & 2)) {
                while (j++ < w) chc += (cc = f_putc(' ', fil));
            }
            chc += (cc = f_puts(p, fil));
            while (j++ < w) chc += (cc = f_putc(' ', fil));
            if (cc != EOF) cc = chc;
            continue;
        case 'C' :                    /* Character */
            cc = f_putc((TCHAR)va_arg(arp, int), fil); continue;
        case 'B' :                    /* Binary */
            r = 2; break;
        case 'O' :                    /* Octal */
            r = 8; break;
        case 'D' :                    /* Signed decimal */
        case 'U' :                    /* Unsigned decimal */
            r = 10; break;
        case 'X' :                    /* Hexdecimal */
            r = 16; break;
        default:                    /* Unknown type (passthrough) */
            cc = f_putc(c, fil); continue;
        }

        /* Get an argument and put it in numeral */
        v = (f & 4) ? (ULONG)va_arg(arp, long) : ((d == 'D') ? (ULONG)(long)va_arg(arp, int) : (ULONG)va_arg(arp, unsigned int));
        if (d == 'D' && (v & 0x80000000)) {
            v = 0 - v;
            f |= 8;
        }
        i = 0;
        do {
            d = (TCHAR)(v % r); v /= r;
            if (d > 9) d += (c == 'x') ? 0x27 : 0x07;
            s[i++] = d + '0';
        } while (v && i < sizeof(s) / sizeof(s[0]));
        if (f & 8) s[i++] = '-';
        j = i; d = (f & 1) ? '0' : ' ';
        res = 0;
        while (!(f & 2) && j++ < w) res += (cc = f_putc(d, fil));
        do res += (cc = f_putc(s[--i], fil)); while(i);
        while (j++ < w) res += (cc = f_putc(' ', fil));
        if (cc != EOF) cc = res;
    }

    va_end(arp);
    return (cc == EOF) ? cc : res;
}

函数功能:f_printf函数向文件中写入一个格式化字符串。

描述:

f_printf函数当(_FS_READONLY == 0)&&(_USE_STRFUNC == 1 || _USE_STRFUNC == 2)时可用。当_USE_STRFUNC == 2时,包含在格式化字符串中的’\n’将被转换成"\r\n"写入文件中。

f_printf函数是f_putc和f_puts的一个封装函数。

f_gets(buf, NUM, &fil);            // 从文件中读取一个字符串的前 NUM个字符
f_putc(ch, &fil);                // 向文件中写入一个字符
f_puts((char *)buffer, &fil);    // 向文件内写入一个字符串

f_printf(&fil, "%6d", -200);         /* "  -200" */
f_printf(&fil, "%02u", 5);           /* "05" */
f_printf(&fil, "%ld", 12345678L);    /* "12345678" */
f_printf(&fil, "%08lX", 1194684UL);  /* "00123ABC" */
f_printf(&fil, "%s", "String");      /* "String" */
f_printf(&fil, "%c", 'a');           /* "a" */

f_chdir:

FRESULT f_chdir (
    const TCHAR *path    /* Pointer to the directory path */
)
{
    FRESULT res;
    DIR dj;
    DEF_NAMEBUF;


    res = chk_mounted(&path, &dj.fs, 0);
    if (res == FR_OK) {
        INIT_BUF(dj);
        res = follow_path(&dj, path);        /* Follow the path */
        FREE_BUF();
        if (res == FR_OK) {                    /* Follow completed */
            if (!dj.dir) {
                dj.fs->cdir = dj.sclust;    /* Start directory itself */
            } else {
                if (dj.dir[DIR_Attr] & AM_DIR)    /* Reached to the directory */
                    dj.fs->cdir = LD_CLUST(dj.dir);
                else
                    res = FR_NO_PATH;        /* Reached but a file */
            }
        }
        if (res == FR_NO_FILE) res = FR_NO_PATH;
    }

    LEAVE_FF(dj.fs, res);
}

函数功能:f_chdir函数改变一个驱动器的当前目录。

描述:

f_chdir函数当_FS_RPATH == 1时可用。
f_chdir函数改变一个逻辑驱动器的当前目录。当一个逻辑驱动器被自动挂载时,它的当前目录被初始化为根目录。注意:当前目录被保存在每个文件系统对象中,因此它也影响使用同一逻辑驱动器的其它任务。

f_chdrive:

FRESULT f_chdrive (
    BYTE drv        /* Drive number */
)
{
    if (drv >= _VOLUMES) return FR_INVALID_DRIVE;

    CurrVol = drv;

    return FR_OK;
}

函数功能:f_chdrive函数改变当前驱动器。

描述:

f_chdrive函数当_FS_RPATH == 1时可用。
f_chdrive函数改变当前驱动器。当前驱动器号初始值为0,注意:当前驱动器被保存为一个静态变量,因此它也影响使用文件函数的其它任务。

   // 改变当前驱动器的当前目录(根目录下的dir1)
     f_chdir("/dir1");
  
     // 改变驱动器2的当前目录(父目录)
     f_chdir("2:..");

f_forward:

/*-----------------------------------------------------------------------*/
/* Forward data to the stream directly (available on only tiny cfg)      */
/*-----------------------------------------------------------------------*/

FRESULT f_forward (
    FIL *fp,                         /* Pointer to the file object */
    UINT (*func)(const BYTE*,UINT),    /* Pointer to the streaming function */
    UINT btr,                        /* Number of bytes to forward */
    UINT *bf                        /* Pointer to number of bytes forwarded */
)
{
    FRESULT res;
    DWORD remain, clst, sect;
    UINT rcnt;
    BYTE csect;


    *bf = 0;    /* Initialize byte counter */

    res = validate(fp->fs, fp->id);                    /* Check validity of the object */
    if (res != FR_OK) LEAVE_FF(fp->fs, res);
    if (fp->flag & FA__ERROR)                        /* Check error flag */
        LEAVE_FF(fp->fs, FR_INT_ERR);
    if (!(fp->flag & FA_READ))                        /* Check access mode */
        LEAVE_FF(fp->fs, FR_DENIED);

    remain = fp->fsize - fp->fptr;
    if (btr > remain) btr = (UINT)remain;            /* Truncate btr by remaining bytes */

    for ( ;  btr && (*func)(0, 0);                    /* Repeat until all data transferred or stream becomes busy */
        fp->fptr += rcnt, *bf += rcnt, btr -= rcnt) {
        csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));    /* Sector offset in the cluster */
        if ((fp->fptr % SS(fp->fs)) == 0) {            /* On the sector boundary? */
            if (!csect) {                            /* On the cluster boundary? */
                clst = (fp->fptr == 0) ?            /* On the top of the file? */
                    fp->sclust : get_fat(fp->fs, fp->clust);
                if (clst <= 1) ABORT(fp->fs, FR_INT_ERR);
                if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
                fp->clust = clst;                    /* Update current cluster */
            }
        }
        sect = clust2sect(fp->fs, fp->clust);        /* Get current data sector */
        if (!sect) ABORT(fp->fs, FR_INT_ERR);
        sect += csect;
        if (move_window(fp->fs, sect))                /* Move sector window */
            ABORT(fp->fs, FR_DISK_ERR);
        fp->dsect = sect;
        rcnt = SS(fp->fs) - (WORD)(fp->fptr % SS(fp->fs));    /* Forward data from sector window */
        if (rcnt > btr) rcnt = btr;
        rcnt = (*func)(&fp->fs->win[(WORD)fp->fptr % SS(fp->fs)], rcnt);
        if (!rcnt) ABORT(fp->fs, FR_INT_ERR);
    }

    LEAVE_FF(fp->fs, FR_OK);
}

函数功能:读取文件数据并将其转发到数据流设备。

描述:

f_forward函数当_USE_FORWARD == 1并且_FS_TINY == 1时可用。
f_forward函数从文件中读取数据并将数据转发到输出流,而不使用数据缓冲区。这适用于小存储系统,因为它在应用模块中不需要任何数据缓冲区。文件对象的文件指针以转发的字节数增加。如果*ByteFwd <ByteToFwd并且没有错误,则意味着由于文件结束或在数据传输过程中流忙,请求的字节不能被传输。

/*-----------------------------------------------------------------------*/
/* 示例代码:数据传输函数,将被f_forward函数调用                                 */
/*-----------------------------------------------------------------------*/

UINT out_stream (   /* 返回已发送字节数或流状态 */
    const BYTE *p,  /* 将被发送的数据块的指针 */
    UINT btf        /* >0: 传输调用(将被发送的字节数)。0: 检测调用 */
)
{
    UINT cnt = 0;

    if (btf == 0) {     /* 检测调用 */
        /* 返回流状态(0: 忙,1: 就绪) */
        /* 当检测调用时,一旦它返回就绪,那么在后续的传输调用时,它必须接收至少一个字节,或者f_forward将以FR_INT_ERROR而失败。 */
        if (FIFO_READY) cnt = 1;
    }
    else {              /* 传输调用 */
        do {    /* 当有数据要发送并且流就绪时重复 */
            FIFO_PORT = *p++;
            cnt++;
        } while (cnt < btf && FIFO_READY);
    }

    return cnt;
}


/*-----------------------------------------------------------------------*/
/* 示例代码:使用f_forward函数                                                    */
/*-----------------------------------------------------------------------*/

FRESULT play_file (
    char *fn        /* 待播放的音频文件名的指针 */
)
{
    FRESULT rc;
    FIL fil;
    UINT dmy;

    /* 以只读模式打开音频文件 */
    rc = f_open(&fil, fn, FA_READ);

    /* 重复,直到文件指针到达文件结束位置 */
    while (rc == FR_OK && fil.fptr < fil.fsize) {

        /* 任何其他处理... */

        /* 定期或请求式填充输出流 */
        rc = f_forward(&fil, out_stream, 1000, &dmy);
    }

    /* 该只读的音频文件对象不需要关闭就可以被丢弃 */
    return rc;
}

f_mkfs:

/*-----------------------------------------------------------------------*/
/* Create File System on the Drive                                       */
/*-----------------------------------------------------------------------*/

FRESULT f_mkfs (
    BYTE drv,        /* Logical drive number */
    BYTE sfd,        /* Partitioning rule 0:FDISK, 1:SFD */
    UINT au            /* Allocation unit size [bytes] */
)
{
    static const WORD vst[] = { 1024,   512,  256,  128,   64,    32,   16,    8,    4,    2,   0};
    static const WORD cst[] = {32768, 16384, 8192, 4096, 2048, 16384, 8192, 4096, 2048, 1024, 512};
    BYTE fmt, md, sys, *tbl, pdrv, part;
    DWORD n_clst, vs, n, wsect;
    UINT i;
    DWORD b_vol, b_fat, b_dir, b_data;    /* LBA */
    DWORD n_vol, n_rsv, n_fat, n_dir;    /* Size */
    FATFS *fs;
    DSTATUS stat;


    /* Check mounted drive and clear work area */
    if (drv >= _VOLUMES) return FR_INVALID_DRIVE;
    if (sfd > 1) return FR_INVALID_PARAMETER;
    if (au & (au - 1)) return FR_INVALID_PARAMETER;
    fs = FatFs[drv];
    if (!fs) return FR_NOT_ENABLED;
    fs->fs_type = 0;
    pdrv = LD2PD(drv);    /* Physical drive */
    part = LD2PT(drv);    /* Partition (0:auto detect, 1-4:get from partition table)*/

    /* Get disk statics */
    stat = disk_initialize(pdrv);
    if (stat & STA_NOINIT) return FR_NOT_READY;
    if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
#if _MAX_SS != 512                    /* Get disk sector size */
    if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &SS(fs)) != RES_OK || SS(fs) > _MAX_SS)
        return FR_DISK_ERR;
#endif
    if (_MULTI_PARTITION && part) {
        /* Get partition information from partition table in the MBR */
        if (disk_read(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
        if (LD_WORD(fs->win+BS_55AA) != 0xAA55) return FR_MKFS_ABORTED;
        tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
        if (!tbl[4]) return FR_MKFS_ABORTED;    /* No partition? */
        b_vol = LD_DWORD(tbl+8);    /* Volume start sector */
        n_vol = LD_DWORD(tbl+12);    /* Volume size */
    } else {
        /* Create a partition in this function */
        if (disk_ioctl(pdrv, GET_SECTOR_COUNT, &n_vol) != RES_OK || n_vol < 128)
            return FR_DISK_ERR;
        b_vol = (sfd) ? 0 : 63;        /* Volume start sector */
        n_vol -= b_vol;                /* Volume size */
    }

    if (!au) {                /* AU auto selection */
        vs = n_vol / (2000 / (SS(fs) / 512));
        for (i = 0; vs < vst[i]; i++) ;
        au = cst[i];
    }
    au /= SS(fs);        /* Number of sectors per cluster */
    if (au == 0) au = 1;
    if (au > 128) au = 128;

    /* Pre-compute number of clusters and FAT syb-type */
    n_clst = n_vol / au;
    fmt = FS_FAT12;
    if (n_clst >= MIN_FAT16) fmt = FS_FAT16;
    if (n_clst >= MIN_FAT32) fmt = FS_FAT32;

    /* Determine offset and size of FAT structure */
    if (fmt == FS_FAT32) {
        n_fat = ((n_clst * 4) + 8 + SS(fs) - 1) / SS(fs);
        n_rsv = 32;
        n_dir = 0;
    } else {
        n_fat = (fmt == FS_FAT12) ? (n_clst * 3 + 1) / 2 + 3 : (n_clst * 2) + 4;
        n_fat = (n_fat + SS(fs) - 1) / SS(fs);
        n_rsv = 1;
        n_dir = (DWORD)N_ROOTDIR * SZ_DIR / SS(fs);
    }
    b_fat = b_vol + n_rsv;                /* FAT area start sector */
    b_dir = b_fat + n_fat * N_FATS;        /* Directory area start sector */
    b_data = b_dir + n_dir;                /* Data area start sector */
    if (n_vol < b_data + au - b_vol) return FR_MKFS_ABORTED;    /* Too small volume */

    /* Align data start sector to erase block boundary (for flash memory media) */
    if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &n) != RES_OK || !n || n > 32768) n = 1;
    n = (b_data + n - 1) & ~(n - 1);    /* Next nearest erase block from current data start */
    n = (n - b_data) / N_FATS;
    if (fmt == FS_FAT32) {        /* FAT32: Move FAT offset */
        n_rsv += n;
        b_fat += n;
    } else {                    /* FAT12/16: Expand FAT size */
        n_fat += n;
    }

    /* Determine number of clusters and final check of validity of the FAT sub-type */
    n_clst = (n_vol - n_rsv - n_fat * N_FATS - n_dir) / au;
    if (   (fmt == FS_FAT16 && n_clst < MIN_FAT16)
        || (fmt == FS_FAT32 && n_clst < MIN_FAT32))
        return FR_MKFS_ABORTED;

    switch (fmt) {    /* Determine system ID for partition table */
    case FS_FAT12:    sys = 0x01; break;
    case FS_FAT16:    sys = (n_vol < 0x10000) ? 0x04 : 0x06; break;
    default:         sys = 0x0C;
    }

    if (_MULTI_PARTITION && part) {
        /* Update system ID in the partition table */
        tbl = &fs->win[MBR_Table + (part - 1) * SZ_PTE];
        tbl[4] = sys;
        if (disk_write(pdrv, fs->win, 0, 1) != RES_OK) return FR_DISK_ERR;
        md = 0xF8;
    } else {
        if (sfd) {    /* No patition table (SFD) */
            md = 0xF0;
        } else {    /* Create partition table (FDISK) */
            mem_set(fs->win, 0, SS(fs));
            tbl = fs->win+MBR_Table;    /* Create partiton table for single partition in the drive */
            tbl[1] = 1;                        /* Partition start head */
            tbl[2] = 1;                        /* Partition start sector */
            tbl[3] = 0;                        /* Partition start cylinder */
            tbl[4] = sys;                    /* System type */
            tbl[5] = 254;                    /* Partition end head */
            n = (b_vol + n_vol) / 63 / 255;
            tbl[6] = (BYTE)((n >> 2) | 63);    /* Partiiton end sector */
            tbl[7] = (BYTE)n;                /* End cylinder */
            ST_DWORD(tbl+8, 63);            /* Partition start in LBA */
            ST_DWORD(tbl+12, n_vol);        /* Partition size in LBA */
            ST_WORD(fs->win+BS_55AA, 0xAA55);    /* MBR signature */
            if (disk_write(pdrv, fs->win, 0, 1) != RES_OK)    /* Write it to the MBR sector */
                return FR_DISK_ERR;
            md = 0xF8;
        }
    }

    /* Create BPB in the VBR */
    tbl = fs->win;                            /* Clear sector */
    mem_set(tbl, 0, SS(fs));
    mem_cpy(tbl, "\xEB\xFE\x90" "MSDOS5.0", 11);/* Boot jump code, OEM name */
    i = SS(fs);                                /* Sector size */
    ST_WORD(tbl+BPB_BytsPerSec, i);
    tbl[BPB_SecPerClus] = (BYTE)au;            /* Sectors per cluster */
    ST_WORD(tbl+BPB_RsvdSecCnt, n_rsv);        /* Reserved sectors */
    tbl[BPB_NumFATs] = N_FATS;                /* Number of FATs */
    i = (fmt == FS_FAT32) ? 0 : N_ROOTDIR;    /* Number of rootdir entries */
    ST_WORD(tbl+BPB_RootEntCnt, i);
    if (n_vol < 0x10000) {                    /* Number of total sectors */
        ST_WORD(tbl+BPB_TotSec16, n_vol);
    } else {
        ST_DWORD(tbl+BPB_TotSec32, n_vol);
    }
    tbl[BPB_Media] = md;                    /* Media descriptor */
    ST_WORD(tbl+BPB_SecPerTrk, 63);            /* Number of sectors per track */
    ST_WORD(tbl+BPB_NumHeads, 255);            /* Number of heads */
    ST_DWORD(tbl+BPB_HiddSec, b_vol);        /* Hidden sectors */
    n = get_fattime();                        /* Use current time as VSN */
    if (fmt == FS_FAT32) {
        ST_DWORD(tbl+BS_VolID32, n);        /* VSN */
        ST_DWORD(tbl+BPB_FATSz32, n_fat);    /* Number of sectors per FAT */
        ST_DWORD(tbl+BPB_RootClus, 2);        /* Root directory start cluster (2) */
        ST_WORD(tbl+BPB_FSInfo, 1);            /* FSInfo record offset (VBR+1) */
        ST_WORD(tbl+BPB_BkBootSec, 6);        /* Backup boot record offset (VBR+6) */
        tbl[BS_DrvNum32] = 0x80;            /* Drive number */
        tbl[BS_BootSig32] = 0x29;            /* Extended boot signature */
        mem_cpy(tbl+BS_VolLab32, "NO NAME    " "FAT32   ", 19);    /* Volume label, FAT signature */
    } else {
        ST_DWORD(tbl+BS_VolID, n);            /* VSN */
        ST_WORD(tbl+BPB_FATSz16, n_fat);    /* Number of sectors per FAT */
        tbl[BS_DrvNum] = 0x80;                /* Drive number */
        tbl[BS_BootSig] = 0x29;                /* Extended boot signature */
        mem_cpy(tbl+BS_VolLab, "NO NAME    " "FAT     ", 19);    /* Volume label, FAT signature */
    }
    ST_WORD(tbl+BS_55AA, 0xAA55);            /* Signature (Offset is fixed here regardless of sector size) */
    if (disk_write(pdrv, tbl, b_vol, 1) != RES_OK)    /* Write it to the VBR sector */
        return FR_DISK_ERR;
    if (fmt == FS_FAT32)                            /* Write backup VBR if needed (VBR+6) */
        disk_write(pdrv, tbl, b_vol + 6, 1);

    /* Initialize FAT area */
    wsect = b_fat;
    for (i = 0; i < N_FATS; i++) {        /* Initialize each FAT copy */
        mem_set(tbl, 0, SS(fs));            /* 1st sector of the FAT  */
        n = md;                                /* Media descriptor byte */
        if (fmt != FS_FAT32) {
            n |= (fmt == FS_FAT12) ? 0x00FFFF00 : 0xFFFFFF00;
            ST_DWORD(tbl+0, n);                /* Reserve cluster #0-1 (FAT12/16) */
        } else {
            n |= 0xFFFFFF00;
            ST_DWORD(tbl+0, n);                /* Reserve cluster #0-1 (FAT32) */
            ST_DWORD(tbl+4, 0xFFFFFFFF);
            ST_DWORD(tbl+8, 0x0FFFFFFF);    /* Reserve cluster #2 for root dir */
        }
        if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
            return FR_DISK_ERR;
        mem_set(tbl, 0, SS(fs));            /* Fill following FAT entries with zero */
        for (n = 1; n < n_fat; n++) {        /* This loop may take a time on FAT32 volume due to many single sector writes */
            if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
                return FR_DISK_ERR;
        }
    }

    /* Initialize root directory */
    i = (fmt == FS_FAT32) ? au : n_dir;
    do {
        if (disk_write(pdrv, tbl, wsect++, 1) != RES_OK)
            return FR_DISK_ERR;
    } while (--i);

#if _USE_ERASE    /* Erase data area if needed */
    {
        DWORD eb[2];

        eb[0] = wsect; eb[1] = wsect + (n_clst - ((fmt == FS_FAT32) ? 1 : 0)) * au - 1;
        disk_ioctl(pdrv, CTRL_ERASE_SECTOR, eb);
    }
#endif

    /* Create FSInfo if needed */
    if (fmt == FS_FAT32) {
        ST_DWORD(tbl+FSI_LeadSig, 0x41615252);
        ST_DWORD(tbl+FSI_StrucSig, 0x61417272);
        ST_DWORD(tbl+FSI_Free_Count, n_clst - 1);    /* Number of free clusters */
        ST_DWORD(tbl+FSI_Nxt_Free, 2);                /* Last allocated cluster# */
        ST_WORD(tbl+BS_55AA, 0xAA55);
        disk_write(pdrv, tbl, b_vol + 1, 1);    /* Write original (VBR+1) */
        disk_write(pdrv, tbl, b_vol + 7, 1);    /* Write backup (VBR+7) */
    }

    return (disk_ioctl(pdrv, CTRL_SYNC, 0) == RES_OK) ? FR_OK : FR_DISK_ERR;
}

函数功能:在驱动器上创建一个文件系统(格式化 SD卡)

描述:

f_mkfs函数当_FS_READOLNY == 0并且_USE_MKFS == 1时可用。
f_mkfs函数在驱动器中创建一个FAT文件系统。对于可移动媒介,有两种分区规则:FDISK和SFD,通过参数PartitioningRule选择。FDISK格式在大多数情况下被推荐使用。该函数当前不支持多分区,因此,物理驱动器上已存在的分区将被删除,并且重新创建一个占据全部磁盘空间的新分区。
根据Microsoft发布的FAT规范,FAT分类:FAT12/FAT16/FAT32,由驱动器上的簇数决定。因此,选择哪种FAT分类,取决于卷大小和指定的簇大小。簇大小影响文件系统的性能,并且大簇会提高性能。

FATFS fs;
  FIL file;
  FRESULT res;
  char array[512]={0}, *parray = array;

  res = f_mount(0, &fs);
  res = f_mkfs(0 , 0, _MAX_SS);
  res = f_open(&file, "data.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  f_printf(&file, "%s\n", "Success");        /* "Success" */
  parray = f_gets(parray , 8, &file);
  LCMTextOutExt(  0, 0, parray );
  f_close(&file);
  f_mount(0, NULL);

f_rename:

/*-----------------------------------------------------------------------*/
/* Rename File/Directory                                                 */
/*-----------------------------------------------------------------------*/

FRESULT f_rename (
    const TCHAR *path_old,    /* Pointer to the old name */
    const TCHAR *path_new    /* Pointer to the new name */
)
{
    FRESULT res;
    DIR djo, djn;
    BYTE buf[21], *dir;
    DWORD dw;
    DEF_NAMEBUF;


    res = chk_mounted(&path_old, &djo.fs, 1);
    if (res == FR_OK) {
        djn.fs = djo.fs;
        INIT_BUF(djo);
        res = follow_path(&djo, path_old);        /* Check old object */
        if (_FS_RPATH && res == FR_OK && (djo.fn[NS] & NS_DOT))
            res = FR_INVALID_NAME;
#if _FS_SHARE
        if (res == FR_OK) res = chk_lock(&djo, 2);
#endif
        if (res == FR_OK) {                        /* Old object is found */
            if (!djo.dir) {                        /* Is root dir? */
                res = FR_NO_FILE;
            } else {
                mem_cpy(buf, djo.dir+DIR_Attr, 21);        /* Save the object information except for name */
                mem_cpy(&djn, &djo, sizeof(DIR));        /* Check new object */
                res = follow_path(&djn, path_new);
                if (res == FR_OK) res = FR_EXIST;        /* The new object name is already existing */
                if (res == FR_NO_FILE) {                 /* Is it a valid path and no name collision? */
/* Start critical section that any interruption or error can cause cross-link */
                    res = dir_register(&djn);            /* Register the new entry */
                    if (res == FR_OK) {
                        dir = djn.dir;                    /* Copy object information except for name */
                        mem_cpy(dir+13, buf+2, 19);
                        dir[DIR_Attr] = buf[0] | AM_ARC;
                        djo.fs->wflag = 1;
                        if (djo.sclust != djn.sclust && (dir[DIR_Attr] & AM_DIR)) {        /* Update .. entry in the directory if needed */
                            dw = clust2sect(djn.fs, LD_CLUST(dir));
                            if (!dw) {
                                res = FR_INT_ERR;
                            } else {
                                res = move_window(djn.fs, dw);
                                dir = djn.fs->win+SZ_DIR;    /* .. entry */
                                if (res == FR_OK && dir[1] == '.') {
                                    dw = (djn.fs->fs_type == FS_FAT32 && djn.sclust == djn.fs->dirbase) ? 0 : djn.sclust;
                                    ST_CLUST(dir, dw);
                                    djn.fs->wflag = 1;
                                }
                            }
                        }
                        if (res == FR_OK) {
                            res = dir_remove(&djo);        /* Remove old entry */
                            if (res == FR_OK)
                                res = sync(djo.fs);
                        }
                    }
/* End critical section */
                }
            }
        }
        FREE_BUF();
    }
    LEAVE_FF(djo.fs, res);
}

函数功能:重命名一个对象。

描述:

f_rename函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_rename函数重命名一个对象,并且也可以将对象移动到其他目录。逻辑驱动器号由原名决定,新名不能包含一个逻辑驱动器号。不要重命名打开的对象。

     /* 重命名一个对象 */
     f_rename("oldname.txt", "newname.txt");
  
     /* 重命名并且移动一个对象到另一个目录 */
     f_rename("oldname.txt", "dir1/newname.txt");

f_chmod:

/*-----------------------------------------------------------------------*/
/* Change Attribute                                                      */
/*-----------------------------------------------------------------------*/

FRESULT f_chmod (
    const TCHAR *path,    /* Pointer to the file path */
    BYTE value,            /* Attribute bits */
    BYTE mask            /* Attribute mask to change */
)
{
    FRESULT res;
    DIR dj;
    BYTE *dir;
    DEF_NAMEBUF;


    res = chk_mounted(&path, &dj.fs, 1);
    if (res == FR_OK) {
        INIT_BUF(dj);
        res = follow_path(&dj, path);        /* Follow the file path */
        FREE_BUF();
        if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
            res = FR_INVALID_NAME;
        if (res == FR_OK) {
            dir = dj.dir;
            if (!dir) {                        /* Is it a root directory? */
                res = FR_INVALID_NAME;
            } else {                        /* File or sub directory */
                mask &= AM_RDO|AM_HID|AM_SYS|AM_ARC;    /* Valid attribute mask */
                dir[DIR_Attr] = (value & mask) | (dir[DIR_Attr] & (BYTE)~mask);    /* Apply attribute change */
                dj.fs->wflag = 1;
                res = sync(dj.fs);
            }
        }
    }

    LEAVE_FF(dj.fs, res);
}

函数功能:修改一个文件或目录的属性。

描述:

f_chmod函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_chmod函数修改一个文件或目录的属性。

f_utime:

/*-----------------------------------------------------------------------*/
/* Change Timestamp                                                      */
/*-----------------------------------------------------------------------*/

FRESULT f_utime (
    const TCHAR *path,    /* Pointer to the file/directory name */
    const FILINFO *fno    /* Pointer to the time stamp to be set */
)
{
    FRESULT res;
    DIR dj;
    BYTE *dir;
    DEF_NAMEBUF;


    res = chk_mounted(&path, &dj.fs, 1);
    if (res == FR_OK) {
        INIT_BUF(dj);
        res = follow_path(&dj, path);    /* Follow the file path */
        FREE_BUF();
        if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
            res = FR_INVALID_NAME;
        if (res == FR_OK) {
            dir = dj.dir;
            if (!dir) {                    /* Root directory */
                res = FR_INVALID_NAME;
            } else {                    /* File or sub-directory */
                ST_WORD(dir+DIR_WrtTime, fno->ftime);
                ST_WORD(dir+DIR_WrtDate, fno->fdate);
                dj.fs->wflag = 1;
                res = sync(dj.fs);
            }
        }
    }

    LEAVE_FF(dj.fs, res);
}

函数功能:f_utime函数修改一个文件或目录的时间戳。

描述:

f_utime函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_utime函数修改一个文件或目录的时间戳(即文件属性里的创建、修改、访问时间)。

 // 设置只读标志,清除存档标志,其他不变
 f_chmod("file.txt", AR_RDO, AR_RDO | AR_ARC);

f_mkdir:

/*-----------------------------------------------------------------------*/
/* Create a Directory                                                    */
/*-----------------------------------------------------------------------*/

FRESULT f_mkdir (
    const TCHAR *path        /* Pointer to the directory path */
)
{
    FRESULT res;
    DIR dj;
    BYTE *dir, n;
    DWORD dsc, dcl, pcl, tim = get_fattime();
    DEF_NAMEBUF;


    res = chk_mounted(&path, &dj.fs, 1);
    if (res == FR_OK) {
        INIT_BUF(dj);
        res = follow_path(&dj, path);            /* Follow the file path */
        if (res == FR_OK) res = FR_EXIST;        /* Any object with same name is already existing */
        if (_FS_RPATH && res == FR_NO_FILE && (dj.fn[NS] & NS_DOT))
            res = FR_INVALID_NAME;
        if (res == FR_NO_FILE) {                /* Can create a new directory */
            dcl = create_chain(dj.fs, 0);        /* Allocate a cluster for the new directory table */
            res = FR_OK;
            if (dcl == 0) res = FR_DENIED;        /* No space to allocate a new cluster */
            if (dcl == 1) res = FR_INT_ERR;
            if (dcl == 0xFFFFFFFF) res = FR_DISK_ERR;
            if (res == FR_OK)                    /* Flush FAT */
                res = move_window(dj.fs, 0);
            if (res == FR_OK) {                    /* Initialize the new directory table */
                dsc = clust2sect(dj.fs, dcl);
                dir = dj.fs->win;
                mem_set(dir, 0, SS(dj.fs));
                mem_set(dir+DIR_Name, ' ', 8+3);    /* Create "." entry */
                dir[DIR_Name] = '.';
                dir[DIR_Attr] = AM_DIR;
                ST_DWORD(dir+DIR_WrtTime, tim);
                ST_CLUST(dir, dcl);
                mem_cpy(dir+SZ_DIR, dir, SZ_DIR);     /* Create ".." entry */
                dir[33] = '.'; pcl = dj.sclust;
                if (dj.fs->fs_type == FS_FAT32 && pcl == dj.fs->dirbase)
                    pcl = 0;
                ST_CLUST(dir+SZ_DIR, pcl);
                for (n = dj.fs->csize; n; n--) {    /* Write dot entries and clear following sectors */
                    dj.fs->winsect = dsc++;
                    dj.fs->wflag = 1;
                    res = move_window(dj.fs, 0);
                    if (res != FR_OK) break;
                    mem_set(dir, 0, SS(dj.fs));
                }
            }
            if (res == FR_OK) res = dir_register(&dj);    /* Register the object to the directoy */
            if (res != FR_OK) {
                remove_chain(dj.fs, dcl);            /* Could not register, remove cluster chain */
            } else {
                dir = dj.dir;
                dir[DIR_Attr] = AM_DIR;                /* Attribute */
                ST_DWORD(dir+DIR_WrtTime, tim);        /* Created time */
                ST_CLUST(dir, dcl);                    /* Table start cluster */
                dj.fs->wflag = 1;
                res = sync(dj.fs);
            }
        }
        FREE_BUF();
    }

    LEAVE_FF(dj.fs, res);
}

函数功能:创建一个目录

描述:

f_mkdir函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_mkdir函数创建一个新目录。

f_unlink:

/*-----------------------------------------------------------------------*/
/* Delete a File or Directory                                            */
/*-----------------------------------------------------------------------*/

FRESULT f_unlink (
    const TCHAR *path        /* Pointer to the file or directory path */
)
{
    FRESULT res;
    DIR dj, sdj;
    BYTE *dir;
    DWORD dclst;
    DEF_NAMEBUF;


    res = chk_mounted(&path, &dj.fs, 1);
    if (res == FR_OK) {
        INIT_BUF(dj);
        res = follow_path(&dj, path);        /* Follow the file path */
        if (_FS_RPATH && res == FR_OK && (dj.fn[NS] & NS_DOT))
            res = FR_INVALID_NAME;            /* Cannot remove dot entry */
#if _FS_SHARE
        if (res == FR_OK) res = chk_lock(&dj, 2);    /* Cannot remove open file */
#endif
        if (res == FR_OK) {                    /* The object is accessible */
            dir = dj.dir;
            if (!dir) {
                res = FR_INVALID_NAME;        /* Cannot remove the start directory */
            } else {
                if (dir[DIR_Attr] & AM_RDO)
                    res = FR_DENIED;        /* Cannot remove R/O object */
            }
            dclst = LD_CLUST(dir);
            if (res == FR_OK && (dir[DIR_Attr] & AM_DIR)) {    /* Is it a sub-dir? */
                if (dclst < 2) {
                    res = FR_INT_ERR;
                } else {
                    mem_cpy(&sdj, &dj, sizeof(DIR));    /* Check if the sub-dir is empty or not */
                    sdj.sclust = dclst;
                    res = dir_sdi(&sdj, 2);        /* Exclude dot entries */
                    if (res == FR_OK) {
                        res = dir_read(&sdj);
                        if (res == FR_OK            /* Not empty dir */
#if _FS_RPATH
                        || dclst == sdj.fs->cdir    /* Current dir */
#endif
                        ) res = FR_DENIED;
                        if (res == FR_NO_FILE) res = FR_OK;    /* Empty */
                    }
                }
            }
            if (res == FR_OK) {
                res = dir_remove(&dj);        /* Remove the directory entry */
                if (res == FR_OK) {
                    if (dclst)                /* Remove the cluster chain if exist */
                        res = remove_chain(dj.fs, dclst);
                    if (res == FR_OK) res = sync(dj.fs);
                }
            }
        }
        FREE_BUF();
    }
    LEAVE_FF(dj.fs, res);
}

函数功能:移除一个对象

描述:

f_unlink函数当_FS_READONLY == 0并且_FS_MINIMIZE == 0时可用。
f_unlink函数移除一个对象。不要移除打开的对象或当前目录。

res = f_mkdir("sub1");                // 在根目录下创建 sub1目录
if (res) die(res);
res = f_mkdir("sub1/sub2");            // 在 sub1目录下创建 sub2目录
if (res) die(res);
res = f_mkdir("sub1/sub2/sub3");
if (res) die(res);
// ...
f_unlink("SD.txt");   //删除跟目录下文件 SD.txt

f_getfree:

/*-----------------------------------------------------------------------*/
/* Get Number of Free Clusters                                           */
/*-----------------------------------------------------------------------*/

FRESULT f_getfree (
    const TCHAR *path,    /* Pointer to the logical drive number (root dir) */
    DWORD *nclst,        /* Pointer to the variable to return number of free clusters */
    FATFS **fatfs        /* Pointer to pointer to corresponding file system object to return */
)
{
    FRESULT res;
    DWORD n, clst, sect, stat;
    UINT i;
    BYTE fat, *p;


    /* Get drive number */
    res = chk_mounted(&path, fatfs, 0);
    if (res == FR_OK) {
        /* If free_clust is valid, return it without full cluster scan */
        if ((*fatfs)->free_clust <= (*fatfs)->n_fatent - 2) {
            *nclst = (*fatfs)->free_clust;
        } else {
            /* Get number of free clusters */
            fat = (*fatfs)->fs_type;
            n = 0;
            if (fat == FS_FAT12) {
                clst = 2;
                do {
                    stat = get_fat(*fatfs, clst);
                    if (stat == 0xFFFFFFFF) { res = FR_DISK_ERR; break; }
                    if (stat == 1) { res = FR_INT_ERR; break; }
                    if (stat == 0) n++;
                } while (++clst < (*fatfs)->n_fatent);
            } else {
                clst = (*fatfs)->n_fatent;
                sect = (*fatfs)->fatbase;
                i = 0; p = 0;
                do {
                    if (!i) {
                        res = move_window(*fatfs, sect++);
                        if (res != FR_OK) break;
                        p = (*fatfs)->win;
                        i = SS(*fatfs);
                    }
                    if (fat == FS_FAT16) {
                        if (LD_WORD(p) == 0) n++;
                        p += 2; i -= 2;
                    } else {
                        if ((LD_DWORD(p) & 0x0FFFFFFF) == 0) n++;
                        p += 4; i -= 4;
                    }
                } while (--clst);
            }
            (*fatfs)->free_clust = n;
            if (fat == FS_FAT32) (*fatfs)->fsi_flag = 1;
            *nclst = n;
        }
    }
    LEAVE_FF(*fatfs, res);
}

函数功能:获取空闲簇的数目

描述:

f_getfree函数当 _FS_READONLY == 0并且 _FS_MINIMIZE == 0时有效。
f_getfree函数获取驱动器上空闲簇的数目。文件系统对象中的成员csize是每簇中的扇区数,因此,以扇区为单位的空闲空间可以被计算出来。当FAT32卷上的FSInfo结构不同步时,该函数返回一个错误的空闲簇计数。

unsigned long total,free;

if(exf_getfree("0",&total,&free))
{
    // 错误代码...
}
//得到磁盘剩余容量
//drv:磁盘编号("0:"/"1:")
//total:总容量     (单位KB)
//free:剩余容量     (单位KB)
//返回值:0,正常.其他,错误代码
u8 exf_getfree(u8 *drv,u32 *total,u32 *free)
{
    FATFS *fs1;
    u8 res;
    DWORD fre_clust=0, fre_sect=0, tot_sect=0;
    //得到磁盘信息及空闲簇数量
    res = f_getfree((const TCHAR*)drv, &fre_clust, &fs1);
    if(res==0)
    {
        tot_sect=(fs1->n_fatent-2)*fs1->csize;    //得到总扇区数
        fre_sect=fre_clust*fs1->csize;            //得到空闲扇区数
#if _MAX_SS!=512                                  //扇区大小不是512字节,则转换为512字节
        tot_sect*=fs1->ssize/512;
        fre_sect*=fs1->ssize/512;
#endif
        *total=tot_sect>>1;    //单位为KB
        *free=fre_sect>>1;    //单位为KB
     }
    return res;
}

f_opendir:

/*-----------------------------------------------------------------------*/
/* Create a Directroy Object                                             */
/*-----------------------------------------------------------------------*/

FRESULT f_opendir (
    DIR *dj,            /* Pointer to directory object to create */
    const TCHAR *path    /* Pointer to the directory path */
)
{
    FRESULT res;
    DEF_NAMEBUF;


    res = chk_mounted(&path, &dj->fs, 0);
    if (res == FR_OK) {
        INIT_BUF(*dj);
        res = follow_path(dj, path);            /* Follow the path to the directory */
        FREE_BUF();
        if (res == FR_OK) {                        /* Follow completed */
            if (dj->dir) {                        /* It is not the root dir */
                if (dj->dir[DIR_Attr] & AM_DIR) {    /* The object is a directory */
                    dj->sclust = LD_CLUST(dj->dir);
                } else {                        /* The object is not a directory */
                    res = FR_NO_PATH;
                }
            }
            if (res == FR_OK) {
                dj->id = dj->fs->id;
                res = dir_sdi(dj, 0);            /* Rewind dir */
            }
        }
        if (res == FR_NO_FILE) res = FR_NO_PATH;
    }

    LEAVE_FF(dj->fs, res);
}

函数功能:打开一个目录

描述:

f_opendir函数当_FS_MINIMIZE <= 1时可用。
f_opendir函数打开一个已存在的目录,并为后续的调用创建一个目录对象。该目录对象结构可以在任何时候不经任何步骤而被丢弃。

f_readdir:

/*-----------------------------------------------------------------------*/
/* Read Directory Entry in Sequense                                      */
/*-----------------------------------------------------------------------*/

FRESULT f_readdir (
    DIR *dj,            /* Pointer to the open directory object */
    FILINFO *fno        /* Pointer to file information to return */
)
{
    FRESULT res;
    DEF_NAMEBUF;


    res = validate(dj->fs, dj->id);            /* Check validity of the object */
    if (res == FR_OK) {
        if (!fno) {
            res = dir_sdi(dj, 0);            /* Rewind the directory object */
        } else {
            INIT_BUF(*dj);
            res = dir_read(dj);                /* Read an directory item */
            if (res == FR_NO_FILE) {        /* Reached end of dir */
                dj->sect = 0;
                res = FR_OK;
            }
            if (res == FR_OK) {                /* A valid entry is found */
                get_fileinfo(dj, fno);        /* Get the object information */
                res = dir_next(dj, 0);        /* Increment index for next */
                if (res == FR_NO_FILE) {
                    dj->sect = 0;
                    res = FR_OK;
                }
            }
            FREE_BUF();
        }
    }

    LEAVE_FF(dj->fs, res);
}

函数功能:读取目录项

描述:

f_readdir函数当 _FS_MINIMIZE <= 1时可用。
f_readdir函数顺序读取目录项。目录中的所有项可以通过重复调用f_readdir函数被读取。当所有目录项已被读取并且没有项要读取时,该函数没有任何错误地返回一个空字符串到f_name[]成员中。当 FileInfo给定一个空指针时,目录对象的读索引将被回绕。
当LFN功能被使能时,在使用f_readdir函数之前,文件信息结构中的lfname和lfsize必须被初始化为有效数值。lfname是一个返回长文件名的字符串缓冲区指针。lfsize是以字符为单位的字符串缓冲区的大小。如果读缓冲区或LFN工作缓冲区的大小(对于LFN)不足,或者对象没有LFN,则一个空字符串将被返回到LFN读缓冲区。

如果LFN包含任何不能被转换为 OEM代码的字符,则一个空字符串将被返回,但是这不是 Unicode API配置的情况。当 lfname是一个空字符串时,没有LFN的任何数据被返回。当对象没有 LFN时,任何小型大写字母可以被包含在SFN中。
当相对路径功能被使能(_FS_RPATH == 1)时,".“和”…"目录项不会被过滤掉,并且它将出现在读目录项中。

DIR dir;
FILINFO f_info;
#define MAXDIR 80

// 以下代码实现读取根目录下所有的子目录名(短文件名)并保存到数组Dirname[MAXDIR][13]中,仅供参考.
if(f_opendir(&dir,"")!=FR_OK)
{
    // 错误处理代码...
}
memset(Dirname[0],0,13*MAXDIR);
maxdir=1;

while(1)
{
    if(f_readdir(&dir,&f_info)==FR_OK)
    {
        if(f_info.fname[0]==0)break;
    }
    else break;

    if(f_info.fattrib & AM_DIR)        // 目录
    {
        if(maxdir<MAXDIR)
        {
            strncpy(Dirname[maxdir],f_info.fname,13);
            maxdir++;
        }
    }
}

f_sync:

/*-----------------------------------------------------------------------*/
/* Synchronize the File Object                                           */
/*-----------------------------------------------------------------------*/

FRESULT f_sync (
    FIL *fp        /* Pointer to the file object */
)
{
    FRESULT res;
    DWORD tim;
    BYTE *dir;


    res = validate(fp->fs, fp->id);        /* Check validity of the object */
    if (res == FR_OK) {
        if (fp->flag & FA__WRITTEN) {    /* Has the file been written? */
#if !_FS_TINY    /* Write-back dirty buffer */
            if (fp->flag & FA__DIRTY) {
                if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
                    LEAVE_FF(fp->fs, FR_DISK_ERR);
                fp->flag &= ~FA__DIRTY;
            }
#endif
            /* Update the directory entry */
            res = move_window(fp->fs, fp->dir_sect);
            if (res == FR_OK) {
                dir = fp->dir_ptr;
                dir[DIR_Attr] |= AM_ARC;                    /* Set archive bit */
                ST_DWORD(dir+DIR_FileSize, fp->fsize);        /* Update file size */
                ST_CLUST(dir, fp->sclust);                    /* Update start cluster */
                tim = get_fattime();                        /* Update updated time */
                ST_DWORD(dir+DIR_WrtTime, tim);
                fp->flag &= ~FA__WRITTEN;
                fp->fs->wflag = 1;
                res = sync(fp->fs);
            }
        }
    }

    LEAVE_FF(fp->fs, res);
}

函数功能:冲洗一个写文件的缓存信息

描述:

f_sync函数当 _FS_READONLY == 0时可用。
f_sync函数和 f_close函数执行同样的过程,但是文件仍处于打开状态,并且可以继续对文件执行读/写/移动
指针操作。这适用于以写模式长时间打开文件,比如数据记录器。定期的或f_write后立即执行f_sync可以将
由于突然断电或移去磁盘而导致数据丢失的风险最小化。在 f_close前立即执行 f_sync没有作用,因为在
f_close中执行了f_sync。换句话说,这两个函数的差异就是文件对象是不是无效的。

f_truncate:

/*-----------------------------------------------------------------------*/
/* Truncate File                                                         */
/*-----------------------------------------------------------------------*/

FRESULT f_truncate (
    FIL *fp        /* Pointer to the file object */
)
{
    FRESULT res;
    DWORD ncl;


    res = validate(fp->fs, fp->id);        /* Check validity of the object */
    if (res == FR_OK) {
        if (fp->flag & FA__ERROR) {            /* Check abort flag */
            res = FR_INT_ERR;
        } else {
            if (!(fp->flag & FA_WRITE))        /* Check access mode */
                res = FR_DENIED;
        }
    }
    if (res == FR_OK) {
        if (fp->fsize > fp->fptr) {
            fp->fsize = fp->fptr;    /* Set file size to current R/W point */
            fp->flag |= FA__WRITTEN;
            if (fp->fptr == 0) {    /* When set file size to zero, remove entire cluster chain */
                res = remove_chain(fp->fs, fp->sclust);
                fp->sclust = 0;
            } else {                /* When truncate a part of the file, remove remaining clusters */
                ncl = get_fat(fp->fs, fp->clust);
                res = FR_OK;
                if (ncl == 0xFFFFFFFF) res = FR_DISK_ERR;
                if (ncl == 1) res = FR_INT_ERR;
                if (res == FR_OK && ncl < fp->fs->n_fatent) {
                    res = put_fat(fp->fs, fp->clust, 0x0FFFFFFF);
                    if (res == FR_OK) res = remove_chain(fp->fs, ncl);
                }
            }
        }
        if (res != FR_OK) fp->flag |= FA__ERROR;
    }

    LEAVE_FF(fp->fs, res);
}

函数功能:截断文件大小

描述:

f_truncate函数当 _FS_READONLY == 0 并且 _FS_MINIMIZE == 0时可用。
f_truncate函数截断文件到当前的文件读/写指针。当文件读/写指针已经指向文件结束时,该函数不起作用。

f_lseek:

/*-----------------------------------------------------------------------*/
/* Seek File R/W Pointer                                                 */
/*-----------------------------------------------------------------------*/

FRESULT f_lseek (
    FIL *fp,        /* Pointer to the file object */
    DWORD ofs        /* File pointer from top of file */
)
{
    FRESULT res;


    res = validate(fp->fs, fp->id);        /* Check validity of the object */
    if (res != FR_OK) LEAVE_FF(fp->fs, res);
    if (fp->flag & FA__ERROR)            /* Check abort flag */
        LEAVE_FF(fp->fs, FR_INT_ERR);

#if _USE_FASTSEEK
    if (fp->cltbl) {    /* Fast seek */
        DWORD cl, pcl, ncl, tcl, dsc, tlen, ulen, *tbl;

        if (ofs == CREATE_LINKMAP) {    /* Create CLMT */
            tbl = fp->cltbl;
            tlen = *tbl++; ulen = 2;    /* Given table size and required table size */
            cl = fp->sclust;            /* Top of the chain */
            if (cl) {
                do {
                    /* Get a fragment */
                    tcl = cl; ncl = 0; ulen += 2;    /* Top, length and used items */
                    do {
                        pcl = cl; ncl++;
                        cl = get_fat(fp->fs, cl);
                        if (cl <= 1) ABORT(fp->fs, FR_INT_ERR);
                        if (cl == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
                    } while (cl == pcl + 1);
                    if (ulen <= tlen) {        /* Store the length and top of the fragment */
                        *tbl++ = ncl; *tbl++ = tcl;
                    }
                } while (cl < fp->fs->n_fatent);    /* Repeat until end of chain */
            }
            *fp->cltbl = ulen;    /* Number of items used */
            if (ulen <= tlen)
                *tbl = 0;        /* Terminate table */
            else
                res = FR_NOT_ENOUGH_CORE;    /* Given table size is smaller than required */

        } else {                        /* Fast seek */
            if (ofs > fp->fsize)        /* Clip offset at the file size */
                ofs = fp->fsize;
            fp->fptr = ofs;                /* Set file pointer */
            if (ofs) {
                fp->clust = clmt_clust(fp, ofs - 1);
                dsc = clust2sect(fp->fs, fp->clust);
                if (!dsc) ABORT(fp->fs, FR_INT_ERR);
                dsc += (ofs - 1) / SS(fp->fs) & (fp->fs->csize - 1);
                if (fp->fptr % SS(fp->fs) && dsc != fp->dsect) {    /* Refill sector cache if needed */
#if !_FS_TINY
#if !_FS_READONLY
                    if (fp->flag & FA__DIRTY) {        /* Write-back dirty sector cache */
                        if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
                            ABORT(fp->fs, FR_DISK_ERR);
                        fp->flag &= ~FA__DIRTY;
                    }
#endif
                    if (disk_read(fp->fs->drv, fp->buf, dsc, 1) != RES_OK)    /* Load current sector */
                        ABORT(fp->fs, FR_DISK_ERR);
#endif
                    fp->dsect = dsc;
                }
            }
        }
    } else
#endif

    /* Normal Seek */
    {
        DWORD clst, bcs, nsect, ifptr;

        if (ofs > fp->fsize                    /* In read-only mode, clip offset with the file size */
#if !_FS_READONLY
             && !(fp->flag & FA_WRITE)
#endif
            ) ofs = fp->fsize;

        ifptr = fp->fptr;
        fp->fptr = nsect = 0;
        if (ofs) {
            bcs = (DWORD)fp->fs->csize * SS(fp->fs);    /* Cluster size (byte) */
            if (ifptr > 0 &&
                (ofs - 1) / bcs >= (ifptr - 1) / bcs) {    /* When seek to same or following cluster, */
                fp->fptr = (ifptr - 1) & ~(bcs - 1);    /* start from the current cluster */
                ofs -= fp->fptr;
                clst = fp->clust;
            } else {                                    /* When seek to back cluster, */
                clst = fp->sclust;                        /* start from the first cluster */
#if !_FS_READONLY
                if (clst == 0) {                        /* If no cluster chain, create a new chain */
                    clst = create_chain(fp->fs, 0);
                    if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
                    if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
                    fp->sclust = clst;
                }
#endif
                fp->clust = clst;
            }
            if (clst != 0) {
                while (ofs > bcs) {                        /* Cluster following loop */
#if !_FS_READONLY
                    if (fp->flag & FA_WRITE) {            /* Check if in write mode or not */
                        clst = create_chain(fp->fs, clst);    /* Force stretch if in write mode */
                        if (clst == 0) {                /* When disk gets full, clip file size */
                            ofs = bcs; break;
                        }
                    } else
#endif
                        clst = get_fat(fp->fs, clst);    /* Follow cluster chain if not in write mode */
                    if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
                    if (clst <= 1 || clst >= fp->fs->n_fatent) ABORT(fp->fs, FR_INT_ERR);
                    fp->clust = clst;
                    fp->fptr += bcs;
                    ofs -= bcs;
                }
                fp->fptr += ofs;
                if (ofs % SS(fp->fs)) {
                    nsect = clust2sect(fp->fs, clst);    /* Current sector */
                    if (!nsect) ABORT(fp->fs, FR_INT_ERR);
                    nsect += ofs / SS(fp->fs);
                }
            }
        }
        if (fp->fptr % SS(fp->fs) && nsect != fp->dsect) {    /* Fill sector cache if needed */
#if !_FS_TINY
#if !_FS_READONLY
            if (fp->flag & FA__DIRTY) {            /* Write-back dirty sector cache */
                if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
                    ABORT(fp->fs, FR_DISK_ERR);
                fp->flag &= ~FA__DIRTY;
            }
#endif
            if (disk_read(fp->fs->drv, fp->buf, nsect, 1) != RES_OK)    /* Fill sector cache */
                ABORT(fp->fs, FR_DISK_ERR);
#endif
            fp->dsect = nsect;
        }
#if !_FS_READONLY
        if (fp->fptr > fp->fsize) {            /* Set file change flag if the file size is extended */
            fp->fsize = fp->fptr;
            fp->flag |= FA__WRITTEN;
        }
#endif
    }

    LEAVE_FF(fp->fs, res);
}

函数功能:移动一个打开的文件对象的文件读/写指针。也可以被用来扩展文件大小(簇预分配)。

描述:

f_lseek函数当FS_MINIMIZE <= 2时可用。
offset只能被指定为相对于文件起始处的字节数。当在写模式下指定了一个超过文件大小的offset时,文件的大小将被扩展,并且该扩展的区域中的数据是未定义的。这适用于为快速写操作迅速地创建一个大的文件。
f_lseek函数成功后,为了确保读/写指针已被正确地移动,必须检查文件对象中的成员fptr。如果fptr不是所期望的值,则发生了下列情况之一。
文件结束。指定的offset被钳在文件大小,因为文件已被以只读模式打开。
磁盘满。卷上没有足够的空闲空间去扩展文件大小。

/* 移动文件读/写指针到相对于文件起始处偏移为5000字节处 */
    res = f_lseek(file, 5000);

    /* 移动文件读/写指针到文件结束处,以便添加数据 */
    res = f_lseek(file, file->fsize);

    /* 向前3000字节 */
    res = f_lseek(file, file->fptr + 3000);

    /* 向后(倒带)2000字节(注意溢出) */
    res = f_lseek(file, file->fptr - 2000);

    /* 簇预分配(为了防止在流写时缓冲区上溢 */
    res = f_open(file, recfile, FA_CREATE_NEW | FA_WRITE); /* 创建一个文件 */

    res = f_lseek(file, PRE_SIZE);         /* 预分配簇 */
    if (res || file->fptr != PRE_SIZE) ... /* 检查文件大小是否已被正确扩展 */

    res = f_lseek(file, DATA_START);       /* 没有簇分配延迟地记录数据流 */
    ...

    res = f_truncate(file);                /* 截断未使用的区域 */
    res = f_lseek(file, 0);                /* 移动到文件起始处 */
    ...

    res = f_close(file);

f_read:

/*-----------------------------------------------------------------------*/
/* Read File                                                             */
/*-----------------------------------------------------------------------*/

FRESULT f_read (
    FIL *fp,         /* Pointer to the file object */
    void *buff,        /* Pointer to data buffer */
    UINT btr,        /* Number of bytes to read */
    UINT *br        /* Pointer to number of bytes read */
)
{
    FRESULT res;
    DWORD clst, sect, remain;
    UINT rcnt, cc;
    BYTE csect, *rbuff = buff;


    *br = 0;    /* Initialize byte counter */

    res = validate(fp->fs, fp->id);                /* Check validity */
    if (res != FR_OK) LEAVE_FF(fp->fs, res);
    if (fp->flag & FA__ERROR)                    /* Aborted file? */
        LEAVE_FF(fp->fs, FR_INT_ERR);
    if (!(fp->flag & FA_READ))                     /* Check access mode */
        LEAVE_FF(fp->fs, FR_DENIED);
    remain = fp->fsize - fp->fptr;
    if (btr > remain) btr = (UINT)remain;        /* Truncate btr by remaining bytes */

    for ( ;  btr;                                /* Repeat until all data read */
        rbuff += rcnt, fp->fptr += rcnt, *br += rcnt, btr -= rcnt) {
        if ((fp->fptr % SS(fp->fs)) == 0) {        /* On the sector boundary? */
            csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));    /* Sector offset in the cluster */
            if (!csect) {                        /* On the cluster boundary? */
                if (fp->fptr == 0) {            /* On the top of the file? */
                    clst = fp->sclust;            /* Follow from the origin */
                } else {                        /* Middle or end of the file */
#if _USE_FASTSEEK
                    if (fp->cltbl)
                        clst = clmt_clust(fp, fp->fptr);    /* Get cluster# from the CLMT */
                    else
#endif
                        clst = get_fat(fp->fs, fp->clust);    /* Follow cluster chain on the FAT */
                }
                if (clst < 2) ABORT(fp->fs, FR_INT_ERR);
                if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
                fp->clust = clst;                /* Update current cluster */
            }
            sect = clust2sect(fp->fs, fp->clust);    /* Get current sector */
            if (!sect) ABORT(fp->fs, FR_INT_ERR);
            sect += csect;
            cc = btr / SS(fp->fs);                /* When remaining bytes >= sector size, */
            if (cc) {                            /* Read maximum contiguous sectors directly */
                if (csect + cc > fp->fs->csize)    /* Clip at cluster boundary */
                    cc = fp->fs->csize - csect;
                if (disk_read(fp->fs->drv, rbuff, sect, (BYTE)cc) != RES_OK)
                    ABORT(fp->fs, FR_DISK_ERR);
#if !_FS_READONLY && _FS_MINIMIZE <= 2            /* Replace one of the read sectors with cached data if it contains a dirty sector */
#if _FS_TINY
                if (fp->fs->wflag && fp->fs->winsect - sect < cc)
                    mem_cpy(rbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), fp->fs->win, SS(fp->fs));
#else
                if ((fp->flag & FA__DIRTY) && fp->dsect - sect < cc)
                    mem_cpy(rbuff + ((fp->dsect - sect) * SS(fp->fs)), fp->buf, SS(fp->fs));
#endif
#endif
                rcnt = SS(fp->fs) * cc;            /* Number of bytes transferred */
                continue;
            }
#if !_FS_TINY
            if (fp->dsect != sect) {            /* Load data sector if not in cache */
#if !_FS_READONLY
                if (fp->flag & FA__DIRTY) {        /* Write-back dirty sector cache */
                    if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
                        ABORT(fp->fs, FR_DISK_ERR);
                    fp->flag &= ~FA__DIRTY;
                }
#endif
                if (disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)    /* Fill sector cache */
                    ABORT(fp->fs, FR_DISK_ERR);
            }
#endif
            fp->dsect = sect;
        }
        rcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs));    /* Get partial sector data from sector buffer */
        if (rcnt > btr) rcnt = btr;
#if _FS_TINY
        if (move_window(fp->fs, fp->dsect))        /* Move sector window */
            ABORT(fp->fs, FR_DISK_ERR);
        mem_cpy(rbuff, &fp->fs->win[fp->fptr % SS(fp->fs)], rcnt);    /* Pick partial sector */
#else
        mem_cpy(rbuff, &fp->buf[fp->fptr % SS(fp->fs)], rcnt);    /* Pick partial sector */
#endif
    }

    LEAVE_FF(fp->fs, FR_OK);
}

函数功能:从已打开的文件中读取数据。

描述:文件对象中的读/写指针以已读取字节数增加。该函数成功后,应该检查 *ByteRead 来检测文件是否结束。在读操作过程中,一旦 *ByteRead < ByteToRead ,则读/写指针到达了文件结束位置。

f_write:

/*-----------------------------------------------------------------------*/
/* Write File                                                            */
/*-----------------------------------------------------------------------*/

FRESULT f_write (
    FIL *fp,            /* Pointer to the file object */
    const void *buff,    /* Pointer to the data to be written */
    UINT btw,            /* Number of bytes to write */
    UINT *bw            /* Pointer to number of bytes written */
)
{
    FRESULT res;
    DWORD clst, sect;
    UINT wcnt, cc;
    const BYTE *wbuff = buff;
    BYTE csect;


    *bw = 0;    /* Initialize byte counter */

    res = validate(fp->fs, fp->id);            /* Check validity */
    if (res != FR_OK) LEAVE_FF(fp->fs, res);
    if (fp->flag & FA__ERROR)                /* Aborted file? */
        LEAVE_FF(fp->fs, FR_INT_ERR);
    if (!(fp->flag & FA_WRITE))                /* Check access mode */
        LEAVE_FF(fp->fs, FR_DENIED);
    if ((DWORD)(fp->fsize + btw) < fp->fsize) btw = 0;    /* File size cannot reach 4GB */

    for ( ;  btw;                            /* Repeat until all data written */
        wbuff += wcnt, fp->fptr += wcnt, *bw += wcnt, btw -= wcnt) {
        if ((fp->fptr % SS(fp->fs)) == 0) {    /* On the sector boundary? */
            csect = (BYTE)(fp->fptr / SS(fp->fs) & (fp->fs->csize - 1));    /* Sector offset in the cluster */
            if (!csect) {                    /* On the cluster boundary? */
                if (fp->fptr == 0) {        /* On the top of the file? */
                    clst = fp->sclust;        /* Follow from the origin */
                    if (clst == 0)            /* When no cluster is allocated, */
                        fp->sclust = clst = create_chain(fp->fs, 0);    /* Create a new cluster chain */
                } else {                    /* Middle or end of the file */
#if _USE_FASTSEEK
                    if (fp->cltbl)
                        clst = clmt_clust(fp, fp->fptr);    /* Get cluster# from the CLMT */
                    else
#endif
                        clst = create_chain(fp->fs, fp->clust);    /* Follow or stretch cluster chain on the FAT */
                }
                if (clst == 0) break;        /* Could not allocate a new cluster (disk full) */
                if (clst == 1) ABORT(fp->fs, FR_INT_ERR);
                if (clst == 0xFFFFFFFF) ABORT(fp->fs, FR_DISK_ERR);
                fp->clust = clst;            /* Update current cluster */
            }
#if _FS_TINY
            if (fp->fs->winsect == fp->dsect && move_window(fp->fs, 0))    /* Write-back sector cache */
                ABORT(fp->fs, FR_DISK_ERR);
#else
            if (fp->flag & FA__DIRTY) {        /* Write-back sector cache */
                if (disk_write(fp->fs->drv, fp->buf, fp->dsect, 1) != RES_OK)
                    ABORT(fp->fs, FR_DISK_ERR);
                fp->flag &= ~FA__DIRTY;
            }
#endif
            sect = clust2sect(fp->fs, fp->clust);    /* Get current sector */
            if (!sect) ABORT(fp->fs, FR_INT_ERR);
            sect += csect;
            cc = btw / SS(fp->fs);            /* When remaining bytes >= sector size, */
            if (cc) {                        /* Write maximum contiguous sectors directly */
                if (csect + cc > fp->fs->csize)    /* Clip at cluster boundary */
                    cc = fp->fs->csize - csect;
                if (disk_write(fp->fs->drv, wbuff, sect, (BYTE)cc) != RES_OK)
                    ABORT(fp->fs, FR_DISK_ERR);
#if _FS_TINY
                if (fp->fs->winsect - sect < cc) {    /* Refill sector cache if it gets invalidated by the direct write */
                    mem_cpy(fp->fs->win, wbuff + ((fp->fs->winsect - sect) * SS(fp->fs)), SS(fp->fs));
                    fp->fs->wflag = 0;
                }
#else
                if (fp->dsect - sect < cc) { /* Refill sector cache if it gets invalidated by the direct write */
                    mem_cpy(fp->buf, wbuff + ((fp->dsect - sect) * SS(fp->fs)), SS(fp->fs));
                    fp->flag &= ~FA__DIRTY;
                }
#endif
                wcnt = SS(fp->fs) * cc;        /* Number of bytes transferred */
                continue;
            }
#if _FS_TINY
            if (fp->fptr >= fp->fsize) {    /* Avoid silly cache filling at growing edge */
                if (move_window(fp->fs, 0)) ABORT(fp->fs, FR_DISK_ERR);
                fp->fs->winsect = sect;
            }
#else
            if (fp->dsect != sect) {        /* Fill sector cache with file data */
                if (fp->fptr < fp->fsize &&
                    disk_read(fp->fs->drv, fp->buf, sect, 1) != RES_OK)
                        ABORT(fp->fs, FR_DISK_ERR);
            }
#endif
            fp->dsect = sect;
        }
        wcnt = SS(fp->fs) - (fp->fptr % SS(fp->fs));/* Put partial sector into file I/O buffer */
        if (wcnt > btw) wcnt = btw;
#if _FS_TINY
        if (move_window(fp->fs, fp->dsect))    /* Move sector window */
            ABORT(fp->fs, FR_DISK_ERR);
        mem_cpy(&fp->fs->win[fp->fptr % SS(fp->fs)], wbuff, wcnt);    /* Fit partial sector */
        fp->fs->wflag = 1;
#else
        mem_cpy(&fp->buf[fp->fptr % SS(fp->fs)], wbuff, wcnt);    /* Fit partial sector */
        fp->flag |= FA__DIRTY;
#endif
    }

    if (fp->fptr > fp->fsize) fp->fsize = fp->fptr;    /* Update file size if needed */
    fp->flag |= FA__WRITTEN;                        /* Set file change flag */

    LEAVE_FF(fp->fs, FR_OK);
}

函数功能:向已打开的问价中写入数据。

描述:文件对象中的读/写指针以已写入字节数增加。该函数成功后,应该检查 *ByteWritten 来检测磁盘是否满。在写操作过程中,一旦 *ByteWritten < *ByteToWritten ,则意味着该卷已满。

这两个函数在调用过程中会将文件读写指针 fp->fptr的值累加,使得下次再次对该文件操作时从上次操作的断点处继续向下操作。

void main (void)
{
    FATFS fs[2];         /* 逻辑驱动器的工作区(文件系统对象) */
    FIL fsrc, fdst;      /* 文件对象 */
    BYTE buffer[4096];   /* 文件拷贝缓冲区 */
    FRESULT res;         /* FatFs 函数公共结果代码 */
    UINT br, bw;         /* 文件读/写字节计数 */

    /* 为逻辑驱动器注册工作区 */
    f_mount(0, &fs[0]);
    f_mount(1, &fs[1]);

    /* 打开驱动器 1 上的源文件 */
    res = f_open(&fsrc, "1:srcfile.dat", FA_OPEN_EXISTING | FA_READ);
    if (res) die(res);

    /* 在驱动器 0 上创建目标文件 */
    res = f_open(&fdst, "0:dstfile.dat", FA_CREATE_ALWAYS | FA_WRITE);
    if (res) die(res);

    /* 拷贝源文件到目标文件 */
    for (;;) {
        res = f_read(&fsrc, buffer, sizeof(buffer), &br);
        if (res || br == 0) break;   /* 文件结束错误 */
        res = f_write(&fdst, buffer, br, &bw);
        if (res || bw < br) break;   /* 磁盘满错误 */
    }

    /* 关闭打开的文件 */
    f_close(&fsrc);
    f_close(&fdst);

    /* 注销工作区(在废弃前) */
    f_mount(0, NULL);
    f_mount(1, NULL);
}

f_open:

/*-----------------------------------------------------------------------*/
/* Open or Create a File                                                 */
/*-----------------------------------------------------------------------*/

FRESULT f_open (
    FIL *fp,            /* Pointer to the blank file object */
    const TCHAR *path,    /* Pointer to the file name */
    BYTE mode            /* Access mode and file open mode flags */
)
{
    FRESULT res;
    DIR dj;
    BYTE *dir;
    DEF_NAMEBUF;


    fp->fs = 0;            /* Clear file object */

#if !_FS_READONLY
    mode &= FA_READ | FA_WRITE | FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW;
    res = chk_mounted(&path, &dj.fs, (BYTE)(mode & ~FA_READ));
#else
    mode &= FA_READ;
    res = chk_mounted(&path, &dj.fs, 0);
#endif
    INIT_BUF(dj);
    if (res == FR_OK)
        res = follow_path(&dj, path);    /* Follow the file path */
    dir = dj.dir;

#if !_FS_READONLY    /* R/W configuration */
    if (res == FR_OK) {
        if (!dir)    /* Current dir itself */
            res = FR_INVALID_NAME;
#if _FS_SHARE
        else
            res = chk_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
#endif
    }
    /* Create or Open a file */
    if (mode & (FA_CREATE_ALWAYS | FA_OPEN_ALWAYS | FA_CREATE_NEW)) {
        DWORD dw, cl;

        if (res != FR_OK) {                    /* No file, create new */
            if (res == FR_NO_FILE)            /* There is no file to open, create a new entry */
#if _FS_SHARE
                res = enq_lock() ? dir_register(&dj) : FR_TOO_MANY_OPEN_FILES;
#else
                res = dir_register(&dj);
#endif
            mode |= FA_CREATE_ALWAYS;        /* File is created */
            dir = dj.dir;                    /* New entry */
        }
        else {                                /* Any object is already existing */
            if (dir[DIR_Attr] & (AM_RDO | AM_DIR)) {    /* Cannot overwrite it (R/O or DIR) */
                res = FR_DENIED;
            } else {
                if (mode & FA_CREATE_NEW)    /* Cannot create as new file */
                    res = FR_EXIST;
            }
        }
        if (res == FR_OK && (mode & FA_CREATE_ALWAYS)) {    /* Truncate it if overwrite mode */
            dw = get_fattime();                    /* Created time */
            ST_DWORD(dir+DIR_CrtTime, dw);
            dir[DIR_Attr] = 0;                    /* Reset attribute */
            ST_DWORD(dir+DIR_FileSize, 0);        /* size = 0 */
            cl = LD_CLUST(dir);                    /* Get start cluster */
            ST_CLUST(dir, 0);                    /* cluster = 0 */
            dj.fs->wflag = 1;
            if (cl) {                            /* Remove the cluster chain if exist */
                dw = dj.fs->winsect;
                res = remove_chain(dj.fs, cl);
                if (res == FR_OK) {
                    dj.fs->last_clust = cl - 1;    /* Reuse the cluster hole */
                    res = move_window(dj.fs, dw);
                }
            }
        }
    }
    else {    /* Open an existing file */
        if (res == FR_OK) {                        /* Follow succeeded */
            if (dir[DIR_Attr] & AM_DIR) {        /* It is a directory */
                res = FR_NO_FILE;
            } else {
                if ((mode & FA_WRITE) && (dir[DIR_Attr] & AM_RDO)) /* R/O violation */
                    res = FR_DENIED;
            }
        }
    }
    if (res == FR_OK) {
        if (mode & FA_CREATE_ALWAYS)            /* Set file change flag if created or overwritten */
            mode |= FA__WRITTEN;
        fp->dir_sect = dj.fs->winsect;            /* Pointer to the directory entry */
        fp->dir_ptr = dir;
#if _FS_SHARE
        fp->lockid = inc_lock(&dj, (mode & ~FA_READ) ? 1 : 0);
        if (!fp->lockid) res = FR_INT_ERR;
#endif
    }

#else                /* R/O configuration */
    if (res == FR_OK) {                    /* Follow succeeded */
        if (!dir) {                        /* Current dir itself */
            res = FR_INVALID_NAME;
        } else {
            if (dir[DIR_Attr] & AM_DIR)    /* It is a directory */
                res = FR_NO_FILE;
        }
    }
#endif
    FREE_BUF();

    if (res == FR_OK) {
        fp->flag = mode;                    /* File access mode */
        fp->sclust = LD_CLUST(dir);            /* File start cluster */
        fp->fsize = LD_DWORD(dir+DIR_FileSize);    /* File size */
        fp->fptr = 0;                        /* File pointer */
        fp->dsect = 0;
#if _USE_FASTSEEK
        fp->cltbl = 0;                        /* Normal seek mode */
#endif
        fp->fs = dj.fs; fp->id = dj.fs->id;    /* Validate file object */
    }

    LEAVE_FF(dj.fs, res);
}

函数功能:创建/打开一个用于访问文件的文件对象

描述:如果函数成功,则创建一个文件对象。该文件对象被后续的读/写函数用来访问文件。如果想要关闭一个打开的文件对象,则使用f_close函数。如果不关闭修改后的文件,那么文件可能会崩溃。

在使用任何文件函数之前,必须使用 f_mount函数为驱动器注册一个工作区。只有这样,其他文件函数才能正常工作。

将打开的文件名、文件目录及读写属性等装入 fp,为后续操作做准备。

f_close:

/*-----------------------------------------------------------------------*/
/* Close File                                                            */
/*-----------------------------------------------------------------------*/

FRESULT f_close (
    FIL *fp        /* Pointer to the file object to be closed */
)
{
    FRESULT res;

#if _FS_READONLY
    FATFS *fs = fp->fs;
    res = validate(fs, fp->id);
    if (res == FR_OK) fp->fs = 0;    /* Discard file object */
    LEAVE_FF(fs, res);

#else
    res = f_sync(fp);        /* Flush cached data */
#if _FS_SHARE
    if (res == FR_OK) {        /* Decrement open counter */
#if _FS_REENTRANT
        res = validate(fp->fs, fp->id);
        if (res == FR_OK) {
            res = dec_lock(fp->lockid);
            unlock_fs(fp->fs, FR_OK);
        }
#else
        res = dec_lock(fp->lockid);
#endif
    }
#endif
    if (res == FR_OK) fp->fs = 0;    /* Discard file object */
    return res;
#endif
}

函数功能:关闭一个已打开的文件

描述:f_close函数关闭一个打开的文件对象。无论向文件写入任何数据,文件的缓存信息都将被写回到磁盘。该函数成功后,文件对象不再有效,并且可以被丢弃。如果文件对象是在只读模式下打开的,不需要使用该函数,也能被丢弃。

FATFS fs;        /* 逻辑驱动器的工作区(文件系统对象) */
FIL fsrc;        /* 文件对象 */
FRESULT res;    /* FatFs 函数公共结果代码 */

void main(void)
{
    // 设备初始化...

    f_mount(0,&fs);
    res = f_open(&fdst,"0:/Demo.TXT", FA_WRITE | FA_READ);
    if(FR_OK == res)
    {
        // ...
        f_close(&fdst);
    }
    else
    {
        // ...
    }

    f_mount(0, NULL);
}

f_mount:

/*-----------------------------------------------------------------------*/
/* Mount/Unmount a Logical Drive                                         */
/*-----------------------------------------------------------------------*/

FRESULT f_mount (
    BYTE vol,        /* Logical drive number to be mounted/unmounted */
    FATFS *fs        /* Pointer to new file system object (NULL for unmount)*/
)
{
    FATFS *rfs;


    if (vol >= _VOLUMES)        /* Check if the drive number is valid */
        return FR_INVALID_DRIVE;
    rfs = FatFs[vol];            /* Get current fs object */

    if (rfs) {
#if _FS_SHARE
        clear_lock(rfs);
#endif
#if _FS_REENTRANT                /* Discard sync object of the current volume */
        if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
#endif
        rfs->fs_type = 0;        /* Clear old fs object */
    }

    if (fs) {
        fs->fs_type = 0;        /* Clear new fs object */
#if _FS_REENTRANT                /* Create sync object for the new volume */
        if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
#endif
    }
    FatFs[vol] = fs;            /* Register new fs object */

    return FR_OK;
}
FATFS *FatFs[_VOLUMES]; /* Pointer to the file system objects (logical drives) */

函数功能:注册/注销一个工作区(挂载/注销分区文件系统)

描述:在使用任何其它文件函数之前,必须使用该函数为每个使用卷注册一个工作区。要注销一个工作区,只要指定 fs为 NULL即可,然后该工作区可以被丢弃。

该函数只初始化给定的工作区,以及将该工作区的地址注册到内部表中,不访问磁盘I/O层。卷装入过程是在 f_mount函数后或存储介质改变后的第一次文件访问时完成的。

通俗了说就是给磁盘分配文件系统的:计算机中的盘符是 C: D: E;FATFS的盘符是 0: 1: 2:

f_mount(0,&fs);  // 为 0号盘符分配新的文件系统 fs,fs是 FATFS类型,用于记录逻辑磁盘工作区
/*-----------------------------------------------------------------------*/
/* Mount/Unmount a Logical Drive                                         */
/*-----------------------------------------------------------------------*/
FRESULT f_mount (
    BYTE vol,        /* 逻辑驱动器安装/卸载 */
    FATFS *fs        /* 新的文件系统对象指针(卸载NULL) */
)
{
    FATFS *rfs;

    if (vol >= _VOLUMES)        /* 检查驱动器号是否有效 */
        return FR_INVALID_DRIVE;
    rfs = FatFs[vol];            /* 获得原盘符下对象 */

    if (rfs) {                    /* 检查原盘符下是否挂有文件系统,若有则释放掉 */
#if _FS_SHARE
        clear_lock(rfs);
#endif
#if _FS_REENTRANT                /* Discard sync object of the current volume */
        if (!ff_del_syncobj(rfs->sobj)) return FR_INT_ERR;
#endif
        rfs->fs_type = 0;        /* Clear old fs object */
    }

    if (fs) {                    /* 检测参数,若为 NULL则表示卸载盘符,下面两步执行无意义 */
        fs->fs_type = 0;        /* 0: 未装载 */
#if _FS_REENTRANT                /* Create sync object for the new volume */
        if (!ff_cre_syncobj(vol, &fs->sobj)) return FR_INT_ERR;
#endif
    }
    FatFs[vol] = fs;            /* 注册新的对象 */

    return FR_OK;
}

来源:https://www.cnblogs.com/Danhuise/

### FATFS 文件系统函数详解 #### 1. 初始化文件系统 `f_mount()` 函数用于挂载或卸载逻辑驱动器。此功能初始化指定卷的工作区并准备访问该卷。 ```c FRESULT f_mount ( FATFS* fs, /* [IN] File system object */ const TCHAR* path,/* [IN] Logical drive number to be mounted/unmounted */ BYTE opt /* [IN] Mount option (0: Unmount, 1: Force mount) */ ); ``` 通过 `fs` 参数传递指向 `FATFS` 对象的指针,而 `path` 则表示要挂载的逻辑驱动器号[^1]。 #### 2. 创建和打开文件 `f_open()` 函数用来创建新文件或将现有文件打开以便后续处理: ```c FRESULT f_open ( FIL* fp, /* [OUT] Pointer to the blank file object */ const TCHAR* path,/* [IN] Pointer to the file name */ BYTE mode /* [IN] Access mode and open mode flags */ ); ``` 其中 `mode` 可以为只读、写入等多种模式组合。 #### 3. 关闭文件 当完成对某个特定文件的操作之后,应该调用 `f_close()` 来关闭它: ```c FRESULT f_close ( FIL* fp /* [IN] Pointer to the open file object */ ); ``` 这会释放与之关联的所有资源,并确保数据被正确保存到存储介质上。 #### 4. 读取文件内容 为了从已打开的文件中读取数据,可以使用 `f_read()` 方法: ```c FRESULT f_read ( FIL* fp, /* [IN] Pointer to the file object */ void* buff, /* [OUT] Data buffer to store read data */ UINT btr, /* [IN] Number of bytes to read */ UINT* br /* [OUT] Number of bytes actually read */ ); ``` 这里 `buff` 是目标缓冲区的位置;`btr` 表示期望读取的数据量大小;实际成功传输了多少字节则由参数 `br` 返回给调用者。 #### 5. 写入文件内容 向已经打开的文件内追加新的二进制流,则需借助于 `f_write()` 接口实现: ```c FRESULT f_write ( FIL* fp, /* [IN] Pointer to the file object */ const void* buff, /* [IN] Data to be written */ UINT btw, /* [IN] Number of bytes to write */ UINT* bw /* [OUT] Number of bytes actually written */ ); ``` 同样地,在执行完毕后可通过检查变量 `bw` 的值确认最终写入了多少有效负载。 #### 6. 获取文件状态信息 如果想要获取当前所选文件的相关属性(比如长度),那么可采用如下方式查询其元数据: ```c FRESULT f_stat ( const TCHAR* path, /* [IN] Object name */ FILINFO* fno /* [OUT] Buffer to store found directory item's information */ ); ``` 上述代码片段中的 `FILINFO` 结构体包含了诸如创建时间戳在内的多种字段描述符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值