外部 flash 挂载 littlefs 文件系统
打开 fal 软件包
打开 littlefs 软件包
打开文件系统
打开 MTD
fal_cfg.h
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-04-28 tyustli first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtthread.h>
#include <board.h>
extern const struct fal_flash_dev nor_flash0;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&nor_flash0, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WROD, "filesystem", "nor_flash", 0 , 16 * 1024 * 1024 , 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
main.c
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-04-28 tyustli first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "spi_flash.h"
#include "spi_flash_sfud.h"
#include "drv_spi.h"
#define FS_PARTITION_NAME "filesystem"
#include "fal.h"
#include "dfs_file.h"
int main(int argc, char *argv[])
{
__HAL_RCC_GPIOB_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14);
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
{
return -RT_ERROR;
};
fal_init();
struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
if (flash_dev == NULL)
{
rt_kprintf("Can't create a mtd device on '%s' partition.\n", FS_PARTITION_NAME);
}
else
{
rt_kprintf("Create a mtd device on the %s partition of flash successful.\n", FS_PARTITION_NAME);
}
if(rt_device_find(FS_PARTITION_NAME) != RT_NULL)
{
dfs_mkfs("lfs", FS_PARTITION_NAME);
if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
{
rt_kprintf("onchip lfs filesystem mount to '/'\n");
}
else
{
rt_kprintf("onchip lfs filesystem mount to '/' failed!\n");
}
}
else
{
rt_kprintf("find filesystem portion failed\r\n");
}
return RT_EOK;
}
spi_flash_port.c
#include <fal.h>
#include <sfud.h>
#include <spi_flash_sfud.h>
sfud_flash sfud_norflash0;
static int fal_sfud_init(void);
static int read(long offset, uint8_t *buf, size_t size);
static int write(long offset, const uint8_t *buf, size_t size);
static int erase(long offset, size_t size);
const struct fal_flash_dev nor_flash0 = { "nor_flash", 0, (16 * 1024 * 1024), 4096, {fal_sfud_init, read, write, erase} };
static int fal_sfud_init(void)
{
sfud_flash_t sfud_flash0 = NULL;
sfud_flash0 = (sfud_flash_t)rt_sfud_flash_find("spi10");
if (NULL == sfud_flash0)
{
return -1;
}
sfud_norflash0 = *sfud_flash0;
return 0;
}
static int read(long offset, uint8_t *buf, size_t size)
{
sfud_read(&sfud_norflash0, nor_flash0.addr + offset, size, buf);
return size;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
if (sfud_write(&sfud_norflash0, nor_flash0.addr + offset, size, buf) != SFUD_SUCCESS)
{
return -1;
}
return size;
}
static int erase(long offset, size_t size)
{
if (sfud_erase(&sfud_norflash0, nor_flash0.addr + offset, size) != SFUD_SUCCESS)
{
return -1;
}
return size;
}
版权声明:本文为tyustli原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。