fdb_low_lvl.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2020, Armink, <armink.ztl@gmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief low level API and definition
  9. */
  10. #ifndef _FDB_LOW_LVL_H_
  11. #define _FDB_LOW_LVL_H_
  12. #include <fdb_cfg.h>
  13. #include <fdb_def.h>
  14. #if (FDB_WRITE_GRAN == 1)
  15. #define FDB_STATUS_TABLE_SIZE(status_number) ((status_number * FDB_WRITE_GRAN + 7)/8)
  16. #else
  17. #define FDB_STATUS_TABLE_SIZE(status_number) (((status_number - 1) * FDB_WRITE_GRAN + 7)/8)
  18. #endif
  19. /* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  20. * would return 16.
  21. */
  22. #define FDB_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
  23. /* align by write granularity */
  24. #define FDB_WG_ALIGN(size) (FDB_ALIGN(size, (FDB_WRITE_GRAN + 7)/8))
  25. /**
  26. * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
  27. * would return 12.
  28. */
  29. #define FDB_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
  30. /* align down by write granularity */
  31. #define FDB_WG_ALIGN_DOWN(size) (FDB_ALIGN_DOWN(size, (FDB_WRITE_GRAN + 7)/8))
  32. #define FDB_STORE_STATUS_TABLE_SIZE FDB_STATUS_TABLE_SIZE(FDB_SECTOR_STORE_STATUS_NUM)
  33. #define FDB_DIRTY_STATUS_TABLE_SIZE FDB_STATUS_TABLE_SIZE(FDB_SECTOR_DIRTY_STATUS_NUM)
  34. /* the data is unused */
  35. #define FDB_DATA_UNUSED 0xFFFFFFFF
  36. fdb_err_t _fdb_kv_load(fdb_kvdb_t db);
  37. size_t _fdb_set_status(uint8_t status_table[], size_t status_num, size_t status_index);
  38. size_t _fdb_get_status(uint8_t status_table[], size_t status_num);
  39. uint32_t _fdb_continue_ff_addr(fdb_db_t db, uint32_t start, uint32_t end);
  40. fdb_err_t _fdb_init_ex(fdb_db_t db, const char *name, const char *part_name, fdb_db_type type, void *user_data);
  41. void _fdb_init_finish(fdb_db_t db, fdb_err_t result);
  42. void _fdb_deinit(fdb_db_t db);
  43. fdb_err_t _fdb_write_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t status_num, size_t status_index, bool sync);
  44. size_t _fdb_read_status(fdb_db_t db, uint32_t addr, uint8_t status_table[], size_t total_num);
  45. fdb_err_t _fdb_flash_read(fdb_db_t db, uint32_t addr, void *buf, size_t size);
  46. fdb_err_t _fdb_flash_erase(fdb_db_t db, uint32_t addr, size_t size);
  47. fdb_err_t _fdb_flash_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync);
  48. #endif /* _FDB_LOW_LVL_H_ */