Skip to content

Commit f210386

Browse files
committed
Merge branch 'update/littlefs_demo_example_v5.1_v2' into 'release/v5.1'
LittleFS demo example added & Update/littlefs demo example (v5.1) v2 See merge request espressif/esp-idf!29450
2 parents 5059908 + 7a235d5 commit f210386

File tree

10 files changed

+223
-0
lines changed

10 files changed

+223
-0
lines changed

components/esp_partition/include/esp_partition.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ typedef enum {
9797
ESP_PARTITION_SUBTYPE_DATA_ESPHTTPD = 0x80, //!< ESPHTTPD partition
9898
ESP_PARTITION_SUBTYPE_DATA_FAT = 0x81, //!< FAT partition
9999
ESP_PARTITION_SUBTYPE_DATA_SPIFFS = 0x82, //!< SPIFFS partition
100+
ESP_PARTITION_SUBTYPE_DATA_LITTLEFS = 0x83, //!< LITTLEFS partition
100101

101102
#if __has_include("extra_partition_subtypes.inc")
102103
#include "extra_partition_subtypes.inc"

components/partition_table/gen_esp32part.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def get_ptype_as_int(ptype):
6868
'esphttpd': 0x80,
6969
'fat': 0x81,
7070
'spiffs': 0x82,
71+
'littlefs': 0x83,
7172
},
7273
}
7374

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
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
project(littlefs_example)

examples/storage/littlefs/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
2+
| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- |
3+
4+
# About LittleFS
5+
6+
[LittleFS](https://github.com/littlefs-project/littlefs) is a file system designed for small devices like microcontrollers and embedded systems. It helps these devices manage and store data efficiently.
7+
- LittleFS is specifically built to be lightweight and requires less storage space and processing power. It takes up very little memory, which is important for small devices with limited resources.
8+
- It has features that ensure data integrity and reliability, so the files stored on the device are less likely to get corrupted or lost.
9+
In a nutshell, LittleFS is a specialized file system that helps small devices effectively handle and store data, allowing them to perform tasks efficiently with minimal resources.
10+
11+
# License
12+
13+
The littlefs is provided under the BSD-3-Clause license. Please refer [littlefs license](https://github.com/littlefs-project/littlefs#license) for more details.
14+
15+
16+
# LittleFS example
17+
18+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
19+
20+
`littlefs` component is managed in ESP-IDF using IDF component manager. This example uses a littlefs port for [ESP-IDF littlefs](https://components.espressif.com/components/joltwallet/littlefs) for which upstream repository is [esp_littlefs](https://github.com/joltwallet/esp_littlefs).
21+
22+
LittleFS partition generator is invoked from CMake build system. We use `littlefs_create_partition_image` in main/CMakeLists.txt to generate the partition image and flash it to the device at build time.
23+
24+
*Note: Currently Windows does not support `LittleFS partition generation` feature.*
25+
26+
This example demonstrates how to use LittleFS with ESP32. Example does the following steps:
27+
28+
1. Use an "all-in-one" `esp_vfs_littlefs_register` function to:
29+
- initialize LittleFS,
30+
- mount LittleFS filesystem using LittleFS library (and format, if the filesystem can not be mounted),
31+
- register LittleFS filesystem in VFS, enabling C standard library and POSIX functions to be used.
32+
2. Create a file using `fopen` and write to it using `fprintf`.
33+
3. Rename the file. Before renaming, check if destination file already exists using `stat` function, and remove it using `unlink` function.
34+
4. Open renamed file for reading, read back the line, and print it to the terminal.
35+
36+
LittleFS partition size is set in partitions_demo_esp_littlefs.csv file. See [Partition Tables](https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/partition-tables.html) documentation for more information.
37+
38+
## How to use example
39+
40+
### Hardware required
41+
42+
This example does not require any special hardware, and can be run on any common development board.
43+
44+
### Build and flash
45+
46+
Replace PORT with serial port name:
47+
48+
```
49+
idf.py -p PORT flash monitor
50+
```
51+
52+
(To exit the serial monitor, type ``Ctrl-]``.)
53+
54+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
55+
56+
## Example output
57+
58+
Here is an example console output. In this case `format_if_mount_failed` parameter was set to `true` in the source code. LittleFS was unformatted, so the initial mount has failed. LittleFS was then formatted, and mounted again.
59+
60+
```
61+
I (264) esp_littlefs: Initializing LittleFS
62+
I (274) esp_littlefs: Partition size: total: 983040, used: 12288
63+
I (274) esp_littlefs: Opening file
64+
I (344) esp_littlefs: File written
65+
I (344) esp_littlefs: Renaming file
66+
I (354) esp_littlefs: Reading file
67+
I (354) esp_littlefs: Read from file: 'Hello World!'
68+
I (354) esp_littlefs: Reading from flashed filesystem example.txt
69+
I (364) esp_littlefs: Read from file: 'Example text to compile into a LittleFS disk image to be flashed to the ESP device.'
70+
I (374) esp_littlefs: LittleFS unmounted
71+
I (374) main_task: Returned from app_main()
72+
```
73+
74+
To erase the contents of LittleFS partition, run `idf.py erase-flash` command. Then upload the example again as described above.
75+
76+
[Here](https://github.com/joltwallet/esp_littlefs#performance) you can find performance results for LittleFS, SPIFFS and FATFS for your reference.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example text to compile into a LittleFS disk image to be flashed to the ESP device.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
idf_component_register(SRCS "esp_littlefs_example.c"
2+
INCLUDE_DIRS "."
3+
)
4+
5+
# Note: you must have a partition named the first argument (here it's "littlefs")
6+
# in your partition table csv file.
7+
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
8+
littlefs_create_partition_image(storage ../flash_data FLASH_IN_PROJECT)
9+
else()
10+
fail_at_build_time(littlefs "Windows does not support LittleFS partition generation")
11+
endif()
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2023 Brian Pugh
3+
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
4+
*
5+
* SPDX-License-Identifier: Unlicense OR CC0-1.0
6+
*/
7+
8+
#include <stdio.h>
9+
#include <string.h>
10+
#include <sys/stat.h>
11+
#include <unistd.h>
12+
#include "esp_err.h"
13+
#include "esp_log.h"
14+
#include "esp_system.h"
15+
#include "esp_littlefs.h"
16+
17+
static const char *TAG = "esp_littlefs";
18+
19+
void app_main(void)
20+
{
21+
ESP_LOGI(TAG, "Initializing LittleFS");
22+
23+
esp_vfs_littlefs_conf_t conf = {
24+
.base_path = "/littlefs",
25+
.partition_label = "storage",
26+
.format_if_mount_failed = true,
27+
.dont_mount = false,
28+
};
29+
30+
// Use settings defined above to initialize and mount LittleFS filesystem.
31+
// Note: esp_vfs_littlefs_register is an all-in-one convenience function.
32+
esp_err_t ret = esp_vfs_littlefs_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 LittleFS partition");
39+
} else {
40+
ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
41+
}
42+
return;
43+
}
44+
45+
size_t total = 0, used = 0;
46+
ret = esp_littlefs_info(conf.partition_label, &total, &used);
47+
if (ret != ESP_OK) {
48+
ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret));
49+
esp_littlefs_format(conf.partition_label);
50+
} else {
51+
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
52+
}
53+
54+
// Use POSIX and C standard library functions to work with files.
55+
// First create a file.
56+
ESP_LOGI(TAG, "Opening file");
57+
FILE *f = fopen("/littlefs/hello.txt", "w");
58+
if (f == NULL) {
59+
ESP_LOGE(TAG, "Failed to open file for writing");
60+
return;
61+
}
62+
fprintf(f, "Hello World!\n");
63+
fclose(f);
64+
ESP_LOGI(TAG, "File written");
65+
66+
// Check if destination file exists before renaming
67+
struct stat st;
68+
if (stat("/littlefs/foo.txt", &st) == 0) {
69+
// Delete it if it exists
70+
unlink("/littlefs/foo.txt");
71+
}
72+
73+
// Rename original file
74+
ESP_LOGI(TAG, "Renaming file");
75+
if (rename("/littlefs/hello.txt", "/littlefs/foo.txt") != 0) {
76+
ESP_LOGE(TAG, "Rename failed");
77+
return;
78+
}
79+
80+
// Open renamed file for reading
81+
ESP_LOGI(TAG, "Reading file");
82+
f = fopen("/littlefs/foo.txt", "r");
83+
if (f == NULL) {
84+
ESP_LOGE(TAG, "Failed to open file for reading");
85+
return;
86+
}
87+
88+
char line[128];
89+
fgets(line, sizeof(line), f);
90+
fclose(f);
91+
// strip newline
92+
char*pos = strchr(line, '\n');
93+
if (pos) {
94+
*pos = '\0';
95+
}
96+
ESP_LOGI(TAG, "Read from file: '%s'", line);
97+
98+
ESP_LOGI(TAG, "Reading from flashed filesystem example.txt");
99+
f = fopen("/littlefs/example.txt", "r");
100+
if (f == NULL) {
101+
ESP_LOGE(TAG, "Failed to open file for reading");
102+
return;
103+
}
104+
fgets(line, sizeof(line), f);
105+
fclose(f);
106+
// strip newline
107+
pos = strchr(line, '\n');
108+
if (pos) {
109+
*pos = '\0';
110+
}
111+
ESP_LOGI(TAG, "Read from file: '%s'", line);
112+
113+
// All done, unmount partition and disable LittleFS
114+
esp_vfs_littlefs_unregister(conf.partition_label);
115+
ESP_LOGI(TAG, "LittleFS unmounted");
116+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## IDF Component Manager Manifest File
2+
dependencies:
3+
joltwallet/littlefs: "~=1.14.2"
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+
storage, data, littlefs, , 0xF0000,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_PARTITION_TABLE_CUSTOM=y
2+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_demo_esp_littlefs.csv"

0 commit comments

Comments
 (0)