Merge pull request #200 from eternal-echo/tc397

支持tc397擦除后为0的flash
This commit is contained in:
朱天龙 (Armink) 2023-02-17 14:38:18 +08:00 committed by GitHub
commit 043af70d5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 12 deletions

View File

@ -21,6 +21,11 @@
#define FDB_STATUS_TABLE_SIZE(status_number) (((status_number - 1) * FDB_WRITE_GRAN + 7)/8)
#endif
/* the data is erased */
#define FDB_BYTE_ERASED 0xFF
/* the data is written */
#define FDB_BYTE_WRITTEN 0x00
/* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
* would return 16.
*/
@ -39,7 +44,11 @@
#define FDB_DIRTY_STATUS_TABLE_SIZE FDB_STATUS_TABLE_SIZE(FDB_SECTOR_DIRTY_STATUS_NUM)
/* the data is unused */
#define FDB_DATA_UNUSED 0xFFFFFFFF
#if (FDB_BYTE_ERASED == 0xFF)
#define FDB_DATA_UNUSED 0xFFFFFFFF
#else
#define FDB_DATA_UNUSED 0x00000000
#endif
fdb_err_t _fdb_kv_load(fdb_kvdb_t db);
size_t _fdb_set_status(uint8_t status_table[], size_t status_num, size_t status_index);

View File

@ -56,7 +56,11 @@
#endif
/* the sector is not combined value */
#if (FDB_BYTE_ERASED == 0xFF)
#define SECTOR_NOT_COMBINED 0xFFFFFFFF
#else
#define SECTOR_NOT_COMBINED 0x00000000
#endif
/* the next address is get failed */
#define FAILED_ADDR 0xFFFFFFFF
@ -721,12 +725,12 @@ static fdb_err_t format_sector(fdb_kvdb_t db, uint32_t addr, uint32_t combined_v
result = _fdb_flash_erase((fdb_db_t)db, addr, db_sec_size(db));
if (result == FDB_NO_ERR) {
/* initialize the header data */
memset(&sec_hdr, 0xFF, SECTOR_HDR_DATA_SIZE);
memset(&sec_hdr, FDB_BYTE_ERASED, SECTOR_HDR_DATA_SIZE);
_fdb_set_status(sec_hdr.status_table.store, FDB_SECTOR_STORE_STATUS_NUM, FDB_SECTOR_STORE_EMPTY);
_fdb_set_status(sec_hdr.status_table.dirty, FDB_SECTOR_DIRTY_STATUS_NUM, FDB_SECTOR_DIRTY_FALSE);
sec_hdr.magic = SECTOR_MAGIC_WORD;
sec_hdr.combined = combined_value;
sec_hdr.reserved = 0xFFFFFFFF;
sec_hdr.reserved = FDB_DATA_UNUSED;
/* save the header */
result = _fdb_flash_write((fdb_db_t)db, addr, (uint32_t *)&sec_hdr, SECTOR_HDR_DATA_SIZE, true);
@ -1070,7 +1074,7 @@ static fdb_err_t align_write(fdb_kvdb_t db, uint32_t addr, const uint32_t *buf,
size_t align_data_size = 1;
#endif
memset(align_data, 0xFF, align_data_size);
memset(align_data, FDB_BYTE_ERASED, align_data_size);
result = _fdb_flash_write((fdb_db_t)db, addr, buf, FDB_WG_ALIGN_DOWN(size), false);
align_remain = size - FDB_WG_ALIGN_DOWN(size);
@ -1094,7 +1098,7 @@ static fdb_err_t create_kv_blob(fdb_kvdb_t db, kv_sec_info_t sector, const char
return FDB_KV_NAME_ERR;
}
memset(&kv_hdr, 0xFF, KV_HDR_DATA_SIZE);
memset(&kv_hdr, FDB_BYTE_ERASED, KV_HDR_DATA_SIZE);
kv_hdr.magic = KV_MAGIC_WORD;
kv_hdr.name_len = strlen(key);
kv_hdr.value_len = len;
@ -1112,7 +1116,7 @@ static fdb_err_t create_kv_blob(fdb_kvdb_t db, kv_sec_info_t sector, const char
result = update_sec_status(db, sector, kv_hdr.len, &is_full);
}
if (result == FDB_NO_ERR) {
uint8_t ff = 0xFF;
uint8_t ff = FDB_BYTE_ERASED;
/* start calculate CRC32 */
kv_hdr.crc32 = fdb_calc_crc32(0, &kv_hdr.name_len, KV_HDR_DATA_SIZE - KV_NAME_LEN_OFFSET);
kv_hdr.crc32 = fdb_calc_crc32(kv_hdr.crc32, key, kv_hdr.name_len);

View File

@ -98,14 +98,18 @@ size_t _fdb_set_status(uint8_t status_table[], size_t status_num, size_t status_
* | 8bit | 0xFFFF | 0x00FF | 0x0000 |
* | 32bit | 0xFFFFFFFF FFFFFFFF | 0x00FFFFFF FFFFFFFF | 0x00FFFFFF 00FFFFFF |
*/
memset(status_table, 0xFF, FDB_STATUS_TABLE_SIZE(status_num));
memset(status_table, FDB_BYTE_ERASED, FDB_STATUS_TABLE_SIZE(status_num));
if (status_index > 0) {
#if (FDB_WRITE_GRAN == 1)
byte_index = (status_index - 1) / 8;
#if (FDB_BYTE_ERASED == 0xFF)
status_table[byte_index] &= (0x00ff >> (status_index % 8));
#else
status_table[byte_index] |= (0x00ff >> (status_index % 8));
#endif
#else
byte_index = (status_index - 1) * (FDB_WRITE_GRAN / 8);
status_table[byte_index] = 0x00;
status_table[byte_index] = FDB_BYTE_WRITTEN;
#endif /* FDB_WRITE_GRAN == 1 */
}
@ -123,7 +127,7 @@ size_t _fdb_get_status(uint8_t status_table[], size_t status_num)
break;
}
#else /* (FDB_WRITE_GRAN == 8) || (FDB_WRITE_GRAN == 32) || (FDB_WRITE_GRAN == 64) */
if (status_table[status_num * FDB_WRITE_GRAN / 8] == 0x00) {
if (status_table[status_num * FDB_WRITE_GRAN / 8] == FDB_BYTE_WRITTEN) {
break;
}
#endif /* FDB_WRITE_GRAN == 1 */
@ -173,7 +177,7 @@ size_t _fdb_read_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size
*/
uint32_t _fdb_continue_ff_addr(fdb_db_t db, uint32_t start, uint32_t end)
{
uint8_t buf[32], last_data = 0x00;
uint8_t buf[32], last_data = FDB_BYTE_WRITTEN;
size_t i, addr = start, read_size;
for (; start < end; start += sizeof(buf)) {
@ -184,14 +188,14 @@ uint32_t _fdb_continue_ff_addr(fdb_db_t db, uint32_t start, uint32_t end)
}
_fdb_flash_read(db, start, (uint32_t *) buf, read_size);
for (i = 0; i < read_size; i++) {
if (last_data != 0xFF && buf[i] == 0xFF) {
if (last_data != FDB_BYTE_ERASED && buf[i] == FDB_BYTE_ERASED) {
addr = start + i;
}
last_data = buf[i];
}
}
if (last_data == 0xFF) {
if (last_data == FDB_BYTE_ERASED) {
return FDB_WG_ALIGN(addr);
} else {
return end;