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");
}
總結
以上是生活随笔為你收集整理的esp32 micropython spiffs_spiffs 文件系统在esp32中的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平安车主信用卡额度一般多少 有车一族值得
- 下一篇: 华夏银行小白卡额度一般多少?主要看京东和