fal_def.h 3.4 KB

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