fal_def.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * File : fal_def.h
  3. * This file is part of FAL (Flash Abstraction Layer) package
  4. * COPYRIGHT (C) 2006 - 2019, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-05-17 armink the first version
  23. */
  24. #ifndef _FAL_DEF_H_
  25. #define _FAL_DEF_H_
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #define FAL_SW_VERSION "0.5.0"
  29. #ifndef FAL_MALLOC
  30. #define FAL_MALLOC malloc
  31. #endif
  32. #ifndef FAL_CALLOC
  33. #define FAL_CALLOC calloc
  34. #endif
  35. #ifndef FAL_REALLOC
  36. #define FAL_REALLOC realloc
  37. #endif
  38. #ifndef FAL_FREE
  39. #define FAL_FREE free
  40. #endif
  41. #ifndef FAL_DEBUG
  42. #define FAL_DEBUG 0
  43. #endif
  44. #ifndef FAL_PRINTF
  45. #ifdef RT_VER_NUM
  46. /* for RT-Thread platform */
  47. extern void rt_kprintf(const char *fmt, ...);
  48. #define FAL_PRINTF rt_kprintf
  49. #else
  50. #define FAL_PRINTF printf
  51. #endif /* RT_VER_NUM */
  52. #endif /* FAL_PRINTF */
  53. #if FAL_DEBUG
  54. #ifdef assert
  55. #undef assert
  56. #endif
  57. #define assert(EXPR) \
  58. if (!(EXPR)) \
  59. { \
  60. FAL_PRINTF("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \
  61. while (1); \
  62. }
  63. /* debug level log */
  64. #ifdef log_d
  65. #undef log_d
  66. #endif
  67. #define log_d(...) FAL_PRINTF("[D/FAL] (%s:%d) ", __FUNCTION__, __LINE__); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\n")
  68. #else
  69. #ifdef assert
  70. #undef assert
  71. #endif
  72. #define assert(EXPR) ((void)0);
  73. /* debug level log */
  74. #ifdef log_d
  75. #undef log_d
  76. #endif
  77. #define log_d(...)
  78. #endif /* FAL_DEBUG */
  79. /* error level log */
  80. #ifdef log_e
  81. #undef log_e
  82. #endif
  83. #define log_e(...) FAL_PRINTF("\033[31;22m[E/FAL] (%s:%d) ", __FUNCTION__, __LINE__);FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  84. /* info level log */
  85. #ifdef log_i
  86. #undef log_i
  87. #endif
  88. #define log_i(...) FAL_PRINTF("\033[32;22m[I/FAL] "); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n")
  89. /* FAL flash and partition device name max length */
  90. #ifndef FAL_DEV_NAME_MAX
  91. #define FAL_DEV_NAME_MAX 24
  92. #endif
  93. struct fal_flash_dev
  94. {
  95. char name[FAL_DEV_NAME_MAX];
  96. /* flash device start address and len */
  97. uint32_t addr;
  98. size_t len;
  99. /* the block size in the flash for erase minimum granularity */
  100. size_t blk_size;
  101. struct
  102. {
  103. int (*init)(void);
  104. int (*read)(long offset, uint8_t *buf, size_t size);
  105. int (*write)(long offset, const uint8_t *buf, size_t size);
  106. int (*erase)(long offset, size_t size);
  107. } ops;
  108. /* write minimum granularity, unit: bit.
  109. 1(nor flash)/ 8(stm32f4)/ 32(stm32f1)/ 64(stm32l4)
  110. 0 will not take effect. */
  111. size_t write_gran;
  112. };
  113. typedef struct fal_flash_dev *fal_flash_dev_t;
  114. /**
  115. * FAL partition
  116. */
  117. struct fal_partition
  118. {
  119. uint32_t magic_word;
  120. /* partition name */
  121. char name[FAL_DEV_NAME_MAX];
  122. /* flash device name for partition */
  123. char flash_name[FAL_DEV_NAME_MAX];
  124. /* partition offset address on flash device */
  125. long offset;
  126. size_t len;
  127. uint32_t reserved;
  128. };
  129. typedef struct fal_partition *fal_partition_t;
  130. #endif /* _FAL_DEF_H_ */