fal_def.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-17 armink the first version
  9. */
  10. #ifndef _FAL_DEF_H_
  11. #define _FAL_DEF_H_
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <rtthread.h>
  15. #define FAL_PRINTF rt_kprintf
  16. #define FAL_MALLOC rt_malloc
  17. #define FAL_CALLOC rt_calloc
  18. #define FAL_REALLOC rt_realloc
  19. #define FAL_FREE rt_free
  20. #ifndef FAL_DEBUG
  21. #define FAL_DEBUG 0
  22. #endif
  23. #if FAL_DEBUG
  24. #ifdef assert
  25. #undef assert
  26. #endif
  27. #define assert(EXPR) \
  28. if (!(EXPR)) \
  29. { \
  30. FAL_PRINTF("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \
  31. while (1); \
  32. }
  33. /* debug level log */
  34. #ifdef log_d
  35. #undef log_d
  36. #endif
  37. #define log_d(...) FAL_PRINTF("[D/FAL] (%s:%d) ", __FUNCTION__, __LINE__); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\n")
  38. #else
  39. #ifdef assert
  40. #undef assert
  41. #endif
  42. #define assert(EXPR) ((void)0);
  43. /* debug level log */
  44. #ifdef log_d
  45. #undef log_d
  46. #endif
  47. #define log_d(...)
  48. #endif /* FAL_DEBUG */
  49. /* error level log */
  50. #ifdef log_e
  51. #undef log_e
  52. #endif
  53. #define log_e(...) FAL_PRINTF("\033[31;22m[E/FAL] (%s:%d) ", __FUNCTION__, __LINE__);FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  54. /* info level log */
  55. #ifdef log_i
  56. #undef log_i
  57. #endif
  58. #define log_i(...) FAL_PRINTF("\033[32;22m[I/FAL] "); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  59. /* FAL flash and partition device name max length */
  60. #ifndef FAL_DEV_NAME_MAX
  61. #define FAL_DEV_NAME_MAX 24
  62. #endif
  63. #ifndef FAL_DEV_BLK_MAX
  64. #define FAL_DEV_BLK_MAX 6
  65. #endif
  66. struct flash_blk
  67. {
  68. size_t size;
  69. size_t count;
  70. };
  71. struct fal_flash_dev
  72. {
  73. char name[FAL_DEV_NAME_MAX];
  74. /* flash device start address and len */
  75. uint32_t addr;
  76. size_t len;
  77. /* the block size in the flash for erase minimum granularity */
  78. size_t blk_size;
  79. struct
  80. {
  81. int (*init)(void);
  82. int (*read)(long offset, uint8_t *buf, size_t size);
  83. int (*write)(long offset, const uint8_t *buf, size_t size);
  84. int (*erase)(long offset, size_t size);
  85. } ops;
  86. /* write minimum granularity, unit: bit.
  87. 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1)/ 64(stm32l4)
  88. 0 will not take effect. */
  89. size_t write_gran;
  90. struct flash_blk blocks[FAL_DEV_BLK_MAX];
  91. };
  92. typedef struct fal_flash_dev *fal_flash_dev_t;
  93. /**
  94. * FAL partition
  95. */
  96. struct fal_partition
  97. {
  98. uint32_t magic_word;
  99. /* partition name */
  100. char name[FAL_DEV_NAME_MAX];
  101. /* flash device name for partition */
  102. char flash_name[FAL_DEV_NAME_MAX];
  103. /* partition offset address on flash device */
  104. long offset;
  105. size_t len;
  106. uint32_t reserved;
  107. };
  108. typedef struct fal_partition *fal_partition_t;
  109. #endif /* _FAL_DEF_H_ */