RT-Thread Studio STM32F249BIT6片内FLASH移植FAL记录

RT-Thread Studio STM32F249BIT6移植FAL记录

刚好需要使用到操作内部flash,发现FAL这个很不错,移植过程遇到一些小bug,故此记录一下。

我使用的是STM32F429BIT6 内部有2M的flash和网上大部分人不一样。

1.先增加软件包

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-42yEQLN0-1641811651714)(https://os.zhengshijian.com//img/image-20220110183319490.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BZSFjp4G-1641811651716)(https://os.zhengshijian.com//img/image-20220110183309722.png)]

在rtt studio没找到flash的地方,直接再board.h中增加宏来打开
在这里插入图片描述

2.增加port文件

image-20220110183409700

下面是这两个文件的代码

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_

#include <rtconfig.h>
#include <board.h>


/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev stm32f4_onchip_flash;

/* flash device table */
#define FAL_FLASH_DEV_TABLE                                          \
{                                                                    \
    &stm32f4_onchip_flash,                                           \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE                                                                  \
{                                                                                       \
    {FAL_PART_MAGIC_WORD,        "bl",      "stm32f4_onchip",         0,  128*1024,   0}, \
    {FAL_PART_MAGIC_WORD,       "app1",     "stm32f4_onchip",   0x20000,  7*128*1024, 0}, \
    {FAL_PART_MAGIC_WORD,        "arg",     "stm32f4_onchip",  0x100000,  128*1024,   0}, \
    {FAL_PART_MAGIC_WORD,       "app2",     "stm32f4_onchip",  0x120000,  7*128*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */

#endif /* _FAL_CFG_H_ */

c文件

/*
 * File      : fal_flash_stm32f4_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 <stm32f4xx.h>

#define DBG_TAG "fal_flash_port"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>

/* base address of the flash sectors */
#define ADDR_FLASH_SECTOR_0      ((uint32_t)0x08000000) /* Base address of Sector 0, 16 K bytes   */
#define ADDR_FLASH_SECTOR_1      ((uint32_t)0x08004000) /* Base address of Sector 1, 16 K bytes   */
#define ADDR_FLASH_SECTOR_2      ((uint32_t)0x08008000) /* Base address of Sector 2, 16 K bytes   */
#define ADDR_FLASH_SECTOR_3      ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 K bytes   */
#define ADDR_FLASH_SECTOR_4      ((uint32_t)0x08010000) /* Base address of Sector 4, 64 K bytes   */
#define ADDR_FLASH_SECTOR_5      ((uint32_t)0x08020000) /* Base address of Sector 5, 128 K bytes  */
#define ADDR_FLASH_SECTOR_6      ((uint32_t)0x08040000) /* Base address of Sector 6, 128 K bytes  */
#define ADDR_FLASH_SECTOR_7      ((uint32_t)0x08060000) /* Base address of Sector 7, 128 K bytes  */
#define ADDR_FLASH_SECTOR_8      ((uint32_t)0x08080000) /* Base address of Sector 8, 128 K bytes  */
#define ADDR_FLASH_SECTOR_9      ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 K bytes  */
#define ADDR_FLASH_SECTOR_10     ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 K bytes */
#define ADDR_FLASH_SECTOR_11     ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 K bytes */

#define ADDR_FLASH_SECTOR_12      ((uint32_t)0x08100000) /* Base address of Sector 0, 16 K bytes   */
#define ADDR_FLASH_SECTOR_13      ((uint32_t)0x08104000) /* Base address of Sector 1, 16 K bytes   */
#define ADDR_FLASH_SECTOR_14      ((uint32_t)0x08108000) /* Base address of Sector 2, 16 K bytes   */
#define ADDR_FLASH_SECTOR_15      ((uint32_t)0x0810C000) /* Base address of Sector 3, 16 K bytes   */
#define ADDR_FLASH_SECTOR_16      ((uint32_t)0x08110000) /* Base address of Sector 4, 64 K bytes   */
#define ADDR_FLASH_SECTOR_17      ((uint32_t)0x08120000) /* Base address of Sector 5, 128 K bytes  */
#define ADDR_FLASH_SECTOR_18      ((uint32_t)0x08140000) /* Base address of Sector 6, 128 K bytes  */
#define ADDR_FLASH_SECTOR_19      ((uint32_t)0x08160000) /* Base address of Sector 7, 128 K bytes  */
#define ADDR_FLASH_SECTOR_20      ((uint32_t)0x08180000) /* Base address of Sector 8, 128 K bytes  */
#define ADDR_FLASH_SECTOR_21      ((uint32_t)0x081A0000) /* Base address of Sector 9, 128 K bytes  */
#define ADDR_FLASH_SECTOR_22      ((uint32_t)0x081C0000) /* Base address of Sector 10, 128 K bytes */
#define ADDR_FLASH_SECTOR_23      ((uint32_t)0x081E0000) /* Base address of Sector 11, 128 K bytes */

/**
 * Get the sector of a given address
 *
 * @param address flash address
 *
 * @return The sector of a given address
 */
static uint32_t stm32_get_sector(uint32_t address)
{
    uint32_t sector = 0;

    if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
    {
        sector = FLASH_SECTOR_0;
    }
    else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
    {
        sector = FLASH_SECTOR_1;
    }
    else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
    {
        sector = FLASH_SECTOR_2;
    }
    else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
    {
        sector = FLASH_SECTOR_3;
    }
    else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
    {
        sector = FLASH_SECTOR_4;
    }
    else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
    {
        sector = FLASH_SECTOR_5;
    }
    else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
    {
        sector = FLASH_SECTOR_6;
    }
    else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
    {
        sector = FLASH_SECTOR_7;
    }
    else if ((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
    {
        sector = FLASH_SECTOR_8;
    }
    else if ((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
    {
        sector = FLASH_SECTOR_9;
    }
    else if ((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
    {
        sector = FLASH_SECTOR_10;
    }else if ((address < ADDR_FLASH_SECTOR_12) && (address >= ADDR_FLASH_SECTOR_11)){
        sector = FLASH_SECTOR_11;
    }
    else if ((address < ADDR_FLASH_SECTOR_13) && (address >= ADDR_FLASH_SECTOR_12))
    {
        sector = FLASH_SECTOR_12;
    }
    else if ((address < ADDR_FLASH_SECTOR_14) && (address >= ADDR_FLASH_SECTOR_13))
    {
        sector = FLASH_SECTOR_13;
    }
    else if ((address < ADDR_FLASH_SECTOR_15) && (address >= ADDR_FLASH_SECTOR_14))
    {
        sector = FLASH_SECTOR_14;
    }
    else if ((address < ADDR_FLASH_SECTOR_16) && (address >= ADDR_FLASH_SECTOR_15))
    {
        sector = FLASH_SECTOR_15;
    }
    else if ((address < ADDR_FLASH_SECTOR_17) && (address >= ADDR_FLASH_SECTOR_16))
    {
        sector = FLASH_SECTOR_16;
    }
    else if ((address < ADDR_FLASH_SECTOR_18) && (address >= ADDR_FLASH_SECTOR_17))
    {
        sector = FLASH_SECTOR_17;
    }
    else if ((address < ADDR_FLASH_SECTOR_19) && (address >= ADDR_FLASH_SECTOR_18))
    {
        sector = FLASH_SECTOR_18;
    }
    else if ((address < ADDR_FLASH_SECTOR_20) && (address >= ADDR_FLASH_SECTOR_19))
    {
        sector = FLASH_SECTOR_19;
    }
    else if ((address < ADDR_FLASH_SECTOR_21) && (address >= ADDR_FLASH_SECTOR_20))
    {
        sector = FLASH_SECTOR_20;
    }else if ((address < ADDR_FLASH_SECTOR_22) && (address >= ADDR_FLASH_SECTOR_21))
    {
        sector = FLASH_SECTOR_21;
    }
    else if ((address < ADDR_FLASH_SECTOR_23) && (address >= ADDR_FLASH_SECTOR_22))
    {
        sector = FLASH_SECTOR_22;
    }else {
        sector = FLASH_SECTOR_23;
    }

    return sector;
}

/**
 * Get the sector size
 *
 * @param sector sector
 *
 * @return sector size
 */
static uint32_t stm32_get_sector_size(uint32_t sector) {
    assert(IS_FLASH_SECTOR(sector));

    switch (sector) {
    case FLASH_SECTOR_0: return 16 * 1024;
    case FLASH_SECTOR_1: return 16 * 1024;
    case FLASH_SECTOR_2: return 16 * 1024;
    case FLASH_SECTOR_3: return 16 * 1024;
    case FLASH_SECTOR_4: return 64 * 1024;
    case FLASH_SECTOR_5: return 128 * 1024;
    case FLASH_SECTOR_6: return 128 * 1024;
    case FLASH_SECTOR_7: return 128 * 1024;
    case FLASH_SECTOR_8: return 128 * 1024;
    case FLASH_SECTOR_9: return 128 * 1024;
    case FLASH_SECTOR_10: return 128 * 1024;
    case FLASH_SECTOR_11: return 128 * 1024;

    case FLASH_SECTOR_12: return 16 * 1024;
    case FLASH_SECTOR_13: return 16 * 1024;
    case FLASH_SECTOR_14: return 16 * 1024;
    case FLASH_SECTOR_15: return 16 * 1024;
    case FLASH_SECTOR_16: return 64 * 1024;
    case FLASH_SECTOR_17: return 128 * 1024;
    case FLASH_SECTOR_18: return 128 * 1024;
    case FLASH_SECTOR_19: return 128 * 1024;
    case FLASH_SECTOR_20: return 128 * 1024;
    case FLASH_SECTOR_21: return 128 * 1024;
    case FLASH_SECTOR_22: return 128 * 1024;
    case FLASH_SECTOR_23: return 128 * 1024;

    default : return 128 * 1024;
    }
}
static int init(void)
{
    /* do nothing now */
}

static int read(long offset, uint8_t *buf, size_t size)
{
    size_t i;
    uint32_t addr = stm32f4_onchip_flash.addr + offset;

    if ((addr + size) > STM32_FLASH_END_ADDRESS)
    {
        LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
        return -1;
    }

    for (i = 0; i < size; i++, addr++, buf++)
    {
        *buf = *(uint8_t *) addr;
    }

    return size;
}

static int write(long offset, const uint8_t *buf, size_t size)
{
    size_t i;
    rt_err_t result      = RT_EOK;
    uint32_t read_data;
    uint32_t addr = stm32f4_onchip_flash.addr + offset;
    uint32_t end_addr = addr + size;

    if ((end_addr) > STM32_FLASH_END_ADDRESS)
    {
        LOG_E("write outrange flash size! addr is (0x%p)", (void*)(addr + size));
        return -RT_EINVAL;
    }

    if (size < 1)
    {
        return -RT_EINVAL;
    }

    HAL_FLASH_Unlock();

    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

    for (size_t i = 0; i < size; i++, addr++, buf++)
    {
        /* write data to flash */
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK)
        {
            if (*(rt_uint8_t *)addr != *buf)
            {
                result = -RT_ERROR;
                break;
            }
        }
        else
        {
            result = -RT_ERROR;
            break;
        }
    }

    HAL_FLASH_Lock();

    if (result != RT_EOK)
    {
        return result;
    }

    return size;
}

static int erase(long offset, size_t size)
{
    uint32_t addr = stm32f4_onchip_flash.addr + offset;

    rt_err_t result = RT_EOK;
    rt_uint32_t FirstSector = 0, NbOfSectors = 0;
    rt_uint32_t SECTORError = 0;

    if ((addr + size) > STM32_FLASH_END_ADDRESS)
    {
        LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
        return -RT_EINVAL;
    }

    /*Variable used for Erase procedure*/
    FLASH_EraseInitTypeDef EraseInitStruct;

    /* Unlock the Flash to enable the flash control register access */
    HAL_FLASH_Unlock();

    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

    /* Get the 1st sector to erase */
    FirstSector = stm32_get_sector(addr);
    /* Get the number of sector to erase from 1st sector*/
    NbOfSectors = stm32_get_sector(addr + size - 1) - FirstSector + 1;
    /* Fill EraseInit structure*/
    EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.VoltageRange  = FLASH_VOLTAGE_RANGE_3;
    EraseInitStruct.Sector        = FirstSector;
    EraseInitStruct.NbSectors     = NbOfSectors;

    if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *)&SECTORError) != HAL_OK)
    {
        result = -RT_ERROR;
        goto __exit;
    }

__exit:
    HAL_FLASH_Lock();

    if (result != RT_EOK)
    {
        return result;
    }

    LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
    return size;

}

static int stm32_flash_init(void)
{
    /* doing nothing now */
}

const struct fal_flash_dev stm32f4_onchip_flash =
{
    .name       = "stm32f4_onchip",
    .addr       = 0x08000000,  // Flash start address
    .len        = 2*1024*1024, //stm32f429bit6 2M Flash
    .blk_size   = 128*1024,    //largest sector size
    //.ops        = {stm32_flash_init, stm32_flash_read, stm32_flash_write, stm32_flash_erase},
    .ops        = {stm32_flash_init, read, write, erase},
    .write_gran = 8
};


3.命令行测试

首先需要先在主函数初始化

image-20220110183937740

下面使用的是msh测试,没有代码测试。

image-20220110184058701

注意点:

  1. 分区表的flash设备名要和fal_flash_dev中定义的名称一致
  2. 我没有用rtthread里的drv_flash_f4.c的函数,最开始用这个出错了,被我改掉了。

image-20220110183652442他这里用的是3个dev,每个dev地址还要自己配置,我这个是2个bank的flash,跟普通的不一样,所以得自己写。应该就是地址出错了,所以用他的flash读写擦函数出错了。
很多教程都是多个dev设备,我这里直接用了单一个dev,然后多个分区表基于这个dev。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kMbGBpbc-1641811651717)(https://os.zhengshijian.com//img/image-20220110183842189.png)]

好了,FAL弄好了,统一了操作接口,OTA就好弄了,后面如果改到外部SPI FLASH也极其方便,现在这个2M Flash够我用,所以也就不弄外部SPI FLASH了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

炒饭多加个蛋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值