Skip to content

Commit 8886e7c

Browse files
committed
[nrf fromlist] settings: ZMS: add a backend for ZMS (Zephyr Memory Storage)
This adds the initial backend support for the ZMS storage system. Upstream PR: zephyrproject-rtos/zephyr#78632 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit e56028b1e78165fed94deaac1f14f658d1e94737)
1 parent b446ca9 commit 8886e7c

File tree

10 files changed

+740
-0
lines changed

10 files changed

+740
-0
lines changed

subsys/settings/Kconfig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,38 @@ config SETTINGS_ENCODE_LEN
3232

3333
choice SETTINGS_BACKEND
3434
prompt "Storage back-end"
35+
default SETTINGS_ZMS if ZMS
3536
default SETTINGS_NVS if NVS
3637
default SETTINGS_FCB if FCB
3738
default SETTINGS_FILE if FILE_SYSTEM
3839
default SETTINGS_NONE
3940
help
4041
Storage back-end to be used by the settings subsystem.
4142

43+
config SETTINGS_ZMS
44+
bool "ZMS (Zephyr Memory Storage)"
45+
depends on ZMS
46+
help
47+
Use ZMS as settings storage backend.
48+
49+
if SETTINGS_ZMS
50+
51+
config SETTINGS_ZMS_NAME_CACHE
52+
bool "ZMS name lookup cache"
53+
help
54+
Enable ZMS name lookup cache, used to reduce the Settings name
55+
lookup time.
56+
57+
config SETTINGS_ZMS_NAME_CACHE_SIZE
58+
int "ZMS name lookup cache size"
59+
default 128
60+
range 1 $(UINT32_MAX)
61+
depends on SETTINGS_ZMS_NAME_CACHE
62+
help
63+
Number of entries in Settings ZMS name cache.
64+
65+
endif # SETTINGS_ZMS
66+
4267
config SETTINGS_FCB
4368
bool "FCB"
4469
depends on FCB
@@ -161,6 +186,20 @@ config SETTINGS_NVS_SECTOR_COUNT
161186
help
162187
Number of sectors used for the NVS settings area
163188

189+
config SETTINGS_ZMS_SECTOR_SIZE_MULT
190+
int "Sector size of the ZMS settings area"
191+
default 1
192+
depends on SETTINGS_ZMS
193+
help
194+
The sector size to use for the ZMS settings area as a multiple of
195+
FLASH_ERASE_BLOCK_SIZE.
196+
197+
config SETTINGS_ZMS_SECTOR_COUNT
198+
int "Sector count of the ZMS settings area"
199+
default 8
200+
help
201+
Number of sectors used for the ZMS settings area
202+
164203
config SETTINGS_SHELL
165204
bool "Settings shell"
166205
depends on SHELL
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (c) 2024 BayLibre SAS
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef __SETTINGS_ZMS_H_
8+
#define __SETTINGS_ZMS_H_
9+
10+
#include <zephyr/fs/zms.h>
11+
#include <zephyr/settings/settings.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
/* In the ZMS backend, each setting is stored in two ZMS entries:
18+
* 1. setting's name
19+
* 2. setting's value
20+
*
21+
* The ZMS entry ID for the setting's value is determined implicitly based on
22+
* the ID of the ZMS entry for the setting's name, once that is found. The
23+
* difference between name and value ID is constant and equal to
24+
* ZMS_NAME_ID_OFFSET.
25+
*
26+
* Setting's name entries start from ZMS_NAMECNT_ID + 1.
27+
* The entry with ID == ZMS_NAMECNT_ID is used to store the largest name ID in use.
28+
*
29+
* Deleted records will not be found, only the last record will be read.
30+
*/
31+
#define ZMS_NAMECNT_ID 0x80000000
32+
#define ZMS_NAME_ID_OFFSET 0x40000000
33+
34+
struct settings_zms {
35+
struct settings_store cf_store;
36+
struct zms_fs cf_zms;
37+
uint32_t last_name_id;
38+
const struct device *flash_dev;
39+
#if CONFIG_SETTINGS_ZMS_NAME_CACHE
40+
struct {
41+
uint32_t name_hash;
42+
uint32_t name_id;
43+
} cache[CONFIG_SETTINGS_ZMS_NAME_CACHE_SIZE];
44+
45+
uint32_t cache_next;
46+
uint32_t cache_total;
47+
bool loaded;
48+
#endif
49+
};
50+
51+
/* register zms to be a source of settings */
52+
int settings_zms_src(struct settings_zms *cf);
53+
54+
/* register zms to be the destination of settings */
55+
int settings_zms_dst(struct settings_zms *cf);
56+
57+
/* Initialize a zms backend. */
58+
int settings_zms_backend_init(struct settings_zms *cf);
59+
60+
#ifdef __cplusplus
61+
}
62+
#endif
63+
64+
#endif /* __SETTINGS_ZMS_H_ */

subsys/settings/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ zephyr_sources_ifdef(CONFIG_SETTINGS_FCB settings_fcb.c)
1414
zephyr_sources_ifdef(CONFIG_SETTINGS_NVS settings_nvs.c)
1515
zephyr_sources_ifdef(CONFIG_SETTINGS_NONE settings_none.c)
1616
zephyr_sources_ifdef(CONFIG_SETTINGS_SHELL settings_shell.c)
17+
zephyr_sources_ifdef(CONFIG_SETTINGS_ZMS settings_zms.c)

0 commit comments

Comments
 (0)