1 /*
2 * Copyright (c) 2022 HPMicro
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
16 #include <hpm_clock_drv.h>
17 #include <stdio.h>
18 #include <los_interrupt.h>
19 #include "hpm_littlefs_drv.h"
20 #include <sys/stat.h>
21 #include <stdlib.h>
22 #include <sys/mount.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <los_fs.h>
26
27 struct HpmLittlefsCfg g_hpmLittlefsCfgs[] = {
28 [0] = {
29 .cfg = {
30 /* partition low-level read func */
31 .readFunc = HpmLittlefsRead,
32 /* partition low-level write func */
33 .writeFunc = HpmLittlefsProg,
34 /* partition low-level erase func */
35 .eraseFunc = HpmLittlefsErase,
36
37 .readSize = 16,
38 .writeSize = 16,
39 .blockSize = 0, /* auto fill in HpmLittlefsDriverInit() */
40 .blockCount = 0, /* auto fill in HpmLittlefsDriverInit() */
41 .cacheSize = 1024,
42
43 .partNo = 0,
44 .lookaheadSize = 16,
45 .blockCycles = 1000,
46 },
47 .ctx = {
48 .startOffset = 5 * 1024 * 1024,
49 .len = 2 * 1024 * 1024,
50 .base = HPM_XPI0_BASE,
51 .mountPoint = "/data",
52 }
53 },
54 };
55
HpmLittlefsInit(void)56 void HpmLittlefsInit(void)
57 {
58 uint32_t num = sizeof(g_hpmLittlefsCfgs) / sizeof(g_hpmLittlefsCfgs[0]);
59 int ret;
60
61 int *lengthArray = (int *)malloc(num * sizeof(int) * 2);
62 int *addrArray = lengthArray + num;
63
64 for (int i = 0; i < num; i++) {
65 lengthArray[i] = g_hpmLittlefsCfgs[i].ctx.len;
66 addrArray[i] = g_hpmLittlefsCfgs[i].ctx.startOffset;
67 }
68
69 LOS_DiskPartition("spiflash", "littlefs", lengthArray, addrArray, num);
70 free(lengthArray);
71
72 for (int i = 0; i < num; i++) {
73 HpmLittlefsDriverInit(&g_hpmLittlefsCfgs[i]);
74 ret = mount(NULL, g_hpmLittlefsCfgs[i].ctx.mountPoint, "littlefs", 0, &g_hpmLittlefsCfgs[i].cfg);
75 if (ret < 0) {
76 printf("Err: hpm littlefs [%s] mount failed!!!\n", g_hpmLittlefsCfgs[i].ctx.mountPoint);
77 continue;
78 }
79
80 DIR *dir = NULL;
81 if ((dir = opendir(g_hpmLittlefsCfgs[i].ctx.mountPoint)) == NULL) {
82 ret = mkdir(g_hpmLittlefsCfgs[i].ctx.mountPoint, S_IRUSR | S_IWUSR);
83 if (ret) {
84 printf("Err: hpm littlefs mkdir [%s] mount failed!!!\n", g_hpmLittlefsCfgs[i].ctx.mountPoint);
85 continue;
86 }
87 } else {
88 closedir(dir);
89 }
90 }
91 }
92
93