esp32 spiffs移植
1、main目录下写入spiffs.c文件
在这里插入代码片
#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 = "example";
void spiffs_initial(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(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);
}
// ESP_LOGI(TAG, "Opening file");
// FILE* f;
// ESP_LOGI(TAG, "Reading file");
// // f = fopen("/spiffs/foo.txt", "r");
// f = fopen("/spiffs/hello.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(conf.partition_label);
// ESP_LOGI(TAG, "SPIFFS unmounted");
}
void fs_shutdown() {
esp_vfs_spiffs_unregister(NULL);
// esp_vfs_spiffs_unregister(conf.partition_label);
ESP_LOGI(TAG, "SPIFFS unmounted");
}
2、在main同级目录添加spiffs_image文件夹
3、配置main目录下的CMakeLists.txt文件
spiffs_create_partition_image(storage ../spiffs_image FLASH_IN_PROJECT)
4、配置分区表
在分区表中添加storage分区
storage, data, spiffs, , 2M,