1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_SAI_EDMA_H_
9 #define _FSL_SAI_EDMA_H_
10 
11 #include "fsl_edma.h"
12 #include "fsl_sai.h"
13 
14 /*!
15  * @addtogroup sai_edma SAI EDMA Driver
16  * @ingroup sai
17  * @{
18  */
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 
24 /*! @name Driver version */
25 /*@{*/
26 #define FSL_SAI_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 4, 0)) /*!< Version 2.4.0 */
27 /*@}*/
28 
29 typedef struct sai_edma_handle sai_edma_handle_t;
30 
31 /*! @brief SAI eDMA transfer callback function for finish and error */
32 typedef void (*sai_edma_callback_t)(I2S_Type *base, sai_edma_handle_t *handle, status_t status, void *userData);
33 
34 /*! @brief SAI DMA transfer handle, users should not touch the content of the handle.*/
35 struct sai_edma_handle
36 {
37     edma_handle_t *dmaHandle;     /*!< DMA handler for SAI send */
38     uint8_t nbytes;               /*!< eDMA minor byte transfer count initially configured. */
39     uint8_t bytesPerFrame;        /*!< Bytes in a frame */
40     uint8_t channel;              /*!< Which data channel */
41     uint8_t count;                /*!< The transfer data count in a DMA request */
42     uint32_t state;               /*!< Internal state for SAI eDMA transfer */
43     sai_edma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
44     void *userData;               /*!< User callback parameter */
45     uint8_t tcd[(SAI_XFER_QUEUE_SIZE + 1U) * sizeof(edma_tcd_t)]; /*!< TCD pool for eDMA transfer. */
46     sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE];                 /*!< Transfer queue storing queued transfer. */
47     size_t transferSize[SAI_XFER_QUEUE_SIZE];                     /*!< Data bytes need to transfer */
48     volatile uint8_t queueUser;                                   /*!< Index for user to queue transfer. */
49     volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
50 };
51 
52 /*******************************************************************************
53  * APIs
54  ******************************************************************************/
55 #if defined(__cplusplus)
56 extern "C" {
57 #endif
58 
59 /*!
60  * @name eDMA Transactional
61  * @{
62  */
63 
64 /*!
65  * @brief Initializes the SAI eDMA handle.
66  *
67  * This function initializes the SAI master DMA handle, which can be used for other SAI master transactional APIs.
68  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
69  *
70  * @param base SAI base pointer.
71  * @param handle SAI eDMA handle pointer.
72  * @param base SAI peripheral base address.
73  * @param callback Pointer to user callback function.
74  * @param userData User parameter passed to the callback function.
75  * @param txDmaHandle eDMA handle pointer, this handle shall be static allocated by users.
76  */
77 void SAI_TransferTxCreateHandleEDMA(I2S_Type *base,
78                                     sai_edma_handle_t *handle,
79                                     sai_edma_callback_t callback,
80                                     void *userData,
81                                     edma_handle_t *txDmaHandle);
82 
83 /*!
84  * @brief Initializes the SAI Rx eDMA handle.
85  *
86  * This function initializes the SAI slave DMA handle, which can be used for other SAI master transactional APIs.
87  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
88  *
89  * @param base SAI base pointer.
90  * @param handle SAI eDMA handle pointer.
91  * @param base SAI peripheral base address.
92  * @param callback Pointer to user callback function.
93  * @param userData User parameter passed to the callback function.
94  * @param rxDmaHandle eDMA handle pointer, this handle shall be static allocated by users.
95  */
96 void SAI_TransferRxCreateHandleEDMA(I2S_Type *base,
97                                     sai_edma_handle_t *handle,
98                                     sai_edma_callback_t callback,
99                                     void *userData,
100                                     edma_handle_t *rxDmaHandle);
101 
102 /*!
103  * @brief Configures the SAI Tx audio format.
104  *
105  * @deprecated Do not use this function.  It has been superceded by @ref SAI_TransferTxSetConfigEDMA
106  *
107  * The audio format can be changed at run-time. This function configures the sample rate and audio data
108  * format to be transferred. This function also sets the eDMA parameter according to formatting requirements.
109  *
110  * @param base SAI base pointer.
111  * @param handle SAI eDMA handle pointer.
112  * @param format Pointer to SAI audio data format structure.
113  * @param mclkSourceClockHz SAI master clock source frequency in Hz.
114  * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If bit clock source is master
115  * clock, this value should equals to masterClockHz in format.
116  * @retval kStatus_Success Audio format set successfully.
117  * @retval kStatus_InvalidArgument The input argument is invalid.
118  */
119 void SAI_TransferTxSetFormatEDMA(I2S_Type *base,
120                                  sai_edma_handle_t *handle,
121                                  sai_transfer_format_t *format,
122                                  uint32_t mclkSourceClockHz,
123                                  uint32_t bclkSourceClockHz);
124 
125 /*!
126  * @brief Configures the SAI Rx audio format.
127  *
128  * @deprecated Do not use this function.  It has been superceded by @ref SAI_TransferRxSetConfigEDMA
129  *
130  * The audio format can be changed at run-time. This function configures the sample rate and audio data
131  * format to be transferred. This function also sets the eDMA parameter according to formatting requirements.
132  *
133  * @param base SAI base pointer.
134  * @param handle SAI eDMA handle pointer.
135  * @param format Pointer to SAI audio data format structure.
136  * @param mclkSourceClockHz SAI master clock source frequency in Hz.
137  * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is the master
138  * clock, this value should equal to masterClockHz in format.
139  * @retval kStatus_Success Audio format set successfully.
140  * @retval kStatus_InvalidArgument The input argument is invalid.
141  */
142 void SAI_TransferRxSetFormatEDMA(I2S_Type *base,
143                                  sai_edma_handle_t *handle,
144                                  sai_transfer_format_t *format,
145                                  uint32_t mclkSourceClockHz,
146                                  uint32_t bclkSourceClockHz);
147 
148 /*!
149  * @brief Configures the SAI Tx.
150  *
151  *
152  * @param base SAI base pointer.
153  * @param handle SAI eDMA handle pointer.
154  * @param saiConfig sai configurations.
155  */
156 void SAI_TransferTxSetConfigEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transceiver_t *saiConfig);
157 
158 /*!
159  * @brief Configures the SAI Rx.
160  *
161  *
162  * @param base SAI base pointer.
163  * @param handle SAI eDMA handle pointer.
164  * @param saiConfig sai configurations.
165  */
166 void SAI_TransferRxSetConfigEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transceiver_t *saiConfig);
167 
168 /*!
169  * @brief Performs a non-blocking SAI transfer using DMA.
170  *
171  * @note This interface returns immediately after the transfer initiates. Call
172  * SAI_GetTransferStatus to poll the transfer status and check whether the SAI transfer is finished.
173  *
174  * @param base SAI base pointer.
175  * @param handle SAI eDMA handle pointer.
176  * @param xfer Pointer to the DMA transfer structure.
177  * @retval kStatus_Success Start a SAI eDMA send successfully.
178  * @retval kStatus_InvalidArgument The input argument is invalid.
179  * @retval kStatus_TxBusy SAI is busy sending data.
180  */
181 status_t SAI_TransferSendEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer);
182 
183 /*!
184  * @brief Performs a non-blocking SAI receive using eDMA.
185  *
186  * @note This interface returns immediately after the transfer initiates. Call
187  * the SAI_GetReceiveRemainingBytes to poll the transfer status and check whether the SAI transfer is finished.
188  *
189  * @param base SAI base pointer
190  * @param handle SAI eDMA handle pointer.
191  * @param xfer Pointer to DMA transfer structure.
192  * @retval kStatus_Success Start a SAI eDMA receive successfully.
193  * @retval kStatus_InvalidArgument The input argument is invalid.
194  * @retval kStatus_RxBusy SAI is busy receiving data.
195  */
196 status_t SAI_TransferReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer);
197 
198 /*!
199  * @brief Terminate all SAI send.
200  *
201  * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
202  * current transfer slot, please call SAI_TransferAbortSendEDMA.
203  *
204  * @param base SAI base pointer.
205  * @param handle SAI eDMA handle pointer.
206  */
207 void SAI_TransferTerminateSendEDMA(I2S_Type *base, sai_edma_handle_t *handle);
208 
209 /*!
210  * @brief Terminate all SAI receive.
211  *
212  * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
213  * current transfer slot, please call SAI_TransferAbortReceiveEDMA.
214  *
215  * @param base SAI base pointer.
216  * @param handle SAI eDMA handle pointer.
217  */
218 void SAI_TransferTerminateReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle);
219 
220 /*!
221  * @brief Aborts a SAI transfer using eDMA.
222  *
223  * This function only aborts the current transfer slots, the other transfer slots' information still kept
224  * in the handler. If users want to terminate all transfer slots, just call SAI_TransferTerminateSendEDMA.
225  *
226  * @param base SAI base pointer.
227  * @param handle SAI eDMA handle pointer.
228  */
229 void SAI_TransferAbortSendEDMA(I2S_Type *base, sai_edma_handle_t *handle);
230 
231 /*!
232  * @brief Aborts a SAI receive using eDMA.
233  *
234  * This function only aborts the current transfer slots, the other transfer slots' information still kept
235  * in the handler. If users want to terminate all transfer slots, just call SAI_TransferTerminateReceiveEDMA.
236  *
237  * @param base SAI base pointer
238  * @param handle SAI eDMA handle pointer.
239  */
240 void SAI_TransferAbortReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle);
241 
242 /*!
243  * @brief Gets byte count sent by SAI.
244  *
245  * @param base SAI base pointer.
246  * @param handle SAI eDMA handle pointer.
247  * @param count Bytes count sent by SAI.
248  * @retval kStatus_Success Succeed get the transfer count.
249  * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
250  */
251 status_t SAI_TransferGetSendCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count);
252 
253 /*!
254  * @brief Gets byte count received by SAI.
255  *
256  * @param base SAI base pointer
257  * @param handle SAI eDMA handle pointer.
258  * @param count Bytes count received by SAI.
259  * @retval kStatus_Success Succeed get the transfer count.
260  * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
261  */
262 status_t SAI_TransferGetReceiveCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count);
263 
264 /*!
265  * @brief Gets valid transfer slot.
266  *
267  * This function can be used to query the valid transfer request slot that the application can submit.
268  * It should be called in the critical section, that means the application could call it in the corresponding callback
269  * function or disable IRQ before calling it in the application, otherwise, the returned value may not correct.
270  *
271  * @param base SAI base pointer
272  * @param handle SAI eDMA handle pointer.
273  * @retval valid slot count that application submit.
274  */
275 uint32_t SAI_TransferGetValidTransferSlotsEDMA(I2S_Type *base, sai_edma_handle_t *handle);
276 
277 /*! @} */
278 
279 #if defined(__cplusplus)
280 }
281 #endif
282 
283 /*!
284  * @}
285  */
286 #endif
287