From 78ad71ac55f43a090e53476dd3c7ad9c54e5a111 Mon Sep 17 00:00:00 2001 From: chenbin Date: Mon, 18 Apr 2022 23:40:37 +0800 Subject: [PATCH 1/2] Fix infinite-loop in rt_mmcsd_blk_remove. --- components/drivers/sdio/block_dev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/drivers/sdio/block_dev.c b/components/drivers/sdio/block_dev.c index c7be39209f..1e12735cba 100644 --- a/components/drivers/sdio/block_dev.c +++ b/components/drivers/sdio/block_dev.c @@ -501,7 +501,7 @@ void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card) rt_list_t *l, *n; struct mmcsd_blk_device *blk_dev; - for (l = (&blk_devices)->next, n = l->next; l != &blk_devices; l = n) + for (l = (&blk_devices)->next, n = l->next; l != &blk_devices; l = n, n = n->next) { blk_dev = (struct mmcsd_blk_device *)rt_list_entry(l, struct mmcsd_blk_device, list); if (blk_dev->card == card) -- Gitee From cbd9f4045d9ae8e298538c7e7974dc97273d8806 Mon Sep 17 00:00:00 2001 From: chenbin Date: Mon, 18 Apr 2022 23:49:58 +0800 Subject: [PATCH 2/2] imx6ull-artpi-smart sdcard Hot plug detection --- .../applications/init_sdtask.c | 102 +++++++++++------- 1 file changed, 63 insertions(+), 39 deletions(-) diff --git a/bsp/imx6ull-artpi-smart/applications/init_sdtask.c b/bsp/imx6ull-artpi-smart/applications/init_sdtask.c index 16377434aa..30a8e3ec98 100644 --- a/bsp/imx6ull-artpi-smart/applications/init_sdtask.c +++ b/bsp/imx6ull-artpi-smart/applications/init_sdtask.c @@ -10,7 +10,10 @@ #if defined(RT_USING_DFS) +#include #include +#include + #include #include @@ -18,74 +21,95 @@ #define DBG_LVL DBG_INFO #include -static void _sdcard_mount(void) +void filesysytem_try_mount(char *device_name, char *mount_point, char *fs_type_name, int mkfs_count) { - rt_device_t device; + struct statfs fs_stat; + int rc = 0; + + LOG_I("mount(\"%s\",\"%s\",\"%s\");", device_name, mount_point, fs_type_name); - device = rt_device_find("sd0"); - if (device == NULL) + if (rt_device_find(device_name) == NULL) { - mmcsd_wait_cd_changed(0); - host_change(); - mmcsd_wait_cd_changed(RT_WAITING_FOREVER); - rt_thread_mdelay(10); - device = rt_device_find("sd0"); + LOG_I("%s not find!!!", device_name); + return; } - - if (device != RT_NULL) + mkdir(mount_point, 0); +_remount: + rc = dfs_mount(device_name, mount_point, fs_type_name, 0, 0); + if (rc == 0) { - if (dfs_mount("sd0", "/mnt", "elm", 0, 0) == RT_EOK) + LOG_I("mounted %s on %s", device_name, mount_point); + if (dfs_statfs(mount_point, &fs_stat) >= 0) { - LOG_I("sd card mount to '/mnt'"); + LOG_I("%s size:%d, total: %d, free: %d", mount_point, + fs_stat.f_bsize, fs_stat.f_blocks, fs_stat.f_bfree); } - else + } + else + { + if (mkfs_count > 0) { - LOG_W("sd card mount to '/mnt' failed!"); + LOG_I("[%s]try mkfs -t %s %s ", mkfs_count, fs_type_name, device_name); + dfs_mkfs(fs_type_name, device_name); + mkfs_count--; + goto _remount; } + LOG_I("mount failed :%d ", rc); } } -static void _sdcard_unmount(void) +void filesysytem_try_unmount(char *mount_point) { - rt_thread_mdelay(200); - dfs_unmount("/mnt"); - LOG_I("Unmount \"/mnt\""); - - mmcsd_wait_cd_changed(0); - host_change(); - mmcsd_wait_cd_changed(RT_WAITING_FOREVER); + struct stat filestat = {0}; + LOG_I("unmount(\"%s\");", mount_point); + if ((dfs_file_stat(mount_point, &filestat) >= 0) && (S_ISDIR(filestat.st_mode))) + { + dfs_unmount(mount_point); + } } static void sd_task_entry(void *parameter) { volatile unsigned int *IN_STATUS; - IN_STATUS = (volatile unsigned int *)rt_ioremap((void *)0x2190030, 4); - - rt_thread_mdelay(200); - if (dfs_mount("sd0", "/mnt", "elm", 0, 0) == RT_EOK) - { - LOG_I("sd card mount to '/mnt'"); - } - else - { - LOG_W("sd card mount to '/mnt' failed!"); - } + IN_STATUS = (volatile unsigned int *)rt_ioremap((void *)0x2190030, 4); //cd status + int change = 0; while (1) { rt_thread_mdelay(200); - if (((*IN_STATUS >>6) & 0x1) == 1) + change = 0; + if (((*IN_STATUS >> 6) & 0x1) == 1) { *IN_STATUS = 0x40; - _sdcard_mount(); + change = 1; } - - if (((*IN_STATUS >>7) & 0x1) == 1) + if (((*IN_STATUS >> 7) & 0x1) == 1) { *IN_STATUS = (0x80); - _sdcard_unmount(); + change = 2; + } + if(change > 0) + { + LOG_D("sdio host change: %d", change); + mmcsd_wait_cd_changed(0); // clear + host_change(); // send cd change to host + + int result = mmcsd_wait_cd_changed(RT_TICK_PER_SECOND); + if (result == MMCSD_HOST_PLUGED) + { + LOG_D("mmcsd change pluged"); + filesysytem_try_mount("sd0","/sd","elm",0); + }else + if(result == MMCSD_HOST_UNPLUGED) + { + LOG_D("mmcsd change unpluged"); + filesysytem_try_unmount("/sd"); + }else{ + LOG_I("mmcsd wait_cd_changed %d",result); + } } + } } -- Gitee