Skip to content

Commit 301853f

Browse files
modules: storage: Add littleFS backend
Add littleFS based flash storage backend for the storage module. Signed-off-by: Trond F. Christiansen <trond.christiansen@nordicsemi.no>
1 parent 0fa57a5 commit 301853f

24 files changed

+1280
-30
lines changed

app/boards/nrf9151dk_nrf9151_ns.overlay

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,37 @@
1414
aliases {
1515
ext-flash = &gd25wb256;
1616
};
17+
18+
fstab {
19+
compatible = "zephyr,fstab";
20+
lfs1: lfs1 {
21+
compatible = "zephyr,fstab,littlefs";
22+
mount-point = "/att_storage";
23+
partition = <&littlefs_storage>;
24+
automount;
25+
read-size = <16>;
26+
prog-size = <16>;
27+
cache-size = <64>;
28+
lookahead-size = <32>;
29+
block-cycles = <512>;
30+
};
31+
};
1732
};
1833

1934
&gd25wb256 {
2035
status = "okay";
36+
37+
/* Partitions will be overridden by PM but needed for DTS parsing */
38+
partitions {
39+
compatible = "fixed-partitions";
40+
#address-cells = <1>;
41+
#size-cells = <1>;
42+
43+
littlefs_storage: partition@0 {
44+
label = "littlefs_storage";
45+
reg = <0x00000000 0x00100000>;
46+
};
47+
};
2148
};
2249

2350
&uart1 {

app/boards/nrf9160dk_nrf9160_ns.overlay

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,36 @@
88
chosen {
99
nordic,pm-ext-flash = &mx25r64;
1010
};
11+
12+
fstab {
13+
compatible = "zephyr,fstab";
14+
lfs1: lfs1 {
15+
compatible = "zephyr,fstab,littlefs";
16+
mount-point = "/att_storage";
17+
partition = <&littlefs_storage>;
18+
automount;
19+
read-size = <16>;
20+
prog-size = <256>;
21+
cache-size = <512>;
22+
lookahead-size = <32>;
23+
block-cycles = <512>;
24+
};
25+
};
1126
};
1227

1328
/* External flash device is disabled by default */
1429
&mx25r64 {
1530
status = "okay";
16-
};
1731

32+
/* Partitions will be overridden by PM but needed for DTS parsing */
33+
partitions {
34+
compatible = "fixed-partitions";
35+
#address-cells = <1>;
36+
#size-cells = <1>;
37+
38+
littlefs_storage: partition@0 {
39+
label = "littlefs_storage";
40+
reg = <0x00000000 0x00100000>;
41+
};
42+
};
43+
};

app/boards/nrf9161dk_nrf9161_ns.overlay

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,37 @@
1414
aliases {
1515
ext-flash = &gd25wb256;
1616
};
17+
18+
fstab {
19+
compatible = "zephyr,fstab";
20+
lfs1: lfs1 {
21+
compatible = "zephyr,fstab,littlefs";
22+
mount-point = "/att_storage";
23+
partition = <&littlefs_storage>;
24+
automount;
25+
read-size = <16>;
26+
prog-size = <16>;
27+
cache-size = <64>;
28+
lookahead-size = <32>;
29+
block-cycles = <512>;
30+
};
31+
};
1732
};
1833

1934
&gd25wb256 {
2035
status = "okay";
36+
37+
/* Partitions will be overridden by PM but needed for DTS parsing */
38+
partitions {
39+
compatible = "fixed-partitions";
40+
#address-cells = <1>;
41+
#size-cells = <1>;
42+
43+
littlefs_storage: partition@0 {
44+
label = "littlefs_storage";
45+
reg = <0x00000000 0x00100000>;
46+
};
47+
};
2148
};
2249

2350
&uart1 {

app/boards/thingy91_nrf9160_ns.overlay

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
accelerometer = &adxl362;
1414
impact-sensor = &adxl372;
1515
};
16+
17+
fstab {
18+
compatible = "zephyr,fstab";
19+
lfs1: lfs1 {
20+
compatible = "zephyr,fstab,littlefs";
21+
mount-point = "/att_storage";
22+
partition = <&littlefs_storage>;
23+
automount;
24+
read-size = <16>;
25+
prog-size = <16>;
26+
cache-size = <64>;
27+
lookahead-size = <32>;
28+
block-cycles = <512>;
29+
};
30+
};
1631
};
1732

1833
&i2c2 {
@@ -30,3 +45,14 @@
3045
hpf = <0>;
3146
};
3247
};
48+
49+
/* Use internal flash for storage (Thingy:91 has no external flash) */
50+
/* Partitions will be overridden by PM but needed for DTS parsing */
51+
&flash0 {
52+
partitions {
53+
littlefs_storage: partition@fa000 {
54+
label = "littlefs_storage";
55+
reg = <0x000fa000 0x00006000>; /* 24KB at end of flash */
56+
};
57+
};
58+
};

app/boards/thingy91x_nrf9151_ns.overlay

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,35 @@
2626
&nrf70 {
2727
compatible = "nordic,nrf7000-spi";
2828
};
29+
30+
31+
/ {
32+
fstab {
33+
compatible = "zephyr,fstab";
34+
lfs1: lfs1 {
35+
compatible = "zephyr,fstab,littlefs";
36+
mount-point = "/att_storage";
37+
partition = <&littlefs_storage>; /* Reference PM partition */
38+
automount;
39+
read-size = <16>;
40+
prog-size = <16>;
41+
cache-size = <64>;
42+
lookahead-size = <32>;
43+
block-cycles = <512>;
44+
};
45+
};
46+
};
47+
48+
&flash_ext {
49+
/* Partitions will be overridden by PM but needed for DTS parsing */
50+
partitions {
51+
compatible = "fixed-partitions";
52+
#address-cells = <1>;
53+
#size-cells = <1>;
54+
55+
littlefs_storage: partition@0 {
56+
label = "littlefs_storage";
57+
reg = <0x00000000 0x00100000>;
58+
};
59+
};
60+
};

app/overlay-memfault.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ CONFIG_MEMFAULT=y
88
CONFIG_MEMFAULT_HEAP_STATS=y
99
CONFIG_MEMFAULT_LOGGING_ENABLE=y
1010
CONFIG_MEMFAULT_LOGGING_RAM_SIZE=4096
11+
CONFIG_MEMFAULT_FS_BYTES_FREE_METRIC=y
12+
CONFIG_MEMFAULT_FS_BYTES_FREE_VFS_PATH="att_storage"
1113
CONFIG_MEMFAULT_COREDUMP_FULL_THREAD_STACKS=y
1214
CONFIG_MEMFAULT_HTTP_DEDICATED_WORKQUEUE_STACK_SIZE=1560
1315
CONFIG_MEMFAULT_HTTP_PERIODIC_UPLOAD=y

app/src/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,8 +1931,20 @@ static void fota_rebooting_entry(void *o)
19311931
{
19321932
ARG_UNUSED(o);
19331933

1934+
struct storage_msg msg = { .type = STORAGE_CLEAR };
1935+
int err;
1936+
19341937
LOG_DBG("%s", __func__);
19351938

1939+
/* Tell storage module to clear any stored data */
1940+
err = zbus_chan_pub(&STORAGE_CHAN, &msg, K_MSEC(ZBUS_PUBLISH_TIMEOUT_MS));
1941+
if (err) {
1942+
LOG_ERR("Failed to publish storage clear message, error: %d", err);
1943+
SEND_FATAL_ERROR();
1944+
1945+
return;
1946+
}
1947+
19361948
/* Reboot the device */
19371949
LOG_WRN("Rebooting the device to apply the FOTA update");
19381950

app/src/modules/cloud/Kconfig.cloud

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ endchoice
111111

112112
config APP_CLOUD_THREAD_STACK_SIZE
113113
int "Thread stack size"
114-
default 5632
114+
default 6352
115115

116116
config APP_CLOUD_MESSAGE_QUEUE_SIZE
117117
int "Message queue size"

app/src/modules/storage/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ target_sources_ifdef(CONFIG_APP_STORAGE_BACKEND_RAM app PRIVATE
1717
backends/ram_ring_buffer_backend.c
1818
)
1919

20+
target_sources_ifdef(CONFIG_APP_STORAGE_BACKEND_LITTLEFS app PRIVATE
21+
backends/littlefs_backend.c
22+
)
23+
2024
target_sources_ifdef(CONFIG_APP_STORAGE_SHELL app PRIVATE
2125
storage_shell.c
2226
)

app/src/modules/storage/Kconfig.storage

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,48 @@ if APP_STORAGE
1212

1313
config APP_STORAGE_THREAD_STACK_SIZE
1414
int "Storage module thread stack size"
15-
default 1536
15+
default 1720 if APP_STORAGE_BACKEND_RAM
16+
default 3000 if APP_STORAGE_BACKEND_LITTLEFS
17+
18+
choice APP_STORAGE_BACKEND
19+
prompt "Storage backend"
20+
default APP_STORAGE_BACKEND_RAM
21+
help
22+
Select the storage backend to use for storing data samples.
1623

1724
config APP_STORAGE_BACKEND_RAM
1825
bool "RAM storage backend"
19-
default y
2026
help
2127
Store data in RAM. Data will be lost on power loss or reset.
2228

29+
config APP_STORAGE_BACKEND_LITTLEFS
30+
bool "LittleFS storage backend"
31+
select FILE_SYSTEM
32+
select FILE_SYSTEM_LITTLEFS
33+
help
34+
Store data in LittleFS filesystem. Data will persist across power
35+
loss and resets, as long as the underlying flash storage is intact.
36+
37+
endchoice # APP_STORAGE_BACKEND
38+
39+
if APP_STORAGE_BACKEND_LITTLEFS
40+
41+
# Setup PM partition size for LittleFS to 64KB by default
42+
config PM_PARTITION_SIZE_LITTLEFS
43+
default 0x10000
44+
45+
config PM_PARTITION_REGION_LITTLEFS_EXTERNAL
46+
default y if PM_EXTERNAL_FLASH_ENABLED
47+
48+
config APP_STORAGE_LITTLEFS_MAX_PATH_LEN
49+
int "Maximum length of storage file names"
50+
default 64
51+
help
52+
Maximum length of file names used by the storage module
53+
in the LittleFS backend.
54+
55+
endif # APP_STORAGE_BACKEND_LITTLEFS
56+
2357
config APP_STORAGE_MAX_TYPES
2458
int "Maximum number of data types"
2559
default 4
@@ -97,6 +131,7 @@ endchoice
97131
config APP_STORAGE_SHELL
98132
bool "Enable storage shell commands"
99133
default y if SHELL
134+
select FILE_SYSTEM_SHELL if APP_STORAGE_BACKEND_LITTLEFS
100135
help
101136
Enable shell commands for interacting with the storage module.
102137
This allows you to manage stored data from the command line.

0 commit comments

Comments
 (0)