1 /*
2  * Copyright 2019-2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef _FSL_FLEXSPI_DMA_H_
9 #define _FSL_FLEXSPI_DMA_H_
10 
11 #include "fsl_flexspi.h"
12 #include "fsl_dma.h"
13 
14 /*!
15  * @addtogroup flexspi_dma
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief FLEXSPI DMA driver version 2.2.1. */
26 #define FSL_FLEXSPI_DMA_DRIVER_VERSION (MAKE_VERSION(2, 2, 1))
27 /*@}*/
28 
29 typedef struct _flexspi_dma_handle flexspi_dma_handle_t;
30 
31 /*! @brief FLEXSPI dma transfer callback function for finish and error */
32 typedef void (*flexspi_dma_callback_t)(FLEXSPI_Type *base,
33                                        flexspi_dma_handle_t *handle,
34                                        status_t status,
35                                        void *userData);
36 
37 /*! @brief dma transfer configuration */
38 typedef enum _flexspi_dma_ntransfer_size
39 {
40     kFLEXPSI_DMAnSize1Bytes = 0x1U, /*!< Source/Destination data transfer size is 1 byte every time */
41     kFLEXPSI_DMAnSize2Bytes = 0x2U, /*!< Source/Destination data transfer size is 2 bytes every time */
42     kFLEXPSI_DMAnSize4Bytes = 0x4U, /*!< Source/Destination data transfer size is 4 bytes every time */
43 } flexspi_dma_transfer_nsize_t;
44 
45 /*! @brief FLEXSPI DMA transfer handle, users should not touch the content of the handle.*/
46 struct _flexspi_dma_handle
47 {
48     dma_handle_t *txDmaHandle;                 /*!< dma handler for FLEXSPI Tx. */
49     dma_handle_t *rxDmaHandle;                 /*!< dma handler for FLEXSPI Rx. */
50     size_t transferSize;                       /*!< Bytes need to transfer. */
51     flexspi_dma_transfer_nsize_t nsize;        /*!< dma SSIZE/DSIZE in each transfer. */
52     uint8_t nbytes;                            /*!< dma minor byte transfer count initially configured. */
53     uint8_t count;                             /*!< The transfer data count in a DMA request. */
54     uint32_t state;                            /*!< Internal state for FLEXSPI dma transfer. */
55     flexspi_dma_callback_t completionCallback; /*!< A callback function called after the dma transfer is finished. */
56     void *userData;                            /*!< User callback parameter */
57 };
58 
59 /*******************************************************************************
60  * APIs
61  ******************************************************************************/
62 #if defined(__cplusplus)
63 extern "C" {
64 #endif
65 
66 /*!
67  * @name FLEXSPI dma Transactional
68  * @{
69  */
70 
71 /*!
72  * @brief Initializes the FLEXSPI handle for transfer which is used in transactional functions and set the callback.
73  *
74  * @param base FLEXSPI peripheral base address
75  * @param handle Pointer to flexspi_dma_handle_t structure
76  * @param callback FLEXSPI callback, NULL means no callback.
77  * @param userData User callback function data.
78  * @param txDmaHandle User requested DMA handle for TX DMA transfer.
79  * @param rxDmaHandle User requested DMA handle for RX DMA transfer.
80  */
81 void FLEXSPI_TransferCreateHandleDMA(FLEXSPI_Type *base,
82                                      flexspi_dma_handle_t *handle,
83                                      flexspi_dma_callback_t callback,
84                                      void *userData,
85                                      dma_handle_t *txDmaHandle,
86                                      dma_handle_t *rxDmaHandle);
87 
88 /*!
89  * @brief Update FLEXSPI DMA transfer source data transfer size(SSIZE) and destination data transfer size(DSIZE).
90  *
91  * @param base FLEXSPI peripheral base address
92  * @param handle Pointer to flexspi_dma_handle_t structure
93  * @param nsize FLEXSPI DMA transfer data transfer size(SSIZE/DSIZE), by default the size is
94  * kFLEXPSI_DMAnSize1Bytes(one byte).
95  * @see flexspi_dma_transfer_nsize_t               .
96  */
97 void FLEXSPI_TransferUpdateSizeDMA(FLEXSPI_Type *base,
98                                    flexspi_dma_handle_t *handle,
99                                    flexspi_dma_transfer_nsize_t nsize);
100 
101 /*!
102  * @brief Transfers FLEXSPI data using an dma non-blocking method.
103  *
104  * This function writes/receives data to/from the FLEXSPI transmit/receive FIFO. This function is non-blocking.
105  * @param base FLEXSPI peripheral base address.
106  * @param handle Pointer to flexspi_dma_handle_t structure
107  * @param xfer FLEXSPI transfer structure.
108  * @retval kStatus_FLEXSPI_Busy     FLEXSPI is busy transfer.
109  * @retval kStatus_InvalidArgument  The watermark configuration is invalid, the watermark should be power of
110                                     2 to do successfully DMA transfer.
111  * @retval kStatus_Success          FLEXSPI successfully start dma transfer.
112  */
113 status_t FLEXSPI_TransferDMA(FLEXSPI_Type *base, flexspi_dma_handle_t *handle, flexspi_transfer_t *xfer);
114 
115 /*!
116  * @brief Aborts the transfer data using dma.
117  *
118  * This function aborts the transfer data using dma.
119  *
120  * @param base FLEXSPI peripheral base address.
121  * @param handle Pointer to flexspi_dma_handle_t structure
122  */
123 void FLEXSPI_TransferAbortDMA(FLEXSPI_Type *base, flexspi_dma_handle_t *handle);
124 
125 /*!
126  * @brief Gets the transferred counts of transfer.
127  *
128  * @param base FLEXSPI peripheral base address.
129  * @param handle Pointer to flexspi_dma_handle_t structure.
130  * @param count Bytes transfer.
131  * @retval kStatus_Success Succeed get the transfer count.
132  * @retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
133  */
134 status_t FLEXSPI_TransferGetTransferCountDMA(FLEXSPI_Type *base, flexspi_dma_handle_t *handle, size_t *count);
135 
136 /* @} */
137 
138 #if defined(__cplusplus)
139 }
140 #endif
141 
142 /* @} */
143 
144 #endif /* _FSL_FLEXSPI_DMA_H_ */
145