fal_def.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #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, __func__ ); \
  49. while (1); \
  50. }
  51. /* debug level log */
  52. #ifdef log_d
  53. #undef log_d
  54. #endif
  55. #include <inttypes.h>
  56. #define log_d(...) FAL_PRINTF("[D/FAL] (%s:%" PRIdLEAST16 ") ", __func__, __LINE__); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\n")
  57. #else
  58. #ifdef assert
  59. #undef assert
  60. #endif
  61. #define assert(EXPR) ((void)0);
  62. /* debug level log */
  63. #ifdef log_d
  64. #undef log_d
  65. #endif
  66. #define log_d(...)
  67. #endif /* FAL_DEBUG */
  68. /* error level log */
  69. #ifdef log_e
  70. #undef log_e
  71. #endif
  72. #define log_e(...) FAL_PRINTF("\033[31;22m[E/FAL] (%s:%d) ", __func__, __LINE__);FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  73. /* info level log */
  74. #ifdef log_i
  75. #undef log_i
  76. #endif
  77. #define log_i(...) FAL_PRINTF("\033[32;22m[I/FAL] "); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  78. /* FAL flash and partition device name max length */
  79. #ifndef FAL_DEV_NAME_MAX
  80. #define FAL_DEV_NAME_MAX 24
  81. #endif
  82. struct fal_flash_dev
  83. {
  84. char name[FAL_DEV_NAME_MAX];
  85. /* flash device start address and len */
  86. uint32_t addr;
  87. size_t len;
  88. /* the block size in the flash for erase minimum granularity */
  89. size_t blk_size;
  90. struct
  91. {
  92. int (*init)(void);
  93. int (*read)(long offset, uint8_t *buf, size_t size);
  94. int (*write)(long offset, const uint8_t *buf, size_t size);
  95. int (*erase)(long offset, size_t size);
  96. } ops;
  97. /* write minimum granularity, unit: bit.
  98. 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1)/ 64(stm32l4)
  99. 0 will not take effect. */
  100. size_t write_gran;
  101. };
  102. typedef struct fal_flash_dev *fal_flash_dev_t;
  103. /**
  104. * FAL partition
  105. */
  106. struct fal_partition
  107. {
  108. uint32_t magic_word;
  109. /* partition name */
  110. char name[FAL_DEV_NAME_MAX];
  111. /* flash device name for partition */
  112. char flash_name[FAL_DEV_NAME_MAX];
  113. /* partition offset address on flash device */
  114. long offset;
  115. size_t len;
  116. uint32_t reserved;
  117. };
  118. typedef struct fal_partition *fal_partition_t;
  119. #endif /* _FAL_DEF_H_ */