ESP32文件系统esp_littlefs实例

陈拓 2021/11/24-2021/11/25

1. 概述

https://github.com/joltwallet/esp_littlefs

LittleFS是一个用于微控制器的小型故障安全文件系统。我们将LittleFS移植到esp-idf(特别是ESP32),因为SPIFFS速度太慢,“臃肿”且脆弱。

2. 用法

有两种方法可以将此组件添加到项目中。

  • 作为ESP-IDF托管组件:在项目目录中运行

idf.py add-dependency joltwallet/littlefs==1.0.0

  • 作为子模块:在项目中,将其作为子模块添加到components/目录中

git submodule add https://github.com/joltwallet/esp_littlefs.git

git submodule update --init --recursive

mv esp_littlefs components/

现在就可以在项目中通过idf.py menuconfig下面的 Component config-> LittleFS配置库了。

3. 实例

User@wreyford提供了一份演示报告https://github.com/wreyford/demo_esp_littlefs

展示了esp_littlefs的使用。 在example/目录中有修改后的副本。

  • 克隆demo

git clone https://github.com/wreyford/demo_esp_littlefs.git

  • 文件夹内容

  • 配置

idf.py menuconfig

1)  设置定制分区表文件partitions_demo_esp_littlefs.csv

分区表文件partitions_demo_esp_littlefs.csv的内容:

2) 设置LittleFS文件系统

  • 编译项目

cd demo_esp_littlefs

idf.py build

如果出现:

刷新esp-idf环境:

get_idf

再编译。

对比之前的分区表:

  • 烧写项目

idf.py -p /dev/ttyS3 -b 115200 flash

  • 运行结果

文件操作示范,创建文件,写文件,文件改名,读文件。

程序流程:

  1. 初始化LittelFS
  2. 创建文件Opening file -> fopen("/littlefs/hello.txt", "w");
  3. 向文件写入字符串File written -> fprintf(f, "LittleFS Rocks!\n");
  4. 改文件名Renaming file -> rename("/littlefs/hello.txt", "/littlefs/foo.txt")
  5. 读文件Reading file -> fopen("/littlefs/foo.txt", "r");
  6. 读一行Read from file -> fgets(line, sizeof(line), f);
  7. 显示读结果 -> ESP_LOGI(TAG, "Read from file: '%s'", line);
  8. 卸载文件系统 -> esp_vfs_littlefs_unregister(conf.partition_label);

对应的C程序:

        printf("Now we are starting the LittleFs Demo ...\n");

        ESP_LOGI(TAG, "Initializing LittelFS");

        esp_vfs_littlefs_conf_t conf = {
            .base_path = "/littlefs",
            .partition_label = "littlefs",
            .format_if_mount_failed = true,
            .dont_mount = false,
        };

        // Use settings defined above to initialize and mount LittleFS filesystem.
        // Note: esp_vfs_littlefs_register is an all-in-one convenience function.
        esp_err_t ret = esp_vfs_littlefs_register(&conf);

        if (ret != ESP_OK)
        {
                if (ret == ESP_FAIL)
                {
                        ESP_LOGE(TAG, "Failed to mount or format filesystem");
                }
                else if (ret == ESP_ERR_NOT_FOUND)
                {
                        ESP_LOGE(TAG, "Failed to find LittleFS partition");
                }
                else
                {
                        ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
                }
                return;
        }

        size_t total = 0, used = 0;
        ret = esp_littlefs_info(conf.partition_label, &total, &used);
        if (ret != ESP_OK)
        {
                ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret));
        }
        else
        {
                ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
        }

        // Use POSIX and C standard library functions to work with files.
        // First create a file.
        ESP_LOGI(TAG, "Opening file");
        FILE *f = fopen("/littlefs/hello.txt", "w");
        if (f == NULL)
        {
                ESP_LOGE(TAG, "Failed to open file for writing");
                return;
        }
        fprintf(f, "LittleFS Rocks!\n");
        fclose(f);
        ESP_LOGI(TAG, "File written");

        // Check if destination file exists before renaming
        struct stat st;
        if (stat("/littlefs/foo.txt", &st) == 0)
        {
                // Delete it if it exists
                unlink("/littlefs/foo.txt");
        }

        // Rename original file
        ESP_LOGI(TAG, "Renaming file");
        if (rename("/littlefs/hello.txt", "/littlefs/foo.txt") != 0)
        {
                ESP_LOGE(TAG, "Rename failed");
                return;
        }

        // Open renamed file for reading
        ESP_LOGI(TAG, "Reading file");
        f = fopen("/littlefs/foo.txt", "r");
        if (f == NULL)
        {
                ESP_LOGE(TAG, "Failed to open file for reading");
                return;
        }
        char line[64];
        fgets(line, sizeof(line), f);
        fclose(f);
        // strip newline
        char *pos = strchr(line, '\n');
        if (pos)
        {
                *pos = '\0';
        }
        ESP_LOGI(TAG, "Read from file: '%s'", line);

        // All done, unmount partition and disable LittleFS
        esp_vfs_littlefs_unregister(conf.partition_label);
        ESP_LOGI(TAG, "LittleFS unmounted");
  • LittleFS在块上运行,在ESP32上块的大小为4096字节
  • 一个新格式化的LittleFS将有2个块在使用中,使它看起来像8KB在使用中。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据引用,和,你在编译过程中遇到了一个错误,即找不到esp_camera.h头文件。这个错误通常是因为编译器无法找到此文件所在的路径。为了解决这个问题,你可以尝试以下几个步骤: 1. 确保你的代码中正确引用了esp_camera.h头文件。检查代码中的#include语句,确保文件名的大小写和路径是正确的。 2. 检查你的开发环境是否正确配置了esp32开发板。确保你已经安装了ESP32核心和相应的库文件。 3. 检查你的代码中是否缺少了必要的库文件。如果缺少了某个库文件,你需要将其添加到你的项目中。 4. 如果你已经安装了正确的库文件,但仍然无法找到esp_camera.h头文件,你可以尝试手动将该文件添加到你的项目中。你可以从官方的ESP32库或其他可靠来源下载该文件,并将其放置在正确的路径下。 请根据以上步骤逐一排查,确保你的开发环境正确配置,并且所有必要的文件都被正确引用。希望这些解决方法能够帮助到你解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [esp32 cam使用Aduino IDE编译并接入blinker时出现的问题以及解决办法](https://blog.csdn.net/m0_58985552/article/details/131664608)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晨之清风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值