1 /*
2 * Copyright (c) 2006-2022, RT-Thread Development Team
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Change Logs:
7 * Date Author Notes
8 * 2021-01-19 wanghaijing the first version
9 */
10
11 #include <rtthread.h>
12 #include <rtdevice.h>
13 #include <dev_spi_flash.h>
14 #include <drv_qspi.h>
15
16 #ifdef BSP_USING_QSPI_FLASH
17
w25qxx_read_status_register2(struct rt_qspi_device * device)18 char w25qxx_read_status_register2(struct rt_qspi_device *device)
19 {
20 /* 0x35 read status register2 */
21 char instruction = 0x35, status;
22
23 rt_qspi_send_then_recv(device, &instruction, 1, &status, 1);
24
25 return status;
26 }
27
w25qxx_write_enable(struct rt_qspi_device * device)28 void w25qxx_write_enable(struct rt_qspi_device *device)
29 {
30 /* 0x06 write enable */
31 char instruction = 0x06;
32
33 rt_qspi_send(device, &instruction, 1);
34 }
35
w25qxx_enter_qspi_mode(struct rt_qspi_device * device)36 void w25qxx_enter_qspi_mode(struct rt_qspi_device *device)
37 {
38 char status = 0;
39 /* 0x38 enter qspi mode */
40 char instruction = 0x38;
41 char write_status2_buf[2] = {0};
42
43 /* 0x31 write status register2 */
44 write_status2_buf[0] = 0x31;
45
46 status = w25qxx_read_status_register2(device);
47 if (!(status & 0x02))
48 {
49 status |= 1 << 1;
50 w25qxx_write_enable(device);
51 write_status2_buf[1] = status;
52 rt_qspi_send(device, &write_status2_buf, 2);
53 rt_qspi_send(device, &instruction, 1);
54 rt_kprintf("flash already enter qspi mode\n");
55 rt_thread_mdelay(10);
56 }
57 }
58
rt_qspi_flash_init(void)59 static int rt_qspi_flash_init(void)
60 {
61 extern rt_spi_flash_device_t rt_sfud_flash_probe(const char *spi_flash_dev_name, const char *spi_dev_name);
62
63 rt_hw_qspi_device_attach("qspi1", "qspi10", RT_NULL, 4, w25qxx_enter_qspi_mode, RT_NULL);
64 if (RT_NULL == rt_sfud_flash_probe("norflash1", "qspi10"))
65 {
66 return -RT_ERROR;
67 }
68 return RT_EOK;
69 }
70 INIT_ENV_EXPORT(rt_qspi_flash_init);
71
72 #endif/* BSP_USING_QSPI_FLASH */
73