Mbed OS 文档翻译 之 参考(API(存储(FATFileSystem)))

FATFileSystem

               

                                                                            FATFileSystem 类层次结构

FAT 文件系统是一个已建立的面向磁盘的文件系统,您可以在 Mbed OS,Windows,Linux 和 Mac OS X 上找到它。由于其年龄和普及性,FAT 文件系统已成为便携式存储的标准,如闪存设备和 SD 卡。

  • 便携 - 由于其对操作系统的几乎全面支持,FAT 文件系统可以从嵌入式系统和 PC 访问存储。

  • 嵌入式 - FAT 文件系统建立在 ChanFS 项目之上,针对嵌入式系统进行了优化。

有关其他信息,请参阅存储概述页面

用例

使用 FAT 文件系统的主要原因是它对便携式存储有用。因此,大多数应用程序将 FAT 与 SD 卡结合使用。

使用 FAT 文件系统的第一步是使用 FAT 格式化存储。您可以在具有本机格式命令的 PC 上或在具有格式功能的 Mbed OS 上执行此操作。

       注意: FAT 文件系统至少需要 256 个擦除块。您可以通过将块设备的大小除以其擦除大小来查找块设备上的块数。

FAT 文件系统支持外部闪存;但是,它必须为内部操作分配一个完整的擦除块,对于某些形式的闪存可能会变大。如果 RAM 消耗成为问题,我们建议切换到 LittleFileSystem。Mbed OS 文件系统 API 使切换文件系统成为一项简单的任务。一种常见的策略是使用 FAT 文件系统进行调试,并在应用程序稳定时切换到 LittleFileSystem。

用法

使用块设备和文件路径实例化 FATFileSystem 类。

它提供的 API 是标准的 Mbed OS 文件系统 API。声明后,Mbed OS 为标准 C 库提供重定向层。

您可以将 FAT 文件系统与其他 Mbed OS 文件系统交换,这是原型应用程序的好方法。

FATFileSystem 类参考

FATFileSystem 类参考

公共成员函数
 FATFileSystem (const char *name=NULL, BlockDevice *bd=NULL)
virtual int mount (BlockDevice *bd)
virtual int unmount ()
virtual int reformat (BlockDevice *bd, int allocation_unit)
virtual int reformat (BlockDevice *bd=NULL)
virtual int remove (const char *path)
virtual int rename (const char *path, const char *newpath)
virtual int stat (const char *path, struct stat *st)
virtual int mkdir (const char *path, mode_t mode)
virtual int statvfs (const char *path, struct statvfs *buf)
 公共成员函数继承自 mbed::FileSystem
 FileSystem (const char *name=NULL)
 公共成员函数继承自 mbed::FileSystemLike
 FileSystemLike (const char *name=NULL)
FileHandleopen (const char *path, int flags)
DirHandleopendir (const char *path)
 公共成员函数继承自 mbed::FileSystemHandle
virtual ~FileSystemHandle ()
 公共成员函数继承自 mbed::FileBase
 FileBase (const char *name, PathType t)
const char * getName (void)
PathType getPathType (void)
静态公共成员函数
static int format (BlockDevice *bd, bd_size_t cluster_size=0)
 静态公共成员函数继承自 mbed::FileBase
static FileBaselookup (const char *name, unsigned int len)
static FileBaseget (int n)
受保护的成员函数
virtual int file_open (mbed::fs_file_t *file, const char *path, int flags)
virtual int file_close (mbed::fs_file_t file)
virtual ssize_t file_read (mbed::fs_file_t file, void *buffer, size_t len)
virtual ssize_t file_write (mbed::fs_file_t file, const void *buffer, size_t len)
virtual int file_sync (mbed::fs_file_t file)
virtual off_t file_seek (mbed::fs_file_t file, off_t offset, int whence)
virtual off_t file_tell (mbed::fs_file_t file)
virtual off_t file_size (mbed::fs_file_t file)
virtual int dir_open (mbed::fs_dir_t *dir, const char *path)
virtual int dir_close (mbed::fs_dir_t dir)
virtual ssize_t dir_read (mbed::fs_dir_t dir, struct dirent *ent)
virtual void dir_seek (mbed::fs_dir_t dir, off_t offset)
virtual off_t dir_tell (mbed::fs_dir_t dir)
virtual void dir_rewind (mbed::fs_dir_t dir)
virtual void lock ()
virtual void unlock ()
virtual int mount (BlockDevice *bd, bool mount)
 受保护的成员函数继承自 mbed::FileSystem
virtual int file_isatty (fs_file_t file)
virtual void file_rewind (fs_file_t file)
virtual size_t dir_size (fs_dir_t dir)
virtual int open (FileHandle **file, const char *path, int flags)
virtual int open (DirHandle **dir, const char *path)

FATFileSystem 示例

main.cpp                                                                                                                                               导入到 Mbed IDE

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "mbed.h"
#include <stdio.h>
#include <errno.h>

// Block devices
#include "SPIFBlockDevice.h"
#include "DataFlashBlockDevice.h"
#include "SDBlockDevice.h"
#include "HeapBlockDevice.h"

// File systems
#include "LittleFileSystem.h"
#include "FATFileSystem.h"


// Physical block device, can be any device that supports the BlockDevice API
SPIFBlockDevice bd(
        MBED_CONF_SPIF_DRIVER_SPI_MOSI,
        MBED_CONF_SPIF_DRIVER_SPI_MISO,
        MBED_CONF_SPIF_DRIVER_SPI_CLK,
        MBED_CONF_SPIF_DRIVER_SPI_CS);

// File system declaration
LittleFileSystem fs("fs");


// Set up the button to trigger an erase
InterruptIn irq(BUTTON1);
void erase() {
    printf("Initializing the block device... ");
    fflush(stdout);
    int err = bd.init();
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }

    printf("Erasing the block device... ");
    fflush(stdout);
    err = bd.erase(0, bd.size());
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }

    printf("Deinitializing the block device... ");
    fflush(stdout);
    err = bd.deinit();
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
}


// Entry point for the example
int main() {
    printf("--- Mbed OS filesystem example ---\n");

    // Setup the erase event on button press, use the event queue
    // to avoid running in interrupt context
    irq.fall(mbed_event_queue()->event(erase));

    // Try to mount the filesystem
    printf("Mounting the filesystem... ");
    fflush(stdout);
    int err = fs.mount(&bd);
    printf("%s\n", (err ? "Fail :(" : "OK"));
    if (err) {
        // Reformat if we can't mount the filesystem
        // this should only happen on the first boot
        printf("No filesystem found, formatting... ");
        fflush(stdout);
        err = fs.reformat(&bd);
        printf("%s\n", (err ? "Fail :(" : "OK"));
        if (err) {
            error("error: %s (%d)\n", strerror(-err), err);
        }
    }

    // Open the numbers file
    printf("Opening \"/fs/numbers.txt\"... ");
    fflush(stdout);
    FILE *f = fopen("/fs/numbers.txt", "r+");
    printf("%s\n", (!f ? "Fail :(" : "OK"));
    if (!f) {
        // Create the numbers file if it doesn't exist
        printf("No file found, creating a new file... ");
        fflush(stdout);
        f = fopen("/fs/numbers.txt", "w+");
        printf("%s\n", (!f ? "Fail :(" : "OK"));
        if (!f) {
            error("error: %s (%d)\n", strerror(errno), -errno);
        }

        for (int i = 0; i < 10; i++) {
            printf("\rWriting numbers (%d/%d)... ", i, 10);
            fflush(stdout);
            err = fprintf(f, "    %d\n", i);
            if (err < 0) {
                printf("Fail :(\n");
                error("error: %s (%d)\n", strerror(errno), -errno);
            }
        }
        printf("\rWriting numbers (%d/%d)... OK\n", 10, 10);

        printf("Seeking file... ");
        fflush(stdout);
        err = fseek(f, 0, SEEK_SET);
        printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
        if (err < 0) {
            error("error: %s (%d)\n", strerror(errno), -errno);
        }
    }

    // Go through and increment the numbers
    for (int i = 0; i < 10; i++) {
        printf("\rIncrementing numbers (%d/%d)... ", i, 10);
        fflush(stdout);

        // Get current stream position
        long pos = ftell(f);

        // Parse out the number and increment
        int32_t number;
        fscanf(f, "%d", &number);
        number += 1;

        // Seek to beginning of number
        fseek(f, pos, SEEK_SET);
    
        // Store number
        fprintf(f, "    %d\n", number);

        // Flush between write and read on same file
        fflush(f);
    }
    printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10);

    // Close the file which also flushes any cached writes
    printf("Closing \"/fs/numbers.txt\"... ");
    fflush(stdout);
    err = fclose(f);
    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }
    
    // Display the root directory
    printf("Opening the root directory... ");
    fflush(stdout);
    DIR *d = opendir("/fs/");
    printf("%s\n", (!d ? "Fail :(" : "OK"));
    if (!d) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    printf("root directory:\n");
    while (true) {
        struct dirent *e = readdir(d);
        if (!e) {
            break;
        }

        printf("    %s\n", e->d_name);
    }

    printf("Closing the root directory... ");
    fflush(stdout);
    err = closedir(d);
    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    // Display the numbers file
    printf("Opening \"/fs/numbers.txt\"... ");
    fflush(stdout);
    f = fopen("/fs/numbers.txt", "r");
    printf("%s\n", (!f ? "Fail :(" : "OK"));
    if (!f) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    printf("numbers:\n");
    while (!feof(f)) {
        int c = fgetc(f);
        printf("%c", c);
    }

    printf("\rClosing \"/fs/numbers.txt\"... ");
    fflush(stdout);
    err = fclose(f);
    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(errno), -errno);
    }

    // Tidy up
    printf("Unmounting... ");
    fflush(stdout);
    err = fs.unmount();
    printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
    if (err < 0) {
        error("error: %s (%d)\n", strerror(-err), err);
    }
        
    printf("Mbed OS filesystem example done!\n");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值