lfs_config.h 4.2 KB

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