日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/10 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 esp32 micropython spiffs_spiffs 文件系统在esp32中的应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

spiffs 介紹

SPIFFS 是一個開源文件系統(tǒng),用于 SPI NOR flash 設備的嵌入式文件系統(tǒng),支持磨損均衡、文件系統(tǒng)一致性檢查等功能。spiffs 源碼地址?github.com

spiffs 特點

而我們知道樂鑫的esp32的大部分存儲都依賴于SPI flash ,spiffs可以說對于esp32 真可謂是最合適不過的了。

因此對于spiffs樂鑫提供了很好的支持,專門提供了工具(spiffsgen.py,mkspiffs)用于對實現(xiàn)spiffs 在esp32 上的創(chuàng)建、格式化等操作。在esp-idf中也提供了專門的接口函數(shù)用于操作spiffs。

源碼分析

樂鑫提供的源碼位于examples/storage/spiffs/ 下,代碼?github.com

①配置csv文件

如果用戶在不想使用spiffs工具去操作spiffs的話,樂鑫提供另外一種方式來定義spiffs的空間大小,那就是在.csv 中定義,csv文件是為esp32構(gòu)建存儲的配置文件,當編譯時編譯器根據(jù)這個文件分配flash的大小

在.csv最后定義了一個spiffs格式的存儲空間,大小是0xF0000 = 960K,因為這個是最后一片存儲空間了,只要地址不大于芯片整個flash的空間即可。

② 掛載文件系統(tǒng)

在使用spiffs之前應該對其進行簡單的配置

esp_vfs_spiffs_conf_t conf = {

.base_path = "/spiffs",//文件系統(tǒng)的目錄地址

.partition_label = NULL,//在.csv文件中的標簽,如果設置為NULL則使用spiffs

.max_files = 5, //同時可以打開最大的文件數(shù)

.format_if_mount_failed = true//如果掛載失敗,則格式化文件系統(tǒng)

};

配置完成后,需要將系統(tǒng)注冊到vfs 操作系統(tǒng)中,vfs類似linux的vfs也是一個虛擬文件系統(tǒng),這個系統(tǒng)的功能就是,使得用戶可以使用C語言的通用庫函數(shù)去訪問不同的操作系統(tǒng)。

esp-idf 提供了注冊函數(shù)將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等函數(shù)操作了。

例程源碼

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

}

總結(jié)

以上是生活随笔為你收集整理的esp32 micropython spiffs_spiffs 文件系统在esp32中的应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。