hal_minifds.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #ifndef __HAL_MINIFDS_H
  2. #define __HAL_MINIFDS_H
  3. #include "system.h"
  4. #include "bsp_flash.h"
  5. #define USE_16BIT_KEY 1
  6. #define MAX_RECORD_LENGTH 0xff
  7. #if USE_16BIT_KEY
  8. #define key_t unsigned short
  9. #define length_t uint8_t
  10. #else
  11. #define length_t unsigned short
  12. #define key_t uint8_t
  13. #endif
  14. /**@brief The record metadata as stored in flash.
  15. * @warning Do not edit or reorder the fields in this structure.
  16. */
  17. typedef struct
  18. {
  19. key_t record_key; //!< The record key.
  20. uint8_t crc8; // < The
  21. length_t length; //!< The length of the record
  22. } minfds_record_t;
  23. /**@brief Functions provided by the API implementation. */
  24. typedef struct
  25. {
  26. /**@brief flash写函数,p_dst为目标地址,p_src为源地址,sizewords为要写入的字(32位数据)(不是字节)的长度,写入成功需要返回0,写入失败返回其他值*/
  27. flash_OPER_Result (*flash_write)(uint32_t *p_dst, uint32_t const *p_src, uint32_t sizewords);
  28. /**@brief page_addr为要擦除的扇区的首地址 擦除成功需要返回0,失败返回其他值*/
  29. flash_OPER_Result (*flash_page_erase)(uint32_t page_addr);
  30. } const flish_api_t;
  31. typedef struct
  32. {
  33. uint32_t start_addr;
  34. uint32_t end_addr;
  35. unsigned short record_count;
  36. uint32_t free_capacity;
  37. uint32_t duty_capacity;
  38. flish_api_t api;
  39. } minfds_t;
  40. /**@brief FDS return values.
  41. */
  42. enum
  43. {
  44. MINIFDS_SUCCESS = 0, //!< Error.
  45. MINIFDS_ERR_NOT_INITIALIZED, //!< Error. The module has not been initialized.
  46. MINIFDS_ERR_NOT_FREE_CAPACITY, //!< Error. The 空间不足>
  47. MINIFDS_ERR_NOT_FIND_RECORD, //!< Error. The 没有找到记录>
  48. MINIFDS_ERR_BUSY, //!< Error. The 忙>
  49. MINIFDS_ERR_API_FAIL, //!< Error. The 底层接口报错>
  50. MINIFDS_ERR_REPEAT_KEY, //!< Error. The key值错误,不能使用0>
  51. MINIFDS_ERR_HARDWARE, //!< Error. The flash被外部程序改变了>
  52. MINIFDS_ERR_PARAMETER, //!< Error. The 参数错误>
  53. MINIFDS_ERR_REPEAT_LENGTH,
  54. };
  55. //----------------------------------------------------------------
  56. /**@brief startaddr 和 endaddr 为扇区的首地址,
  57. */
  58. #define DEFINE_MINIINF(name, startaddr, endaddr, fun_write, fun_erase) \
  59. minfds_t name = {.start_addr = startaddr, \
  60. .end_addr = endaddr, \
  61. .record_count = 0, \
  62. .free_capacity = endaddr - startaddr, \
  63. .api.flash_write = fun_write, \
  64. .api.flash_page_erase = fun_erase};
  65. char minfds_record_add(minfds_t *p_minifds, key_t key, uint8_t *data, length_t length);
  66. char minfds_record_find(minfds_t *p_minifds, key_t key, uint8_t *data, length_t *length);
  67. char minfds_record_get_length(minfds_t *p_minifds, key_t key, length_t *length);
  68. char minfds_record_find_mem(minfds_t *p_minifds, uint8_t *mem, int mem_len,key_t * key);
  69. char minfds_record_update(minfds_t *p_minifds, key_t key, uint8_t *data, length_t length);
  70. char minfds_record_delete(minfds_t *p_minifds, key_t key);
  71. char minfds_record_delete_all(minfds_t *p_minifds);
  72. char minfds_init(minfds_t *p_minifds);
  73. char minfds_format(minfds_t *p_minifds);
  74. unsigned short minfds_record_count_get(minfds_t *p_minifds);
  75. char record_index(minfds_t *p_minifds,minfds_record_t *record,uint8_t * data,int index);
  76. //----------------------------------------------------------------
  77. char pm_add(minfds_t *p_minifds,const char * key,int key_len,uint8_t *data, const length_t length);
  78. char pm_find(minfds_t *p_minifds,const char * key,int key_len, uint8_t *data,length_t *length);
  79. char pm_get_length(minfds_t *p_minifds, const char * key,int key_len, length_t *length);
  80. char pm_update(minfds_t *p_minifds, const char * key,int key_len, uint8_t *data, length_t length);
  81. char pm_delete(minfds_t *p_minifds, const char * key,int key_len);
  82. #endif