1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  */
9 #include <rtthread.h>
10 
11 #if defined(RT_USING_DFS)
12 
13 #include <dfs.h>
14 #include <dfs_fs.h>
15 #include <dfs_file.h>
16 
17 #include <ioremap.h>
18 #include <drv_sdio.h>
19 
20 #define DBG_TAG "app.filesystem"
21 #define DBG_LVL DBG_INFO
22 #include <rtdbg.h>
23 
filesysytem_try_mount(char * device_name,char * mount_point,char * fs_type_name,int mkfs_count)24 void filesysytem_try_mount(char *device_name, char *mount_point, char *fs_type_name, int mkfs_count)
25 {
26     struct statfs fs_stat;
27     int rc = 0;
28 
29     LOG_I("mount(\"%s\",\"%s\",\"%s\");", device_name, mount_point, fs_type_name);
30 
31     if (rt_device_find(device_name) == NULL)
32     {
33         LOG_I("%s not find!!!", device_name);
34         return;
35     }
36     mkdir(mount_point, 0);
37 _remount:
38     rc = dfs_mount(device_name, mount_point, fs_type_name, 0, 0);
39     if (rc == 0)
40     {
41         LOG_I("mounted %s on %s", device_name, mount_point);
42         if (dfs_statfs(mount_point, &fs_stat) >= 0)
43         {
44             LOG_I("%s size:%d, total: %d, free: %d", mount_point,
45                   fs_stat.f_bsize, fs_stat.f_blocks, fs_stat.f_bfree);
46         }
47     }
48     else
49     {
50         if (mkfs_count > 0)
51         {
52             LOG_I("[%s]try mkfs -t %s %s ", mkfs_count, fs_type_name, device_name);
53             dfs_mkfs(fs_type_name, device_name);
54             mkfs_count--;
55             goto _remount;
56         }
57         LOG_I("mount failed :%d ", rc);
58     }
59 }
60 
filesysytem_try_unmount(char * mount_point)61 void filesysytem_try_unmount(char *mount_point)
62 {
63     struct stat filestat = {0};
64     LOG_I("unmount(\"%s\");", mount_point);
65     if ((dfs_file_stat(mount_point, &filestat) >= 0) && (S_ISDIR(filestat.st_mode)))
66     {
67         dfs_unmount(mount_point);
68     }
69 }
70 
sd_task_entry(void * parameter)71 static void sd_task_entry(void *parameter)
72 {
73     volatile unsigned int *IN_STATUS;
74 
75     IN_STATUS = (volatile unsigned int *)rt_ioremap((void *)0x2190030, 4); // cd status
76 
77     int change = 0;
78     while (1)
79     {
80         rt_thread_mdelay(200);
81         change = 0;
82         if (((*IN_STATUS >> 6) & 0x1) == 1)
83         {
84             *IN_STATUS = 0x40;
85             change = 1;
86         }
87         if (((*IN_STATUS >> 7) & 0x1) == 1)
88         {
89             *IN_STATUS = (0x80);
90             change = 2;
91         }
92         if (change > 0)
93         {
94             LOG_D("sdio host change: %d", change);
95             mmcsd_wait_cd_changed(0); // clear
96             host_change();            // send cd change to host
97 
98             int result = mmcsd_wait_cd_changed(RT_TICK_PER_SECOND);
99             if (result == MMCSD_HOST_PLUGED)
100             {
101                 LOG_D("mmcsd change pluged");
102                 filesysytem_try_mount("sd0", "/mnt/sd0", "elm", 0);
103             }
104             else if (result == MMCSD_HOST_UNPLUGED)
105             {
106                 LOG_D("mmcsd change unpluged");
107                 filesysytem_try_unmount("/mnt/sd0");
108             }
109             else
110             {
111                 LOG_I("mmcsd wait_cd_changed %d", result);
112             }
113         }
114     }
115 }
116 
sd_task_init(void)117 int sd_task_init(void)
118 {
119     rt_thread_t tid;
120     tid = rt_thread_create("tsdcard", sd_task_entry, RT_NULL,
121                            2048, RT_THREAD_PRIORITY_MAX - 2, 20);
122     if (tid != RT_NULL)
123     {
124         rt_thread_startup(tid);
125     }
126     else
127     {
128         LOG_E("create sd mount task error!");
129     }
130 
131     return RT_EOK;
132 }
133 INIT_APP_EXPORT(sd_task_init);
134 #endif
135