diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index 4980f48..cfed461 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -24,6 +24,7 @@ jobs: - "examples/espidf-peripherals-uart" - "examples/espidf-peripherals-usb" - "examples/espidf-storage-sdcard" + - "examples/espidf-storage-spiffs" - "examples/espidf-ulp-adc" - "examples/espidf-ulp-pulse" - "examples/pumbaa-blink" diff --git a/examples/espidf-storage-spiffs/.gitignore b/examples/espidf-storage-spiffs/.gitignore new file mode 100644 index 0000000..03f4a3c --- /dev/null +++ b/examples/espidf-storage-spiffs/.gitignore @@ -0,0 +1 @@ +.pio diff --git a/examples/espidf-storage-spiffs/CMakeLists.txt b/examples/espidf-storage-spiffs/CMakeLists.txt new file mode 100644 index 0000000..1894b31 --- /dev/null +++ b/examples/espidf-storage-spiffs/CMakeLists.txt @@ -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) diff --git a/examples/espidf-storage-spiffs/README.rst b/examples/espidf-storage-spiffs/README.rst new file mode 100644 index 0000000..25efc94 --- /dev/null +++ b/examples/espidf-storage-spiffs/README.rst @@ -0,0 +1,35 @@ +.. Copyright 2014-present PlatformIO + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +How to build PlatformIO based project +===================================== + +1. `Install PlatformIO Core `_ +2. Download `development platform with examples `_ +3. Extract ZIP archive +4. Run these commands: + +.. code-block:: bash + + # Change directory to example + > cd platform-espressif32/examples/espidf-storage-spiffs + + # Build project + > platformio run + + # Upload firmware + > platformio run --target upload + + # Upload SPIFFS image + > platformio run --target uploadfs + + # Clean build files + > platformio run --target clean diff --git a/examples/espidf-storage-spiffs/data/hello.txt b/examples/espidf-storage-spiffs/data/hello.txt new file mode 100644 index 0000000..5a83677 --- /dev/null +++ b/examples/espidf-storage-spiffs/data/hello.txt @@ -0,0 +1 @@ +Hello World from SPIFFS. diff --git a/examples/espidf-storage-spiffs/include/README b/examples/espidf-storage-spiffs/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/examples/espidf-storage-spiffs/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/examples/espidf-storage-spiffs/lib/README b/examples/espidf-storage-spiffs/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/examples/espidf-storage-spiffs/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/examples/espidf-storage-spiffs/partitions_example.csv b/examples/espidf-storage-spiffs/partitions_example.csv new file mode 100644 index 0000000..92db904 --- /dev/null +++ b/examples/espidf-storage-spiffs/partitions_example.csv @@ -0,0 +1,6 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, 0x9000, 0x6000, +phy_init, data, phy, 0xf000, 0x1000, +factory, app, factory, 0x10000, 1M, +spiffs, data, spiffs, , 0xF0000, diff --git a/examples/espidf-storage-spiffs/platformio.ini b/examples/espidf-storage-spiffs/platformio.ini new file mode 100644 index 0000000..83870a9 --- /dev/null +++ b/examples/espidf-storage-spiffs/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:esp32dev] +platform = espressif32 +framework = espidf +board = esp32dev +monitor_speed = 115200 +board_build.partitions = partitions_example.csv diff --git a/examples/espidf-storage-spiffs/sdkconfig.defaults b/examples/espidf-storage-spiffs/sdkconfig.defaults new file mode 100644 index 0000000..b9bb0c0 --- /dev/null +++ b/examples/espidf-storage-spiffs/sdkconfig.defaults @@ -0,0 +1,3 @@ +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_example.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions_example.csv" diff --git a/examples/espidf-storage-spiffs/src/CMakeLists.txt b/examples/espidf-storage-spiffs/src/CMakeLists.txt new file mode 100644 index 0000000..026db13 --- /dev/null +++ b/examples/espidf-storage-spiffs/src/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "spiffs_example_main.c" + INCLUDE_DIRS ".") \ No newline at end of file diff --git a/examples/espidf-storage-spiffs/src/spiffs_example_main.c b/examples/espidf-storage-spiffs/src/spiffs_example_main.c new file mode 100644 index 0000000..279f3df --- /dev/null +++ b/examples/espidf-storage-spiffs/src/spiffs_example_main.c @@ -0,0 +1,73 @@ +/* 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 +#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(conf.partition_label, &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); + } + + // Open renamed file for reading + ESP_LOGI(TAG, "Reading file"); + FILE* f = fopen("/spiffs/hello.txt", "r"); + if (f == NULL) { + ESP_LOGI(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"); +} diff --git a/examples/espidf-storage-spiffs/test/README b/examples/espidf-storage-spiffs/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/examples/espidf-storage-spiffs/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html