lfs_config.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include <furi.h>
  3. #ifdef FURI_NDEBUG
  4. #define LFS_NO_ASSERT
  5. #define LFS_ASSERT(x)
  6. #else
  7. #define LFS_ASSERT furi_assert
  8. #endif
  9. #define LFS_TAG "Lfs"
  10. #define LFS_TRACE(...) FURI_LOG_T(LFS_TAG, __VA_ARGS__);
  11. #define LFS_DEBUG(...) FURI_LOG_D(LFS_TAG, __VA_ARGS__);
  12. #define LFS_WARN(...) FURI_LOG_W(LFS_TAG, __VA_ARGS__);
  13. #define LFS_ERROR(...) FURI_LOG_E(LFS_TAG, __VA_ARGS__);
  14. // Because crc
  15. #undef LFS_CONFIG
  16. // System includes
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <inttypes.h>
  21. #ifndef LFS_NO_MALLOC
  22. #include <stdlib.h>
  23. #endif
  24. #ifndef LFS_NO_ASSERT
  25. #include <assert.h>
  26. #endif
  27. #if !defined(LFS_NO_DEBUG) || \
  28. !defined(LFS_NO_WARN) || \
  29. !defined(LFS_NO_ERROR) || \
  30. defined(LFS_YES_TRACE)
  31. #include <stdio.h>
  32. #endif
  33. #ifdef __cplusplus
  34. extern "C"
  35. {
  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 smallest power of 2 greater 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 ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  97. (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  98. (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  99. return a;
  100. #elif !defined(LFS_NO_INTRINSICS) && ( \
  101. (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  102. (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  103. (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __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 ) && defined( ORDER_LITTLE_ENDIAN ) && BYTE_ORDER == ORDER_LITTLE_ENDIAN ) || \
  119. (defined(__BYTE_ORDER ) && defined(__ORDER_LITTLE_ENDIAN ) && __BYTE_ORDER == __ORDER_LITTLE_ENDIAN ) || \
  120. (defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
  121. return __builtin_bswap32(a);
  122. #elif !defined(LFS_NO_INTRINSICS) && ( \
  123. (defined( BYTE_ORDER ) && defined( ORDER_BIG_ENDIAN ) && BYTE_ORDER == ORDER_BIG_ENDIAN ) || \
  124. (defined(__BYTE_ORDER ) && defined(__ORDER_BIG_ENDIAN ) && __BYTE_ORDER == __ORDER_BIG_ENDIAN ) || \
  125. (defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && __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 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. free(p);
  153. #else
  154. (void)p;
  155. #endif
  156. }
  157. #ifdef __cplusplus
  158. } /* extern "C" */
  159. #endif