tinyflashdb.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022-2023, smartmx - smartmx@qq.com
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-03 smartmx the first version
  9. * 2022-02-08 smartmx fix bugs
  10. * 2022-02-12 smartmx fix bugs, add support for 2 byte write flash
  11. * 2022-03-15 smartmx fix bugs, add support for stm32l4 flash
  12. * 2022-08-02 smartmx add TFDB_VALUE_AFTER_ERASE_SIZE option
  13. * 2023-02-22 smartmx add dual flash index function
  14. * 2023-11-07 smartmx add TFDB_LOG macro.
  15. *
  16. */
  17. #ifndef _TINY_FLASH_DB_H_
  18. #define _TINY_FLASH_DB_H_
  19. #include "tfdb_port.h"
  20. #define TFDB_VERSION "0.0.7"
  21. #define TFDB_MAX(A, B) (((A) > (B)) ? (A) : (B))
  22. #if TFDB_WRITE_UNIT_BYTES <= 4
  23. #define TFDB_ALIGNED_RW_BUFFER_SIZE(VALUE_LENGTH, ALIGNED_SIZE) (TFDB_MAX(VALUE_LENGTH + 1 + ALIGNED_SIZE, 4) / (ALIGNED_SIZE))
  24. #define TFDB_DUAL_ALIGNED_RW_BUFFER_SIZE(VALUE_LENGTH, ALIGNED_SIZE) (TFDB_MAX(VALUE_LENGTH + 3 + ALIGNED_SIZE, 4) / (ALIGNED_SIZE))
  25. #else
  26. #define TFDB_ALIGNED_RW_BUFFER_SIZE(VALUE_LENGTH, ALIGNED_SIZE) (TFDB_MAX(VALUE_LENGTH + 1 + ALIGNED_SIZE, 8) / (ALIGNED_SIZE))
  27. #define TFDB_DUAL_ALIGNED_RW_BUFFER_SIZE(VALUE_LENGTH, ALIGNED_SIZE) (TFDB_MAX(VALUE_LENGTH + 3 + ALIGNED_SIZE, 8) / (ALIGNED_SIZE))
  28. #endif /* TFDB_WRITE_UNIT_BYTES < 8 */
  29. #define TFDB_DUAL_VALUE_LENGTH(VALUE_LENGTH) (VALUE_LENGTH + 2)
  30. typedef struct _tfdb_index_struct
  31. {
  32. tfdb_addr_t flash_addr; /* the start address of the flash block */
  33. uint16_t flash_size; /* the size of the flash block */
  34. uint8_t value_length; /* the length of value that saved in this flash block */
  35. uint8_t end_byte; /* must different to TFDB_VALUE_AFTER_ERASE */
  36. /* 0x00 is recommended for end_byte, because almost all flash is 0xff after erase. */
  37. } tfdb_index_t;
  38. extern TFDB_Err_Code tfdb_get(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t *addr_cache, void *value_to);
  39. extern TFDB_Err_Code tfdb_get_pre(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t *addr_cache, tfdb_addr_t *pre_addr_cache, void *value_to);
  40. extern TFDB_Err_Code tfdb_set(const tfdb_index_t *index, uint8_t *rw_buffer, tfdb_addr_t *addr_cache, void *value_from);
  41. typedef struct _tfdb_dual_index_struct
  42. {
  43. tfdb_index_t indexes[2];
  44. } tfdb_dual_index_t;
  45. typedef struct _tfdb_dual_cache_struct
  46. {
  47. tfdb_addr_t addr_cache[2];
  48. uint16_t seq[2];
  49. } tfdb_dual_cache_t;
  50. extern TFDB_Err_Code tfdb_dual_get(const tfdb_dual_index_t *index, uint8_t *rw_buffer, uint8_t *rw_buffer_bak, tfdb_dual_cache_t *cache, void *value_to);
  51. extern TFDB_Err_Code tfdb_dual_set(const tfdb_dual_index_t *index, uint8_t *rw_buffer, uint8_t *rw_buffer_bak, tfdb_dual_cache_t *cache, void *value_from);
  52. #endif