FAL (Flash Abstraction Layer) Flash 抽象层(3)

60 篇文章 12 订阅

使用 FAL 的基本步骤如下所示:

  1. 添加spi设备。
  2. 打开 FAL:从 Env 中打开 fal 软件包并下载到工程。
  3. FAL 移植:定义 flash 设备、定义 flash 设备表、定义 flash 分区表。以下主要对步骤 2 展开讲解。
  4. 调用 fal_init() 初始化该库:移植完成后,可在应用层调用,如在 main 函数中调用。
文件说明
applications/main.capp 入口(fal 例程程序)
portsfal 移植文件
ports/fal/fal_cfg.hfal 配置文件(Flash 设备配置 和 分区表配置)
ports/fal/fal_flash_sfud_port.cfal 操作片外 Nor Flash 的移植文件(将 Flash 读写擦接口注册到 fal)
ports/fal/fal_flash_stm32l4_port.cfal 操作片内 Flash 的移植文件(将 Flash 读写擦接口注册到 fal)
packages/falfal 软件包(fal 源码实现)
packages/fal/inc/fal.hfal 软件包对外提供的操作接口
fal_partition 结构体定义如下所示:

struct fal_partition
{
    uint32_t magic_word;

    /* FLASH 分区名称 */
    char name[FAL_DEV_NAME_MAX];
    /* FLASH 分区所在的 FLASH 设备名称 */
    char flash_name[FAL_DEV_NAME_MAX];

    /* FLASH 分区在 FLASH 设备的偏移地址 */
    long offset;
    size_t len;

    uint8_t reserved;
};
成员变量说明
magic_word魔法数,系统使用,用户无需关心
name分区名字,最大 23 个ASCII字符
flash_name分区所属的 Flash 设备名字,最大 23 个ASCII字符
offset分区起始地址相对 Flash 设备起始地址的偏移量
len分区大小,单位字节
reserved保留项
fal_flash_dev 结构体定义如下所示:

struct fal_flash_dev
{
    char name[FAL_DEV_NAME_MAX];

    /* FLASH 设备的起始地址 */
    uint32_t addr;
    size_t len;
    /* FLASH 设备最小擦除的块大小 */
    size_t blk_size;

    struct
    {
        int (*init)(void);
        int (*read)(long offset, uint8_t *buf, size_t size);
        int (*write)(long offset, const uint8_t *buf, size_t size);
        int (*erase)(long offset, size_t size);
    } ops;
};
成员变量说明
nameFlash 设备名字,最大 23 个ASCII字符
addrFlash 设备的起始地址(片内 Flash 为 0x08000000,片外 Flash 为 0x00)
lenFlash 设备容量,单位字节
blk_sizeFlash 设备最小擦除单元的大小,单位字节
ops.initFlash 设备的初始化函数,会在 fal_init 接口中调用
ops.readFlash 设备数据读取接口
ops.writeFlash 设备数据写入接口
ops.eraseFlash 设备数据擦除接口

 

 

 

具体操作

配置spi1

 

配置 FAL 分区

接添加 FAL 组件的初始化代码

extern const struct fal_flash_dev stm32_onchip_flash;

//extern const struct fal_flash_dev stm32_onchip_flash_128k;
extern  struct fal_flash_dev nor_flash0;

/* flash device table */
#define FAL_FLASH_DEV_TABLE                                          \
{                                                                    \
    &stm32_onchip_flash,                                         \
    &nor_flash0,                                        \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG

/* partition table */
#define FAL_PART_TABLE                                                                                                     \
{                                                                                                                          \
    {FAL_PART_MAGIC_WROD,        "boot",      "onchip_flash",    0,         32 * 1024, 0},   \
    {FAL_PART_MAGIC_WROD,        "app",       "onchip_flash",    32*1024,   (512-32) * 1024, 0},  \
    {FAL_PART_MAGIC_WROD,        "download",  "norflash0",       0 ,        1024 * 1024, 0},  \
    {FAL_PART_MAGIC_WROD,        "font",      "norflash0",      1024*1024,   512 * 1024,  0}, \
    {FAL_PART_MAGIC_WROD,        "ef",        "norflash0",      (1024+512)*1024,  (3072) * 1024,  0}, \

}

内涉及到drv_flash_f4.c

//lash 设备名称为 onchip_flash,设备容量为 512K,最小擦除单元为 2K,无初始化接口。
const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE,(2 * 1024), {NULL, stm32_flash_read, stm32_flash_write, stm32_flash_erase} };

 

 

https://github.com/RT-Thread/IoT_Board/tree/master/examples/13_component_fal

 

    config BSP_USING_ON_CHIP_FLASH
        bool "Enable on-chip FLASH"
        default n

const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, (1024 * 1024),  0,  {NULL, stm32_flash_read, stm32_flash_write, stm32_flash_erase} };

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值