桌面小屏幕实战课程:DesktopScreen 7 文件系统
飞书文档http://https://x509p6c8to.feishu.cn/docx/doxcnPM42XBp7fgS9GtzNLRgGcj
spiffs文件系统移植
/home/kemp/work/esp/esp-idf/examples/storage/spiffs
复制分区表/home/kemp/work/esp/DesktopScreen/partitions.csv
idf.py menuconfig设置以下参数
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
源码下载方式参考:
源码下载方式
文件系统API
SPIFFS 文件系统 - - ‒ ESP-IDF 编程指南 release-v4.1 文档
/* HTTP File Server ExampleThis example code is in the Public Domain (or CC0 licensed, at your option.)Unless required by applicable law or agreed to in writing, thissoftware is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES ORCONDITIONS OF ANY KIND, either express or implied.
*/#include <sys/param.h>
#include <stdio.h>
#include <string.h>
#include <sys/unistd.h>
#include <sys/stat.h>#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_spiffs.h"
#include "esp_err.h"#include "ds_spiffs.h"/* This example demonstrates how to create file server* using esp_http_server. This file has only startup code.* Look in file_server.c for the implementation */static const char *TAG="spiffs";esp_vfs_spiffs_conf_t conf = {.base_path = "/spiffs",.partition_label = NULL,.max_files = 5, // This decides the maximum number of files that can be created on the storage.format_if_mount_failed = true
};/* Function to initialize SPIFFS */
esp_err_t init_spiffs(void)
{ESP_LOGI(TAG, "Initializing SPIFFS");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 ESP_FAIL;}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));return ESP_FAIL;}ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);return ESP_OK;
}void ds_spiffs_deinit(){// All done, unmount partition and disable SPIFFSesp_vfs_spiffs_unregister(conf.partition_label);ESP_LOGI(TAG, "SPIFFS unmounted");
}void ds_spiffs_test(){// 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 renamingstruct stat st;if (stat("/spiffs/foo.txt", &st) == 0) {// Delete it if it existsunlink("/spiffs/foo.txt");}// Rename original fileESP_LOGI(TAG, "Renaming file");if (rename("/spiffs/hello.txt", "/spiffs/foo.txt") != 0) {ESP_LOGE(TAG, "Rename failed");return;}// Open renamed file for readingESP_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 newlinechar* pos = strchr(line, '\n');if (pos) {*pos = '\0';}ESP_LOGI(TAG, "Read from file: '%s'", line);
}