Browse Source

Adapt to the LFS2.0

xieyangrun 6 years ago
parent
commit
1b6c9ef2e6
3 changed files with 65 additions and 17 deletions
  1. 20 11
      dfs_lfs.c
  2. 40 3
      lfs_config.h
  3. 5 3
      lfs_crc.c

+ 20 - 11
dfs_lfs.c

@@ -29,8 +29,16 @@
     #define LFS_BLOCK_SIZE 4096
 #endif
 
-#ifndef LFS_LOOKAHEAD
-    #define LFS_LOOKAHEAD 512
+#ifndef LFS_CACHE_SIZE
+    #define LFS_CACHE_SIZE LFS_PROG_SIZE
+#endif
+
+#ifndef LFS_BLOCK_CYCLES
+    #define LFS_BLOCK_CYCLES 0
+#endif
+
+#ifndef LFS_LOOKAHEAD_MAX
+    #define LFS_LOOKAHEAD_MAX 128
 #endif
 
 typedef struct _dfs_lfs_s
@@ -221,12 +229,15 @@ static void _lfs_load_config(struct lfs_config* lfs_cfg, struct rt_mtd_nor_devic
         lfs_cfg->block_size = LFS_BLOCK_SIZE;
     }
 
+    lfs_cfg->cache_size = LFS_CACHE_SIZE;
+    lfs_cfg->block_cycles = LFS_BLOCK_CYCLES;
+
     lfs_cfg->block_count = mtd_nor->block_end - mtd_nor->block_start;
 
-    lfs_cfg->lookahead = 32 * ((lfs_cfg->block_count + 31) / 32);
-    if (lfs_cfg->lookahead > LFS_LOOKAHEAD)
+    lfs_cfg->lookahead_size = 32 * ((lfs_cfg->block_count + 31) / 32);
+    if (lfs_cfg->lookahead_size > LFS_LOOKAHEAD_MAX)
     {
-        lfs_cfg->lookahead = LFS_LOOKAHEAD;
+        lfs_cfg->lookahead_size = LFS_LOOKAHEAD_MAX;
     }
 
     lfs_cfg->read = &_lfs_flash_read;
@@ -408,7 +419,7 @@ static int _dfs_lfs_statfs(struct dfs_filesystem* dfs, struct statfs* buf)
     dfs_lfs = (dfs_lfs_t*)dfs->data;
 
     /* Get total sectors and free sectors */
-    result = lfs_traverse(&dfs_lfs->lfs, _dfs_lfs_statfs_count, &in_use);
+    result = lfs_fs_traverse(&dfs_lfs->lfs, _dfs_lfs_statfs_count, &in_use);
     if (result != LFS_ERR_OK)
     {
         return _lfs_result_to_dfs(result);
@@ -582,7 +593,7 @@ static int _dfs_lfs_open(struct dfs_fd* file)
         {
             file->data = (void*)dfs_lfs_fd;
             file->pos = dfs_lfs_fd->u.file.pos;
-            file->size = dfs_lfs_fd->u.file.size;
+            file->size = dfs_lfs_fd->u.file.ctz.size;
             return RT_EOK;
         }
 
@@ -695,7 +706,7 @@ static int _dfs_lfs_write(struct dfs_fd* file, const void* buf, size_t len)
 
     /* update position and file size */
     file->pos = dfs_lfs_fd->u.file.pos;
-    file->size = dfs_lfs_fd->u.file.size;
+    file->size = dfs_lfs_fd->u.file.ctz.size;
 
     return ssize;
 }
@@ -851,8 +862,6 @@ static const struct dfs_filesystem_ops _dfs_lfs_ops = {
 int dfs_lfs_init(void)
 {
     /* register ram file system */
-    dfs_register(&_dfs_lfs_ops);
-
-    return 0;
+    return dfs_register(&_dfs_lfs_ops);
 }
 INIT_COMPONENT_EXPORT(dfs_lfs_init);

+ 40 - 3
lfs_config.h

@@ -7,6 +7,7 @@
 #include <stdint.h>
 #include <stdbool.h>
 #include <string.h>
+#include <inttypes.h>
 
 // Macros, may be replaced by system specific wrappers. Arguments to these
 // macros must not have side-effects as the macros can be removed for a smaller
@@ -55,6 +56,15 @@ static inline uint32_t lfs_min(uint32_t a, uint32_t b) {
     return (a < b) ? a : b;
 }
 
+// Align to nearest multiple of a size
+static inline uint32_t lfs_aligndown(uint32_t a, uint32_t alignment) {
+    return a - (a % alignment);
+}
+
+static inline uint32_t lfs_alignup(uint32_t a, uint32_t alignment) {
+    return lfs_aligndown(a + alignment-1, alignment);
+}
+
 // Find the next smallest power of 2 less than or equal to a
 static inline uint32_t lfs_npw2(uint32_t a) {
 #if !defined(LFS_NO_INTRINSICS) && (defined(__GNUC__) || defined(__CC_ARM))
@@ -98,7 +108,7 @@ static inline int lfs_scmp(uint32_t a, uint32_t b) {
     return (int)(unsigned)(a - b);
 }
 
-// Convert from 32-bit little-endian to native order
+// Convert between 32-bit little-endian and native order
 static inline uint32_t lfs_fromle32(uint32_t a) {
 #if !defined(LFS_NO_INTRINSICS) && ( \
     (defined(  BYTE_ORDER  ) &&   BYTE_ORDER   ==   ORDER_LITTLE_ENDIAN  ) || \
@@ -118,19 +128,44 @@ static inline uint32_t lfs_fromle32(uint32_t a) {
 #endif
 }
 
-// Convert to 32-bit little-endian from native order
 static inline uint32_t lfs_tole32(uint32_t a) {
     return lfs_fromle32(a);
 }
 
+// Convert between 32-bit big-endian and native order
+static inline uint32_t lfs_frombe32(uint32_t a) {
+#if !defined(LFS_NO_INTRINSICS) && ( \
+    (defined(  BYTE_ORDER  ) &&   BYTE_ORDER   ==   ORDER_LITTLE_ENDIAN  ) || \
+    (defined(__BYTE_ORDER  ) && __BYTE_ORDER   == __ORDER_LITTLE_ENDIAN  ) || \
+    (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__))
+    return __builtin_bswap32(a);
+#elif !defined(LFS_NO_INTRINSICS) && ( \
+    (defined(  BYTE_ORDER  ) &&   BYTE_ORDER   ==   ORDER_BIG_ENDIAN  ) || \
+    (defined(__BYTE_ORDER  ) && __BYTE_ORDER   == __ORDER_BIG_ENDIAN  ) || \
+    (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__))
+    return a;
+#else
+    return (((uint8_t*)&a)[0] << 24) |
+           (((uint8_t*)&a)[1] << 16) |
+           (((uint8_t*)&a)[2] <<  8) |
+           (((uint8_t*)&a)[3] <<  0);
+#endif
+}
+
+static inline uint32_t lfs_tobe32(uint32_t a) {
+    return lfs_frombe32(a);
+}
+
 // Calculate CRC-32 with polynomial = 0x04c11db7
-void lfs_crc(uint32_t *crc, const void *buffer, size_t size);
+uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size);
 
 // Allocate memory, only used if buffers are not provided to littlefs
+// Note, memory must be 64-bit aligned
 static inline void *lfs_malloc(size_t size) {
 #ifndef LFS_NO_MALLOC
     return rt_malloc(size);
 #else
+    (void)size;
     return NULL;
 #endif
 }
@@ -139,6 +174,8 @@ static inline void *lfs_malloc(size_t size) {
 static inline void lfs_free(void *p) {
 #ifndef LFS_NO_MALLOC
     rt_free(p);
+#else
+    (void)p;
 #endif
 }
 

+ 5 - 3
lfs_crc.c

@@ -1,7 +1,7 @@
 #include "lfs_util.h"
 
 // Software CRC implementation with small lookup table
-void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
+uint32_t lfs_crc(uint32_t crc, const void *buffer, size_t size) {
     static const uint32_t rtable[16] = {
         0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
         0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
@@ -12,7 +12,9 @@ void lfs_crc(uint32_t *restrict crc, const void *buffer, size_t size) {
     const uint8_t *data = buffer;
 
     for (size_t i = 0; i < size; i++) {
-        *crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 0)) & 0xf];
-        *crc = (*crc >> 4) ^ rtable[(*crc ^ (data[i] >> 4)) & 0xf];
+        crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 0)) & 0xf];
+        crc = (crc >> 4) ^ rtable[(crc ^ (data[i] >> 4)) & 0xf];
     }
+
+    return crc;
 }