1、新建基于F407ZG工程
开发板使用的是正点原子STM32F407ZG探索者。新建工程工打开CubeMx进行SPI接口、RCC时钟、USART1配置如下:
生成代码,关闭Cubemx。必须关闭Cubemx,rtthread studio才会更新工程
2、配置使能rtt的SPI flash和FAL功能
3、修改FLA分区信息
编译报错,增加fal_cfg.h头文件路径。
我们使用的是W25Q128芯片,芯片大小为65536*256bit即16M,对fal_flash_sfud_port.c文件进行更改。
struct fal_flash_dev nor_flash0 =
{
.name = FAL_USING_NOR_FLASH_DEV_NAME,
.addr = 0,
.len = 16 * 1024 * 1024, //w25q128 大小为65536*256 = 16777216byte = 16M 128Mbit
.blk_size = 4096,
.ops = {init, read, write, erase},
.write_gran = 1
};
我们没有使能片上的flash,需要对分区表进行调整,修改fal_cfg.h文件如下。
/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* 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>
#define NOR_FLASH_DEV_NAME "W25Q128"
/* ===================== 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, "download", NOR_FLASH_DEV_NAME, 0, 896*1024, 0}, \
{FAL_PART_MAGIC_WORD, "factory", NOR_FLASH_DEV_NAME, 896*1024, 896*1024, 0}, \
{FAL_PART_MAGIC_WORD, "filesystem", NOR_FLASH_DEV_NAME, 2*896*1024, 14592*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */
修改main.c文件,进行fal初始化
#include <rtthread.h>
#include "fal.h"
int main(void)
{
fal_init();
rt_kprintf("Hello RT-Thread!\r\n");
while (1)
{
rt_thread_mdelay(1000);
}
return RT_EOK;
}
3、SPI接口使能及挂载
修改board.h文件,使能SPI1接口
增加drv_spiflash.c文件,用于SPI flash的挂载。
#include "board.h"
#include <rtthread.h>
#include <rtdevice.h>
#include "drv_spi.h"
#include "drv_config.h"
#include <string.h>
#include "spi_flash_sfud.h"
static int rt_hw_spi_flash_init(void)
{
__HAL_RCC_GPIOF_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14);
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
{
return -RT_ERROR;
};
return RT_EOK;
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
4、下载验证