GD32手把手教你移植FlashDB(片外Flash) -- 3.FlashDB使用

GD32手把手教你移植FlashDB(片外Flash) – 1.FlashDB-sfud移植
GD32手把手教你移植FlashDB(片外Flash) – 2.FlashDB移植
GD32手把手教你移植FlashDB(片外Flash) – 3.FlashDB使用

示例代码: https://gitee.com/ljmRD/GD32F427_FlashDB
在这里插入图片描述

1.kvdbtsdb初始化需要注意的地方

在这里插入图片描述

main()

/************************************************
 * WKS GD32F427ZGT6核心板
 * SPI FLASH 实验
 ************************************************/

#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/LED/led.h"
#include "./BSP/KEY/key.h"
#include "./BSP/NORFLASH/norflash.h"
#include "SEGGER_RTT.h"
#include <string.h>
#include <stdio.h>
#include "sfud.h"
#include "fal.h"
#include "flashdb.h"

static uint32_t boot_count = 0;
static time_t boot_time[10] = {0, 1, 2, 3};
/* default KV nodes */
static struct fdb_default_kv_node default_kv_table[] = {
    {"username", "armink", 0},                       /* string KV */
    {"password", "123456", 0},                       /* string KV */
    {"boot_count", &boot_count, sizeof(boot_count)}, /* int type KV */
    {"boot_time", &boot_time, sizeof(boot_time)},    /* int array type KV */
};
/* KVDB object */
static struct fdb_kvdb kvdb = {0};
/* TSDB object */
struct fdb_tsdb tsdb = {0};
/* counts for simulated timestamp */
static int counts = 0;

extern void kvdb_basic_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_string_sample(fdb_kvdb_t kvdb);
extern void kvdb_type_blob_sample(fdb_kvdb_t kvdb);
extern void tsdb_sample(fdb_tsdb_t tsdb);

static void lock(fdb_db_t db)
{
    __disable_irq();
}

static void unlock(fdb_db_t db)
{
    __enable_irq();
}

static uint8_t flashdb_test_kvdb(void)
{
    /* KVDB Sample */

    fdb_err_t result;
    struct fdb_default_kv default_kv;

    default_kv.kvs = default_kv_table;
    default_kv.num = sizeof(default_kv_table) / sizeof(default_kv_table[0]);
    /* set the lock and unlock function if you want */
    fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_LOCK, (void *)lock);
    fdb_kvdb_control(&kvdb, FDB_KVDB_CTRL_SET_UNLOCK, (void *)unlock);
    /* Key-Value database initialization
     *
     *       &kvdb: database object
     *       "env": database name
     * "fdb_kvdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
     *              Please change to YOUR partition name.
     * &default_kv: The default KV nodes. It will auto add to KVDB when first initialize successfully.
     *        NULL: The user data if you need, now is empty.
     */
    result = fdb_kvdb_init(&kvdb, "env", "KVDB", &default_kv, NULL);

    if (result != FDB_NO_ERR)
    {
        return -1;
        //		printf("\r\n FDB_NO_ERR = SET !!! \r\n");
    }

    /* run basic KV samples */
    kvdb_basic_sample(&kvdb);
    /* run string KV samples */
    kvdb_type_string_sample(&kvdb);
    /* run blob KV samples */
    kvdb_type_blob_sample(&kvdb);
    return 0;
}

static fdb_time_t get_time(void)
{
    /* Using the counts instead of timestamp.
     * Please change this function to return RTC time.
     */
    return ++counts;
}

static uint8_t flashdb_test_tsdb(void)
{ /* TSDB Sample */
    fdb_err_t result;
    /* set the lock and unlock function if you want */
    fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_LOCK, (void *)lock);
    fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_SET_UNLOCK, (void *)unlock);
    /* Time series database initialization
     *
     *       &tsdb: database object
     *       "log": database name
     * "fdb_tsdb1": The flash partition name base on FAL. Please make sure it's in FAL partition table.
     *              Please change to YOUR partition name.
     *    get_time: The get current timestamp function.
     *         128: maximum length of each log
     *        NULL: The user data if you need, now is empty.
     */
    result = fdb_tsdb_init(&tsdb, "log", "TSDB", get_time, 128, NULL);
    /* read last saved time for simulated timestamp */
    fdb_tsdb_control(&tsdb, FDB_TSDB_CTRL_GET_LAST_TIME, &counts);

    if (result != FDB_NO_ERR)
    {
        return -1;
        //		printf("\r\n FDB_NO_ERR = SET !!! \r\n");
    }

    /* run TSDB sample */
    tsdb_sample(&tsdb);
    return 0;
}

int main(void)
{

    uint16_t i = 0;

    delay_init(200); /* 初始化延时函数 */
    led_init();      /* 初始化LED */
    norflash_init(); /* 初始化NORFLASH */

    sfud_init(); // 初始化sfud
    fal_init();  // 初始化fal

    flashdb_test_kvdb(); // kvdb测试
    flashdb_test_tsdb(); // tsdb测试

    while (1)
    {

        i++;
        if (i == 20)
        {
            LED0_TOGGLE(); /* LED0闪烁 */
            i = 0;
        }

        delay_ms(10);
    }
}

在这里插入图片描述

2.分配大空间可能带来的问题注意

在这里插入图片描述

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值