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