TinyFlashDB:一种超轻量的可纠错的通用单片机Flash存储方案_e2prom flashdb

    printf("get ok, addr:%x, value:%x\n", addr, test_value);
}

}


## 三、TinyFlashDB API介绍



typedef struct _tfdb_index_struct{
tfdb_addr_t flash_addr;/* the start address of the flash block */
uint16_t flash_size;/* the size of the flash block */
uint8_t value_length;/* the length of value that saved in this flash block */
uint8_t end_byte; /* must different to TFDB_VALUE_AFTER_ERASE */
/* 0x00 is recommended for end_byte, because almost all flash is 0xff after erase. */
}tfdb_index_t;


结构体功能:在TinyFlashDB中,API的操作都需要指定的参数index,该index结构体中存储了flash的地址,flash的大小,存储的变量的长度,结束标志位。在读取flash扇区时会去校验此信息。



TFDB_Err_Code tfdb_get(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t addr_cache, void value_to);


* 函数功能:从index指向的扇区中获取一个index中指定变量长度的变量,flash头部数据校验出错不会重新初始化flash。
* 参数 index:tfdb操作的index指针。
* 参数 rw\_buffer:写入和读取的缓存,所有flash的操作最后都会将整理后的数据拷贝到该buffer中,再调用tfdb\_port\_write或者tfdb\_port\_read进行写入。当芯片对于写入的数据区缓存有特殊要求(例如4字节对齐,256字节对齐等),可以通过该参数将符合要求的变量指针传递给函数使用。至少为4字节长度。
* 参数 addr\_cache:可以是NULL,或者是地址缓存变量的指针,当addr\_cache不为NULL,并且也不为0时,则认为addr\_cache已经初始化成功,不再校验flash头部,直接从该addr\_cache的地址读取数据。
* 参数 value\_to:要存储数据内容的地址。
* 返回值:TFDB\_NO\_ERR成功,其他失败。



TFDB_Err_Code tfdb_set(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t addr_cache, void value_from);


* 函数功能:在index指向的扇区中写入一个index中指定变量长度的变量,flash头部数据校验出错重新初始化flash。
* 参数 index:tfdb操作的index指针。
* 参数 rw\_buffer:写入和读取的缓存,所有flash的操作最后都会将整理后的数据拷贝到该buffer中,再调用tfdb\_port\_write或者tfdb\_port\_read进行写入。当芯片对于写入的数据区缓存有特殊要求(例如4字节对齐,256字节对齐等),可以通过该参数将符合要求的变量指针传递给函数使用。至少为4字节长度。
* 参数 addr\_cache:可以是NULL,或者是地址缓存变量的指针,当addr\_cache不为NULL,并且也不为0时,则认为addr\_cache已经初始化成功,不再校验flash头部,直接从该addr\_cache的地址读取数据。
* 参数 value\_from:要存储的数据内容。
* 返回值:TFDB\_NO\_ERR成功,其他失败。


## 四、TinyFlashDB设计原理


观察上方代码,可以发现TinyFlashDB的操作都需要tfdb\_index\_t定义的index参数。


Flash初始化后头部信息为4字节,所以只支持1、2、4、8字节操作的flash:头部初始化时会读取头部,所以函数中rw\_buffer指向的数据第一要求至少为4字节,如果最小写入单位是8字节,则为第一要求最少为8字节。


第一字节第二字节第三字节第四字节和其他对齐字节




|  |  |  |  |
| --- | --- | --- | --- |
| flash\_size高8位字节 | flash\_size低8位字节 | value\_length | end\_byte |


数据存储时,会根据flash支持的字节操作进行对齐,所以函数中rw\_buffer指向的数据第二要求至少为下面函数中计算得出的aligned\_value\_size个字节:



aligned_value_size  = index->value_length + 2;/\* data + verify + end\_byte \*/

#if (TFDB_WRITE_UNIT_BYTES2)
/* aligned with TFDB_WRITE_UNIT_BYTES */
aligned_value_size = ((aligned_value_size + 1) & 0xfe);
#elif (TFDB_WRITE_UNIT_BYTES
4)
/* aligned with TFDB_WRITE_UNIT_BYTES */
aligned_value_size = ((aligned_value_size + 3) & 0xfc);
#elif (TFDB_WRITE_UNIT_BYTES==8)
/* aligned with TFDB_WRITE_UNIT_BYTES */
aligned_value_size = ((aligned_value_size + 7) & 0xf8);
#endif


前value\_length个字节第value\_length+1字节第value\_length+2字节其他对齐字节




|  |  |  |  |
| --- | --- | --- | --- |
| value\_from数据内容 | value\_from的和校验 | end\_byte | end\_byte |


每次写入后都会再读取出来进行校验,如果校验不通过,就会继续在下一个地址写入。指导达到最大写入次数(TFDB\_WRITE\_MAX\_RETRY)或者头部校验错误。


读取数据时也会计算和校验,不通过的话继续读取,直到返回校验通过的最新数据,或者读取失败。


## 五、TinyFlashDB移植和配置


移植使用只需要在tfdb\_port.c中,编写完成三个接口函数,也要在tfdb\_port.h中添加相应的头文件和根据不同芯片修改宏定义



TFDB_Err_Code tfdb_port_read(tfdb_addr_t addr, uint8_t *buf, size_t size);

TFDB_Err_Code tfdb_port_erase(tfdb_addr_t addr, size_t size);

TFDB_Err_Code tfdb_port_write(tfdb_addr_t addr, const uint8_t *buf, size_t size);


所有的配置项都在tfdb\_port.h中



/* use string.h or self functions */
#define TFDB_USE_STRING_H 1

#if TFDB_USE_STRING_H
#include “string.h”
#define tfdb_memcpy memcpy
#define tfdb_memcmp memcmp
#define TFDB_MEMCMP_SAME 0
#else
#define tfdb_memcpy
#define tfdb_memcmp
#define TFDB_MEMCMP_SAME
#endif

#define TFDB_DEBUG printf

/* The data value in flash after erased, most are 0xff, some flash maybe different. */
#define TFDB_VALUE_AFTER_ERASE 0xff

/* the flash write granularity, unit: byte
* only support 1(stm32f4)/ 2(CH559)/ 4(stm32f1)/ 8(stm32L4) */
#define TFDB_WRITE_UNIT_BYTES 8 /* @note you must define it for a value */

/* @note the max retry times when flash is error ,set 0 will disable retry count */
#define TFDB_WRITE_MAX_RETRY 32

/* must not use pointer type. Please use uint32_t, uint16_t or uint8_t. */
typedef uint32_t tfdb_addr_t;


## 六、移植到STM32单片机


项目地址:`https://github.com/smartmx/TFDB/tree/main```


使用一下命令克隆Demo,裸机移植例程



git clone -b raw https://github.com/smartmx/TFDB.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值