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  * 2020-07-07     thread-liu   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 
22 /**
23 * @brief QSPI MSP Initialization
24 * This function configures the hardware resources used in this example
25 * @param hqspi: QSPI handle pointer
26 * @retval None
27 */
HAL_QSPI_MspInit(QSPI_HandleTypeDef * hqspi)28 void HAL_QSPI_MspInit(QSPI_HandleTypeDef* hqspi)
29 {
30   GPIO_InitTypeDef GPIO_InitStruct = {0};
31   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
32 
33   if(hqspi->Instance==QUADSPI)
34   {
35   /* USER CODE BEGIN QUADSPI_MspInit 0 */
36     if (IS_ENGINEERING_BOOT_MODE())
37     {
38         PeriphClkInit.Sdmmc12ClockSelection = RCC_QSPICLKSOURCE_ACLK;
39         PeriphClkInit.PeriphClockSelection  = RCC_PERIPHCLK_QSPI;
40         if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
41         {
42             Error_Handler();
43         }
44     }
45    /* USER CODE END QUADSPI_MspInit 0 */
46     /* Peripheral clock enable */
47     __HAL_RCC_QSPI_CLK_ENABLE();
48 
49     __HAL_RCC_GPIOF_CLK_ENABLE();
50     __HAL_RCC_GPIOB_CLK_ENABLE();
51     /**QUADSPI GPIO Configuration
52     PF6     ------> QUADSPI_BK1_IO3
53     PF7     ------> QUADSPI_BK1_IO2
54     PF8     ------> QUADSPI_BK1_IO0
55     PF9     ------> QUADSPI_BK1_IO1
56     PF10     ------> QUADSPI_CLK
57     PB6     ------> QUADSPI_BK1_NCS
58     */
59     GPIO_InitStruct.Pin = GPIO_PIN_10;
60     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
61     GPIO_InitStruct.Pull = GPIO_NOPULL;
62     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
63     GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
64     HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
65 
66     GPIO_InitStruct.Pin = GPIO_PIN_6;
67     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
68     GPIO_InitStruct.Pull = GPIO_PULLUP;
69     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
70     GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
71     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
72 
73     GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_6;
74     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
75     GPIO_InitStruct.Pull = GPIO_NOPULL;
76     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
77     GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
78     HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
79 
80     GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
81     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
82     GPIO_InitStruct.Pull = GPIO_NOPULL;
83     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
84     GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
85     HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
86 
87     /**QUADSPI GPIO Configuration
88     PC0     ------> QUADSPI_BK2_NCS
89     PH3     ------> QUADSPI_BK2_IO1
90     PG7     ------> QUADSPI_BK2_IO3
91     PG10     ------> QUADSPI_BK2_IO2
92     PH2     ------> QUADSPI_BK2_IO0
93     */
94     GPIO_InitStruct.Pin = GPIO_PIN_0;
95     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
96     GPIO_InitStruct.Pull = GPIO_PULLUP;
97     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
98     GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
99     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
100 
101     GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_2;
102     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
103     GPIO_InitStruct.Pull = GPIO_NOPULL;
104     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
105     GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
106     HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
107 
108     GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_10;
109     GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
110     GPIO_InitStruct.Pull = GPIO_NOPULL;
111     GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
112     GPIO_InitStruct.Alternate = GPIO_AF11_QUADSPI;
113     HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
114 
115   /* USER CODE BEGIN QUADSPI_MspInit 1 */
116 
117   /* USER CODE END QUADSPI_MspInit 1 */
118   }
119 }
120 
121 /**
122 * @brief QSPI MSP De-Initialization
123 * This function freeze the hardware resources used in this example
124 * @param hqspi: QSPI handle pointer
125 * @retval None
126 */
HAL_QSPI_MspDeInit(QSPI_HandleTypeDef * hqspi)127 void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef* hqspi)
128 {
129   if(hqspi->Instance==QUADSPI)
130   {
131   /* USER CODE BEGIN QUADSPI_MspDeInit 0 */
132 
133   /* USER CODE END QUADSPI_MspDeInit 0 */
134     /* Peripheral clock disable */
135     __HAL_RCC_QSPI_CLK_DISABLE();
136 
137     /**QUADSPI GPIO Configuration
138     PC0     ------> QUADSPI_BK2_NCS
139     PF10     ------> QUADSPI_CLK
140     PB6     ------> QUADSPI_BK1_NCS
141     PH3     ------> QUADSPI_BK2_IO1
142     PG7     ------> QUADSPI_BK2_IO3
143     PG10     ------> QUADSPI_BK2_IO2
144     PF7     ------> QUADSPI_BK1_IO2
145     PF6     ------> QUADSPI_BK1_IO3
146     PH2     ------> QUADSPI_BK2_IO0
147     PF8     ------> QUADSPI_BK1_IO0
148     PF9     ------> QUADSPI_BK1_IO1
149     */
150     HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
151 
152     HAL_GPIO_DeInit(GPIOF, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
153 
154     HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
155 
156     HAL_GPIO_DeInit(GPIOH, GPIO_PIN_3|GPIO_PIN_2);
157 
158     HAL_GPIO_DeInit(GPIOG, GPIO_PIN_7|GPIO_PIN_10);
159 
160   /* USER CODE BEGIN QUADSPI_MspDeInit 1 */
161 
162   /* USER CODE END QUADSPI_MspDeInit 1 */
163   }
164 
165 }
166 
rt_hw_qspi_flash_with_sfud_init(void)167 static int rt_hw_qspi_flash_with_sfud_init(void)
168 {
169     rt_hw_qspi_device_attach("qspi1", "qspi10", RT_NULL, 4, RT_NULL, RT_NULL);
170     /* init MX25L51245G */
171     if (RT_NULL == rt_sfud_flash_probe("MX25L51245G", "qspi10"))
172     {
173         return -RT_ERROR;
174     }
175 
176     return RT_EOK;
177 }
178 INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);
179 
180 #endif /* BSP_USING_QSPI_FLASH */
181