mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-05-20 08:36:41 +08:00
feat(spiffs): Add SPIFFS base example
This commit is contained in:
6
examples/storage/spiffs/CMakeLists.txt
Normal file
6
examples/storage/spiffs/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(spiffs)
|
9
examples/storage/spiffs/Makefile
Normal file
9
examples/storage/spiffs/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := spiffs
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
62
examples/storage/spiffs/README.md
Normal file
62
examples/storage/spiffs/README.md
Normal file
@ -0,0 +1,62 @@
|
||||
# SPIFFS example
|
||||
|
||||
This example demonstrates how to use SPIFFS at this software platform. Example does the following steps:
|
||||
|
||||
1. Enable SPIFFS at "menuconfig"
|
||||
2. Use an "all-in-one" `esp_vfs_spiffs_register` function to:
|
||||
- initialize SPIFFS,
|
||||
- mount SPIFFS filesystem using SPIFFS library (and format, if the filesystem can not be mounted),
|
||||
- register SPIFFS filesystem in VFS, enabling C standard library and POSIX functions to be used.
|
||||
3. Create a file using `fopen` and write to it using `fprintf`.
|
||||
4. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function.
|
||||
5. Open renamed file for reading, read back the line, and print it to the terminal.
|
||||
|
||||
SPIFFS partition size is set in partitions_example.csv file. See `docs/en/api-guides/partition-tables.rst` documentation for more information.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware required
|
||||
|
||||
This example does not require any special hardware, and can be run on any common development board.
|
||||
|
||||
### Configure the project
|
||||
|
||||
If using Make based build system, run `make menuconfig` and set serial port under Serial Flasher Options.
|
||||
|
||||
If using CMake based build system, no configuration is required.
|
||||
|
||||
### Build and flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
make -j4 flash monitor
|
||||
```
|
||||
|
||||
Or, for CMake based build system (replace PORT with serial port name):
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example output
|
||||
|
||||
Here is an example console output. In this case `format_if_mount_failed` parameter was set to `true` in the source code. SPIFFS was unformatted, so the initial mount has failed. SPIFFS was then formatted, and mounted again.
|
||||
|
||||
```
|
||||
I (324) example: Initializing SPIFFS
|
||||
W (324) SPIFFS: mount failed, -10025. formatting...
|
||||
I (19414) example: Partition size: total: 896321, used: 0
|
||||
I (19414) example: Opening file
|
||||
I (19504) example: File written
|
||||
I (19544) example: Renaming file
|
||||
I (19584) example: Reading file
|
||||
I (19584) example: Read from file: 'Hello World!'
|
||||
I (19584) example: SPIFFS unmounted
|
||||
```
|
||||
|
||||
To erase the contents of SPIFFS partition, run `make erase_flash` command (or `idf.py erase_flash`, if using CMake build system). Then upload the example again as described above.
|
4
examples/storage/spiffs/main/CMakeLists.txt
Normal file
4
examples/storage/spiffs/main/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
set(COMPONENT_SRCS "spiffs_example_main.c")
|
||||
set(COMPONENT_ADD_INCLUDEDIRS ".")
|
||||
|
||||
register_component()
|
4
examples/storage/spiffs/main/component.mk
Normal file
4
examples/storage/spiffs/main/component.mk
Normal file
@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
99
examples/storage/spiffs/main/spiffs_example_main.c
Normal file
99
examples/storage/spiffs/main/spiffs_example_main.c
Normal file
@ -0,0 +1,99 @@
|
||||
/* SPIFFS filesystem example.
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
|
||||
#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 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");
|
||||
}
|
6
examples/storage/spiffs/partitions_example.csv
Normal file
6
examples/storage/spiffs/partitions_example.csv
Normal file
@ -0,0 +1,6 @@
|
||||
# Name, Type, SubType, Offset, Size, Flags
|
||||
# Note: if you change the phy_init or app partition offset, make sure to change the offset in Kconfig.projbuild
|
||||
nvs, data, nvs, 0x9000, 0x6000,
|
||||
phy_init, data, phy, 0xf000, 0x1000,
|
||||
factory, app, factory, 0x10000, 512K,
|
||||
storage, data, spiffs, , 448K,
|
|
5
examples/storage/spiffs/sdkconfig.defaults
Normal file
5
examples/storage/spiffs/sdkconfig.defaults
Normal file
@ -0,0 +1,5 @@
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_example.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions_example.csv"
|
||||
|
||||
CONFIG_USING_SPIFFS=y
|
Reference in New Issue
Block a user