修改文件目录

This commit is contained in:
sc committed 2025-11-25 16:34:13 +08:00
1 parent 4718409c91
commit f9c5fd9fc3
16 files changed
+716 -258

No files matched your search

+128
View File
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-11-22 Administrator the first version
*//*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-11-22 Administrator the first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "fal_cfg.h"
#include "spi_flash.h"
#include "spi_flash_sfud.h"
#include "drv_qspi.h"
#include "dfs_fs.h"
#include "fal.h"
/* 添加 DEBUG 头文件 */
#include <rtdbg.h>
char w25qxx_read_status_register2(struct rt_qspi_device *device)
{
/* 0x35 read status register2 */
char instruction = 0x35, status;
rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
return status;
}
void w25qxx_write_enable(struct rt_qspi_device *device)
{
/* 0x06 write enable */
char instruction = 0x06;
rt_qspi_send(device, &instruction, 1);
}
void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
{
char status = 0;
/* 0x38 enter qspi mode */
char instruction = 0x38;
char write_status2_buf[2] = {0};
/* 0x31 write status register2 */
write_status2_buf[0] = 0x31;
status = w25qxx_read_status_register2(device);
if (!(status & 0x02))
{
status |= 1 << 1;
w25qxx_write_enable(device);
write_status2_buf[1] = status;
rt_qspi_send(device, &write_status2_buf, 2);
rt_qspi_send(device, &instruction, 1);
LOG_D("flash already enter qspi mode");
rt_thread_mdelay(10);
}
}
int spi_flash_init(void)
{
rt_err_t ret = stm32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
if (ret != RT_EOK)
{
LOG_E("qspi attach device failed\n");
return -RT_ERROR;
}
/* init W25Q256 */
// spi_flash_dev_name 需要用配置文件内的,spi_dev_name实际是SPI挂在参数
if(rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "qspi10") == RT_NULL)
{
LOG_E("rt sfud flash error");
return -1;
}
LOG_I("SFUD flash '%s' probed successfully.", FAL_USING_NOR_FLASH_DEV_NAME);
/* 初始化 fal */
int fal_ = fal_init();
if (fal_ > 0)
{
LOG_I("FAL Number of partitions = %d",fal_);
}else{
return -1;
}
/* 创建 MTD 块设备 */
if (fal_mtd_nor_device_create("sysdata") == RT_NULL)
{
LOG_E("Failed to create MTD device for sysdata!");
return -1;
}
LOG_I("MTD device 'sysdata' created.");
return RT_EOK;
}
INIT_DEVICE_EXPORT(spi_flash_init);
int qspi_fal(void)
{
LOG_I("Create file path /sys");
mkdir("/sys", 0777);
/* 挂载 littlefs */
if (dfs_mount("sysdata", "/sys", "lfs", 0, NULL) == RT_EOK)
{
LOG_I("Filesystem initialized!");
}
else
{
LOG_E("Format memory sysdata");
/* 格式化文件系统 */
dfs_mkfs("lfs", "sysdata");
/* 挂载 littlefs */
if (dfs_mount("sysdata", "/sys", "lfs", 0, NULL) == RT_EOK)
{
LOG_I("Filesystem initialized!");
}
else
{
LOG_E("Failed to initialize filesystem!");
}
}
return RT_EOK;
}
INIT_APP_EXPORT(qspi_fal);
+95
View File
@@ -0,0 +1,95 @@
/*
* 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<5)
{
if (rt_device_find("sd") != RT_NULL)
{
LOG_I("Create file path /sddisk");
mkdir("/sddisk", 0777);
if (dfs_mount("sd", "/sddisk", "elm", 0, NULL) == RT_EOK)
{
LOG_I("SD card mounted to '/sddisk' 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 'sd' not found, retrying...");
}
// 避免空转,延时 100ms 再试
rt_thread_mdelay(100);
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,
1024*2, // 避免溢出
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);
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-11-21 Administrator the first version
*/
#include <rtthread.h>
#include <dfs_fs.h>
#include <dfs_posix.h>
#define DBG_TAG "tmpfs_init"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
int init_tmpfs_root(void)
{
// 挂载 TMPFS 到根目录(注意类型是 "tmp")
if (dfs_mount(RT_NULL, "/", "tmp", 0, RT_NULL) != RT_EOK) {
LOG_E("Failed to mount TMPFS!");
return -1;
}
LOG_I("TMPFS mounted as root.");
/* 3. 测试创建目录 */
if (mkdir("/run", 0777) == 0) {
LOG_I("Created /run directory");
} else {
LOG_E("mkdir failed, errno = %d", errno);
}
return 0;
}
/* 在应用初始化阶段运行(确保系统已就绪) */
INIT_DEVICE_EXPORT(init_tmpfs_root);
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-11-22 Administrator the first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtconfig.h>
#include <fal.h>
extern struct fal_flash_dev nor_flash0;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&nor_flash0, \
}
/* 分区表:根据你的需求划分 */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "sysdata","W25Q128", 0, 16 * 1024 * 1024, 0 }, \
}
#endif /* _FAL_CFG_H_ */