文章目录[隐藏]
准备工作
1. FlashDB是基于FAL Flash抽象层,首先对FAL的移植:
FAL介绍
FAL (Flash Abstraction Layer) Flash 抽象层,是对 Flash 及基于 Flash
的分区进行管理、操作的抽象层,对上层统一了 Flash 及 分区操作的 API (框架图如下所示),并具有以下特性:
- 支持静态可配置的分区表,并可关联多个 Flash 设备;
- 分区表支持 自动装载 。避免在多固件项目,分区表被多次定义的问题;
- 代码精简,对操作系统 无依赖 ,可运行于裸机平台,比如对资源有一定要求的 Bootloader;
- 统一的操作接口。保证了文件系统、OTA、NVM(例如:EasyFlash) 等对 Flash 有一定依赖的组件,底层 Flash 驱动的可重用性; 自带基于 Finsh/MSH 的测试命令,可以通过 Shell 按字节寻址的方式操作(读写擦) Flash
或分区,方便开发者进行调试、测试
首先先完成FAL文件管理系统工作的移植
移植FAL文件说明和下载包链接如下:
https://gitee.com/RT-Thread-Mirror/fal/tree/master
2. FAL重要部分说明:
2.1在FAL例程目录中我们使用以下这个文件作为我们的移植使用文件:
2.2 搭建完成MDK开发环境后,打开 fal_flash_sfud_port.c 文件
2.3 红框部分使我们的设备名字和要初始化的一些相关参数
图中指示说明:
- 设备的名字,可以自己随意取,但是不能超过长度限制
- W25Q64的实际FLASH大小
- W25Q64一个擦除的扇区是多大
- 设备的初始化,设备flash读 写 擦除
- 写颗粒度W25Q默认1
3. 配置FAL的分区和分区大小
3.1打开以下 fal_cfg.h 文件
3.2 配置flash的分区和分区大小
图中指示说明:
- 设备的名字,建议当前分区用哪个设备起对应的名字,跟 5 对应上
- 初始化的设备驱动表,跟图2.3对上
- 魔术字,默认即可
- 分区的名字,建议自己使用的分区储存什么对应的文件,起对应相关的文件名字
- flash设备的名字,建议分区对应的Flash设备起对应flash设备的名字
- 分区的起始地址
- 分区的大小,同一个flash设备有多个分区的话,建议当前分区首地址是上一个分区的地址的大小
- 默认0即可
4. FAL是基于 SFUD 移植,请看 第二步
【FlashDB】第二步 FlashDB 移植 STM32L475 使用QSPI驱动外部 flash W25Q64之 SFUD 移植
下边是 fal_flash_sfud_port.c 的接口代码
/*
* File : fal_flash_sfud_port.c
* This file is part of FAL (Flash Abstraction Layer) package
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-01-26 armink the first version
*/
#include <fal.h>
#include <sfud.h>
#define FAL_USING_SFUD_PORT
#ifdef FAL_USING_SFUD_PORT
#ifdef RT_USING_SFUD
#include <spi_flash_sfud.h>
#endif
#ifndef FAL_USING_NOR_FLASH_DEV_NAME
#define FAL_USING_NOR_FLASH_DEV_NAME "norflash0"
#endif
static int 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);
static sfud_flash_t sfud_dev = NULL;
struct fal_flash_dev nor_flash0 =
{
.name = FAL_USING_NOR_FLASH_DEV_NAME,
.addr = 0,
.len = 8 * 1024 * 1024,
.blk_size = 4096,
.ops = {init, read, write, erase},
.write_gran = 1
};
static int init(void)
{
if (sfud_init() == SFUD_SUCCESS)
{
/* enable qspi fast read mode, set four data lines width */
sfud_dev = sfud_get_device(SFUD_W25_DEVICE_INDEX);
sfud_qspi_fast_read_enable(sfud_dev, 4);
}
if (NULL == sfud_dev)
{
return -1;
}
/* update the flash chip information */
nor_flash0.blk_size = sfud_dev->chip.erase_gran;
nor_flash0.len = sfud_dev->chip.capacity;
return 0;
}
static int read(long offset, uint8_t *buf, size_t size)
{
assert(sfud_dev);
assert(sfud_dev->init_ok);
sfud_read(sfud_dev, nor_flash0.addr + offset, size, buf);
return size;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
assert(sfud_dev);
assert(sfud_dev->init_ok);
if (sfud_write(sfud_dev, nor_flash0.addr + offset, size, buf) != SFUD_SUCCESS)
{
return -1;
}
return size;
}
static int erase(long offset, size_t size)
{
assert(sfud_dev);
assert(sfud_dev->init_ok);
if (sfud_erase(sfud_dev, nor_flash0.addr + offset, size) != SFUD_SUCCESS)
{
return -1;
}
return size;
}
#endif /* FAL_USING_SFUD_PORT */
下边是 fal_cfg.h 的配置代码
/*
* File : fal_cfg.h
* This file is part of FAL (Flash Abstraction Layer) package
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-05-17 armink the first version
*/
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#define FAL_DEBUG 1
#define FAL_PART_HAS_TABLE_CFG
#define NOR_FLASH_DEV_NAME "norflash0"
/* ===================== Flash device Configuration ========================= */
extern 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_WORD, "KVDB", NOR_FLASH_DEV_NAME, 0, 1 * 1024 * 1024, 0}, \
{FAL_PART_MAGIC_WORD, "TSDB", NOR_FLASH_DEV_NAME, 1 * 1024 * 1024, 4 * 1024 * 1024, 0} \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
版权声明:本文为CSDN博主「小石头有大内涵」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/shileiwu0505/article/details/123028933
暂无评论