1 /*
2  * Copyright 2017 - 2021 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_SAI_SDMA_H_
9 #define _FSL_SAI_SDMA_H_
10 
11 #include "fsl_sai.h"
12 #include "fsl_sdma.h"
13 
14 /*!
15  * @addtogroup sai_sdma SAI SDMA Driver
16  * @ingroup sai
17  * @{
18  */
19 
20 /*******************************************************************************
21  * Definitions
22  ******************************************************************************/
23 
24 /*! @name Driver version */
25 /*@{*/
26 #define FSL_SAI_SDMA_DRIVER_VERSION (MAKE_VERSION(2, 5, 3)) /*!< Version 2.5.3 */
27 /*@}*/
28 
29 typedef struct _sai_sdma_handle sai_sdma_handle_t;
30 
31 /*! @brief SAI SDMA transfer callback function for finish and error */
32 typedef void (*sai_sdma_callback_t)(I2S_Type *base, sai_sdma_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_sdma_handle
36 {
37     sdma_handle_t *dmaHandle;     /*!< DMA handler for SAI send */
38     uint8_t bytesPerFrame;        /*!< Bytes in a frame */
39     uint8_t channel;              /*!< start data channel */
40     uint8_t channelNums;          /*!< total transfer channel numbers, used for multififo */
41     uint8_t channelMask;          /*!< enabled channel mask value, refernece _sai_channel_mask */
42     uint8_t fifoOffset;           /*!< fifo address offset between multifo*/
43     uint32_t count;               /*!< The transfer data count in a DMA request */
44     uint32_t state;               /*!< Internal state for SAI SDMA transfer */
45     uint32_t eventSource;         /*!< SAI event source number */
46     sai_sdma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
47     void *userData;               /*!< User callback parameter */
48     sdma_buffer_descriptor_t bdPool[SAI_XFER_QUEUE_SIZE]; /*!< BD pool for SDMA transfer. */
49     sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE];         /*!< Transfer queue storing queued transfer. */
50     size_t transferSize[SAI_XFER_QUEUE_SIZE];             /*!< Data bytes need to transfer */
51     volatile uint8_t queueUser;                           /*!< Index for user to queue transfer. */
52     volatile uint8_t queueDriver;                         /*!< Index for driver to get the transfer data and size */
53 };
54 
55 /*******************************************************************************
56  * APIs
57  ******************************************************************************/
58 #if defined(__cplusplus)
59 extern "C" {
60 #endif
61 /*!
62  * @name SDMA Transactional
63  * @{
64  */
65 
66 /*!
67  * @brief Initializes the SAI SDMA handle.
68  *
69  * This function initializes the SAI master DMA handle, which can be used for other SAI master transactional APIs.
70  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
71  *
72  * @param base SAI base pointer.
73  * @param handle SAI SDMA handle pointer.
74  * @param base SAI peripheral base address.
75  * @param callback Pointer to user callback function.
76  * @param userData User parameter passed to the callback function.
77  * @param dmaHandle SDMA handle pointer, this handle shall be static allocated by users.
78  * @param eventSource SAI event source number.
79  */
80 void SAI_TransferTxCreateHandleSDMA(I2S_Type *base,
81                                     sai_sdma_handle_t *handle,
82                                     sai_sdma_callback_t callback,
83                                     void *userData,
84                                     sdma_handle_t *dmaHandle,
85                                     uint32_t eventSource);
86 
87 /*!
88  * @brief Initializes the SAI Rx SDMA handle.
89  *
90  * This function initializes the SAI slave DMA handle, which can be used for other SAI master transactional APIs.
91  * Usually, for a specified SAI instance, call this API once to get the initialized handle.
92  *
93  * @param base SAI base pointer.
94  * @param handle SAI SDMA handle pointer.
95  * @param base SAI peripheral base address.
96  * @param callback Pointer to user callback function.
97  * @param userData User parameter passed to the callback function.
98  * @param dmaHandle SDMA handle pointer, this handle shall be static allocated by users.
99  * @param eventSource SAI event source number.
100  */
101 void SAI_TransferRxCreateHandleSDMA(I2S_Type *base,
102                                     sai_sdma_handle_t *handle,
103                                     sai_sdma_callback_t callback,
104                                     void *userData,
105                                     sdma_handle_t *dmaHandle,
106                                     uint32_t eventSource);
107 
108 /*!
109  * @brief Configures the SAI Tx audio format.
110  *
111  * The audio format can be changed at run-time. This function configures the sample rate and audio data
112  * format to be transferred. This function also sets the SDMA parameter according to formatting requirements.
113  *
114  * @param base SAI base pointer.
115  * @param handle SAI SDMA handle pointer.
116  * @param format Pointer to SAI audio data format structure.
117  * @param mclkSourceClockHz SAI master clock source frequency in Hz.
118  * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If bit clock source is master
119  * clock, this value should equals to masterClockHz in format.
120  * @retval kStatus_Success Audio format set successfully.
121  * @retval kStatus_InvalidArgument The input argument is invalid.
122  */
123 void SAI_TransferTxSetFormatSDMA(I2S_Type *base,
124                                  sai_sdma_handle_t *handle,
125                                  sai_transfer_format_t *format,
126                                  uint32_t mclkSourceClockHz,
127                                  uint32_t bclkSourceClockHz);
128 
129 /*!
130  * @brief Configures the SAI Rx audio format.
131  *
132  * The audio format can be changed at run-time. This function configures the sample rate and audio data
133  * format to be transferred. This function also sets the SDMA parameter according to formatting requirements.
134  *
135  * @param base SAI base pointer.
136  * @param handle SAI SDMA handle pointer.
137  * @param format Pointer to SAI audio data format structure.
138  * @param mclkSourceClockHz SAI master clock source frequency in Hz.
139  * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is the master
140  * clock, this value should equal to masterClockHz in format.
141  * @retval kStatus_Success Audio format set successfully.
142  * @retval kStatus_InvalidArgument The input argument is invalid.
143  */
144 void SAI_TransferRxSetFormatSDMA(I2S_Type *base,
145                                  sai_sdma_handle_t *handle,
146                                  sai_transfer_format_t *format,
147                                  uint32_t mclkSourceClockHz,
148                                  uint32_t bclkSourceClockHz);
149 
150 /*!
151  * @brief Performs a non-blocking SAI transfer using DMA.
152  *
153  * @note This interface returns immediately after the transfer initiates. Call
154  * SAI_GetTransferStatus to poll the transfer status and check whether the SAI transfer is finished.
155  *
156  * @param base SAI base pointer.
157  * @param handle SAI SDMA handle pointer.
158  * @param xfer Pointer to the DMA transfer structure.
159  * @retval kStatus_Success Start a SAI SDMA send successfully.
160  * @retval kStatus_InvalidArgument The input argument is invalid.
161  * @retval kStatus_TxBusy SAI is busy sending data.
162  */
163 status_t SAI_TransferSendSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transfer_t *xfer);
164 
165 /*!
166  * @brief Performs a non-blocking SAI receive using SDMA.
167  *
168  * @note This interface returns immediately after the transfer initiates. Call
169  * the SAI_GetReceiveRemainingBytes to poll the transfer status and check whether the SAI transfer is finished.
170  *
171  * @param base SAI base pointer
172  * @param handle SAI SDMA handle pointer.
173  * @param xfer Pointer to DMA transfer structure.
174  * @retval kStatus_Success Start a SAI SDMA receive successfully.
175  * @retval kStatus_InvalidArgument The input argument is invalid.
176  * @retval kStatus_RxBusy SAI is busy receiving data.
177  */
178 status_t SAI_TransferReceiveSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transfer_t *xfer);
179 
180 /*!
181  * @brief Aborts a SAI transfer using SDMA.
182  *
183  * @param base SAI base pointer.
184  * @param handle SAI SDMA handle pointer.
185  */
186 void SAI_TransferAbortSendSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
187 
188 /*!
189  * @brief Aborts a SAI receive using SDMA.
190  *
191  * @param base SAI base pointer
192  * @param handle SAI SDMA handle pointer.
193  */
194 void SAI_TransferAbortReceiveSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
195 
196 /*!
197  * @brief Terminate all the SAI sdma receive transfer.
198  *
199  * @param base SAI base pointer.
200  * @param handle SAI SDMA handle pointer.
201  */
202 void SAI_TransferTerminateReceiveSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
203 
204 /*!
205  * @brief Terminate all the SAI sdma send transfer.
206  *
207  * @param base SAI base pointer.
208  * @param handle SAI SDMA handle pointer.
209  */
210 void SAI_TransferTerminateSendSDMA(I2S_Type *base, sai_sdma_handle_t *handle);
211 
212 /*!
213  * brief Configures the SAI RX.
214  *
215  * param base SAI base pointer.
216  * param handle SAI SDMA handle pointer.
217  * param saiConig sai configurations.
218  */
219 void SAI_TransferRxSetConfigSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transceiver_t *saiConfig);
220 
221 /*!
222  * brief Configures the SAI Tx.
223  *
224  * param base SAI base pointer.
225  * param handle SAI SDMA handle pointer.
226  * param saiConig sai configurations.
227  */
228 void SAI_TransferTxSetConfigSDMA(I2S_Type *base, sai_sdma_handle_t *handle, sai_transceiver_t *saiConfig);
229 /*! @} */
230 
231 #if defined(__cplusplus)
232 }
233 #endif
234 
235 /*!
236  * @}
237  */
238 #endif
239