1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <lfs.h>
23 #include <los_fs.h>
24
25 #include <B91/flash.h>
26
27 #define LITTLEFS_PATH "/littlefs/"
28
29 #define LITTLEFS_PHYS_ADDR (1024 * 1024)
30 #define LITTLEFS_PHYS_SIZE (16 * 1024)
31
32 #define READ_SIZE 16
33 #define PROG_SIZE 16
34 #define BLOCK_SIZE 4096
35 #define BLOCK_COUNT 32
36 #define CACHE_SIZE 512
37 #define LOOKAHEAD_SIZE 64
38 #define BLOCK_CYCLES 500
39
littlefs_block_read(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,unsigned char * buffer,lfs_size_t size)40 int littlefs_block_read(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, unsigned char *buffer,
41 lfs_size_t size)
42 {
43 uint32_t addr = block * (cfg->block_size) + off;
44
45 flash_read_page(LITTLEFS_PHYS_ADDR + addr, size, buffer);
46
47 return LFS_ERR_OK;
48 }
49
littlefs_block_write(const struct lfs_config * cfg,lfs_block_t block,lfs_off_t off,const unsigned char * buffer,lfs_size_t size)50 int littlefs_block_write(const struct lfs_config *cfg, lfs_block_t block, lfs_off_t off, const unsigned char *buffer,
51 lfs_size_t size)
52 {
53 uint32_t addr = block * (cfg->block_size) + off;
54
55 flash_write_page(LITTLEFS_PHYS_ADDR + addr, size, (unsigned char *)buffer);
56
57 return LFS_ERR_OK;
58 }
59
littlefs_block_erase(const struct lfs_config * cfg,lfs_block_t block)60 int littlefs_block_erase(const struct lfs_config *cfg, lfs_block_t block)
61 {
62 uint32_t addr = block * (cfg->block_size);
63
64 flash_erase_sector(LITTLEFS_PHYS_ADDR + addr);
65
66 return LFS_ERR_OK;
67 }
68
littlefs_block_sync(const struct lfs_config * cfg)69 int littlefs_block_sync(const struct lfs_config *cfg)
70 {
71 return LFS_ERR_OK;
72 }
73
74 static struct PartitionCfg partCfg = {
75 .readFunc = NULL,
76 .writeFunc = NULL,
77 .eraseFunc = NULL,
78 .readSize = READ_SIZE,
79 .writeSize = PROG_SIZE,
80 .blockSize = BLOCK_SIZE,
81 .blockCount = BLOCK_COUNT,
82 .cacheSize = CACHE_SIZE,
83 .partNo = 0,
84 .lookaheadSize = LOOKAHEAD_SIZE,
85 .blockCycles = BLOCK_CYCLES
86 };
87
LittlefsConfigGet(void)88 struct PartitionCfg *LittlefsConfigGet(void)
89 {
90 return &partCfg;
91 }
92