Skip to content

Commit

Permalink
settings: ZMS: add a backend for ZMS (Zephyr Memory Storage)
Browse files Browse the repository at this point in the history
This adds the initial backend support for the ZMS storage system.

Signed-off-by: Riadh Ghaddab <rghaddab@baylibre.com>
  • Loading branch information
rghaddab committed Sep 18, 2024
1 parent 119b238 commit 9713a91
Show file tree
Hide file tree
Showing 10 changed files with 771 additions and 0 deletions.
40 changes: 40 additions & 0 deletions subsys/settings/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ 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
Expand Down Expand Up @@ -77,6 +78,31 @@ config SETTINGS_NVS_NAME_CACHE_SIZE

endif # SETTINGS_NVS

config SETTINGS_ZMS
bool "Zephyr Memory storage support"
depends on ZMS
depends on FLASH_MAP
help
Enables ZMS storage support

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_CUSTOM
bool "CUSTOM"
help
Expand Down Expand Up @@ -132,6 +158,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
65 changes: 65 additions & 0 deletions subsys/settings/include/settings/settings_zms.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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

Check notice on line 32 in subsys/settings/include/settings/settings_zms.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/settings/include/settings/settings_zms.h:32 -#define ZMS_NAMECNT_ID 0x80000000 +#define ZMS_NAMECNT_ID 0x80000000

Check notice on line 32 in subsys/settings/include/settings/settings_zms.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/settings/include/settings/settings_zms.h:32 -#define ZMS_NAMECNT_ID 0x80000000 +#define ZMS_NAMECNT_ID 0x80000000

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

Check notice on line 61 in subsys/settings/include/settings/settings_zms.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/settings/include/settings/settings_zms.h:61 -

Check notice on line 61 in subsys/settings/include/settings/settings_zms.h

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/settings/include/settings/settings_zms.h:61 -
}
#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 @@ -13,3 +13,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 9713a91

Please sign in to comment.