esp32 micropython spiffs_spiffs 文件系统在esp32中的应用

spiffs 介绍

SPIFFS 是一个开源文件系统,用于 SPI NOR flash 设备的嵌入式文件系统,支持磨损均衡、文件系统一致性检查等功能。spiffs 源码地址​github.com

spiffs 特点

而我们知道乐鑫的esp32的大部分存储都依赖于SPI flash ,spiffs可以说对于esp32 真可谓是最合适不过的了。

因此对于spiffs乐鑫提供了很好的支持,专门提供了工具(spiffsgen.py,mkspiffs)用于对实现spiffs 在esp32 上的创建、格式化等操作。在esp-idf中也提供了专门的接口函数用于操作spiffs。

源码分析

乐鑫提供的源码位于examples/storage/spiffs/ 下,代码​github.com

①配置csv文件

如果用户在不想使用spiffs工具去操作spiffs的话,乐鑫提供另外一种方式来定义spiffs的空间大小,那就是在.csv 中定义,csv文件是为esp32构建存储的配置文件,当编译时编译器根据这个文件分配flash的大小

在.csv最后定义了一个spiffs格式的存储空间,大小是0xF0000 = 960K,因为这个是最后一片存储空间了,只要地址不大于芯片整个flash的空间即可。

② 挂载文件系统

在使用spiffs之前应该对其进行简单的配置

esp_vfs_spiffs_conf_t conf = {

.base_path = "/spiffs",//文件系统的目录地址

.partition_label = NULL,//在.csv文件中的标签,如果设置为NULL则使用spiffs

.max_files = 5, //同时可以打开最大的文件数

.format_if_mount_failed = true//如果挂载失败,则格式化文件系统

};

配置完成后,需要将系统注册到vfs 操作系统中,vfs类似linux的vfs也是一个虚拟文件系统,这个系统的功能就是,使得用户可以使用C语言的通用库函数去访问不同的操作系统。

esp-idf 提供了注册函数将spiffs 挂载并注册到vfs中。

/**

* Register and mount SPIFFS to VFS with given path prefix.

*

* @param conf Pointer to esp_vfs_spiffs_conf_t configuration structure

*

* @return

* - ESP_OK if success

* - ESP_ERR_NO_MEM if objects could not be allocated

* - ESP_ERR_INVALID_STATE if already mounted or partition is encrypted

* - ESP_ERR_NOT_FOUND if partition for SPIFFS was not found

* - ESP_FAIL if mount or format fails

*/

esp_err_t esp_vfs_spiffs_register(const esp_vfs_spiffs_conf_t * conf);

查看spiffs 的信息

size_t total = 0, used = 0;

ret = esp_spiffs_info(NULL, &total, &used);

if (ret != ESP_OK) {

ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));

} else {

ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);

}

挂载成功之后,就可以使用c 标准库中的fopen,fread,fwrite等函数操作了。

例程源码

#include

#include

#include

#include

#include "esp_err.h"

#include "esp_log.h"

#include "esp_spiffs.h"

static const char *TAG = "example";

void app_main(void)

{

ESP_LOGI(TAG, "Initializing SPIFFS");

esp_vfs_spiffs_conf_t conf = {

.base_path = "/spiffs",

.partition_label = NULL,

.max_files = 5,

.format_if_mount_failed = true

};

// Use settings defined above to initialize and mount SPIFFS filesystem.

// Note: esp_vfs_spiffs_register is an all-in-one convenience function.

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;

}

size_t total = 0, used = 0;

ret = esp_spiffs_info(NULL, &total, &used);

if (ret != ESP_OK) {

ESP_LOGE(TAG, "Failed to get SPIFFS 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("/spiffs/hello.txt", "w");

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");

// Check if destination file exists before renaming

struct stat st;

if (stat("/spiffs/foo.txt", &st) == 0) {

// Delete it if it exists

unlink("/spiffs/foo.txt");

}

// Rename original file

ESP_LOGI(TAG, "Renaming file");

if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {

ESP_LOGE(TAG, "Rename failed");

return;

}

// Open renamed file for reading

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);

// 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 SPIFFS

esp_vfs_spiffs_unregister(NULL);

ESP_LOGI(TAG, "SPIFFS unmounted");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值