1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-11-27     zylx         first version
9  * 2019-04-11     ZYH          port from stm32f7serial
10  */
11 
12 #include <board.h>
13 #include <drv_qspi.h>
14 #include <rtdevice.h>
15 #include <rthw.h>
16 #include <finsh.h>
17 
18 #ifdef BSP_USING_QSPI_FLASH
19 
20 #include "dev_spi_flash.h"
21 #include "dev_spi_flash_sfud.h"
22 
n25qxxa_read_status_register2(struct rt_qspi_device * device)23 char n25qxxa_read_status_register2(struct rt_qspi_device *device)
24 {
25     /* 0x35 read status register2 */
26     char instruction = 0x35, status;
27 
28     rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
29 
30     return status;
31 }
32 
n25qxxa_write_enable(struct rt_qspi_device * device)33 void n25qxxa_write_enable(struct rt_qspi_device *device)
34 {
35     /* 0x06 write enable */
36     char instruction = 0x06;
37 
38     rt_qspi_send(device, &instruction, 1);
39 }
40 
n25qxxa_enter_qspi_mode(struct rt_qspi_device * device)41 void n25qxxa_enter_qspi_mode(struct rt_qspi_device *device)
42 {
43     char status = 0;
44     /* 0x38 enter qspi mode */
45     char instruction = 0x38;
46     char write_status2_buf[2] = {0};
47 
48     /* 0x31 write status register2 */
49     write_status2_buf[0] = 0x31;
50 
51     status = n25qxxa_read_status_register2(device);
52     if (!(status & 0x02))
53     {
54         status |= 1 << 1;
55         n25qxxa_write_enable(device);
56         write_status2_buf[1] = status;
57         rt_qspi_send(device, &write_status2_buf, 2);
58         rt_qspi_send(device, &instruction, 1);
59         rt_kprintf("flash already enter qspi mode\n");
60         rt_thread_mdelay(10);
61     }
62 }
63 
rt_hw_qspi_flash_with_sfud_init(void)64 static int rt_hw_qspi_flash_with_sfud_init(void)
65 {
66     rt_hw_qspi_device_attach("qspi1", "qspi10", RT_NULL, 4, n25qxxa_enter_qspi_mode, RT_NULL);
67 
68     /* init n25qxx */
69     if (RT_NULL == rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "qspi10"))
70     {
71         return -RT_ERROR;
72     }
73 
74     return RT_EOK;
75 }
76 INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_with_sfud_init);
77 
78 #endif/* BSP_USING_QSPI_FLASH */
79