You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.3 KiB
128 lines
3.3 KiB
/*
|
|
* 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_COMPONENT_EXPORT(qspi_fal);
|
|
|