demo工程暂存 优化菜单界面UI和功能
This commit is contained in:
67
MCU/examples/common/flashdb/fal_flash_port.c
Normal file
67
MCU/examples/common/flashdb/fal_flash_port.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-01-26 armink the first version
|
||||
*/
|
||||
|
||||
#include <fal.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "fr30xx.h"
|
||||
|
||||
#include "FreeRTOS.h" // pvPortMalloc
|
||||
|
||||
static int init(void);
|
||||
static int read(long offset, uint8_t *buf, size_t size);
|
||||
static int write(long offset, uint8_t *buf, size_t size);
|
||||
static int erase(long offset, size_t size);
|
||||
|
||||
//static sfud_flash_t sfud_dev = NULL;
|
||||
const struct fal_flash_dev onchip_flash =
|
||||
{
|
||||
.name = "flashdb_onchip",
|
||||
.addr = 0,
|
||||
.len = 2 * 1024 * 1024,
|
||||
.blk_size = 4*1024,
|
||||
.ops = {init, read, write, erase},
|
||||
.write_gran = 16
|
||||
};
|
||||
|
||||
static int init(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int read(long offset, uint8_t *buf, size_t size)
|
||||
{
|
||||
flash_read(QSPI0, offset, size, buf);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static int write(long offset, uint8_t *buf, size_t size)
|
||||
{
|
||||
uint8_t *temp_ptr = NULL;
|
||||
|
||||
if(((uint32_t )buf & 0xff000000) == FLASH_DAC_BASE) {
|
||||
temp_ptr = pvPortMalloc(size);
|
||||
memcpy(temp_ptr,buf,size);
|
||||
flash_write(QSPI0, offset, size, temp_ptr);
|
||||
vPortFree(temp_ptr);
|
||||
}
|
||||
else
|
||||
flash_write(QSPI0, offset, size, buf);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
static int erase(long offset, size_t size)
|
||||
{
|
||||
flash_erase(QSPI0, offset,size);
|
||||
|
||||
return size;
|
||||
}
|
86
MCU/examples/common/flashdb/fdb_app.c
Normal file
86
MCU/examples/common/flashdb/fdb_app.c
Normal file
@ -0,0 +1,86 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "fr30xx.h"
|
||||
#include "flashdb.h"
|
||||
|
||||
/* KVDB object */
|
||||
static struct fdb_kvdb kvdb={0};
|
||||
|
||||
/* critical zone protection */
|
||||
static CPU_SR cpu_sr = 0;
|
||||
|
||||
static void lock(fdb_db_t db)
|
||||
{
|
||||
/* used to avoid reentry */
|
||||
if (cpu_sr & 0xff00) {
|
||||
while(1);
|
||||
}
|
||||
|
||||
cpu_sr = CPU_SR_Save(0x20);
|
||||
cpu_sr |= 0xff00;
|
||||
}
|
||||
|
||||
static void unlock(fdb_db_t db)
|
||||
{
|
||||
cpu_sr &= 0xff;
|
||||
CPU_SR_Restore(cpu_sr);
|
||||
}
|
||||
|
||||
int flashdb_init(void)
|
||||
{
|
||||
fdb_err_t result;
|
||||
|
||||
/* 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", "FlashEnv", NULL, NULL);
|
||||
|
||||
if (result != FDB_NO_ERR) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
fdb_err_t flashdb_set(uint32_t key, uint8_t *value, uint32_t length)
|
||||
{
|
||||
struct fdb_blob blob;
|
||||
|
||||
return fdb_kv_set_blob(&kvdb, key, fdb_blob_make(&blob, value, length));
|
||||
}
|
||||
|
||||
size_t flashdb_get(uint32_t key, uint8_t *value, uint32_t length)
|
||||
{
|
||||
struct fdb_blob blob;
|
||||
|
||||
return fdb_kv_get_blob(&kvdb, key, fdb_blob_make(&blob, value, length));
|
||||
}
|
||||
|
||||
size_t flashdb_get_length(uint32_t key)
|
||||
{
|
||||
struct fdb_kv kv;
|
||||
|
||||
if (fdb_kv_get_obj(&kvdb, key, &kv) == NULL) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return kv.value_len;
|
||||
}
|
||||
}
|
||||
|
||||
fdb_err_t flashdb_del(uint32_t key)
|
||||
{
|
||||
return fdb_kv_del(&kvdb, key);
|
||||
}
|
||||
|
26
MCU/examples/common/flashdb/fdb_app.h
Normal file
26
MCU/examples/common/flashdb/fdb_app.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef _FAL_APP_H_
|
||||
#define _FAL_APP_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "fdb_def.h"
|
||||
|
||||
#define FDB_KEY_BT_LINKKEY 0x00010000 //!< bt link key storage index
|
||||
#define FDB_KEY_CONTROLLER_INFO 0x00010001 //!< controller information storage index
|
||||
#define FDB_KEY_BTDM_LIB_BASE 0x00020000 //!< base index used to store host data
|
||||
#define FDB_KEY_USER_BASE 0x00030000
|
||||
#define FDB_KEY_USER_RANDOM_SEED 0x00030001
|
||||
|
||||
int flashdb_init(void);
|
||||
|
||||
fdb_err_t flashdb_set(uint32_t key, uint8_t *value, uint32_t length);
|
||||
|
||||
size_t flashdb_get(uint32_t key, uint8_t *value, uint32_t length);
|
||||
|
||||
size_t flashdb_get_length(uint32_t key);
|
||||
|
||||
fdb_err_t flashdb_del(uint32_t key);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user