Skip to content

Commit b471645

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 b99b214b949d3ae10bf02eb60f4ade4fb42400a1)
1 parent a62dded commit b471645

File tree

10 files changed

+739
-0
lines changed

10 files changed

+739
-0
lines changed

subsys/settings/Kconfig

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,39 @@ 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+
select SYS_HASH_FUNC32
54+
help
55+
Enable ZMS name lookup cache, used to reduce the Settings name
56+
lookup time.
57+
58+
config SETTINGS_ZMS_NAME_CACHE_SIZE
59+
int "ZMS name lookup cache size"
60+
default 128
61+
range 1 $(UINT32_MAX)
62+
depends on SETTINGS_ZMS_NAME_CACHE
63+
help
64+
Number of entries in Settings ZMS name cache.
65+
66+
endif # SETTINGS_ZMS
67+
4268
config SETTINGS_FCB
4369
bool "FCB"
4470
depends on FCB
@@ -132,6 +158,21 @@ config SETTINGS_NVS_SECTOR_COUNT
132158
help
133159
Number of sectors used for the NVS settings area
134160

161+
config SETTINGS_ZMS_SECTOR_SIZE_MULT
162+
int "Sector size of the ZMS settings area"
163+
default 1
164+
depends on SETTINGS_ZMS
165+
help
166+
The sector size to use for the ZMS settings area as a multiple of
167+
FLASH_ERASE_BLOCK_SIZE.
168+
169+
config SETTINGS_ZMS_SECTOR_COUNT
170+
int "Sector count of the ZMS settings area"
171+
default 8
172+
depends on SETTINGS_ZMS
173+
help
174+
Number of sectors used for the ZMS settings area
175+
135176
config SETTINGS_SHELL
136177
bool "Settings shell"
137178
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
@@ -13,3 +13,4 @@ zephyr_sources_ifdef(CONFIG_SETTINGS_FCB settings_fcb.c)
1313
zephyr_sources_ifdef(CONFIG_SETTINGS_NVS settings_nvs.c)
1414
zephyr_sources_ifdef(CONFIG_SETTINGS_NONE settings_none.c)
1515
zephyr_sources_ifdef(CONFIG_SETTINGS_SHELL settings_shell.c)
16+
zephyr_sources_ifdef(CONFIG_SETTINGS_ZMS settings_zms.c)

0 commit comments

Comments
 (0)