1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <dirent.h>
16 #include <string.h>
17 #include <sys/mount.h>
18 #include <sys/stat.h>
19 #include "fcntl.h"
20 #include "lfs.h"
21 #include "los_compiler.h"
22 #include "los_memory.h"
23 #include "los_task.h"
24 #include "los_fs.h"
25 #include "ohos_init.h"
26 #include "ohos_types.h"
27
28 static const char *LITTLEFS_MOUNT_POINT = "/Openvalley";
29 static const char TAG[] = {"Littlefs"};
30 #define LFS_LOG printf
31
32 #include "esp_partition.h"
33 /* ESP32的分区类型, 0x00-0x3F系统保留分区类型,0x40-0xFE自定义分区 */
34 #define LITTLEFS_PARTITION_TYPE ESP_PARTITION_TYPE_DATA
35 /* ESP32的子分区类型,0x00-0xFE,0xFF(ESP_PARTITION_SUBTYPE_ANY)所有子分区 */
36 #define LITTLEFS_PARTITION_SUBTYPE ESP_PARTITION_SUBTYPE_DATA_SPIFFS
37 /* ESP32的分区名称 */
38 #define LITTLEFS_PARTITION_NAME NULL
39
40 #define READ_SIZE 256 /* 最小读取字节数,所有的读取操作字节数必须是它的倍数(影响内存消耗) */
41 #define PROG_SIZE 256 /* 最小写入字节数,所有的写入操作字节数必须是它的倍数(影响内存消耗) */
42 #define BLOCK_SIZE 4096 /* 擦除块字节数,不会影响内存消耗,每个文件至少占用一个块,必须是READ_SIZE/PROG_SIZE的倍数 */
43 #define CACHE_SIZE 256 /* 块缓存的大小,缓存越大磁盘访问越小,性能越高,必须是READ_SIZE/PROG_SIZE的倍数,且是BLOCK_SIZE的因数 */
44 #define LOOKAHEAD_SIZE 16 /* 块分配预测深度,分配块时每次步进多少个块,必须为8的整数倍,对于内存消耗影响不大 */
45 #define BLOCK_CYCLES 16 /* 逐出元数据日志并将元数据移动到另一个块之前的擦除周期数,值越大性能越好,但磨损越不均匀,-1将禁用块级磨损均衡 */
46
47 #define BLOCK_SIZE_BIT_MOVE 10
48
calc_lfs_size(lfs_size_t block_count,lfs_size_t block_size)49 static lfs_size_t calc_lfs_size(lfs_size_t block_count, lfs_size_t block_size)
50 {
51 return ((block_count)*(block_size))>>BLOCK_SIZE_BIT_MOVE;
52 }
53
GetLittlefsMountPoint(void)54 const char *GetLittlefsMountPoint(void)
55 {
56 return LITTLEFS_MOUNT_POINT;
57 }
58
59 /* lfs读接口 */
littlefs_block_read(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,char * buffer,lfs_size_t size)60 int littlefs_block_read(const struct lfs_config *cfg, lfs_block_t block,
61 lfs_off_t off, char *buffer, lfs_size_t size)
62 {
63 return spi_flash_read((size_t)cfg->context + cfg->block_size * block + off, buffer, size);
64 }
65
66 /* lfs写接口 */
littlefs_block_write(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,const void * buffer,lfs_size_t size)67 int littlefs_block_write(const struct lfs_config *cfg, lfs_block_t block,
68 lfs_off_t off, const void *buffer, lfs_size_t size)
69 {
70 return spi_flash_write((size_t)cfg->context + cfg->block_size * block + off, buffer, size);
71 }
72
73 /* lfs擦除接口 */
littlefs_block_erase(const struct lfs_config * cfg,lfs_block_t block)74 int littlefs_block_erase(const struct lfs_config *cfg, lfs_block_t block)
75 {
76 return spi_flash_erase_range((size_t)cfg->context + cfg->block_size * block, cfg->block_size);
77 }
78
79 /* lfs同步接口 */
littlefs_block_sync(const struct lfs_config * cfg)80 int littlefs_block_sync(const struct lfs_config *cfg)
81 {
82 return LFS_ERR_OK;
83 }
84
littlefs_config(struct PartitionCfg * pCfg)85 static int littlefs_config(struct PartitionCfg *pCfg)
86 {
87 #if defined(LITTLEFS_PHYS_ADDR) && defined(BLOCK_COUNT)
88 pCfg->partNo = (void *)LITTLEFS_PHYS_ADDR;
89 pCfg->blockCount = BLOCK_COUNT,
90 #else
91 const esp_partition_t *part;
92 part = esp_partition_find_first(LITTLEFS_PARTITION_TYPE, LITTLEFS_PARTITION_SUBTYPE, LITTLEFS_PARTITION_NAME);
93 if (!part) {
94 LFS_LOG("Error %s.esp_partition_find_first\n", TAG);
95 return LOS_NOK;
96 }
97 pCfg->partNo = (void *)part->address;
98 pCfg->blockCount = part->size / BLOCK_SIZE;
99 #endif
100 pCfg->readSize = READ_SIZE;
101 pCfg->writeSize = PROG_SIZE;
102 pCfg->cacheSize = CACHE_SIZE;
103 pCfg->blockCycles = BLOCK_CYCLES;
104 pCfg->lookaheadSize = LOOKAHEAD_SIZE;
105 pCfg->blockSize = BLOCK_SIZE;
106 pCfg->readFunc = NULL;
107 pCfg->writeFunc = NULL;
108 pCfg->eraseFunc = NULL;
109 return LOS_OK;
110 }
111
112 /* lfs初始化 */
LittlefsInit(void)113 INT32 LittlefsInit(void)
114 {
115 int err = 0;
116 struct PartitionCfg cfg = {0};
117 if (littlefs_config(&cfg) == LOS_NOK) {
118 return LOS_NOK;
119 }
120 /* 设置挂载Littlefs */
121 err = mount(NULL, LITTLEFS_MOUNT_POINT, "littlefs", 0, &cfg);
122 if (err != LOS_OK) {
123 LFS_LOG("Error %s.mount=0x%X\n", TAG, err);
124 return LOS_NOK;
125 }
126 LFS_LOG("%s.mount=%s addr=0x%X size=%dK OK!!!\n", TAG, LITTLEFS_MOUNT_POINT, (size_t)cfg.partNo,
127 calc_lfs_size(cfg.blockCount, cfg.blockSize));
128 return LOS_OK;
129 }
130
131 SYS_FEATURE_INIT(LittlefsInit);
132