From e302e49019ee0fc3e96d6771ea885ab6633f96a6 Mon Sep 17 00:00:00 2001 From: Eric Chan <55314107+LinuxMint-User@users.noreply.github.com> Date: Mon, 12 Jan 2026 13:37:28 +0800 Subject: [PATCH] Fix: add sdcard detect before mount Add sdcard detect before mount in case auto mount failed. --- .../board/ports/drv_filesystem.c | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c b/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c index c54157eb0de..40b68a1df19 100644 --- a/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c +++ b/bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c @@ -1,19 +1,21 @@ /* - * Copyright (c) 2006-2021, RT-Thread Development Team + * Copyright (c) 2006-2026, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: - * Date Author Notes - * 2018-12-13 balanceTWK add sdcard port file - * 2021-05-10 Meco Man fix a bug that cannot use fatfs in the main thread at starting up - * 2021-07-28 Meco Man implement romfs as the root filesystem + * Date Author Notes + * 2018-12-13 balanceTWK add sdcard port file + * 2021-05-10 Meco Man fix a bug that cannot use fatfs in the main thread at starting up + * 2021-07-28 Meco Man implement romfs as the root filesystem + * 2026-01-13 LinuxMint-User add sdcard detect before mount in case auto mount failed */ #include #include #include #include +#include "drv_sdmmc.h" #if DFS_FILESYSTEMS_MAX < 4 #error "Please define DFS_FILESYSTEMS_MAX more than 4" @@ -29,15 +31,28 @@ #ifdef BSP_USING_SDCARD_FATFS static int onboard_sdcard_mount(void) { - if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK) + rt_device_t device; + + device = rt_device_find("sd0"); + if (device == NULL) { - LOG_I("SD card mount to '/sdcard'"); + mmcsd_wait_cd_changed(0); + stm32_mmcsd_change(); + mmcsd_wait_cd_changed(rt_tick_from_millisecond(500)); + device = rt_device_find("sd0"); } - else + + if (device != RT_NULL) { - LOG_E("SD card mount to '/sdcard' failed!"); + if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK) + { + LOG_I("SD card mount to '/sdcard'"); + } + else + { + LOG_W("SD card mount to '/sdcard' failed!"); + } } - return RT_EOK; } #endif /* BSP_USING_SDCARD_FATFS */ @@ -112,3 +127,4 @@ static int filesystem_mount(void) return RT_EOK; } INIT_APP_EXPORT(filesystem_mount); +