Skip to content

Commit 0811af6

Browse files
committed
modules: Add storage module
The storage module adds the ability to buffer data in a chosen backend. Currently, RAM backend is implemented. The module subscribes to a list of zbus channels and stores all the sampled data it receives on those channels, as long as there is available space. The RAM buffer acts as a ring buffer, where the oldest entry is discarded if all capacity is used. There are two operational modes: - Pass-through: Data is passed on immediately with no storage - Buffer: Data is stored and passed on when the storage module receives a message over its zbus channel to do so. Signed-off-by: Jan Tore Guggedal <jantore.guggedal@nordicsemi.no>
1 parent 9bd504a commit 0811af6

File tree

18 files changed

+3823
-0
lines changed

18 files changed

+3823
-0
lines changed

app/src/modules/storage/.DS_Store

6 KB
Binary file not shown.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# Copyright (c) 2025 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
target_include_directories(app PRIVATE .)
8+
9+
target_sources(app PRIVATE
10+
storage.c
11+
storage_data_types.c
12+
)
13+
14+
zephyr_linker_sources(SECTIONS storage_sections.ld)
15+
16+
target_sources_ifdef(CONFIG_APP_STORAGE_BACKEND_RAM app PRIVATE
17+
backends/ram_ring_buffer_backend.c
18+
)
19+
20+
target_sources_ifdef(CONFIG_APP_STORAGE_BACKEND_INTERNAL_FLASH app PRIVATE
21+
backends/flash_backend.c
22+
)
23+
24+
target_sources_ifdef(CONFIG_APP_STORAGE_SHELL app PRIVATE
25+
storage_shell.c
26+
)
27+
28+
target_include_directories(app PRIVATE .)
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Copyright (c) 2025 Nordic Semiconductor ASA
2+
#
3+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
4+
5+
menuconfig APP_STORAGE
6+
bool "Storage module"
7+
default y
8+
select ZBUS
9+
select ZBUS_RUNTIME_OBSERVERS
10+
11+
if APP_STORAGE
12+
13+
config APP_STORAGE_THREAD_STACK_SIZE
14+
int "Storage module thread stack size"
15+
default 1024
16+
17+
choice APP_STORAGE_BACKEND
18+
prompt "Storage backend selection"
19+
default APP_STORAGE_BACKEND_RAM
20+
help
21+
Select which storage backend to use.
22+
23+
config APP_STORAGE_BACKEND_RAM
24+
bool "RAM storage backend"
25+
help
26+
Store data in RAM. Data will be lost on power loss or reset.
27+
28+
config APP_STORAGE_BACKEND_INTERNAL_FLASH
29+
bool "Internal flash storage backend"
30+
select FLASH
31+
select FLASH_MAP
32+
help
33+
Store data in internal flash memory. Data persists across reboots.
34+
35+
endchoice
36+
37+
config APP_STORAGE_MESSAGE_QUEUE_SIZE
38+
int "Size of message queue"
39+
default 10
40+
help
41+
Size of the message queue for the storage module's zbus subscriber.
42+
This determines how many messages can be queued before being processed.
43+
44+
config APP_STORAGE_MAX_TYPES
45+
int "Maximum number of data types"
46+
default 3
47+
help
48+
Maximum number of different data types that can be registered
49+
for storage. Each type requires memory for storing its records,
50+
so this value affects RAM usage. Total RAM usage will be:
51+
MAX_TYPES * MAX_RECORDS_PER_TYPE * RECORD_SIZE bytes.
52+
53+
config APP_STORAGE_MAX_RECORDS_PER_TYPE
54+
int "Maximum records per data type"
55+
default 32
56+
help
57+
Maximum number of records that can be stored for each data type.
58+
For example, if set to 32, you can store up to 32 battery samples,
59+
32 environmental samples, etc. This value affects RAM usage.
60+
Total RAM usage will be:
61+
MAX_TYPES * MAX_RECORDS_PER_TYPE * RECORD_SIZE bytes.
62+
63+
config APP_STORAGE_RAM_LIMIT_KB
64+
int "Maximum RAM usage in KB"
65+
default 64
66+
help
67+
Maximum amount of RAM in kilobytes that the storage module
68+
can use for storing records. This limit is checked at build time.
69+
The actual RAM usage is:
70+
MAX_TYPES * MAX_RECORDS_PER_TYPE * RECORD_SIZE bytes.
71+
Set this based on your system's available RAM.
72+
73+
config APP_STORAGE_FIFO_ITEM_COUNT
74+
int "FIFO item count"
75+
default 20
76+
help
77+
Maximum umber of memory slabs available to use in the storage module's FIFO.
78+
The FIFO is used when data is sent from the storage module to other modules.
79+
This option is also used to set the number of each registered data type that can be stored
80+
in the FIFO.
81+
For example, if set to 20, there will be 20 memory slabs available for each registered
82+
data type, and a total of 20 items can be stored in the FIFO at any time.
83+
84+
if APP_STORAGE_BACKEND_INTERNAL_FLASH
85+
86+
config APP_STORAGE_FLASH_PARTITION_LABEL
87+
string "Flash partition label"
88+
default "storage"
89+
help
90+
Label for the flash partition used by the storage module.
91+
Must match a partition defined in the devicetree.
92+
93+
config APP_STORAGE_FLASH_AREA_SIZE
94+
int "Flash storage area size"
95+
default 4096
96+
help
97+
Size in bytes of the flash area used for storage.
98+
Must be a multiple of the flash page size.
99+
100+
endif # APP_STORAGE_BACKEND_INTERNAL_FLASH
101+
102+
config APP_STORAGE_WATCHDOG_TIMEOUT_SECONDS
103+
int "Storage module watchdog timeout in seconds"
104+
default 60
105+
help
106+
If the storage module thread is stuck for this amount of time,
107+
the device will reset.
108+
109+
config APP_STORAGE_MSG_PROCESSING_TIMEOUT_SECONDS
110+
int "Maximum time in seconds allowed for processing a single message"
111+
default 5
112+
help
113+
Maximum time allowed for processing a single message in the storage module.
114+
Must be less than APP_STORAGE_WATCHDOG_TIMEOUT_SECONDS.
115+
116+
choice APP_STORAGE_INITIAL_MODE
117+
prompt "Storage module initial mode"
118+
default APP_STORAGE_INITIAL_MODE_PASS_THROUGH
119+
help
120+
Select the initial mode of the storage module.
121+
122+
config APP_STORAGE_INITIAL_MODE_PASS_THROUGH
123+
bool "Pass-through mode"
124+
help
125+
In this mode, the storage module will pass messages directly to STORAGE_DATA messages
126+
without any buffering of data.
127+
128+
config APP_STORAGE_INITIAL_MODE_BUFFER
129+
bool "Buffer mode"
130+
help
131+
In this mode, the storage module will buffer data samples in the configured backend.
132+
Data is sent from the storage module upon request from other modules.
133+
134+
endchoice
135+
136+
137+
config APP_STORAGE_SHELL
138+
bool "Enable storage shell commands"
139+
default y
140+
help
141+
Enable shell commands for interacting with the storage module.
142+
This allows you to manage stored data from the command line.
143+
144+
config APP_STORAGE_SHELL_STATS
145+
bool "Enable storage shell stats commands"
146+
help
147+
Enable shell commands for displaying statistics about the storage module.
148+
This includes information about stored records, types, and memory usage.
149+
Enabling this option will increase the code size of the storage module.
150+
151+
module = APP_STORAGE
152+
module-str = Storage
153+
source "subsys/logging/Kconfig.template.log_config"
154+
155+
endif # APP_STORAGE

0 commit comments

Comments
 (0)