项目使用RT-T操作系统,通过easyflash管理内存,以下记录是在项目中配置的相关过程,仅供参考。
单片机:AT32F435
Flash:W25Q64
硬件接线方式:qspi
以下为通过ENV配置过程:
1.配置FAL
2.在FAL里面把SFUD万能串行flash驱动勾选
3.配置设备驱动层
4.配置硬件设备
5.配置easyflash软件包
6.把MCU片上flash也配置上
7.添加qspi_flash_init.c文件到工程里面
根据自己硬件设计修改对应的设备名称
#include <rtthread.h>
#include "spi_flash.h"
#include "spi_flash_sfud.h"
#include "drv_qspi.h"
#if defined(BSP_USING_QSPI1)
/**
* 读取qspi状态寄存器2
* @param device : qspi设备
* @return
*/
char w25qxx_read_status_register2(struct rt_qspi_device *device)
{
/* 0x35 read status register2 */
char instruction = 0x35, status;
rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);//读取状态寄存器
return status;
}
/**
* qspi写使能
* @param device : qspi设备
*/
void w25qxx_write_enable(struct rt_qspi_device *device)
{
/* 0x06 write enable */
char instruction = 0x06;
rt_qspi_send(device, &instruction, 1);//发送数据
}
/**
* 使能qspi模式
* @param device : qspi设备
*/
void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
{
char status = 0;
/* 0x38 enter qspi mode */
char instruction = 0x38;
char write_status2_buf[2] = {0};
/* 0x31 write status register2 */
write_status2_buf[0] = 0x31;
status = w25qxx_read_status_register2(device);//读取状态寄存器2
if (!(status & 0x02))//判读当前flash通信模式
{
status |= 1 << 1;
w25qxx_write_enable(device); //w25q写使能
write_status2_buf[1] = status; //写状态寄存器
rt_qspi_send(device, &write_status2_buf, 2);//
rt_qspi_send(device, &instruction, 1);//设置qspi模式
rt_kprintf("flash already enter qspi mode\n");
rt_thread_mdelay(10);
}
}
/**
*挂在设备到qspi总线上
* @return 挂载状态
*/
static int rt_hw_qspi_flash_with_sfud_init(void)
{
/*W25Q256挂在qspi总线上*/
rt_err_t ret = at32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
if (ret != RT_EOK)
{
rt_kprintf("qspi attach device failed\n");
return -RT_ERROR;
}
rt_kprintf("qspi attach device success\n");
return RT_EOK;
}
/*设备自动初始化 */
INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);
/**
* 创建块设备
* @return
*/
static int rt_hw_qspi_block_device_init(void)
{
/* W25Q256 注册为快设备*/
if (RT_NULL == rt_sfud_flash_probe("W25Q64", "qspi10"))
{
rt_kprintf("flash sfud failed\n");
return -RT_ERROR;
}
rt_kprintf("flash sfud success\n");
return RT_EOK;
}
/*设备自动初始化 */
INIT_DEVICE_EXPORT(rt_hw_qspi_block_device_init);
#endif
8.easyflash接口的名字改一下 #define FAL_EF_PART_NAME "easyflash" 改为fal内存表里面有的名字
9.easyflash_init();这个函数要自己主动调用
10.Flash 抽象层fal_init();也要自己初始化调用
11.MCU的QSPI的接口也要按照真实的引脚配置去配置一下自动生成的不一定正确
12.Flash 设备表定义在
fal_cfg.h 头文件中,定义分区表前需 新建 fal_cfg.h 文件 ,请将该文件统一放在对应 BSP 或工程目录的 port 文件夹下,并将该头文件路径加入到工程。
*
* 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 "W25Q64"
/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev at32_onchip_flash;
extern struct fal_flash_dev nor_flash0;
/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&at32_onchip_flash, \
&nor_flash0, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "bl", "onchip_flash", 0, 64*1024, 0}, \
{FAL_PART_MAGIC_WORD, "app", "onchip_flash", 64*1024, 704*1024, 0}, \
{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_ */