bsp: nxp/frdm-mcxn947: FS: use generic SPI API.

This patch replaces SPI flash mount procedure with generic SPI API. This
patch also try to format the external flash on the first mount failure,
in case of empty or invalid flash content is present on the external SPI
flash.

Signed-off-by: Yilin Sun <imi415@imi.moe>
This commit is contained in:
Yilin Sun 2024-08-16 17:16:29 +08:00 committed by Meco Man
parent 12214dc18c
commit fd3f603b27
1 changed files with 46 additions and 32 deletions

View File

@ -18,62 +18,76 @@
#include "dfs.h"
#include "dfs_file.h"
#define DBG_TAG "spi-flash"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#if DFS_FILESYSTEMS_MAX < 4
#error "Please define DFS_FILESYSTEMS_MAX more than 4"
#endif
#if DFS_FILESYSTEM_TYPES_MAX < 4
#error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
#endif
#define DBG_TAG "app.filesystem_spi_flash"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define W25Q64_SPI_DEVICE_NAME "spi70"
#define W25Q64_SPI_BUS_NAME "spi7"
#define W25Q64_SPI_FLASH_NAME "w25qxx"
#define W25Q64_SPI_FLASH_CS_PIN 96
rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin);
#define W25Q64_FS_MOUNT_PATH "/"
static int filesystem_mount(void)
static int app_filesystem_init(void)
{
struct rt_spi_device *spi70 = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
rt_err_t ret = RT_EOK;
struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
if(!spi70)
if (!spi_device)
{
LOG_W("spi sample run failed! can't find %s device!\n","spi7");
LOG_W("spi sample run failed! can't find %s device!\n", W25Q64_SPI_BUS_NAME);
return -RT_ERROR;
}
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB;
cfg.max_hz = 50 * 1000 *1000;
rt_spi_configure(spi70, &cfg);
struct rt_spi_configuration cfg =
{
.data_width = 8,
.mode = RT_SPI_MASTER | RT_SPI_MODE_3 | RT_SPI_MSB,
.max_hz = 50 * 1000 * 1000,
};
ret = rt_spi_configure(spi_device, &cfg);
if (ret != RT_EOK)
{
LOG_E("SPI bus configuration failed.\n");
return -RT_ERROR;
}
/* legcy issue */
ret = rt_spi_bus_attach_device_cspin(spi_device, W25Q64_SPI_DEVICE_NAME, W25Q64_SPI_BUS_NAME, W25Q64_SPI_FLASH_CS_PIN, RT_NULL);
if (ret != RT_EOK)
{
LOG_E("SPI flash device attach failed.\n");
return -RT_ERROR;
}
rt_hw_spi_device_attach(W25Q64_SPI_BUS_NAME, W25Q64_SPI_DEVICE_NAME, 96);
if(RT_NULL == rt_sfud_flash_probe(W25Q64_SPI_FLASH_NAME, W25Q64_SPI_DEVICE_NAME))
if (RT_NULL == rt_sfud_flash_probe(W25Q64_SPI_FLASH_NAME, W25Q64_SPI_DEVICE_NAME))
{
LOG_E("Flash sfud Failed!\n");
return -RT_ERROR;
}
if(dfs_mount(W25Q64_SPI_FLASH_NAME, "/", "elm", 0, 0))
if (dfs_mount(W25Q64_SPI_FLASH_NAME, W25Q64_FS_MOUNT_PATH, "elm", 0, 0) != 0)
{
LOG_E("dfs mount dev:%s failed!\n", W25Q64_SPI_FLASH_NAME);
return -RT_ERROR;
LOG_W("Initial ELM FAT mount failed, trying to format block device.\n");
if (dfs_mkfs("elm", W25Q64_SPI_FLASH_NAME) != 0)
{
LOG_E("Failed to create ELM FAT filesystem.\n");
return -4;
}
if (dfs_mount(W25Q64_SPI_FLASH_NAME, W25Q64_FS_MOUNT_PATH, "elm", 0, 0) != 0)
{
LOG_E("Failed to mount ELM FAT filesystem, check mount point.\n");
return -5;
}
}
LOG_I("ELM FAT filesystem mounted.\n");
return RT_EOK;
}
INIT_APP_EXPORT(filesystem_mount);
#endif /* BSP_USING_SPI7/RT_USING_SFUD/RT_USING_DFS/RT_USING_DFS_ELMFAT */
INIT_APP_EXPORT(app_filesystem_init);
#endif /* BSP_USING_SPI7/RT_USING_SFUD/RT_USING_DFS/RT_USING_DFS_ELMFAT */