fal_def.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #define FAL_SW_VERSION "0.5.99"
  15. #ifndef FAL_MALLOC
  16. #define FAL_MALLOC malloc
  17. #endif
  18. #ifndef FAL_CALLOC
  19. #define FAL_CALLOC calloc
  20. #endif
  21. #ifndef FAL_REALLOC
  22. #define FAL_REALLOC realloc
  23. #endif
  24. #ifndef FAL_FREE
  25. #define FAL_FREE free
  26. #endif
  27. #ifndef FAL_DEBUG
  28. #define FAL_DEBUG 0
  29. #endif
  30. #ifndef FAL_PRINTF
  31. #ifdef RT_VER_NUM
  32. /* for RT-Thread platform */
  33. extern void rt_kprintf(const char *fmt, ...);
  34. #define FAL_PRINTF rt_kprintf
  35. #else
  36. #define FAL_PRINTF printf
  37. #endif /* RT_VER_NUM */
  38. #endif /* FAL_PRINTF */
  39. #if FAL_DEBUG
  40. #ifdef assert
  41. #undef assert
  42. #endif
  43. #define assert(EXPR) \
  44. if (!(EXPR)) \
  45. { \
  46. FAL_PRINTF("(%s) app fal_def.h has assert failed at %s.\n", #EXPR, __FUNCTION__); \
  47. return 1; \
  48. }
  49. /* debug level log */
  50. #ifdef log_d
  51. #undef log_d
  52. #endif
  53. #define log_d(...) FAL_PRINTF("[D/FAL] (%s:%d) ", __FUNCTION__, __LINE__); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\n")
  54. #else
  55. #ifdef assert
  56. #undef assert
  57. #endif
  58. #define assert(EXPR) ((void)0);
  59. /* debug level log */
  60. #ifdef log_d
  61. #undef log_d
  62. #endif
  63. #define log_d(...)
  64. #endif /* FAL_DEBUG */
  65. /* error level log */
  66. #ifdef log_e
  67. #undef log_e
  68. #endif
  69. #define log_e(...) FAL_PRINTF("\033[31;22m[E/FAL] (%s:%d) ", __FUNCTION__, __LINE__);FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  70. /* info level log */
  71. #ifdef log_i
  72. #undef log_i
  73. #endif
  74. #define log_i(...) FAL_PRINTF("\033[32;22m[I/FAL] "); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  75. /* FAL flash and partition device name max length */
  76. #ifndef FAL_DEV_NAME_MAX
  77. #define FAL_DEV_NAME_MAX 24
  78. #endif
  79. struct fal_flash_dev
  80. {
  81. char name[FAL_DEV_NAME_MAX];
  82. /* flash device start address and len */
  83. uint32_t addr;
  84. size_t len;
  85. /* the block size in the flash for erase minimum granularity */
  86. size_t blk_size;
  87. struct
  88. {
  89. int (*init)(void);
  90. int (*read)(long offset, uint8_t *buf, size_t size);
  91. int (*write)(long offset, const uint8_t *buf, size_t size);
  92. int (*erase)(long offset, size_t size);
  93. } ops;
  94. /* write minimum granularity, unit: bit.
  95. 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1)/ 64(stm32l4)
  96. 0 will not take effect. */
  97. size_t write_gran;
  98. };
  99. typedef struct fal_flash_dev *fal_flash_dev_t;
  100. /**
  101. * FAL partition
  102. */
  103. struct fal_partition
  104. {
  105. uint32_t magic_word;
  106. /* partition name */
  107. char name[FAL_DEV_NAME_MAX];
  108. /* flash device name for partition */
  109. char flash_name[FAL_DEV_NAME_MAX];
  110. /* partition offset address on flash device */
  111. long offset;
  112. size_t len;
  113. uint32_t reserved;
  114. };
  115. typedef struct fal_partition *fal_partition_t;
  116. #endif /* _FAL_DEF_H_ */