lfs_config.h 5.5 KB

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