|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2021-07-29 andy the first version
|
|
|
|
*/
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include "dfs_fs.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define DBG_TAG "mount_fs"
|
|
|
|
#define DBG_LVL DBG_LOG
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
rt_sem_t mount_sem = RT_NULL;
|
|
|
|
|
|
|
|
void sd_mount(void *parameter)
|
|
|
|
{
|
|
|
|
LOG_I("SD mount thread started, waiting for SD card...");
|
|
|
|
int retry = 0;
|
|
|
|
while (retry<10)
|
|
|
|
{
|
|
|
|
if (rt_device_find("sd0") != RT_NULL)
|
|
|
|
{
|
|
|
|
if (dfs_mount("sd0", "/", "elm", 0, 0) == RT_EOK)
|
|
|
|
{
|
|
|
|
LOG_I("SD card mounted to '/' successfully");
|
|
|
|
// 挂载成功,释放信号量
|
|
|
|
if (mount_sem != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_sem_release(mount_sem);
|
|
|
|
}
|
|
|
|
break; // 退出线程循环
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_W("SD card mount failed, retrying...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_D("SD device 'sd0' not found, retrying...");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 避免空转,延时 100ms 再试
|
|
|
|
rt_thread_mdelay(500);
|
|
|
|
retry++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int stm32_sdcard_mount(void)
|
|
|
|
{
|
|
|
|
rt_thread_t tid;
|
|
|
|
|
|
|
|
// 删除旧的信号量(如果存在)
|
|
|
|
if (mount_sem != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_sem_delete(mount_sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 创建计数信号量,初始值为 0
|
|
|
|
mount_sem = rt_sem_create("sd_mount", 0, RT_IPC_FLAG_FIFO);
|
|
|
|
if (mount_sem == RT_NULL)
|
|
|
|
{
|
|
|
|
LOG_E("Failed to create semaphore for SD mount!");
|
|
|
|
return -RT_ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
tid = rt_thread_create("sd_mount",
|
|
|
|
sd_mount,
|
|
|
|
RT_NULL,
|
|
|
|
4096, // 建议增大栈,避免溢出
|
|
|
|
RT_THREAD_PRIORITY_MAX - 2,
|
|
|
|
20);
|
|
|
|
if (tid != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_thread_startup(tid);
|
|
|
|
LOG_I("SD mount thread created and started");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG_E("Failed to create SD mount thread!");
|
|
|
|
rt_sem_delete(mount_sem);
|
|
|
|
mount_sem = RT_NULL;
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
// 使用 INIT_COMPONENTS_INIT 或 INIT_APP_EXPORT 自动启动
|
|
|
|
//INIT_COMPONENTS_INIT(stm32_sdcard_mount); // 在组件初始化阶段运行
|
|
|
|
INIT_APP_EXPORT(stm32_sdcard_mount);
|