lfs_config.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifndef _LFS_CONFIG_H_
  2. #define _LFS_CONFIG_H_
  3. #include <rtthread.h>
  4. // System includes
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <string.h>
  8. #include <inttypes.h>
  9. // Macros, may be replaced by system specific wrappers. Arguments to these
  10. // macros must not have side-effects as the macros can be removed for a smaller
  11. // code footprint
  12. // Logging functions
  13. #ifdef LFS_YES_TRACE
  14. #define LFS_TRACE(fmt, ...) \
  15. rt_kprintf("lfs_trace:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  16. #else
  17. #define LFS_TRACE(fmt, ...)
  18. #endif
  19. #ifndef LFS_NO_DEBUG
  20. #define LFS_DEBUG(fmt, ...) \
  21. rt_kprintf("lfs debug:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  22. #else
  23. #define LFS_DEBUG(fmt, ...)
  24. #endif
  25. #ifndef LFS_NO_WARN
  26. #define LFS_WARN(fmt, ...) \
  27. rt_kprintf("lfs warn:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  28. #else
  29. #define LFS_WARN(fmt, ...)
  30. #endif
  31. #ifndef LFS_NO_ERROR
  32. #define LFS_ERROR(fmt, ...) \
  33. rt_kprintf("lfs error:%d: " fmt "\n", __LINE__, __VA_ARGS__)
  34. #else
  35. #define LFS_ERROR(fmt, ...)
  36. #endif
  37. // Runtime assertions
  38. #ifndef LFS_NO_ASSERT
  39. #define LFS_ASSERT(test) RT_ASSERT(test)
  40. #else
  41. #define LFS_ASSERT(test)
  42. #endif
  43. // Builtin functions, these may be replaced by more efficient
  44. // toolchain-specific implementations. LFS_NO_INTRINSICS falls back to a more
  45. // expensive basic C implementation for debugging purposes
  46. // Min/max functions for unsigned 32-bit numbers
  47. static inline uint32_t lfs_max(uint32_t a, uint32_t b) {
  48. return (a > b) ? a : b;
  49. }
  50. static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
  51. return (a < b) ? a : b;
  52. }
  53. // Align to nearest multiple of a size
  54. static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
  55. return a - (a % alignment);
  56. }
  57. static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
  58. return lfs_aligndown(a + alignment-1, alignment);
  59. }
  60. // Find the next smallest power of 2 less than or equal to a
  61. static inline uint32_t lfs_npw2(uint32_t a) {
  62. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  63. return 32 - __builtin_clz(a-1);
  64. #else
  65. uint32_t r = 0;
  66. uint32_t s;
  67. a -= 1;
  68. s = (a > 0xffff) << 4; a >>= s; r |= s;
  69. s = (a > 0xff ) << 3; a >>= s; r |= s;
  70. s = (a > 0xf ) << 2; a >>= s; r |= s;
  71. s = (a > 0x3 ) << 1; a >>= s; r |= s;
  72. return (r | (a >> 1)) + 1;
  73. #endif
  74. }
  75. // Count the number of trailing binary zeros in a
  76. // lfs_ctz(0) may be undefined
  77. static inline uint32_t lfs_ctz(uint32_t a) {
  78. #if !defined(LFS_NO_INTRINSICS) && defined(__GNUC__)
  79. return __builtin_ctz(a);
  80. #else
  81. return lfs_npw2((a & -a) + 1) - 1;
  82. #endif
  83. }
  84. // Count the number of binary ones in a
  85. static inline uint32_t lfs_popc(uint32_t a) {
  86. #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
  87. return __builtin_popcount(a);
  88. #else
  89. a = a - ((a >> 1) & 0x55555555);
  90. a = (a & 0x33333333) + ((a >> 2) & 0x33333333);
  91. return (((a + (a >> 4)) & 0xf0f0f0f) * 0x1010101) >> 24;
  92. #endif
  93. }
  94. // Find the sequence comparison of a and b, this is the distance
  95. // between a and b ignoring overflow
  96. static inline int lfs_scmp(uint32_t a, uint32_t b) {
  97. return (int)(unsigned)(a - b);
  98. }
  99. // Convert between 32-bit little-endian and native order
  100. static inline uint32_t lfs_fromle32(uint32_t a) {
  101. #if !defined(LFS_NO_INTRINSICS) && ( \
  102. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  103. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  104. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  105. return a;
  106. #elif !defined(LFS_NO_INTRINSICS) && ( \
  107. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  108. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  109. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  110. return __builtin_bswap32(a);
  111. #else
  112. return (((uint8_t*)&a)[0] << 0) |
  113. (((uint8_t*)&a)[1] << 8) |
  114. (((uint8_t*)&a)[2] << 16) |
  115. (((uint8_t*)&a)[3] << 24);
  116. #endif
  117. }
  118. static inline uint32_t lfs_tole32(uint32_t a) {
  119. return lfs_fromle32(a);
  120. }
  121. // Convert between 32-bit big-endian and native order
  122. static inline uint32_t lfs_frombe32(uint32_t a) {
  123. #if !defined(LFS_NO_INTRINSICS) && ( \
  124. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  125. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  126. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  127. return __builtin_bswap32(a);
  128. #elif !defined(LFS_NO_INTRINSICS) && ( \
  129. (defined( BYTE_ORDER ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  130. (defined(__BYTE_ORDER ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  131. (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
  132. return a;
  133. #else
  134. return (((uint8_t*)&a)[0] << 24) |
  135. (((uint8_t*)&a)[1] << 16) |
  136. (((uint8_t*)&a)[2] << 8) |
  137. (((uint8_t*)&a)[3] << 0);
  138. #endif
  139. }
  140. static inline uint32_t lfs_tobe32(uint32_t a) {
  141. return lfs_frombe32(a);
  142. }
  143. // Calculate CRC-32 with polynomial = 0x04c11db7
  144. uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
  145. // Allocate memory, only used if buffers are not provided to littlefs
  146. // Note, memory must be 64-bit aligned
  147. static inline void *lfs_malloc(size_t size) {
  148. #ifndef LFS_NO_MALLOC
  149. return rt_malloc(size);
  150. #else
  151. (void)size;
  152. return NULL;
  153. #endif
  154. }
  155. // Deallocate memory, only used if buffers are not provided to littlefs
  156. static inline void lfs_free(void *p) {
  157. #ifndef LFS_NO_MALLOC
  158. rt_free(p);
  159. #else
  160. (void)p;
  161. #endif
  162. }
  163. #endif