基于RTT系统的littlefs文件系统移植说明(STM32片内FLASH)

参考文件

  1. 参考RTT官方littlefs移植文档;
  2. 使用前,前先移植好fal,详见《stm32f103rc片内Flash使用FAL库配置说明》。
    https://blog.csdn.net/tigerots/article/details/104158917

配置说明

一.使能DFS框架

  • 在 BSP 工作目录下打开 env,输入menuconfig,在如下路径下 中打开 DFS 框架,配置内容如下入所示。

    RT-Thread Components 
    				→ Device virtual file system 
    

在这里插入图片描述

二.配置 littlefs

  • 在如下路径中中打开 littlefs,此处应该注意,最新版本中lfs enables wear leveling. 0 is disable,值必须不能为0,磨损均衡值应设置为100-1000之间(TBD)。详细配置如下图所示:
RT-Thread online packages 
	→ system packages 
		→ Littlefs: A high-integrity embedded file system 

在这里插入图片描述

三.使能 MTD 设备

  • 在如下路径中中使能 MTD 设备,详细配置如下入所示:
 RT-Thread Components 
	 	→ Device Drivers 

在这里插入图片描述

四. 配置 fal

1.使用片内Flash

2.使用片外Flash

  • 后续完善!

五. 编译测试

#include <rtdevice.h>


/* 添加 fal 头文件 */
#include <fal.h>
/* 添加文件系统头文件 */
#include <dfs_fs.h>
/* 当需要使用文件操作时,需要包含这个头文件 */
#include <dfs_posix.h> 

/* 添加 DEBUG 头文件 */
#define DBG_SECTION_NAME               "littlefs.sample"
#define DBG_LEVEL                      DBG_INFO
#include <rtdbg.h>



/* 定义要使用的分区名字 */
#define FS_PARTITION_NAME              "para" 

// 测试要写入的数据,该文件包含一个字符数组 index_html,根据自己需要修改
#include "index.h" 

char buffer[1024];
static void readwrite_sample(void)
{
    int fd, size;
    DIR *dirp;
    struct dirent *d;
    
    char s[] = "write read test by Tigerots ...";

    
    // 打开目录
    dirp = opendir("/");
    if (dirp == RT_NULL)
    {
        rt_kprintf("open directory error!\n");
    }
    else
    {
        // 读取目录 
        while ((d = readdir(dirp)) != RT_NULL)
        {
            strcat(buffer, d->d_name);
            strcat(buffer, " ");
        }
        // 关闭目录 
        closedir(dirp);
    }
    
    
    //创建目录
    fd = mkdir("/webnet", 0x777);
    if(fd<0)
    {
        rt_kprintf("创建目录失败");
    }
    // 以创建和读写模式打开/text.txt文件
    // 如果该文件不存在则创建该文件
    fd = open("/index.html", O_WRONLY|O_CREAT);
    if (fd>= 0)
    {
        write(fd, index_html, sizeof(index_html));
        close(fd);
        rt_kprintf("写入数据完成");
    }
    
    // 以只读模式打开文件
    fd = open("/index.html", O_RDONLY);
    if (fd>= 0)
    {
        size = read(fd, buffer, sizeof(buffer));
        close(fd);
        rt_kprintf("Read from file: %s \n", buffer);
        if (size < 0)
            return ;
    }
    
    fd = open("/main.txt", O_WRONLY|O_CREAT);
    if (fd>= 0)
    {
        write(fd, s, sizeof(s));
        close(fd);
        rt_kprintf("写入数据完成");
    }
    
    // 以只读模式打开文件
    fd = open("/main.txt", O_RDONLY);
    if (fd>= 0)
    {
        size = read(fd, buffer, sizeof(buffer));
        close(fd);
        rt_kprintf("Read from file: %s \n", buffer);
        if (size < 0)
            return ;
    }
    
    // 打开目录
    dirp = opendir("/");
    if (dirp == RT_NULL)
    {
        rt_kprintf("open directory error!\n");
    }
    else
    {
        // 读取目录 
        memset(buffer, 0, sizeof(buffer));
        while ((d = readdir(dirp)) != RT_NULL)
        {
            strcat(buffer, d->d_name);
            strcat(buffer, " ");
        }
        // 关闭目录 
        closedir(dirp);
    }
}

void littlefs_sample(void)
{
    struct rt_device *mtd_dev = RT_NULL;

    // 初始化 fal    
    fal_init();
    // 创建 mtd 设备
    mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
    if (!mtd_dev)
    {
        LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
    }
    else
    {
        // 挂载littlefs
        // 参数: 块设备名、文件系统挂载点路径,挂载文件系统类型,读写标志位,文件系统的私有数据
        if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == 0)
        {
            LOG_I("para initialized!");
            readwrite_sample();
        }
        else
        {
            /* 格式化文件系统 */
            dfs_mkfs("lfs", FS_PARTITION_NAME);
            /* 挂载 littlefs */
            if (dfs_mount("para", "/", "lfs", 0, 0) == 0)
            {
                LOG_I("para initialized!");
            }
            else
            {
                LOG_E("Failed to initialize filesystem!");
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/tigerots/article/details/104198901