首先要把我写的前面2章搞通,到这里基本也就水到渠成了。
挂内部flash
https://blog.csdn.net/qq_35593582/article/details/117536968
挂SPI flash进系统:
https://blog.csdn.net/qq_35593582/article/details/119065138
以上2步都搞定,绕过那些坑之后,只需要以下3步,并且无坑实现。
1.改一下挂载的spi flash设备名字
在之前的文章里main函数内设备的名字叫"w25q64”,把它改成"nor_flash0"。
改完之后的main函数是这样的:
int main(void)
{
rt_hw_spi_device_attach("spi1","spi10",GPIOA,GPIO_PIN_15);
if(rt_sfud_flash_probe("nor_flash0","spi10"))
{
rt_kprintf("rt sfud flash success");
}
else {
rt_kprintf("rt sfud flash error");
}
fal_init();
// fal_init_check();
easyflash_init();
}
步骤很明确,就像把大象装进冰箱里一样。
首先初始化SPI总线
再在SPI总线上挂了一个nor_flash0设备。
当nor_flash设备已经进系统了之后,再进fal。所以下一步是进fal对flash的接口驱动。
2.打开fal_flash_sfud_port.c配置flash信息。
#ifndef FAL_USING_NOR_FLASH_DEV_NAME
#define FAL_USING_NOR_FLASH_DEV_NAME "nor_flash0"
#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
};
上面的宏定义DEV_NAME就是这个设备在系统里面注册的名字,所以在main函数里面改的跟这里一致。
下面nor_flash参数,注意len是根据你的flash大小来的,我的是Q64
blk_size=4096。
一般要改的也就是这几个。
这里我猜想一下,应该是有什么接口可以省略这一步,因为像这种flash在执行
rt_sfud_flash_probe("nor_flash0","spi10")
的时候,是能够读到flash这些相关信息的,所以理论上应该有什么办法做到全自动对接,但我也懒得研究了。
3.打开fal_cfg.h,配置nor_flash
extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k;
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_16k, \
&stm32_onchip_flash_64k, \
&stm32_onchip_flash_128k, \
&nor_flash0,\
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "bl", "onchip_flash_16k", 0, 64*1024, 0}, \
{FAL_PART_MAGIC_WORD, "app", "onchip_flash_64k", 0, 64*1024, 0}, \
{FAL_PART_MAGIC_WORD, "app2", "onchip_flash_128k", 0, 4*128*1024, 0}, \
{FAL_PART_MAGIC_WORD, "ota", "onchip_flash_128k", 4*128*1024, 2*128*1024, 0}, \
{FAL_PART_MAGIC_WORD, "easyflash", "nor_flash0", 0, 4*1024*1024, 0}, \
}//0x0808000,start ADDR_FLASH_SECTOR_8
#endif /* FAL_PART_HAS_TABLE_CFG */
设备分区设备和分区表内添加nor_flash设备。
这样就可以了,跟以前一样操作easyflash。
这哥们写的也不错,可以参考。