From 3167d58e0da75ae09b2eaa1ac28e7d90950dcaea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20H=C3=BC=C3=9Fler?= Date: Tue, 2 Jan 2024 23:51:21 +0100 Subject: [PATCH] pkg/flashdb: add configurable sector size --- pkg/flashdb/Kconfig | 9 +++++++++ pkg/flashdb/include/fal_cfg.h | 38 +++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/pkg/flashdb/Kconfig b/pkg/flashdb/Kconfig index 8d66a50be0..0d9fdef41c 100644 --- a/pkg/flashdb/Kconfig +++ b/pkg/flashdb/Kconfig @@ -41,6 +41,15 @@ config MODULE_FLASHDB_KVDB_AUTO_UPDATE database. If the version changes, it will automatically trigger an upgrade action and update the new default KV collection to the current database. +config FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB + int "Minimal virtual sector size in KiB for FlashDB" + default 4 + help + By default, KVDB will use 1 times the block size as the sector size, that is, 4096. + At this time, the KVDB cannot store a KV longer than 4096. If you want to save, for example, + a KV with a length of 10K, you can use the control function to set the sector size to 12K or + larger. + config MODULE_FLASHDB_MTD endif # PACKAGE_FLASHDB diff --git a/pkg/flashdb/include/fal_cfg.h b/pkg/flashdb/include/fal_cfg.h index 3ea4930978..8f960e9588 100644 --- a/pkg/flashdb/include/fal_cfg.h +++ b/pkg/flashdb/include/fal_cfg.h @@ -26,12 +26,40 @@ #define FAL_CFG_H #include "board.h" +#include "macros/units.h" #include "mtd_default.h" #ifdef __cplusplus extern "C" { #endif +#if !defined(CONFIG_FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB) || defined(DOXYGEN) +/** + * @brief Minimal virtual sector size in KiB for FlashDB + * + * This is just a reasonable default for an automatic partition configuration, hence "DEFAULT". + * The "MIN" stands for a required minimum to guarantee an expected size of key value pairs. + * The actually acceptable sector size must be a multiple of the physical sector size though. + * Thus, it can be larger than the minimum required size. + * + * The default is 4 (4096). + * FlashDB sector size is different from MTD sector size as it is a + * virtual measure of granularity and not a device property. + * The virtual sector size must be a multiple of the physical sector size. + * + * From the documentation of FLashDB: + * The internal storage structure of FlashDB is composed of N sectors, and each formatting takes + * sector as the smallest unit. A sector is usually N times the size of the Flash block. + * For example, the block size of Nor Flash is generally 4096. + * + * By default, KVDB will use 1 times the block size as the sector size, that is, 4096. + * At this time, the KVDB cannot store a KV longer than 4096. If you want to save, for example, + * a KV with a length of 10K, you can use the control function to set the sector size to 12K or larger. + * + */ +#define CONFIG_FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB 4 +#endif + /** * @brief Partition table is defined at compile time (not read from flash) */ @@ -66,9 +94,15 @@ extern struct fal_flash_dev mtd_flash0; #if !defined(FAL_PART0_LENGTH) || defined(DOXYGEN) /** - * @brief Have at least the length of partition 0 defined + * @brief Have at least the length of partition 0 defined, which must be at least two sectors + * and a multiple of the virtual sector size. + * + * The virtual sector size is however bound to the physical sector size of @ref FAL_MTD. + * So make sure that @ref CONFIG_FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB times 1024 is a multiple of the MTD sector size. + * For example if the MTD sector size is 4KiB, then @ref CONFIG_FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB must be a multiple of 4. + * If the MTD sector size is 1KiB, then you have all options for @ref CONFIG_FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB. */ -#define FAL_PART0_LENGTH (2 * 4096u) +#define FAL_PART0_LENGTH (2 * (CONFIG_FLASHDB_MIN_SECTOR_SIZE_DEFAULT_KiB * KiB(1))) #endif /**