summaryrefslogtreecommitdiff
path: root/bsp/bouffalo_lab/libraries/rt_drivers/port/mnt.c
blob: 891c100d4f719b2b604cf1e43409967093b849d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022/12/25     flyingcys    first version
 */
#include <rtthread.h>

#ifdef RT_USING_DFS
#include <dfs_fs.h>
#include "dfs_romfs.h"

#define DBG_TAG "app.filesystem"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

static const struct romfs_dirent _romfs_root[] =
{
#ifdef BSP_USING_ON_CHIP_FLASH
    {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
#endif
    {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0}
};

const struct romfs_dirent romfs_root =
{
    ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root)  / sizeof(_romfs_root[0])
};

static void sd_mount(void *parameter)
{
    while (1)
    {
        rt_thread_mdelay(500);

        if (rt_device_find("sd0") != RT_NULL)
        {
            if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
            {
                LOG_I("sd card mount to '/sdcard'");
                break;
            }
            else
            {
                LOG_W("sd card mount to '/sdcard' failed! %d\n", rt_get_errno());
            }
        }
    }
}

int mount_init(void)
{
    if(dfs_mount(RT_NULL, "/", "rom", 0, &romfs_root) != 0)
    {
        LOG_E("rom mount to '/' failed!");
    }

#ifdef BSP_USING_ON_CHIP_FLASH_FS
    struct rt_device *flash_dev = RT_NULL;

    /* 使用 filesystem 分区创建块设备,块设备名称为 filesystem */
    flash_dev = fal_blk_device_create("filesystem");
    if(flash_dev == RT_NULL)
    {
        LOG_E("Failed to create device.\n");
        return -RT_ERROR;
    }

    if (dfs_mount("filesystem", "/flash", "lfs", 0, 0) != 0)
    {
        LOG_I("file system initialization failed!\n");
        if(dfs_mkfs("lfs", "filesystem") == 0)
        {
            if (dfs_mount("filesystem", "/flash", "lfs", 0, 0) == 0)
            {
                LOG_I("mount to '/flash' success!");
            }
        }
    }
    else
    {
        LOG_I("mount to '/flash' success!");
    }
#endif

#ifdef BSP_USING_SDH
    rt_thread_t tid;

    tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
                            2048, RT_THREAD_PRIORITY_MAX - 2, 20);
    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        LOG_E("create sd_mount thread err!");
    }
#endif

    return RT_EOK;
}
INIT_APP_EXPORT(mount_init);

#endif /* RT_USING_DFS */