diff --git a/arduino/libretuya/port/flashdb/fal_cfg.h b/arduino/libretuya/port/flashdb/fal_cfg.h new file mode 100644 index 0000000..a0feee3 --- /dev/null +++ b/arduino/libretuya/port/flashdb/fal_cfg.h @@ -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, \ + }, \ + } diff --git a/arduino/libretuya/port/flashdb/fdb_cfg.h b/arduino/libretuya/port/flashdb/fdb_cfg.h new file mode 100644 index 0000000..33b554c --- /dev/null +++ b/arduino/libretuya/port/flashdb/fdb_cfg.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2020, Armink, + * + * 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_ */ diff --git a/arduino/realtek-ambz/port/flashdb/fal_flash_ambz_port.c b/arduino/realtek-ambz/port/flashdb/fal_flash_ambz_port.c new file mode 100644 index 0000000..9435090 --- /dev/null +++ b/arduino/realtek-ambz/port/flashdb/fal_flash_ambz_port.c @@ -0,0 +1,31 @@ +/* Copyright (c) Kuba Szczodrzyński 2022-05-24. */ + +#include +#include + +#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, +}; diff --git a/builder/arduino-common.py b/builder/arduino-common.py index c5a47b4..76a4dbd 100644 --- a/builder/arduino-common.py +++ b/builder/arduino-common.py @@ -39,6 +39,7 @@ env.AddLibrary( "+", "+", "+", + "+", "+", ], includes=[ @@ -46,9 +47,13 @@ env.AddLibrary( "!", "!", "!", + "!", "!", ], ) +# Sources - external library ports +env.AddLibraryFlashDB(version="03500fa") + # Build all libraries env.BuildLibraries(safe=False) diff --git a/builder/frameworks/realtek-ambz-arduino.py b/builder/frameworks/realtek-ambz-arduino.py index ff6d7bb..937ab8f 100644 --- a/builder/frameworks/realtek-ambz-arduino.py +++ b/builder/frameworks/realtek-ambz-arduino.py @@ -112,6 +112,18 @@ env.AddLibrary( ], ) +# Sources - external library ports +env.AddLibrary( + name="ambz_arduino_port", + base_dir="$ARDUINO_DIR", + srcs=[ + "+", + ], + includes=[ + "+", + ], +) + # Libs & linker config env.Append( LIBS=[ diff --git a/builder/libs/flashdb.py b/builder/libs/flashdb.py new file mode 100644 index 0000000..8051ba7 --- /dev/null +++ b/builder/libs/flashdb.py @@ -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=[ + "+", + "+", + ], + includes=[ + "+", + "+", + ], + ) + + +env.AddMethod(env_add_flashdb, "AddLibraryFlashDB") diff --git a/builder/main.py b/builder/main.py index 057f06d..e95023e 100644 --- a/builder/main.py +++ b/builder/main.py @@ -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) diff --git a/platform.json b/platform.json index d0d8843..262cb9a 100644 --- a/platform.json +++ b/platform.json @@ -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": [