1 /*
2 * Copyright (c) 2021 GOODIX.
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 "littlefs.h"
16 #include "gr55xx_hal.h"
17 #include "hal_flash.h"
18 #include "hdf_log.h"
19
20 #define SIZE_TO_KB 1024
21 static uint32_t g_lfs_start_addr;
22
littlefs_flash_init(const struct lfs_config * cfg)23 int32_t littlefs_flash_init(const struct lfs_config *cfg)
24 {
25 uint32_t flash_id;
26 uint32_t flash_size;
27 uint32_t nvds_start_addr;
28
29 hal_flash_get_info(&flash_id, &flash_size);
30
31 nvds_start_addr = EXFLASH_START_ADDR + flash_size - hal_flash_sector_size() * NVDS_NUM_SECTOR;
32 g_lfs_start_addr = nvds_start_addr - cfg->block_count * cfg->block_size;
33
34 HDF_LOGI("littlefs flash start addr=0x%x, all size=%dKB", g_lfs_start_addr,
35 cfg->block_count * cfg->block_size / SIZE_TO_KB);
36
37 return 0;
38 }
39
littlefs_block_read(const struct lfs_config * c,lfs_block_t block,lfs_off_t off,uint8_t * buf,lfs_size_t size)40 int32_t littlefs_block_read(const struct lfs_config *c, lfs_block_t block,
41 lfs_off_t off, uint8_t *buf, lfs_size_t size)
42 {
43 uint32_t addr = g_lfs_start_addr + c->block_size * block + off;
44
45 return size == hal_flash_read(addr, buf, size) ? LFS_ERR_OK : LFS_ERR_IO;
46 }
47
littlefs_block_write(const struct lfs_config * c,lfs_block_t block,lfs_off_t off,const uint8_t * dst,lfs_size_t size)48 int32_t littlefs_block_write(const struct lfs_config *c, lfs_block_t block,
49 lfs_off_t off, const uint8_t *dst, lfs_size_t size)
50 {
51 uint32_t addr = g_lfs_start_addr + c->block_size * block + off;
52
53 return size == hal_flash_write(addr, dst, size) ? LFS_ERR_OK : LFS_ERR_IO;
54 }
55
littlefs_block_erase(const struct lfs_config * c,lfs_block_t block)56 int32_t littlefs_block_erase(const struct lfs_config *c, lfs_block_t block)
57 {
58 uint32_t addr = g_lfs_start_addr + block * c->block_size;
59
60 return hal_flash_erase (addr, c->block_size) ? LFS_ERR_OK : LFS_ERR_IO;
61 }
62
littlefs_block_sync(const struct lfs_config * c)63 int32_t littlefs_block_sync(const struct lfs_config *c)
64 {
65 return 0;
66 }
67