1.查询qspi配置过程

  文件路径: /drivers/board.h

  rtthread qspi w25q256   sfud-风君雪科技博客

    #define BSP_USING_QSPI     //注释中详细描述了qspi配置过程

2.使能rtt qspi驱动

    打开 RT-Thread Settings
    选择SPI功能后,右键详细配置:分别勾

  rtthread qspi w25q256   sfud-风君雪科技博客

3.使能使用 qspi bsp

  打开 /drivers/board.h

  #define BSP_USING_QSPI  

4.初始化qspi硬件

  通过 stm32cubemx工具使能qspi硬件,在一下路径

  CoreSrcstm32f7xx_hal_msp.c中将HAL_QSPI_MspInit拷贝到 /drivers/board.c中

  rtthread qspi w25q256   sfud-风君雪科技博客

 5.打开相应的HAL库

  打开 /drivers/ stm32f7xx_hal_conf.h

  使能 #define HAL_QSPI_MODULE_ENABLED
  使能 #define HAL_SPI_MODULE_ENABLED

 6.编译验证

  烧录,在串口终端输入**>list_device   可以看到spi总线

  rtthread qspi w25q256   sfud-风君雪科技博客

7.挂载W25qspi设备

   

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
/**
  *  读取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
");
        rt_thread_mdelay(10);
    }
}

/**
 *挂在设备到qspi总线上
 * @return 挂载状态
 */
static rt_err_t rt_hw_qspi_flash_with_sfud_init(void)
{
    /*W25Q256挂在qspi总线上*/
    rt_err_t ret = stm32_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
");
        return -RT_ERROR;
    }
    rt_kprintf("qspi attach device success
");
    return RT_EOK;
}

/*设备自动初始化 */
INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);

static int rt_hw_qspi_block_device_init(void)
{
    /*   W25Q256 注册为快设备*/
    if (RT_NULL == rt_sfud_flash_probe("W25Q256", "qspi10"))
    {
        rt_kprintf("flash sfud failed
");
        return -RT_ERROR;
    }
    rt_kprintf("flash sfud success
");
    return RT_EOK;
}

/*设备自动初始化 */
INIT_DEVICE_EXPORT(rt_hw_qspi_block_device_init);

#define W25Q_SPI_DEVICE_NAME     "qspi10"
static void qspi_w25q_sample(int argc, char *argv[])
{
    struct rt_spi_device *qspi_dev_w25q;
    char name[RT_NAME_MAX];
    rt_uint8_t w25x_read_id = 0x90;
    rt_uint8_t id[5] = {0};

    if (argc == 2)
    {
        rt_strncpy(name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
    }

    /* 查找 spi 设备获取设备句柄 */
    qspi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);//根据 SPI 设备名称查找设备获取设备句柄
    if (!qspi_dev_w25q)
    {
        rt_kprintf("qspi sample run failed! can't find %s device!
", name);
    }
    else
    {
        /* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */
        rt_qspi_send_then_recv(qspi_dev_w25q, &w25x_read_id, 1, id, 5);//先发送后接收数据id
        rt_kprintf("use rt_qspi_send_then_recv() read w25q ID is:%x%x
", id[3], id[4]);
    }
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(qspi_w25q_sample, qspi w25q sample);

   

8.创建 快设备

  rtthread qspi w25q256   sfud-风君雪科技博客

9.挂载文件系统

  使能 fatfs
  打开 RT-Thread Settings
  选择fatfs功能后,右键详细配置:分别勾选

  rtthread qspi w25q256   sfud-风君雪科技博客

static int mnt_init(void)
{
    dfs_mkfs("elm", "W25Q256");//格式化设备
    if(dfs_mount("W25Q256", "/", "elm", 0, 0)==0)//挂载文件
    {
        rt_kprintf("dfs mount success
");
        return RT_EOK;
    }else {
        rt_kprintf("dfs mount failed
");
        return -RT_ERROR;
    }
}
/* 组件自动初始化 */
INIT_COMPONENT_EXPORT(mnt_init);

10.编译验证
  烧录,在串口终端输入 ls
  rtthread qspi w25q256   sfud-风君雪科技博客