[core] Add FlashDB KVS library

This commit is contained in:
Kuba Szczodrzyński
2022-05-24 17:43:30 +02:00
parent 91ae692058
commit 9a3c077ef1
8 changed files with 164 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-24. */
#pragma once
// Flash device configuration
extern const struct fal_flash_dev flash0;
#define FAL_FLASH_DEV_NAME "flash0"
#define FAL_FLASH_DEV_TABLE \
{ &flash0, }
// Partition table
#define FAL_PART_HAS_TABLE_CFG
#define FAL_PART_TABLE \
{ \
{ \
.magic_word = FAL_PART_MAGIC_WORD, \
.name = "userdata", \
.flash_name = FAL_FLASH_DEV_NAME, \
.offset = FLASH_USERDATA_OFFSET, \
.len = 0x4000, \
}, \
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef _FDB_CFG_H_
#define _FDB_CFG_H_
/* using KVDB feature */
#define FDB_USING_KVDB
#ifdef FDB_USING_KVDB
/* Auto update KV to latest default when current KVDB version number is changed. @see fdb_kvdb.ver_num */
// #define FDB_KV_AUTO_UPDATE
#endif
/* using TSDB (Time series database) feature */
// #define FDB_USING_TSDB
/* Using FAL storage mode */
#define FDB_USING_FAL_MODE
#ifdef FDB_USING_FAL_MODE
/* the flash write granularity, unit: bit
* only support 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1) */
#define FDB_WRITE_GRAN 8
#endif
/* Using file storage mode by LIBC file API, like fopen/fread/fwrte/fclose */
// #define FDB_USING_FILE_LIBC_MODE
/* Using file storage mode by POSIX file API, like open/read/write/close */
// #define FDB_USING_FILE_POSIX_MODE
/* MCU Endian Configuration, default is Little Endian Order. */
// #define FDB_BIG_ENDIAN
/* log print macro. default EF_PRINT macro is printf() */
#define FDB_PRINT(...)
/* print debug information */
// #define FDB_DEBUG_ENABLE
#endif /* _FDB_CFG_H_ */

View File

@@ -0,0 +1,31 @@
/* Copyright (c) Kuba Szczodrzyński 2022-05-24. */
#include <fal.h>
#include <flash_api.h>
#define FLASH_ERASE_MIN_SIZE (4 * 1024)
static int read(long offset, uint8_t *buf, size_t size) {
return size * flash_stream_read(NULL, offset, size, buf);
}
static int write(long offset, const uint8_t *buf, size_t size) {
return size * flash_stream_write(NULL, offset, size, buf);
}
static int erase(long offset, size_t size) {
size = ((size - 1) / FLASH_ERASE_MIN_SIZE) + 1;
for (uint16_t i = 0; i < size; i++) {
flash_erase_sector(NULL, offset + i * FLASH_ERASE_MIN_SIZE);
}
return size;
}
const struct fal_flash_dev flash0 = {
.name = FAL_FLASH_DEV_NAME,
.addr = 0x0,
.len = FLASH_LENGTH,
.blk_size = FLASH_ERASE_MIN_SIZE,
.ops = {NULL, read, write, erase},
.write_gran = 1,
};

View File

@@ -39,6 +39,7 @@ env.AddLibrary(
"+<common/*.c*>",
"+<core/*.c*>",
"+<libraries/**/*.c*>",
"+<port/**/*.c*>",
"+<posix/*.c>",
],
includes=[
@@ -46,9 +47,13 @@ env.AddLibrary(
"!<compat>",
"!<core>",
"!<libraries/*>",
"!<port/*>",
"!<posix>",
],
)
# Sources - external library ports
env.AddLibraryFlashDB(version="03500fa")
# Build all libraries
env.BuildLibraries(safe=False)

View File

@@ -112,6 +112,18 @@ env.AddLibrary(
],
)
# Sources - external library ports
env.AddLibrary(
name="ambz_arduino_port",
base_dir="$ARDUINO_DIR",
srcs=[
"+<port/**/*.c*>",
],
includes=[
"+<port/*>",
],
)
# Libs & linker config
env.Append(
LIBS=[

29
builder/libs/flashdb.py Normal file
View File

@@ -0,0 +1,29 @@
# Copyright (c) Kuba Szczodrzyński 2022-05-24.
from SCons.Script import DefaultEnvironment
env = DefaultEnvironment()
platform = env.PioPlatform()
def env_add_flashdb(
env,
version: str,
):
package_dir = platform.get_package_dir(f"library-flashdb@{version}")
env.AddLibrary(
name=f"flashdb{version}",
base_dir=package_dir,
srcs=[
"+<src/*.c>",
"+<port/fal/src/*.c>",
],
includes=[
"+<inc>",
"+<port/fal/inc>",
],
)
env.AddMethod(env_add_flashdb, "AddLibraryFlashDB")

View File

@@ -9,6 +9,7 @@ board = env.BoardConfig()
env.SConscript("utils.py", exports="env")
# Vendor-specific library ports
env.SConscript("libs/lwip.py", exports="env")
env.SConscript("libs/flashdb.py", exports="env")
# Firmware name
if env.get("PROGNAME", "program") == "program":
@@ -35,11 +36,14 @@ env.Replace(
flash_layout: dict = board.get("flash")
if flash_layout:
defines = {}
flash_size = 0
for name, layout in flash_layout.items():
name = name.upper()
(offset, _, length) = layout.partition("+")
defines[f"FLASH_{name}_OFFSET"] = offset
defines[f"FLASH_{name}_LENGTH"] = length
flash_size = max(flash_size, int(offset, 16) + int(length, 16))
defines["FLASH_LENGTH"] = flash_size
env.Append(CPPDEFINES=defines.items())
env.Replace(**defines)

View File

@@ -43,6 +43,11 @@
"version": "https://github.com/arduino/ArduinoCore-API",
"manifest": {
"description": "Hardware independent layer of the Arduino cores"
},
"libraries": {
"flashdb": [
"03500fa"
]
}
},
"library-lwip": {
@@ -53,6 +58,14 @@
"description": "lwIP - A Lightweight TCPIP stack"
}
},
"library-flashdb": {
"type": "framework",
"optional": true,
"base_url": "https://github.com/armink/FlashDB",
"manifest": {
"description": "An ultra-lightweight database that supports key-value and time series data"
}
},
"toolchain-gccarmnoneeabi": {
"type": "toolchain",
"optionalVersions": [