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 */
10
11 #include <board.h>
12 #include <drv_qspi.h>
13 #include <rtdevice.h>
14 #include <rthw.h>
15 #include <finsh.h>
16
17 #ifdef BSP_USING_QSPI_FLASH
18
19 #include "dev_spi_flash.h"
20 #include "dev_spi_flash_sfud.h"
21
w25qxx_read_status_register2(struct rt_qspi_device * device)22 char w25qxx_read_status_register2(struct rt_qspi_device *device)
23 {
24 /* 0x35 read status register2 */
25 char instruction = 0x35, status;
26
27 rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
28
29 return status;
30 }
31
w25qxx_write_enable(struct rt_qspi_device * device)32 void w25qxx_write_enable(struct rt_qspi_device *device)
33 {
34 /* 0x06 write enable */
35 char instruction = 0x06;
36
37 rt_qspi_send(device, &instruction, 1);
38 }
39
w25qxx_enter_qspi_mode(struct rt_qspi_device * device)40 void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
41 {
42 char status = 0;
43 /* 0x38 enter qspi mode */
44 char instruction = 0x38;
45 char write_status2_buf[2] = {0};
46
47 /* 0x31 write status register2 */
48 write_status2_buf[0] = 0x31;
49
50 status = w25qxx_read_status_register2(device);
51 if (!(status & 0x02))
52 {
53 status |= 1 << 1;
54 w25qxx_write_enable(device);
55 write_status2_buf[1] = status;
56 rt_qspi_send(device, &write_status2_buf, 2);
57 rt_qspi_send(device, &instruction, 1);
58 rt_kprintf("flash already enter qspi mode\n");
59 rt_thread_mdelay(10);
60 }
61 }
62
rt_hw_qspi_flash_with_sfud_init(void)63 static int rt_hw_qspi_flash_with_sfud_init(void)
64 {
65 rt_hw_qspi_device_attach("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
66
67 /* init W25Q256 */
68 if (RT_NULL == rt_sfud_flash_probe("W25Q256", "qspi10"))
69 {
70 return -RT_ERROR;
71 }
72
73 return RT_EOK;
74 }
75 INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);
76
77 #endif/* BSP_USING_QSPI_FLASH */
78