fdb_low_lvl.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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) \
  16. ((status_number * FDB_WRITE_GRAN + 7) / 8)
  17. #else
  18. #define FDB_STATUS_TABLE_SIZE(status_number) \
  19. (((status_number - 1) * FDB_WRITE_GRAN + 7) / 8)
  20. #endif
  21. /* the data is erased */
  22. #define FDB_BYTE_ERASED 0xFF
  23. /* the data is written */
  24. #define FDB_BYTE_WRITTEN 0x00
  25. /* Return the most contiguous size aligned at specified width. RT_ALIGN(13, 4)
  26. * would return 16.
  27. */
  28. /* align by write granularity */
  29. #define FDB_WG_ALIGN(size) (FDB_ALIGN(size, (FDB_WRITE_GRAN + 7) / 8))
  30. #define FDB_ALIGN align_by // fix ALIGN issue on 64bit platform
  31. /**
  32. * Return the down number of aligned at specified width. RT_ALIGN_DOWN(13, 4)
  33. * would return 12.
  34. */
  35. #define FDB_ALIGN_DOWN(size, align) ((size) & ~((align)-1))
  36. /* align down by write granularity */
  37. #define FDB_WG_ALIGN_DOWN(size) (FDB_ALIGN_DOWN(size, (FDB_WRITE_GRAN + 7) / 8))
  38. #define FDB_STORE_STATUS_TABLE_SIZE \
  39. FDB_STATUS_TABLE_SIZE(FDB_SECTOR_STORE_STATUS_NUM)
  40. #define FDB_DIRTY_STATUS_TABLE_SIZE \
  41. FDB_STATUS_TABLE_SIZE(FDB_SECTOR_DIRTY_STATUS_NUM)
  42. /* the data is unused */
  43. #if (FDB_BYTE_ERASED == 0xFF)
  44. #define FDB_DATA_UNUSED 0xFFFFFFFF
  45. #else
  46. #define FDB_DATA_UNUSED 0x00000000
  47. #endif
  48. fdb_err_t _fdb_kv_load(fdb_kvdb_t db);
  49. size_t _fdb_set_status(uint8_t status_table[],
  50. size_t status_num,
  51. size_t status_index);
  52. size_t _fdb_get_status(uint8_t status_table[], size_t status_num);
  53. uint32_t _fdb_continue_ff_addr(fdb_db_t db, uint32_t start, uint32_t end);
  54. fdb_err_t _fdb_init_ex(fdb_db_t db,
  55. const char* name,
  56. const char* part_name,
  57. fdb_db_type type,
  58. void* user_data);
  59. void _fdb_init_finish(fdb_db_t db, fdb_err_t result);
  60. void _fdb_deinit(fdb_db_t db);
  61. const char* _fdb_db_path(fdb_db_t db);
  62. fdb_err_t _fdb_write_status(fdb_db_t db,
  63. uint32_t addr,
  64. uint8_t status_table[],
  65. size_t status_num,
  66. size_t status_index,
  67. pika_bool sync);
  68. size_t _fdb_read_status(fdb_db_t db,
  69. uint32_t addr,
  70. uint8_t status_table[],
  71. size_t total_num);
  72. fdb_err_t _fdb_flash_read(fdb_db_t db, uint32_t addr, void* buf, size_t size);
  73. fdb_err_t _fdb_flash_erase(fdb_db_t db, uint32_t addr, size_t size);
  74. fdb_err_t _fdb_flash_write(fdb_db_t db,
  75. uint32_t addr,
  76. const void* buf,
  77. size_t size,
  78. pika_bool sync);
  79. #endif /* _FDB_LOW_LVL_H_ */