lfs_config.h 5.6 KB

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