1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-03-18     Carl      the first version
9  */
10 
11 #include <rtthread.h>
12 
13 #ifdef BSP_USING_SDC
14 
15 #include <dfs_elm.h>
16 #include <dfs_fs.h>
17 #include <dfs_file.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sys/stat.h>
21 #include <sys/statfs.h>
22 #include "drv_sdctrl.h"
23 
24 #define DBG_TAG "app.card"
25 #define DBG_LVL DBG_INFO
26 #include <rtdbg.h>
27 
_sdcard_mount(void)28 static rt_err_t _sdcard_mount(void)
29 {
30     rt_device_t device;
31 
32     device = rt_device_find("sd0");
33     rt_kprintf("rt_device_find %x \r\n", device);
34     if (device == NULL)
35     {
36         mmcsd_wait_cd_changed(0);
37         ft2004_mmcsd_change();
38         if (mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000)) == -RT_ETIMEOUT)
39         {
40             rt_kprintf("timeout \r\n");
41             return -RT_ERROR;
42         }
43         device = rt_device_find("sd0");
44     }
45 
46     rt_thread_mdelay(1000);
47     LOG_I("dfs_mount  \r\n");
48     if (device != RT_NULL)
49     {
50         if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
51         {
52             LOG_I("sd card mount to '/'");
53         }
54         else
55         {
56             LOG_W("sd card mount to '/' failed!");
57             return -RT_ERROR;
58         }
59     }
60 
61     return RT_EOK;
62 }
63 
_sdcard_unmount(void)64 static void _sdcard_unmount(void)
65 {
66     rt_thread_mdelay(200);
67     dfs_unmount("/");
68     LOG_I("Unmount \"/\"");
69 
70     mmcsd_wait_cd_changed(0);
71     ft2004_mmcsd_change();
72     mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000));
73     LOG_I("Unmount is over \r\n");
74 }
75 
sd_mount(void * parameter)76 static void sd_mount(void *parameter)
77 {
78     rt_uint8_t state = 0; /* 1. is valid card ,0 is removal */
79 #ifdef BSP_SDC_IRQ_CARD_REMOVE
80     rt_uint32_t status;
81 #endif
82     while (1)
83     {
84         switch (state)
85         {
86         case 0:
87             if (ft2004_card_status() == 1)
88             {
89 #ifdef BSP_SDC_IRQ_CARD_REMOVE
90                 ft2004_card_remove_check(0, RT_NULL); /* Clear removal flag bit */
91 #endif
92                 if (_sdcard_mount() == RT_EOK)
93                 {
94                     state = 1;
95                 }
96                 else
97                 {
98                     /* For the critical case of frequent plug */
99                     rt_kprintf("dfs_unmount \r\n");
100                     _sdcard_unmount();
101                     ft2004_sdctrl_reset();
102                 }
103             }
104             else
105             {
106                 rt_thread_mdelay(100);
107             }
108             break;
109         case 1:
110 
111 #ifdef BSP_SDC_IRQ_CARD_REMOVE
112             if (ft2004_card_remove_check(RT_WAITING_FOREVER, &status) == RT_EOK)
113             {
114                 if (status & SDCTR_CARD_REMOVE_FLG)
115                 {
116                     state = 0;
117                     _sdcard_unmount();
118                 }
119             }
120 #else
121             if (ft2004_card_status() == 0)
122             {
123                 state = 0;
124                 _sdcard_unmount();
125             }
126 #endif
127             else
128             {
129                 rt_thread_mdelay(100);
130             }
131             break;
132         default:
133             state = 0;
134             break;
135         }
136     }
137 }
138 
ft2004_sdcard_mount(void)139 int ft2004_sdcard_mount(void)
140 {
141     rt_thread_t tid;
142 
143     tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
144                            8192, 2, 20);
145 
146     if (tid != RT_NULL)
147     {
148         rt_thread_startup(tid);
149     }
150     else
151     {
152         LOG_E("create sd_mount thread err!");
153     }
154     return RT_EOK;
155 }
156 INIT_APP_EXPORT(ft2004_sdcard_mount);
157 
158 #endif /* BSP_USING_SDCARD */
159