ESP32开发—文件系统spiffs

文件系统有很多种,不同的操作的系统或者存储介质会选择不一样的文件系统,对于spiffs来说就是为了嵌入式设备定制精简版文件系统,优点是占用的内存非常小,而已不使用malloc分配内存,系统使用的内存由用户传入静态内存,缺点就是能存储的文件个数有限制文件大小有限制,而且不能建立文件夹只有一级目录

参考了这篇文章,说的很详细:http://t.csdn.cn/MhX4j

SPIFFS 是一个用于 SPI NOR flash 设备的嵌入式文件系统,支持磨损均衡、文件系统一致性检查等功能。

它的初始化结构体如下:

esp_vfs_spiffs_conf_t conf = {
	.base_path = "/spiffs",//根目录
	.partition_label = NULL,//分区表的标签
	.max_files = 5,//该目录下能存储的最大文件数目
	.format_if_mount_failed = true//如果挂载失败则会格式化文件系统
};

它的API主要有:

esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t *conf);//注册
esp_err_t esp_vfs_spiffs_unregister(const char *partition_label);//取消VFS上的SPIFFS初始化
bool esp_spiffs_mounted(const char *partition_label);//检查文件系统是否挂载
esp_err_t esp_spiffs_format(const char *partition_label);//格式化当前分区的文件系统
esp_err_t esp_spiffs_info(const char *partition_label, size_t *total_bytes, size_t *used_bytes);//获取某分区文件系统的参数    

根据官方给出的实例进行修改,并放到开发板上进行实验,此实验基于之前的定时器实验。

代码如下,进行了一些自我理解后的注释,理解有问题的话,欢迎大家指出来

#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_spiffs.h"

static const char *TAG = "spiffs";
esp_vfs_spiffs_conf_t conf = {
      .base_path = "/spiffs",         //根目录
      .partition_label = NULL,        //分区标签,指针类型
      .max_files = 5,                 //该目录下能存储的最大文件数目
      .format_if_mount_failed = true  //如果挂载失败则会重启
    };


//初始化spiffs
void init_spiffs(void){
    ESP_LOGI(TAG, "Initializing SPIFFS");


    //挂载spiffs文件,挂载之后可以使用C库进行读写文件
    esp_err_t ret = esp_vfs_spiffs_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 SPIFFS partition");
        } else {
            ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
        }
        return;
    }

//这部分可以暂时不看
#ifdef CONFIG_EXAMPLE_SPIFFS_CHECK_ON_START
    ESP_LOGI(TAG, "Performing SPIFFS_check().");
    ret = esp_spiffs_check(conf.partition_label);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
        return;
    } else {
        ESP_LOGI(TAG, "SPIFFS_check() successful");
    }
#endif

    //获取文件系统已经使用的大小和剩余大小
    size_t total = 0, used = 0;
    ret = esp_spiffs_info(conf.partition_label, &total, &used);
    if (ret != ESP_OK) {
        ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
        esp_spiffs_format(conf.partition_label);
        return;
    } else {
        ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
    }

#
    // 检查分区大小信息
    if (used > total) {
        ESP_LOGW(TAG, "Number of used bytes cannot be larger than total. Performing SPIFFS_check().");
        ret = esp_spiffs_check(conf.partition_label);
        // 也可以用来修复损坏的文件,清理未引用的页面等。
        // More info at https://github.com/pellepl/spiffs/wiki/FAQ#powerlosses-contd-when-should-i-run-spiffs_check
        if (ret != ESP_OK) {
            ESP_LOGE(TAG, "SPIFFS_check() failed (%s)", esp_err_to_name(ret));
            return;
        } else {
            ESP_LOGI(TAG, "SPIFFS_check() successful");
        }
    }
}


//测试spiffs的例子
void ds_spiffs_test(void){
    // Use POSIX and C standard library functions to work with files.
    // First create a file.
    //创建文件并写
    ESP_LOGI(TAG, "Opening file");
    FILE* f = fopen("/spiffs/hello.txt", "w");//建立一个名为/spiffs/hello.txt的只写文件
    if (f == NULL) {
        ESP_LOGE(TAG, "Failed to open file for writing");
        return;
    }
    fprintf(f, "Hello World!\n");//写入字符
    fclose(f);//关闭文件
    ESP_LOGI(TAG, "File written");

    //重命名之前检查目标文件是否存在
    struct stat st;
    if (stat("/spiffs/foo.txt", &st) == 0) //获取文件信息,获取成功返回0
    {
        //从文件系统中删除一个名称。
        //如果名称是文件的最后一个连接,并且没有其它进程将文件打开,
        //名称对应的文件会实际被删除。
        unlink("/spiffs/foo.txt");
    }

    //重命名创建的文件
    ESP_LOGI(TAG, "Renaming file");
    if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {
        ESP_LOGE(TAG, "Rename failed");
        return;
    }

    //打开重命名的文件并读取
    ESP_LOGI(TAG, "Reading file");
    f = fopen("/spiffs/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);
    
    char* pos = strchr(line, '\n');//指针pos指向第一个找到‘\n’
    if (pos) {
        *pos = '\0';//将‘\n’替换为‘\0’
    }
    ESP_LOGI(TAG, "Read from file: '%s'", line);
}

//卸载文件系统
void ds_spiffs_deinit(void){
    // All done, unmount partition and disable SPIFFS
    esp_vfs_spiffs_unregister(conf.partition_label);
    ESP_LOGI(TAG, "SPIFFS unmounted");
}

主要是定义了三个函数,方便在其他程序里调用。

void init_spiffs();//初始化
void ds_spiffs_test();//测试
void ds_spiffs_deinit();//卸载

这里我恶补了关于文件操作的很多知识点。

fopen()函数详解

stat函数详解

Linux系统调用—unlinux函数详解

fgets函数及其用法


然后,我们需要配置一下分区,这里从例程里面把分区表拷贝出来就可以了,然后在图形界面选中自定义分区表并修改一下分区表的名称,与我们复制过来的文件名对应就行。

最后,上开发板测试!

 可以看到已经成功把例程中的字符串给打印出来了。

成功!!

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要确保你已经在ESP32开发板上成功烧录了SPIFFS文件系统,并且其中已经包含了你想要读取的图片文件。 接下来,你可以使用ESP32的IDF框架提供的SPIFFS API来读取这些图片文件。以下是一个简单的示例代码: ```c #include <stdio.h> #include <string.h> #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_log.h" #include "esp_spiffs.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "driver/ledc.h" #include "esp32/rom/uart.h" #include "esp_vfs.h" #include "esp_vfs_fat.h" // 定义SPIFFS挂载点的名称 #define SPIFFS_BASE_PATH "/spiffs" // 定义要读取的图片文件名称 #define IMAGE_FILENAME "/spiffs/image.jpg" void app_main() { // 初始化SPIFFS文件系统 esp_vfs_spiffs_conf_t conf = { .base_path = SPIFFS_BASE_PATH, .partition_label = NULL, .max_files = 5, .format_if_mount_failed = true }; esp_err_t ret = esp_vfs_spiffs_register(&conf); if (ret != ESP_OK) { ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret)); return; } // 打开要读取的图片文件 FILE* f = fopen(IMAGE_FILENAME, "rb"); if (f == NULL) { ESP_LOGE(TAG, "Failed to open file for reading"); return; } // 获取文件大小 fseek(f, 0, SEEK_END); size_t file_size = ftell(f); fseek(f, 0, SEEK_SET); // 读取文件数据 uint8_t* file_data = (uint8_t*)malloc(file_size); if (file_data == NULL) { ESP_LOGE(TAG, "Failed to allocate memory for file data"); fclose(f); return; } size_t bytes_read = fread(file_data, 1, file_size, f); if (bytes_read != file_size) { ESP_LOGE(TAG, "Failed to read file data"); free(file_data); fclose(f); return; } // 关闭文件 fclose(f); // 在这里,你可以使用你喜欢的图形库来显示图片数据 // 例如,如果你使用ESP32的IDF框架自带的LVGL库,可以使用以下代码来显示图片: lv_img_dsc_t img_dsc; img_dsc.data = file_data; img_dsc.header.cf = LV_IMG_CF_JPEG; img_dsc.header.w = 320; // 图片的宽度 img_dsc.header.h = 240; // 图片的高度 lv_obj_t* img_obj = lv_img_create(lv_scr_act(), NULL); lv_img_set_src(img_obj, &img_dsc); // 释放文件数据内存 free(file_data); } ``` 需要注意的是,在这个示例代码中,使用了ESP32的IDF框架自带的LVGL库来显示图片。如果你使用其他图形库,你需要相应地修改代码。同时,这个示例代码中读取了JPEG格式的图片文件,如果你的图片文件格式不同,你需要相应地修改代码。 另外,需要注意的是,SPIFFS文件系统的读取速度比较慢,如果你的图片文件比较大,可能会导致程序运行缓慢。在实际开发中,你可能需要考虑使用其他更快的存储介质,例如SD卡。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值