1 /*
2  * Copyright 2019-2020 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #ifndef FSL_ASRC_P2P_EDMA_H_
8 #define FSL_ASRC_P2P_EDMA_H_
9 
10 #include "fsl_edma.h"
11 #include "fsl_asrc.h"
12 
13 /*!
14  * @addtogroup asrc_edma_driver
15  * @{
16  */
17 
18 /*******************************************************************************
19  * Definitions
20  ******************************************************************************/
21 
22 /*! @name Driver version */
23 /*@{*/
24 #define FSL_ASRC_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 2, 0)) /*!< Version 2.2.0 */
25 /*@}*/
26 /*!< @brief ASRC IN edma QUEUE size */
27 #define ASRC_XFER_IN_QUEUE_SIZE  4U
28 #define ASRC_XFER_OUT_QUEUE_SIZE (ASRC_XFER_QUEUE_SIZE * 2U)
29 
30 typedef struct _asrc_edma_handle asrc_edma_handle_t;
31 
32 /*! @brief ASRC eDMA transfer callback function for finish and error */
33 typedef void (*asrc_edma_callback_t)(ASRC_Type *base, asrc_edma_handle_t *handle, status_t status, void *userData);
34 
35 /*! @brief ASRC trigger peripheral function pointer */
36 typedef void (*asrc_start_peripheral_t)(bool start);
37 /*! @brief destination peripheral configuration */
38 typedef struct _asrc_p2p_edma_config
39 {
40     asrc_start_peripheral_t startPeripheral; /*!< trigger peripheral start */
41 } asrc_p2p_edma_config_t;
42 
43 /*!@ brief asrc in edma handler */
44 typedef struct _asrc_in_edma_handle
45 {
46     edma_handle_t *inDmaHandle; /*!< DMA handler for ASRC in */
47 
48     uint8_t tcd[(ASRC_XFER_IN_QUEUE_SIZE + 1U) * sizeof(edma_tcd_t)]; /*!< TCD pool for eDMA send. */
49 
50     uint32_t sampleWidth;                         /*!< input data width */
51     uint32_t fifoThreshold;                       /*!< ASRC input fifo threshold */
52     uint32_t *asrcQueue[ASRC_XFER_IN_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer. */
53     size_t transferSize[ASRC_XFER_IN_QUEUE_SIZE]; /*!< Data bytes need to transfer */
54     volatile uint8_t queueUser;                   /*!< Index for user to queue transfer. */
55     volatile uint8_t queueDriver;                 /*!< Index for driver to get the transfer data and size */
56     uint32_t state;                               /*!< Internal state for ASRC eDMA transfer */
57 
58     const asrc_p2p_edma_config_t *peripheralConfig; /*!< peripheral configuration pointer */
59 } asrc_in_edma_handle_t;
60 
61 /*!@ brief asrc out edma handler */
62 typedef struct _asrc_out_edma_handle
63 {
64     edma_handle_t *outDmaHandle; /*!< DMA handler for ASRC out */
65 
66     uint8_t tcd[(ASRC_XFER_OUT_QUEUE_SIZE + 1U) * sizeof(edma_tcd_t)]; /*!< TCD pool for eDMA send. */
67 
68     uint32_t sampleWidth;                           /*!< output data width */
69     uint32_t fifoThreshold;                         /*!< ASRC output fifo threshold */
70     uint32_t *asrcQueue[ASRC_XFER_OUT_QUEUE_SIZE];  /*!< Transfer queue storing queued transfer. */
71     size_t transferSize[ASRC_XFER_OUT_QUEUE_SIZE];  /*!< Data bytes need to transfer */
72     volatile uint8_t queueUser;                     /*!< Index for user to queue transfer. */
73     volatile uint8_t queueDriver;                   /*!< Index for driver to get the transfer data and size */
74     uint32_t state;                                 /*!< Internal state for ASRC eDMA transfer */
75     const asrc_p2p_edma_config_t *peripheralConfig; /*!< peripheral configuration pointer */
76 } asrc_out_edma_handle_t;
77 
78 /*! @brief ASRC DMA transfer handle.*/
79 struct _asrc_edma_handle
80 {
81     asrc_in_edma_handle_t in;        /*!< asrc in handler */
82     asrc_out_edma_handle_t out;      /*!< asrc out handler */
83     asrc_channel_pair_t channelPair; /*!< channel pair */
84     void *userData;                  /*!< User callback parameter */
85     asrc_edma_callback_t callback;   /*!< Callback for users while transfer finish or error occurs */
86 };
87 
88 /*******************************************************************************
89  * APIs
90  ******************************************************************************/
91 #if defined(__cplusplus)
92 extern "C" {
93 #endif
94 
95 /*!
96  * @name eDMA Transactional
97  * @{
98  */
99 
100 /*!
101  * @brief Initializes the ASRC IN eDMA handle.
102  *
103  * This function initializes the ASRC DMA handle, which can be used for other ASRC transactional APIs.
104  * Usually, for a specified ASRC channel pair, call this API once to get the initialized handle.
105  *
106  * @param base ASRC base pointer.
107  * @param channelPair ASRC channel pair
108  * @param handle ASRC eDMA handle pointer.
109  * @param callback Pointer to user callback function.
110  * @param inDmaHandle DMA handler for ASRC in.
111  * @param periphConfig peripheral configuration.
112  * @param userData User parameter passed to the callback function.
113  */
114 void ASRC_TransferInCreateHandleEDMA(ASRC_Type *base,
115                                      asrc_edma_handle_t *handle,
116                                      asrc_channel_pair_t channelPair,
117                                      asrc_edma_callback_t callback,
118                                      edma_handle_t *inDmaHandle,
119                                      const asrc_p2p_edma_config_t *periphConfig,
120                                      void *userData);
121 
122 /*!
123  * @brief Initializes the ASRC OUT eDMA handle.
124  *
125  * This function initializes the ASRC DMA handle, which can be used for other ASRC transactional APIs.
126  * Usually, for a specified ASRC channel pair, call this API once to get the initialized handle.
127  *
128  * @param base ASRC base pointer.
129  * @param channelPair ASRC channel pair
130  * @param handle ASRC eDMA handle pointer.
131  * @param callback Pointer to user callback function.
132  * @param outDmaHandle DMA handler for ASRC out.
133  * @param periphConfig peripheral configuration.
134  * @param userData User parameter passed to the callback function.
135  */
136 void ASRC_TransferOutCreateHandleEDMA(ASRC_Type *base,
137                                       asrc_edma_handle_t *handle,
138                                       asrc_channel_pair_t channelPair,
139                                       asrc_edma_callback_t callback,
140                                       edma_handle_t *outDmaHandle,
141                                       const asrc_p2p_edma_config_t *periphConfig,
142                                       void *userData);
143 
144 /*!
145  * @brief Configures the ASRC P2P channel pair.
146  *
147  *
148  * @param base ASRC base pointer.
149  * @param handle ASRC eDMA handle pointer.
150  * @param asrcConfig asrc configurations.
151  * @param inSampleRate ASRC input sample rate.
152  * @param outSampleRate ASRC output sample rate.
153  */
154 status_t ASRC_TransferSetChannelPairConfigEDMA(ASRC_Type *base,
155                                                asrc_edma_handle_t *handle,
156                                                asrc_channel_pair_config_t *asrcConfig,
157                                                uint32_t inSampleRate,
158                                                uint32_t outSampleRate);
159 
160 /*!
161  * @brief Get output sample buffer size can be transferred by edma.
162  *
163  * @note This API is depends on the ASRC output configuration, should be called after the
164  * ASRC_TransferSetChannelPairConfigEDMA.
165  *
166  * @param base asrc base pointer.
167  * @param handle ASRC channel pair edma handle.
168  * @param inSampleRate input sample rate.
169  * @param outSampleRate output sample rate.
170  * @param inSamplesize input sampleS size.
171  * @retval output buffer size in byte.
172  */
173 uint32_t ASRC_GetOutSamplesSizeEDMA(
174     ASRC_Type *base, asrc_edma_handle_t *handle, uint32_t inSampleRate, uint32_t outSampleRate, uint32_t inSamplesize);
175 
176 /*!
177  * @brief Performs a non-blocking ASRC m2m convert using EDMA.
178  *
179  * @note This interface returns immediately after the transfer initiates.
180 
181  * @param base ASRC base pointer.
182  * @param handle ASRC eDMA handle pointer.
183  * @param xfer Pointer to the DMA transfer structure.
184  * @retval kStatus_Success Start a ASRC eDMA send successfully.
185  * @retval kStatus_InvalidArgument The input argument is invalid.
186  * @retval kStatus_ASRCQueueFull ASRC EDMA driver queue is full.
187  */
188 status_t ASRC_TransferEDMA(ASRC_Type *base, asrc_edma_handle_t *handle, asrc_transfer_t *xfer);
189 
190 /*!
191  * @brief Aborts a ASRC IN transfer using eDMA.
192  *
193  * This function only aborts the current transfer slots, the other transfer slots' information still kept
194  * in the handler. If users want to terminate all transfer slots, just call ASRC_TransferTerminalP2PEDMA.
195  *
196  * @param base ASRC base pointer.
197  * @param handle ASRC eDMA handle pointer.
198  */
199 void ASRC_TransferInAbortEDMA(ASRC_Type *base, asrc_edma_handle_t *handle);
200 
201 /*!
202  * @brief Aborts a ASRC OUT transfer using eDMA.
203  *
204  * This function only aborts the current transfer slots, the other transfer slots' information still kept
205  * in the handler. If users want to terminate all transfer slots, just call ASRC_TransferTerminalP2PEDMA.
206  *
207  * @param base ASRC base pointer.
208  * @param handle ASRC eDMA handle pointer.
209  */
210 void ASRC_TransferOutAbortEDMA(ASRC_Type *base, asrc_edma_handle_t *handle);
211 
212 /*!
213  * @brief Terminate In ASRC Convert.
214  *
215  * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
216  * current transfer slot, please call ASRC_TransferAbortPP2PEDMA.
217  *
218  * @param base ASRC base pointer.
219  * @param handle ASRC eDMA handle pointer.
220  */
221 void ASRC_TransferInTerminalEDMA(ASRC_Type *base, asrc_edma_handle_t *handle);
222 
223 /*!
224  * @brief Terminate Out ASRC Convert.
225  *
226  * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
227  * current transfer slot, please call ASRC_TransferAbortPP2PEDMA.
228  *
229  * @param base ASRC base pointer.
230  * @param handle ASRC eDMA handle pointer.
231  */
232 void ASRC_TransferOutTerminalEDMA(ASRC_Type *base, asrc_edma_handle_t *handle);
233 
234 /*! @} */
235 
236 #if defined(__cplusplus)
237 }
238 #endif
239 
240 /*!
241  * @}
242  */
243 #endif
244