]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC/Nuvoton_Code/StdDriver/inc/qspi.h
Add Cortex M23 GCC and IAR ports. Add demo projects for Nuvoton NuMaker-PFM-2351.
[freertos] / FreeRTOS / Demo / CORTEX_MPU_M23_Nuvoton_NuMaker_PFM_M2351_IAR_GCC / Nuvoton_Code / StdDriver / inc / qspi.h
1 /******************************************************************************
2  * @file     qspi.h
3  * @version  V3.00
4  * @brief    M2351 series QSPI driver header file
5  *
6  * @copyright (C) 2017 Nuvoton Technology Corp. All rights reserved.
7 *****************************************************************************/
8 #ifndef __QSPI_H__
9 #define __QSPI_H__
10
11 #ifdef __cplusplus
12 extern "C"
13 {
14 #endif
15
16
17 /** @addtogroup Standard_Driver Standard Driver
18   @{
19 */
20
21 /** @addtogroup QSPI_Driver QSPI Driver
22   @{
23 */
24
25 /** @addtogroup QSPI_EXPORTED_CONSTANTS QSPI Exported Constants
26   @{
27 */
28
29 #define QSPI_MODE_0        (QSPI_CTL_TXNEG_Msk)                             /*!< CLKPOL=0; RXNEG=0; TXNEG=1 */
30 #define QSPI_MODE_1        (QSPI_CTL_RXNEG_Msk)                             /*!< CLKPOL=0; RXNEG=1; TXNEG=0 */
31 #define QSPI_MODE_2        (QSPI_CTL_CLKPOL_Msk | QSPI_CTL_RXNEG_Msk)       /*!< CLKPOL=1; RXNEG=1; TXNEG=0 */
32 #define QSPI_MODE_3        (QSPI_CTL_CLKPOL_Msk | QSPI_CTL_TXNEG_Msk)       /*!< CLKPOL=1; RXNEG=0; TXNEG=1 */
33
34 #define QSPI_SLAVE         (QSPI_CTL_SLAVE_Msk)                             /*!< Set as slave */
35 #define QSPI_MASTER        (0x0UL)                                          /*!< Set as master */
36
37 #define QSPI_SS                (QSPI_SSCTL_SS_Msk)                          /*!< Set SS */
38 #define QSPI_SS_ACTIVE_HIGH    (QSPI_SSCTL_SSACTPOL_Msk)                    /*!< SS active high */
39 #define QSPI_SS_ACTIVE_LOW     (0x0UL)                                      /*!< SS active low */
40
41 /* QSPI Interrupt Mask */
42 #define QSPI_UNIT_INT_MASK                (0x001UL)                        /*!< Unit transfer interrupt mask */
43 #define QSPI_SSACT_INT_MASK               (0x002UL)                        /*!< Slave selection signal active interrupt mask */
44 #define QSPI_SSINACT_INT_MASK             (0x004UL)                        /*!< Slave selection signal inactive interrupt mask */
45 #define QSPI_SLVUR_INT_MASK               (0x008UL)                        /*!< Slave under run interrupt mask */
46 #define QSPI_SLVBE_INT_MASK               (0x010UL)                        /*!< Slave bit count error interrupt mask */
47 #define QSPI_SLVTO_INT_MASK               (0x020UL)                        /*!< Slave Mode Time-out interrupt mask */
48 #define QSPI_TXUF_INT_MASK                (0x040UL)                        /*!< Slave TX underflow interrupt mask */
49 #define QSPI_FIFO_TXTH_INT_MASK           (0x080UL)                        /*!< FIFO TX threshold interrupt mask */
50 #define QSPI_FIFO_RXTH_INT_MASK           (0x100UL)                        /*!< FIFO RX threshold interrupt mask */
51 #define QSPI_FIFO_RXOV_INT_MASK           (0x200UL)                        /*!< FIFO RX overrun interrupt mask */
52 #define QSPI_FIFO_RXTO_INT_MASK           (0x400UL)                        /*!< FIFO RX time-out interrupt mask */
53
54 /* QSPI Status Mask */
55 #define QSPI_BUSY_MASK                    (0x01UL)                         /*!< Busy status mask */
56 #define QSPI_RX_EMPTY_MASK                (0x02UL)                         /*!< RX empty status mask */
57 #define QSPI_RX_FULL_MASK                 (0x04UL)                         /*!< RX full status mask */
58 #define QSPI_TX_EMPTY_MASK                (0x08UL)                         /*!< TX empty status mask */
59 #define QSPI_TX_FULL_MASK                 (0x10UL)                         /*!< TX full status mask */
60 #define QSPI_TXRX_RESET_MASK              (0x20UL)                         /*!< TX or RX reset status mask */
61 #define QSPI_SPIEN_STS_MASK               (0x40UL)                         /*!< SPIEN status mask */
62 #define QSPI_SSLINE_STS_MASK              (0x80UL)                         /*!< QSPIx_SS line status mask */
63
64 /*@}*/ /* end of group QSPI_EXPORTED_CONSTANTS */
65
66
67 /** @addtogroup QSPI_EXPORTED_FUNCTIONS QSPI Exported Functions
68   @{
69 */
70
71 /**
72   * @brief      Clear the unit transfer interrupt flag.
73   * @param[in]  qspi The pointer of the specified QSPI module.
74   * @return     None.
75   * @details    Write 1 to UNITIF bit of QSPI_STATUS register to clear the unit transfer interrupt flag.
76   */
77 #define QSPI_CLR_UNIT_TRANS_INT_FLAG(qspi)   ( (qspi)->STATUS = QSPI_STATUS_UNITIF_Msk )
78
79 /**
80   * @brief      Disable 2-bit Transfer mode.
81   * @param[in]  qspi The pointer of the specified QSPI module.
82   * @return     None.
83   * @details    Clear TWOBIT bit of QSPI_CTL register to disable 2-bit Transfer mode.
84   */
85 #define QSPI_DISABLE_2BIT_MODE(qspi)   ( (qspi)->CTL &= ~QSPI_CTL_TWOBIT_Msk )
86
87 /**
88   * @brief      Disable Slave 3-wire mode.
89   * @param[in]  qspi The pointer of the specified QSPI module.
90   * @return     None.
91   * @details    Clear SLV3WIRE bit of QSPI_SSCTL register to disable Slave 3-wire mode.
92   */
93 #define QSPI_DISABLE_3WIRE_MODE(qspi)   ( (qspi)->SSCTL &= ~QSPI_SSCTL_SLV3WIRE_Msk )
94
95 /**
96   * @brief      Disable Dual I/O mode.
97   * @param[in]  qspi The pointer of the specified QSPI module.
98   * @return     None.
99   * @details    Clear DUALIOEN bit of QSPI_CTL register to disable Dual I/O mode.
100   */
101 #define QSPI_DISABLE_DUAL_MODE(qspi)   ( (qspi)->CTL &= ~QSPI_CTL_DUALIOEN_Msk )
102
103 /**
104   * @brief      Disable Quad I/O mode.
105   * @param[in]  qspi The pointer of the specified QSPI module.
106   * @return     None.
107   * @details    Clear QUADIOEN bit of QSPI_CTL register to disable Quad I/O mode.
108   */
109 #define QSPI_DISABLE_QUAD_MODE(qspi)   ( (qspi)->CTL &= ~QSPI_CTL_QUADIOEN_Msk )
110
111 /**
112   * @brief      Enable 2-bit Transfer mode.
113   * @param[in]  qspi The pointer of the specified QSPI module.
114   * @return     None.
115   * @details    Set TWOBIT bit of QSPI_CTL register to enable 2-bit Transfer mode.
116   */
117 #define QSPI_ENABLE_2BIT_MODE(qspi)   ( (qspi)->CTL |= QSPI_CTL_TWOBIT_Msk )
118
119 /**
120   * @brief      Enable Slave 3-wire mode.
121   * @param[in]  qspi The pointer of the specified QSPI module.
122   * @return     None.
123   * @details    Set SLV3WIRE bit of QSPI_SSCTL register to enable Slave 3-wire mode.
124   */
125 #define QSPI_ENABLE_3WIRE_MODE(qspi)   ( (qspi)->SSCTL |= QSPI_SSCTL_SLV3WIRE_Msk )
126
127 /**
128   * @brief      Enable Dual input mode.
129   * @param[in]  qspi The pointer of the specified QSPI module.
130   * @return     None.
131   * @details    Clear DATDIR bit and set DUALIOEN bit of QSPI_CTL register to enable Dual input mode.
132   */
133 #define QSPI_ENABLE_DUAL_INPUT_MODE(qspi)   ( (qspi)->CTL = ((qspi)->CTL & (~QSPI_CTL_DATDIR_Msk)) | QSPI_CTL_DUALIOEN_Msk )
134
135 /**
136   * @brief      Enable Dual output mode.
137   * @param[in]  qspi The pointer of the specified QSPI module.
138   * @return     None.
139   * @details    Set DATDIR bit and DUALIOEN bit of QSPI_CTL register to enable Dual output mode.
140   */
141 #define QSPI_ENABLE_DUAL_OUTPUT_MODE(qspi)   ( (qspi)->CTL |= (QSPI_CTL_DATDIR_Msk | QSPI_CTL_DUALIOEN_Msk) )
142
143 /**
144   * @brief      Enable Quad input mode.
145   * @param[in]  qspi The pointer of the specified QSPI module.
146   * @return     None.
147   * @details    Clear DATDIR bit and set QUADIOEN bit of QSPI_CTL register to enable Quad input mode.
148   */
149 #define QSPI_ENABLE_QUAD_INPUT_MODE(qspi)   ( (qspi)->CTL = ((qspi)->CTL & (~QSPI_CTL_DATDIR_Msk)) | QSPI_CTL_QUADIOEN_Msk )
150
151 /**
152   * @brief      Enable Quad output mode.
153   * @param[in]  qspi The pointer of the specified QSPI module.
154   * @return     None.
155   * @details    Set DATDIR bit and QUADIOEN bit of QSPI_CTL register to enable Quad output mode.
156   */
157 #define QSPI_ENABLE_QUAD_OUTPUT_MODE(qspi)   ( (qspi)->CTL |= (QSPI_CTL_DATDIR_Msk | QSPI_CTL_QUADIOEN_Msk) )
158
159 /**
160   * @brief      Trigger RX PDMA function.
161   * @param[in]  qspi The pointer of the specified QSPI module.
162   * @return     None.
163   * @details    Set RXPDMAEN bit of QSPI_PDMACTL register to enable RX PDMA transfer function.
164   */
165 #define QSPI_TRIGGER_RX_PDMA(qspi)   ( (qspi)->PDMACTL |= QSPI_PDMACTL_RXPDMAEN_Msk )
166
167 /**
168   * @brief      Trigger TX PDMA function.
169   * @param[in]  qspi The pointer of the specified QSPI module.
170   * @return     None.
171   * @details    Set TXPDMAEN bit of QSPI_PDMACTL register to enable TX PDMA transfer function.
172   */
173 #define QSPI_TRIGGER_TX_PDMA(qspi)   ( (qspi)->PDMACTL |= QSPI_PDMACTL_TXPDMAEN_Msk )
174
175 /**
176   * @brief      Disable RX PDMA transfer.
177   * @param[in]  qspi The pointer of the specified QSPI module.
178   * @return     None.
179   * @details    Clear RXPDMAEN bit of QSPI_PDMACTL register to disable RX PDMA transfer function.
180   */
181 #define QSPI_DISABLE_RX_PDMA(qspi) ( (qspi)->PDMACTL &= ~QSPI_PDMACTL_RXPDMAEN_Msk )
182
183 /**
184   * @brief      Disable TX PDMA transfer.
185   * @param[in]  qspi The pointer of the specified QSPI module.
186   * @return     None.
187   * @details    Clear TXPDMAEN bit of QSPI_PDMACTL register to disable TX PDMA transfer function.
188   */
189 #define QSPI_DISABLE_TX_PDMA(qspi) ( (qspi)->PDMACTL &= ~QSPI_PDMACTL_TXPDMAEN_Msk )
190
191 /**
192   * @brief      Get the count of available data in RX FIFO.
193   * @param[in]  qspi The pointer of the specified QSPI module.
194   * @return     The count of available data in RX FIFO.
195   * @details    Read RXCNT (QSPI_STATUS[27:24]) to get the count of available data in RX FIFO.
196   */
197 #define QSPI_GET_RX_FIFO_COUNT(qspi)   ( ((qspi)->STATUS & QSPI_STATUS_RXCNT_Msk) >> QSPI_STATUS_RXCNT_Pos )
198
199 /**
200   * @brief      Get the RX FIFO empty flag.
201   * @param[in]  qspi The pointer of the specified QSPI module.
202   * @retval     0 RX FIFO is not empty.
203   * @retval     1 RX FIFO is empty.
204   * @details    Read RXEMPTY bit of QSPI_STATUS register to get the RX FIFO empty flag.
205   */
206 #define QSPI_GET_RX_FIFO_EMPTY_FLAG(qspi)   ( ((qspi)->STATUS & QSPI_STATUS_RXEMPTY_Msk) >> QSPI_STATUS_RXEMPTY_Pos )
207
208 /**
209   * @brief      Get the TX FIFO empty flag.
210   * @param[in]  qspi The pointer of the specified QSPI module.
211   * @retval     0 TX FIFO is not empty.
212   * @retval     1 TX FIFO is empty.
213   * @details    Read TXEMPTY bit of QSPI_STATUS register to get the TX FIFO empty flag.
214   */
215 #define QSPI_GET_TX_FIFO_EMPTY_FLAG(qspi)   ( ((qspi)->STATUS & QSPI_STATUS_TXEMPTY_Msk) >> QSPI_STATUS_TXEMPTY_Pos )
216
217 /**
218   * @brief      Get the TX FIFO full flag.
219   * @param[in]  qspi The pointer of the specified QSPI module.
220   * @retval     0 TX FIFO is not full.
221   * @retval     1 TX FIFO is full.
222   * @details    Read TXFULL bit of QSPI_STATUS register to get the TX FIFO full flag.
223   */
224 #define QSPI_GET_TX_FIFO_FULL_FLAG(qspi)   ( ((qspi)->STATUS & QSPI_STATUS_TXFULL_Msk) >> QSPI_STATUS_TXFULL_Pos )
225
226 /**
227   * @brief      Get the datum read from RX register.
228   * @param[in]  qspi The pointer of the specified QSPI module.
229   * @return     Data in RX register.
230   * @details    Read QSPI_RX register to get the received datum.
231   */
232 #define QSPI_READ_RX(qspi)   ( (qspi)->RX )
233
234 /**
235   * @brief      Write datum to TX register.
236   * @param[in]  qspi The pointer of the specified QSPI module.
237   * @param[in]  u32TxData The datum which user attempt to transfer through QSPI bus.
238   * @return     None.
239   * @details    Write u32TxData to QSPI_TX register.
240   */
241 #define QSPI_WRITE_TX(qspi, u32TxData)   ( (qspi)->TX = (u32TxData) )
242
243 /**
244   * @brief      Set QSPIx_SS pin to high state.
245   * @param[in]  qspi The pointer of the specified QSPI module.
246   * @return     None.
247   * @details    Disable automatic slave selection function and set QSPIx_SS pin to high state.
248   */
249 #define QSPI_SET_SS_HIGH(qspi)   ( (qspi)->SSCTL = ((qspi)->SSCTL & (~QSPI_SSCTL_AUTOSS_Msk)) | (QSPI_SSCTL_SSACTPOL_Msk | QSPI_SSCTL_SS_Msk) )
250
251 /**
252   * @brief      Set QSPIx_SS pin to low state.
253   * @param[in]  qspi The pointer of the specified QSPI module.
254   * @return     None.
255   * @details    Disable automatic slave selection function and set QSPIx_SS pin to low state.
256   */
257 #define QSPI_SET_SS_LOW(qspi)   ( (qspi)->SSCTL = ((qspi)->SSCTL & (~(QSPI_SSCTL_AUTOSS_Msk | QSPI_SSCTL_SSACTPOL_Msk))) | QSPI_SSCTL_SS_Msk )
258
259 /**
260   * @brief      Enable Byte Reorder function.
261   * @param[in]  qspi The pointer of the specified QSPI module.
262   * @return     None.
263   * @details    Enable Byte Reorder function. The suspend interval depends on the setting of SUSPITV (QSPI_CTL[7:4]).
264   */
265 #define QSPI_ENABLE_BYTE_REORDER(qspi)   ( (qspi)->CTL |=  QSPI_CTL_REORDER_Msk )
266
267 /**
268   * @brief      Disable Byte Reorder function.
269   * @param[in]  qspi The pointer of the specified QSPI module.
270   * @return     None.
271   * @details    Clear REORDER bit field of QSPI_CTL register to disable Byte Reorder function.
272   */
273 #define QSPI_DISABLE_BYTE_REORDER(qspi)   ( (qspi)->CTL &= ~QSPI_CTL_REORDER_Msk )
274
275 /**
276   * @brief      Set the length of suspend interval.
277   * @param[in]  qspi The pointer of the specified QSPI module.
278   * @param[in]  u32SuspCycle Decides the length of suspend interval. It could be 0 ~ 15.
279   * @return     None.
280   * @details    Set the length of suspend interval according to u32SuspCycle.
281   *             The length of suspend interval is ((u32SuspCycle + 0.5) * the length of one QSPI bus clock cycle).
282   */
283 #define QSPI_SET_SUSPEND_CYCLE(qspi, u32SuspCycle)   ( (qspi)->CTL = ((qspi)->CTL & ~QSPI_CTL_SUSPITV_Msk) | ((u32SuspCycle) << QSPI_CTL_SUSPITV_Pos) )
284
285 /**
286   * @brief      Set the QSPI transfer sequence with LSB first.
287   * @param[in]  qspi The pointer of the specified QSPI module.
288   * @return     None.
289   * @details    Set LSB bit of QSPI_CTL register to set the QSPI transfer sequence with LSB first.
290   */
291 #define QSPI_SET_LSB_FIRST(qspi)   ( (qspi)->CTL |= QSPI_CTL_LSB_Msk )
292
293 /**
294   * @brief      Set the QSPI transfer sequence with MSB first.
295   * @param[in]  qspi The pointer of the specified QSPI module.
296   * @return     None.
297   * @details    Clear LSB bit of QSPI_CTL register to set the QSPI transfer sequence with MSB first.
298   */
299 #define QSPI_SET_MSB_FIRST(qspi)   ( (qspi)->CTL &= ~QSPI_CTL_LSB_Msk )
300
301 /**
302   * @brief      Set the data width of a QSPI transaction.
303   * @param[in]  qspi The pointer of the specified QSPI module.
304   * @param[in]  u32Width The bit width of one transaction.
305   * @return     None.
306   * @details    The data width can be 8 ~ 32 bits.
307   */
308 #define QSPI_SET_DATA_WIDTH(qspi, u32Width)   ( (qspi)->CTL = ((qspi)->CTL & ~QSPI_CTL_DWIDTH_Msk) | (((u32Width) & 0x1F) << QSPI_CTL_DWIDTH_Pos) )
309
310 /**
311   * @brief      Get the QSPI busy state.
312   * @param[in]  qspi The pointer of the specified QSPI module.
313   * @retval     0 QSPI controller is not busy.
314   * @retval     1 QSPI controller is busy.
315   * @details    This macro will return the busy state of QSPI controller.
316   */
317 #define QSPI_IS_BUSY(qspi)   ( ((qspi)->STATUS & QSPI_STATUS_BUSY_Msk) >> QSPI_STATUS_BUSY_Pos )
318
319 /**
320   * @brief      Enable QSPI controller.
321   * @param[in]  qspi The pointer of the specified QSPI module.
322   * @return     None.
323   * @details    Set SPIEN (QSPI_CTL[0]) to enable QSPI controller.
324   */
325 #define QSPI_ENABLE(qspi)   ( (qspi)->CTL |= QSPI_CTL_SPIEN_Msk )
326
327 /**
328   * @brief      Disable QSPI controller.
329   * @param[in]  qspi The pointer of the specified QSPI module.
330   * @return     None.
331   * @details    Clear SPIEN (QSPI_CTL[0]) to disable QSPI controller.
332   */
333 #define QSPI_DISABLE(qspi)   ( (qspi)->CTL &= ~QSPI_CTL_SPIEN_Msk )
334
335
336
337 /* Function prototype declaration */
338 uint32_t QSPI_Open(QSPI_T *qspi, uint32_t u32MasterSlave, uint32_t u32QSPIMode, uint32_t u32DataWidth, uint32_t u32BusClock);
339 void QSPI_Close(QSPI_T *qspi);
340 void QSPI_ClearRxFIFO(QSPI_T *qspi);
341 void QSPI_ClearTxFIFO(QSPI_T *qspi);
342 void QSPI_DisableAutoSS(QSPI_T *qspi);
343 void QSPI_EnableAutoSS(QSPI_T *qspi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel);
344 uint32_t QSPI_SetBusClock(QSPI_T *qspi, uint32_t u32BusClock);
345 void QSPI_SetFIFO(QSPI_T *qspi, uint32_t u32TxThreshold, uint32_t u32RxThreshold);
346 uint32_t QSPI_GetBusClock(QSPI_T *qspi);
347 void QSPI_EnableInt(QSPI_T *qspi, uint32_t u32Mask);
348 void QSPI_DisableInt(QSPI_T *qspi, uint32_t u32Mask);
349 uint32_t QSPI_GetIntFlag(QSPI_T *qspi, uint32_t u32Mask);
350 void QSPI_ClearIntFlag(QSPI_T *qspi, uint32_t u32Mask);
351 uint32_t QSPI_GetStatus(QSPI_T *qspi, uint32_t u32Mask);
352
353
354 /*@}*/ /* end of group QSPI_EXPORTED_FUNCTIONS */
355
356 /*@}*/ /* end of group QSPI_Driver */
357
358 /*@}*/ /* end of group Standard_Driver */
359
360 #ifdef __cplusplus
361 }
362 #endif
363
364 #endif /* __QSPI_H__ */
365
366 /*** (C) COPYRIGHT 2017 Nuvoton Technology Corp. ***/