SFUD配置
1.在组件中启用SFUD

2.由于外部W25Q128使用的是SPI2,所以需要在board.h中开启。注意在STM32CUBE中打开SPI功能,我外部

3.新建一个源文件w25q128.c
#include <w25q128.h>
#include <sfud.h>
#include "main.h"
int rt_hw_spi_flash_init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
rt_hw_spi_device_attach("spi2", "spi20", GPIOB, GPIO_PIN_11);
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi20"))
{
return -RT_ERROR;
};
return RT_EOK;
}
w25q128.h
#ifndef APPLICATIONS_W25Q128_H_
#define APPLICATIONS_W25Q128_H_
int rt_hw_spi_flash_init(void);
#endif /* APPLICATIONS_W25Q128_H_ */
4.mian中初始化
rt_hw_spi_flash_init();编译后通过终端可发送sf产看命令
先初始化硬件
发送sf probe spi20 因为是SPI2的第一个设备所以是spi20,按照硬件来

终端提示挂在成功并显示内存容量,W25q128,是128兆位,需除以8就是实际容量即16MB
FAL配置
1.配置FAL

注意这里的W25Q128要和 rt_sfud_flash_probe(“W25Q128”, “spi20”)一致
编译之后如果会报错,找不到fal_cfg.h;放到drivers下就可以了
由于只对外部SPI FLASH进行分区管理,所以删除原来关于片上stm32f2_onchip_flash等定义
修改后fal_cfg.h代码如下,分区默认没有设计,按照自己要求分区即可
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_
#include <rtconfig.h>
#include <board.h>
#define NOR_FLASH_DEV_NAME "W25Q128"
/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev stm32f2_onchip_flash;
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, "easyflash", NOR_FLASH_DEV_NAME, 0, 1024*1024, 0}, \
{FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
main函数中加入
rt_hw_spi_flash_init();
fal_init();
进行初始化,另外需要修改存储器容量搜索如下进行修改
const struct fal_flash_dev nor_flash0 = { "norflash0", 0, 16*1024*1024, 4096, {NULL, read, write, erase} };
将8修改成16,对应16M
终端调试正常

另外在调试中发现(sfud_norflash0.init_ok) has assert failed at read.错误,搞了半天。后来发现是自己在RT-Thread studio,中启用了FAL但是调试的时候编译不通过,怀疑默认的FAL固件有问题,于是从GITHUB上移植的FAL程序,把之前的删除了。最后总是提示这个,名称也对应了找不到问题点。于是觉得移植过程可能跟它自己生成的fal在某些地方匹配不了,于是还原了默认生成的fal。这里注意如果你删除自带的fal,通过配置停用再启用是不会还原默认的fal的。我没办法重新创建了工程重新生成,最后解决了。