Skip to content

Commit 21bde40

Browse files
committed
完善M33文件系统相关工程挂载逻辑
1 parent 1c8c52a commit 21bde40

File tree

2 files changed

+202
-42
lines changed
  • projects
    • Edgi_Talk_M33_SDCARD/board/ports/filesystem
    • Edgi_Talk_M33_WavPlayer/board/ports/filesystem

2 files changed

+202
-42
lines changed
Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,126 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
*/
9+
110
#include <rtthread.h>
211

312
#ifdef RT_USING_DFS
413
#include <dfs_fs.h>
14+
#include <drivers/mmcsd_core.h>
515

6-
int mnt_init(void)
16+
#define DBG_TAG "app.filesystem"
17+
#define DBG_LVL DBG_INFO
18+
#include <rtdbg.h>
19+
20+
static void _sdcard_mount(void)
721
{
822
rt_device_t device;
23+
const char *sd_device_names[] = {"sd", "sd0"};
24+
int i;
25+
const char *device_name = RT_NULL;
926

10-
rt_thread_mdelay(500);
27+
/* Try to find SD card device */
28+
for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++)
29+
{
30+
device = rt_device_find(sd_device_names[i]);
31+
if (device != RT_NULL)
32+
{
33+
device_name = sd_device_names[i];
34+
LOG_I("Found sd card device '%s'", device_name);
35+
break;
36+
}
37+
}
1138

12-
/* 检测sd0设备是否存在 */
13-
device = rt_device_find("sd0");
1439
if (device == RT_NULL)
1540
{
16-
rt_kprintf("SD card device 'sd0' not found!\n");
17-
return -1;
41+
/* Clear previous CD status and wait for SD card initialization */
42+
mmcsd_wait_cd_changed(0);
43+
/* Wait for SD card detection, timeout 5 seconds */
44+
if (mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000)) == -RT_ETIMEOUT)
45+
{
46+
LOG_W("Wait for SD card timeout!");
47+
return;
48+
}
49+
50+
/* Try again to find SD card device */
51+
for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++)
52+
{
53+
device = rt_device_find(sd_device_names[i]);
54+
if (device != RT_NULL)
55+
{
56+
device_name = sd_device_names[i];
57+
LOG_I("Found sd card device '%s'", device_name);
58+
break;
59+
}
60+
}
61+
}
62+
63+
if (device == RT_NULL || device_name == RT_NULL)
64+
{
65+
LOG_W("sd card device not found!");
66+
return;
1867
}
1968

20-
/* 尝试挂载SD卡 */
21-
if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
69+
rt_thread_mdelay(200);
70+
71+
/* Try to mount */
72+
if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK)
2273
{
23-
rt_kprintf("SD card mount to '/' success!\n");
24-
return 0;
74+
LOG_I("sd card mount to '/' success!");
75+
return;
2576
}
2677

27-
/* 挂载失败,尝试格式化 */
28-
rt_kprintf("SD card mount failed, try to mkfs...\n");
29-
if (dfs_mkfs("elm", "sd0") == 0)
78+
/* Mount failed, try to format */
79+
LOG_W("sd card mount to '/' failed, try to mkfs...");
80+
if (dfs_mkfs("elm", device_name) == 0)
3081
{
31-
rt_kprintf("SD card mkfs success!\n");
82+
LOG_I("sd card mkfs success!");
3283

33-
/* 格式化成功后再次尝试挂载 */
34-
if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
84+
/* Try to mount again after formatting */
85+
if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK)
3586
{
36-
rt_kprintf("SD card mount to '/' success!\n");
37-
return 0;
87+
LOG_I("sd card mount to '/' success!");
3888
}
89+
else
90+
{
91+
LOG_E("sd card mount to '/' failed after mkfs!");
92+
}
93+
}
94+
else
95+
{
96+
LOG_E("sd card mkfs failed!");
97+
}
98+
}
99+
100+
static void sd_mount_thread(void *parameter)
101+
{
102+
rt_thread_mdelay(200);
103+
_sdcard_mount();
104+
}
105+
106+
int mnt_init(void)
107+
{
108+
rt_thread_t tid;
109+
110+
/* Create a separate thread for SD card mounting to avoid blocking system startup */
111+
tid = rt_thread_create("sd_mount", sd_mount_thread, RT_NULL,
112+
2048, RT_THREAD_PRIORITY_MAX - 2, 20);
113+
if (tid != RT_NULL)
114+
{
115+
rt_thread_startup(tid);
116+
}
117+
else
118+
{
119+
LOG_E("create sd_mount thread err!");
39120
}
40121

41-
rt_kprintf("SD card mount to '/' failed!\n");
42-
return -1;
122+
return RT_EOK;
43123
}
44124
INIT_APP_EXPORT(mnt_init);
45125

46-
#endif
126+
#endif /* RT_USING_DFS */
Lines changed: 101 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,126 @@
1+
/*
2+
* Copyright (c) 2006-2018, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
*/
9+
110
#include <rtthread.h>
211

312
#ifdef RT_USING_DFS
413
#include <dfs_fs.h>
14+
#include <drivers/mmcsd_core.h>
515

6-
int mnt_init(void)
16+
#define DBG_TAG "app.filesystem"
17+
#define DBG_LVL DBG_INFO
18+
#include <rtdbg.h>
19+
20+
static void _sdcard_mount(void)
721
{
822
rt_device_t device;
23+
const char *sd_device_names[] = {"sd", "sd0"};
24+
int i;
25+
const char *device_name = RT_NULL;
926

10-
rt_thread_mdelay(500);
27+
/* Try to find SD card device */
28+
for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++)
29+
{
30+
device = rt_device_find(sd_device_names[i]);
31+
if (device != RT_NULL)
32+
{
33+
device_name = sd_device_names[i];
34+
LOG_I("Found sd card device '%s'", device_name);
35+
break;
36+
}
37+
}
1138

12-
/* 检测sd0设备是否存在 */
13-
device = rt_device_find("sd0");
1439
if (device == RT_NULL)
1540
{
16-
rt_kprintf("SD card device 'sd0' not found!\n");
17-
return -1;
41+
/* Clear previous CD status and wait for SD card initialization */
42+
mmcsd_wait_cd_changed(0);
43+
/* Wait for SD card detection, timeout 5 seconds */
44+
if (mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000)) == -RT_ETIMEOUT)
45+
{
46+
LOG_W("Wait for SD card timeout!");
47+
return;
48+
}
49+
50+
/* Try again to find SD card device */
51+
for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++)
52+
{
53+
device = rt_device_find(sd_device_names[i]);
54+
if (device != RT_NULL)
55+
{
56+
device_name = sd_device_names[i];
57+
LOG_I("Found sd card device '%s'", device_name);
58+
break;
59+
}
60+
}
61+
}
62+
63+
if (device == RT_NULL || device_name == RT_NULL)
64+
{
65+
LOG_W("sd card device not found!");
66+
return;
1867
}
1968

20-
/* 尝试挂载SD卡 */
21-
if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
69+
rt_thread_mdelay(200);
70+
71+
/* Try to mount */
72+
if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK)
2273
{
23-
rt_kprintf("SD card mount to '/' success!\n");
24-
return 0;
74+
LOG_I("sd card mount to '/' success!");
75+
return;
2576
}
2677

27-
/* 挂载失败,尝试格式化 */
28-
rt_kprintf("SD card mount failed, try to mkfs...\n");
29-
if (dfs_mkfs("elm", "sd0") == 0)
78+
/* Mount failed, try to format */
79+
LOG_W("sd card mount to '/' failed, try to mkfs...");
80+
if (dfs_mkfs("elm", device_name) == 0)
3081
{
31-
rt_kprintf("SD card mkfs success!\n");
82+
LOG_I("sd card mkfs success!");
3283

33-
/* 格式化成功后再次尝试挂载 */
34-
if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
84+
/* Try to mount again after formatting */
85+
if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK)
3586
{
36-
rt_kprintf("SD card mount to '/' success!\n");
37-
return 0;
87+
LOG_I("sd card mount to '/' success!");
3888
}
89+
else
90+
{
91+
LOG_E("sd card mount to '/' failed after mkfs!");
92+
}
93+
}
94+
else
95+
{
96+
LOG_E("sd card mkfs failed!");
97+
}
98+
}
99+
100+
static void sd_mount_thread(void *parameter)
101+
{
102+
rt_thread_mdelay(200);
103+
_sdcard_mount();
104+
}
105+
106+
int mnt_init(void)
107+
{
108+
rt_thread_t tid;
109+
110+
/* Create a separate thread for SD card mounting to avoid blocking system startup */
111+
tid = rt_thread_create("sd_mount", sd_mount_thread, RT_NULL,
112+
2048, RT_THREAD_PRIORITY_MAX - 2, 20);
113+
if (tid != RT_NULL)
114+
{
115+
rt_thread_startup(tid);
116+
}
117+
else
118+
{
119+
LOG_E("create sd_mount thread err!");
39120
}
40121

41-
rt_kprintf("SD card mount to '/' failed!\n");
42-
return -1;
122+
return RT_EOK;
43123
}
44124
INIT_APP_EXPORT(mnt_init);
45125

46-
#endif
126+
#endif /* RT_USING_DFS */

0 commit comments

Comments
 (0)