Skip to content

Commit 55f50dc

Browse files
committed
Add ESP-IDF example with SPIFFS
Resolve #472
1 parent 2546ff6 commit 55f50dc

File tree

13 files changed

+239
-0
lines changed

13 files changed

+239
-0
lines changed

.github/workflows/examples.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
- "examples/espidf-peripherals-uart"
2525
- "examples/espidf-peripherals-usb"
2626
- "examples/espidf-storage-sdcard"
27+
- "examples/espidf-storage-spiffs"
2728
- "examples/espidf-ulp-adc"
2829
- "examples/espidf-ulp-pulse"
2930
- "examples/pumbaa-blink"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.pio
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The following lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(spiffs)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.. Copyright 2014-present PlatformIO <[email protected]>
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License.
11+
12+
How to build PlatformIO based project
13+
=====================================
14+
15+
1. `Install PlatformIO Core <http://docs.platformio.org/page/core.html>`_
16+
2. Download `development platform with examples <https://github.com/platformio/platform-espressif32/archive/develop.zip>`_
17+
3. Extract ZIP archive
18+
4. Run these commands:
19+
20+
.. code-block:: bash
21+
22+
# Change directory to example
23+
> cd platform-espressif32/examples/espidf-storage-spiffs
24+
25+
# Build project
26+
> platformio run
27+
28+
# Upload firmware
29+
> platformio run --target upload
30+
31+
# Upload SPIFFS image
32+
> platformio run --target uploadfs
33+
34+
# Clean build files
35+
> platformio run --target clean
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World from SPIFFS.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
3+
nvs, data, nvs, 0x9000, 0x6000,
4+
phy_init, data, phy, 0xf000, 0x1000,
5+
factory, app, factory, 0x10000, 1M,
6+
spiffs, data, spiffs, , 0xF0000,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter, extra scripting
4+
; Upload options: custom port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
;
7+
; Please visit documentation for the other options and examples
8+
; http://docs.platformio.org/page/projectconf.html
9+
10+
[env:esp32dev]
11+
platform = espressif32
12+
framework = espidf
13+
board = esp32dev
14+
monitor_speed = 115200
15+
board_build.partitions = partitions_example.csv
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_PARTITION_TABLE_CUSTOM=y
2+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_example.csv"
3+
CONFIG_PARTITION_TABLE_FILENAME="partitions_example.csv"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "spiffs_example_main.c"
2+
INCLUDE_DIRS ".")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* SPIFFS filesystem example.
2+
This example code is in the Public Domain (or CC0 licensed, at your option.)
3+
4+
Unless required by applicable law or agreed to in writing, this
5+
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6+
CONDITIONS OF ANY KIND, either express or implied.
7+
*/
8+
9+
#include <stdio.h>
10+
#include <string.h>
11+
#include <sys/unistd.h>
12+
#include <sys/stat.h>
13+
#include "esp_err.h"
14+
#include "esp_log.h"
15+
#include "esp_spiffs.h"
16+
17+
static const char *TAG = "example";
18+
19+
void app_main(void)
20+
{
21+
ESP_LOGI(TAG, "Initializing SPIFFS");
22+
23+
esp_vfs_spiffs_conf_t conf = {
24+
.base_path = "/spiffs",
25+
.partition_label = NULL,
26+
.max_files = 5,
27+
.format_if_mount_failed = true
28+
};
29+
30+
// Use settings defined above to initialize and mount SPIFFS filesystem.
31+
// Note: esp_vfs_spiffs_register is an all-in-one convenience function.
32+
esp_err_t ret = esp_vfs_spiffs_register(&conf);
33+
34+
if (ret != ESP_OK) {
35+
if (ret == ESP_FAIL) {
36+
ESP_LOGE(TAG, "Failed to mount or format filesystem");
37+
} else if (ret == ESP_ERR_NOT_FOUND) {
38+
ESP_LOGE(TAG, "Failed to find SPIFFS partition");
39+
} else {
40+
ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
41+
}
42+
return;
43+
}
44+
45+
size_t total = 0, used = 0;
46+
ret = esp_spiffs_info(conf.partition_label, &total, &used);
47+
if (ret != ESP_OK) {
48+
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret));
49+
} else {
50+
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
51+
}
52+
53+
// Open renamed file for reading
54+
ESP_LOGI(TAG, "Reading file");
55+
FILE* f = fopen("/spiffs/hello.txt", "r");
56+
if (f == NULL) {
57+
ESP_LOGI(TAG, "Failed to open file for reading");
58+
return;
59+
}
60+
char line[64];
61+
fgets(line, sizeof(line), f);
62+
fclose(f);
63+
// strip newline
64+
char* pos = strchr(line, '\n');
65+
if (pos) {
66+
*pos = '\0';
67+
}
68+
ESP_LOGI(TAG, "Read from file: '%s'", line);
69+
70+
// All done, unmount partition and disable SPIFFS
71+
esp_vfs_spiffs_unregister(conf.partition_label);
72+
ESP_LOGI(TAG, "SPIFFS unmounted");
73+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This directory is intended for PIO Unit Testing and project tests.
3+
4+
Unit Testing is a software testing method by which individual units of
5+
source code, sets of one or more MCU program modules together with associated
6+
control data, usage procedures, and operating procedures, are tested to
7+
determine whether they are fit for use. Unit testing finds problems early
8+
in the development cycle.
9+
10+
More information about PIO Unit Testing:
11+
- https://docs.platformio.org/page/plus/unit-testing.html

0 commit comments

Comments
 (0)