b_mod_fs

数据结构

typedef struct
{
    uint8_t  index;
    uint32_t dev_no;
    uint32_t base_addr;
    uint32_t total_size;
    uint16_t sector_size;
} bFSPartition_t;

typedef struct
{
#if defined(FS_FATFS)
    FIL bfile;
#endif
#if defined(FS_LITTLEFS)
    struct lfs_file_config cfg;
    lfs_file_t             bfile;
    uint8_t                buf[LFS_CACHE_SIZE];
#endif
    void *reserved;
} bFSFile_t;

#define BFS_O_RD (0x1)
#define BFS_O_WR (0x2)
#define BFS_O_RDWR (0x4)
#define BFS_O_CREAT (0x8)
#define BFS_O_EXCL (0x10)
#define BFS_O_TRUNC (0x20)
#define BFS_O_APPEND (0x40)

#define BFS_SEEK_SET (0x1)
#define BFS_SEEK_CUR (0x2)
#define BFS_SEEK_END (0x3)

#define BFS_FD_IS_VALID(f) ((f) > 0)

接口介绍

int bFSInit(const bFSPartition_t *partition, uint8_t partition_number);

int bFSMount(uint8_t index, uint8_t mkfs);
int bFSUnmount(uint8_t index);
int bFSMkfs(uint8_t index);
int bFSGetInfo(uint8_t index, uint32_t *ptotal_size, uint32_t *pfree_size);

int bFSOpen(bFSFile_t *fil, const char *path, int flag);
int bFSWrite(int fd, uint8_t *pbuf, uint32_t len);
int bFSRead(int fd, uint8_t *pbuf, uint32_t len);
int bFSClose(int fd);
int bFSLseek(int fd, int32_t offset, int whence);
int bFSFileGetInfo(int fd, uint32_t *pfile_size);

使用例子

// bos/test/fs
/**
 * \file kv_main.c
 * \brief
 * \version 0.1
 * \date 2022-10-29
 * \author notrynohigh (notrynohigh@outlook.com)
 *
 * Copyright (c) 2020 by notrynohigh. All Rights Reserved.
 */
#include "../port.h"
#include "b_config.h"
#include "b_os.h"

const bFSPartition_t bPartitionTable[] = {{0, bTESTFLASH, 0, 2 * 1024 * 1024, 4096},
                                          {1, bTESTFLASH, 2 * 1024 * 1024, 2 * 1024 * 1024, 4096}};

void bTestFs()
{
    static uint8_t   f = 0;
    static bFSFile_t file;
    int              fd = -1;
    uint8_t          tmp[128];
    uint32_t         tsize = 0, fsize = 0;
    if (f == 0)
    {
        fd = bFSOpen(&file, "0:hello", BFS_O_RDWR | BFS_O_CREAT | BFS_O_APPEND);
        if (BFS_FD_IS_VALID(fd))
        {
            bFSFileGetInfo(fd, &fsize);
            b_log("file-0-size:%d(B)\r\n", fsize);
            bFSWrite(fd, "baby", strlen("baby"));
            bFSClose(fd);
            fd = -1;
        }

        fd = bFSOpen(&file, "1:hello", BFS_O_RDWR | BFS_O_CREAT | BFS_O_APPEND);
        if (BFS_FD_IS_VALID(fd))
        {
            bFSFileGetInfo(fd, &fsize);
            b_log("file-1-size:%d(B)\r\n", fsize);
            bFSWrite(fd, "os", strlen("os"));
            bFSClose(fd);
            fd = -1;
        }
        f = 1;
    }
    else
    {
        bFSGetInfo(0, &tsize, &fsize);
        b_log("disk[0]-total:%d(KB) free:%d(KB)\r\n", tsize / 1024, fsize / 1024);
        bFSGetInfo(1, &tsize, &fsize);
        b_log("disk[1]-total:%d(KB) free:%d(KB)\r\n", tsize / 1024, fsize / 1024);

        memset(tmp, 0, sizeof(tmp));

        fd = bFSOpen(&file, "0:hello", BFS_O_RD);
        if (BFS_FD_IS_VALID(fd))
        {
            bFSLseek(fd, 0 - strlen("baby"), BFS_SEEK_END);
            bFSRead(fd, tmp, sizeof(tmp));
            bFSClose(fd);
            fd = -1;
        }

        fd = bFSOpen(&file, "1:hello", BFS_O_RD);
        if (BFS_FD_IS_VALID(fd))
        {
            bFSLseek(fd, 0 - strlen("os"), BFS_SEEK_END);
            bFSRead(fd, &tmp[strlen(tmp)], strlen("os"));
            bFSClose(fd);
            fd = -1;
        }

        b_log("%s\r\n", tmp);

        f = 0;
    }
}

int main()
{
    port_init();
    bInit();

    bFSInit(&bPartitionTable[0], 2);

    bFSMount(0, 1);  // mount partition 0
    bFSMount(1, 1);  // mount partition 1

    b_log("mount ok...\r\n");

    while (1)
    {
        bExec();
        BOS_PERIODIC_TASK(bTestFs, 1000);
    }
    return 0;
}