Skip to content

Commit

Permalink
[nrf fromlist] settings: ZMS: add a backend for ZMS (Zephyr Memory St…
Browse files Browse the repository at this point in the history
…orage)

This adds the initial backend support for the ZMS storage system.

Upstream PR: zephyrproject-rtos/zephyr#78632

Signed-off-by: Riadh Ghaddab <rghaddab@baylibre.com>
(cherry picked from commit c949205ee2ddcdffa2e6813baaac45c90a339750)
  • Loading branch information
rghaddab committed Sep 28, 2024
1 parent c3aca40 commit b573411
Show file tree
Hide file tree
Showing 10 changed files with 739 additions and 0 deletions.
39 changes: 39 additions & 0 deletions subsys/settings/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,38 @@ config SETTINGS_ENCODE_LEN

choice SETTINGS_BACKEND
prompt "Storage back-end"
default SETTINGS_ZMS if ZMS
default SETTINGS_NVS if NVS
default SETTINGS_FCB if FCB
default SETTINGS_FILE if FILE_SYSTEM
default SETTINGS_NONE
help
Storage back-end to be used by the settings subsystem.

config SETTINGS_ZMS
bool "ZMS (Zephyr Memory Storage)"
depends on ZMS
help
Use ZMS as settings storage backend.

if SETTINGS_ZMS

config SETTINGS_ZMS_NAME_CACHE
bool "ZMS name lookup cache"
help
Enable ZMS name lookup cache, used to reduce the Settings name
lookup time.

config SETTINGS_ZMS_NAME_CACHE_SIZE
int "ZMS name lookup cache size"
default 128
range 1 $(UINT32_MAX)
depends on SETTINGS_ZMS_NAME_CACHE
help
Number of entries in Settings ZMS name cache.

endif # SETTINGS_ZMS

config SETTINGS_FCB
bool "FCB"
depends on FCB
Expand Down Expand Up @@ -161,6 +186,20 @@ config SETTINGS_NVS_SECTOR_COUNT
help
Number of sectors used for the NVS settings area

config SETTINGS_ZMS_SECTOR_SIZE_MULT
int "Sector size of the ZMS settings area"
default 1
depends on SETTINGS_ZMS
help
The sector size to use for the ZMS settings area as a multiple of
FLASH_ERASE_BLOCK_SIZE.

config SETTINGS_ZMS_SECTOR_COUNT
int "Sector count of the ZMS settings area"
default 8
help
Number of sectors used for the ZMS settings area

config SETTINGS_SHELL
bool "Settings shell"
depends on SHELL
Expand Down
64 changes: 64 additions & 0 deletions subsys/settings/include/settings/settings_zms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2024 BayLibre SAS
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef __SETTINGS_ZMS_H_
#define __SETTINGS_ZMS_H_

#include <zephyr/fs/zms.h>
#include <zephyr/settings/settings.h>

#ifdef __cplusplus
extern "C" {
#endif

/* In the ZMS backend, each setting is stored in two ZMS entries:
* 1. setting's name
* 2. setting's value
*
* The ZMS entry ID for the setting's value is determined implicitly based on
* the ID of the ZMS entry for the setting's name, once that is found. The
* difference between name and value ID is constant and equal to
* ZMS_NAME_ID_OFFSET.
*
* Setting's name entries start from ZMS_NAMECNT_ID + 1.
* The entry with ID == ZMS_NAMECNT_ID is used to store the largest name ID in use.
*
* Deleted records will not be found, only the last record will be read.
*/
#define ZMS_NAMECNT_ID 0x80000000
#define ZMS_NAME_ID_OFFSET 0x40000000

struct settings_zms {
struct settings_store cf_store;
struct zms_fs cf_zms;
uint32_t last_name_id;
const struct device *flash_dev;
#if CONFIG_SETTINGS_ZMS_NAME_CACHE
struct {
uint32_t name_hash;
uint32_t name_id;
} cache[CONFIG_SETTINGS_ZMS_NAME_CACHE_SIZE];

uint32_t cache_next;
uint32_t cache_total;
bool loaded;
#endif
};

/* register zms to be a source of settings */
int settings_zms_src(struct settings_zms *cf);

/* register zms to be the destination of settings */
int settings_zms_dst(struct settings_zms *cf);

/* Initialize a zms backend. */
int settings_zms_backend_init(struct settings_zms *cf);

#ifdef __cplusplus
}
#endif

#endif /* __SETTINGS_ZMS_H_ */
1 change: 1 addition & 0 deletions subsys/settings/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ zephyr_sources_ifdef(CONFIG_SETTINGS_FCB settings_fcb.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_NVS settings_nvs.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_NONE settings_none.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_SHELL settings_shell.c)
zephyr_sources_ifdef(CONFIG_SETTINGS_ZMS settings_zms.c)
Loading

0 comments on commit b573411

Please sign in to comment.