diff --git a/projects/Edgi_Talk_M33_SDCARD/board/ports/filesystem/mnt.c b/projects/Edgi_Talk_M33_SDCARD/board/ports/filesystem/mnt.c index 184a4e87..68a9cf0c 100644 --- a/projects/Edgi_Talk_M33_SDCARD/board/ports/filesystem/mnt.c +++ b/projects/Edgi_Talk_M33_SDCARD/board/ports/filesystem/mnt.c @@ -1,46 +1,126 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ + #include #ifdef RT_USING_DFS #include +#include -int mnt_init(void) +#define DBG_TAG "app.filesystem" +#define DBG_LVL DBG_INFO +#include + +static void _sdcard_mount(void) { rt_device_t device; + const char *sd_device_names[] = {"sd", "sd0"}; + int i; + const char *device_name = RT_NULL; - rt_thread_mdelay(500); + /* Try to find SD card device */ + for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++) + { + device = rt_device_find(sd_device_names[i]); + if (device != RT_NULL) + { + device_name = sd_device_names[i]; + LOG_I("Found sd card device '%s'", device_name); + break; + } + } - /* 检测sd0设备是否存在 */ - device = rt_device_find("sd0"); if (device == RT_NULL) { - rt_kprintf("SD card device 'sd0' not found!\n"); - return -1; + /* Clear previous CD status and wait for SD card initialization */ + mmcsd_wait_cd_changed(0); + /* Wait for SD card detection, timeout 5 seconds */ + if (mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000)) == -RT_ETIMEOUT) + { + LOG_W("Wait for SD card timeout!"); + return; + } + + /* Try again to find SD card device */ + for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++) + { + device = rt_device_find(sd_device_names[i]); + if (device != RT_NULL) + { + device_name = sd_device_names[i]; + LOG_I("Found sd card device '%s'", device_name); + break; + } + } + } + + if (device == RT_NULL || device_name == RT_NULL) + { + LOG_W("sd card device not found!"); + return; } - /* 尝试挂载SD卡 */ - if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) + rt_thread_mdelay(200); + + /* Try to mount */ + if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK) { - rt_kprintf("SD card mount to '/' success!\n"); - return 0; + LOG_I("sd card mount to '/' success!"); + return; } - /* 挂载失败,尝试格式化 */ - rt_kprintf("SD card mount failed, try to mkfs...\n"); - if (dfs_mkfs("elm", "sd0") == 0) + /* Mount failed, try to format */ + LOG_W("sd card mount to '/' failed, try to mkfs..."); + if (dfs_mkfs("elm", device_name) == 0) { - rt_kprintf("SD card mkfs success!\n"); + LOG_I("sd card mkfs success!"); - /* 格式化成功后再次尝试挂载 */ - if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) + /* Try to mount again after formatting */ + if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK) { - rt_kprintf("SD card mount to '/' success!\n"); - return 0; + LOG_I("sd card mount to '/' success!"); } + else + { + LOG_E("sd card mount to '/' failed after mkfs!"); + } + } + else + { + LOG_E("sd card mkfs failed!"); + } +} + +static void sd_mount_thread(void *parameter) +{ + rt_thread_mdelay(200); + _sdcard_mount(); +} + +int mnt_init(void) +{ + rt_thread_t tid; + + /* Create a separate thread for SD card mounting to avoid blocking system startup */ + tid = rt_thread_create("sd_mount", sd_mount_thread, 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!"); } - rt_kprintf("SD card mount to '/' failed!\n"); - return -1; + return RT_EOK; } INIT_APP_EXPORT(mnt_init); -#endif +#endif /* RT_USING_DFS */ diff --git a/projects/Edgi_Talk_M33_WavPlayer/board/ports/filesystem/mnt.c b/projects/Edgi_Talk_M33_WavPlayer/board/ports/filesystem/mnt.c index 184a4e87..68a9cf0c 100644 --- a/projects/Edgi_Talk_M33_WavPlayer/board/ports/filesystem/mnt.c +++ b/projects/Edgi_Talk_M33_WavPlayer/board/ports/filesystem/mnt.c @@ -1,46 +1,126 @@ +/* + * Copyright (c) 2006-2018, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + */ + #include #ifdef RT_USING_DFS #include +#include -int mnt_init(void) +#define DBG_TAG "app.filesystem" +#define DBG_LVL DBG_INFO +#include + +static void _sdcard_mount(void) { rt_device_t device; + const char *sd_device_names[] = {"sd", "sd0"}; + int i; + const char *device_name = RT_NULL; - rt_thread_mdelay(500); + /* Try to find SD card device */ + for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++) + { + device = rt_device_find(sd_device_names[i]); + if (device != RT_NULL) + { + device_name = sd_device_names[i]; + LOG_I("Found sd card device '%s'", device_name); + break; + } + } - /* 检测sd0设备是否存在 */ - device = rt_device_find("sd0"); if (device == RT_NULL) { - rt_kprintf("SD card device 'sd0' not found!\n"); - return -1; + /* Clear previous CD status and wait for SD card initialization */ + mmcsd_wait_cd_changed(0); + /* Wait for SD card detection, timeout 5 seconds */ + if (mmcsd_wait_cd_changed(rt_tick_from_millisecond(5000)) == -RT_ETIMEOUT) + { + LOG_W("Wait for SD card timeout!"); + return; + } + + /* Try again to find SD card device */ + for (i = 0; i < sizeof(sd_device_names) / sizeof(sd_device_names[0]); i++) + { + device = rt_device_find(sd_device_names[i]); + if (device != RT_NULL) + { + device_name = sd_device_names[i]; + LOG_I("Found sd card device '%s'", device_name); + break; + } + } + } + + if (device == RT_NULL || device_name == RT_NULL) + { + LOG_W("sd card device not found!"); + return; } - /* 尝试挂载SD卡 */ - if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) + rt_thread_mdelay(200); + + /* Try to mount */ + if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK) { - rt_kprintf("SD card mount to '/' success!\n"); - return 0; + LOG_I("sd card mount to '/' success!"); + return; } - /* 挂载失败,尝试格式化 */ - rt_kprintf("SD card mount failed, try to mkfs...\n"); - if (dfs_mkfs("elm", "sd0") == 0) + /* Mount failed, try to format */ + LOG_W("sd card mount to '/' failed, try to mkfs..."); + if (dfs_mkfs("elm", device_name) == 0) { - rt_kprintf("SD card mkfs success!\n"); + LOG_I("sd card mkfs success!"); - /* 格式化成功后再次尝试挂载 */ - if (dfs_mount("sd0", "/", "elm", 0, 0) == 0) + /* Try to mount again after formatting */ + if (dfs_mount(device_name, "/", "elm", 0, 0) == RT_EOK) { - rt_kprintf("SD card mount to '/' success!\n"); - return 0; + LOG_I("sd card mount to '/' success!"); } + else + { + LOG_E("sd card mount to '/' failed after mkfs!"); + } + } + else + { + LOG_E("sd card mkfs failed!"); + } +} + +static void sd_mount_thread(void *parameter) +{ + rt_thread_mdelay(200); + _sdcard_mount(); +} + +int mnt_init(void) +{ + rt_thread_t tid; + + /* Create a separate thread for SD card mounting to avoid blocking system startup */ + tid = rt_thread_create("sd_mount", sd_mount_thread, 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!"); } - rt_kprintf("SD card mount to '/' failed!\n"); - return -1; + return RT_EOK; } INIT_APP_EXPORT(mnt_init); -#endif +#endif /* RT_USING_DFS */