1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2019 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_flexio_i2s_edma.h"
10
11 /* Component ID definition, used by tools. */
12 #ifndef FSL_COMPONENT_ID
13 #define FSL_COMPONENT_ID "platform.drivers.flexio_i2s_edma"
14 #endif
15
16 /*******************************************************************************
17 * Definitations
18 ******************************************************************************/
19 /* Used for 32byte aligned */
20 #define STCD_ADDR(address) (edma_tcd_t *)(((uint32_t)(address) + 32U) & ~0x1FU)
21
22 /*<! Structure definition for flexio_i2s_edma_private_handle_t. The structure is private. */
23 typedef struct _flexio_i2s_edma_private_handle
24 {
25 FLEXIO_I2S_Type *base;
26 flexio_i2s_edma_handle_t *handle;
27 } flexio_i2s_edma_private_handle_t;
28
29 /*!@brief _flexio_i2s_edma_transfer_state */
30 enum
31 {
32 kFLEXIO_I2S_Busy = 0x0U, /*!< FLEXIO I2S is busy */
33 kFLEXIO_I2S_Idle, /*!< Transfer is done. */
34 };
35
36 /*<! Private handle only used for internally. */
37 static flexio_i2s_edma_private_handle_t s_edmaPrivateHandle[2];
38
39 /*******************************************************************************
40 * Prototypes
41 ******************************************************************************/
42 /*!
43 * @brief FLEXIO I2S EDMA callback for send.
44 *
45 * @param handle pointer to flexio_i2s_edma_handle_t structure which stores the transfer state.
46 * @param userData Parameter for user callback.
47 * @param done If the DMA transfer finished.
48 * @param tcds The TCD index.
49 */
50 static void FLEXIO_I2S_TxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
51
52 /*!
53 * @brief FLEXIO I2S EDMA callback for receive.
54 *
55 * @param handle pointer to flexio_i2s_edma_handle_t structure which stores the transfer state.
56 * @param userData Parameter for user callback.
57 * @param done If the DMA transfer finished.
58 * @param tcds The TCD index.
59 */
60 static void FLEXIO_I2S_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
61
62 /*******************************************************************************
63 * Code
64 ******************************************************************************/
FLEXIO_I2S_TxEDMACallback(edma_handle_t * handle,void * userData,bool done,uint32_t tcds)65 static void FLEXIO_I2S_TxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
66 {
67 flexio_i2s_edma_private_handle_t *privHandle = (flexio_i2s_edma_private_handle_t *)userData;
68 flexio_i2s_edma_handle_t *flexio_i2sHandle = privHandle->handle;
69
70 /* If finished a block, call the callback function */
71 (void)memset(&flexio_i2sHandle->queue[flexio_i2sHandle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
72 flexio_i2sHandle->queueDriver = (flexio_i2sHandle->queueDriver + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
73 if (flexio_i2sHandle->callback != NULL)
74 {
75 (flexio_i2sHandle->callback)(privHandle->base, flexio_i2sHandle, kStatus_Success, flexio_i2sHandle->userData);
76 }
77
78 /* If all data finished, just stop the transfer */
79 if (flexio_i2sHandle->queue[flexio_i2sHandle->queueDriver].data == NULL)
80 {
81 FLEXIO_I2S_TransferAbortSendEDMA(privHandle->base, flexio_i2sHandle);
82 }
83 }
84
FLEXIO_I2S_RxEDMACallback(edma_handle_t * handle,void * userData,bool done,uint32_t tcds)85 static void FLEXIO_I2S_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
86 {
87 flexio_i2s_edma_private_handle_t *privHandle = (flexio_i2s_edma_private_handle_t *)userData;
88 flexio_i2s_edma_handle_t *flexio_i2sHandle = privHandle->handle;
89
90 /* If finished a block, call the callback function */
91 (void)memset(&flexio_i2sHandle->queue[flexio_i2sHandle->queueDriver], 0, sizeof(flexio_i2s_transfer_t));
92 flexio_i2sHandle->queueDriver = (flexio_i2sHandle->queueDriver + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
93 if (flexio_i2sHandle->callback != NULL)
94 {
95 (flexio_i2sHandle->callback)(privHandle->base, flexio_i2sHandle, kStatus_Success, flexio_i2sHandle->userData);
96 }
97
98 /* If all data finished, just stop the transfer */
99 if (flexio_i2sHandle->queue[flexio_i2sHandle->queueDriver].data == NULL)
100 {
101 FLEXIO_I2S_TransferAbortReceiveEDMA(privHandle->base, flexio_i2sHandle);
102 }
103 }
104
105 /*!
106 * brief Initializes the FlexIO I2S eDMA handle.
107 *
108 * This function initializes the FlexIO I2S master DMA handle which can be used for other FlexIO I2S master
109 * transactional APIs.
110 * Usually, for a specified FlexIO I2S instance, call this API once to get the initialized handle.
111 *
112 * param base FlexIO I2S peripheral base address.
113 * param handle FlexIO I2S eDMA handle pointer.
114 * param callback FlexIO I2S eDMA callback function called while finished a block.
115 * param userData User parameter for callback.
116 * param dmaHandle eDMA handle for FlexIO I2S. This handle is a static value allocated by users.
117 */
FLEXIO_I2S_TransferTxCreateHandleEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,flexio_i2s_edma_callback_t callback,void * userData,edma_handle_t * dmaHandle)118 void FLEXIO_I2S_TransferTxCreateHandleEDMA(FLEXIO_I2S_Type *base,
119 flexio_i2s_edma_handle_t *handle,
120 flexio_i2s_edma_callback_t callback,
121 void *userData,
122 edma_handle_t *dmaHandle)
123 {
124 assert((handle != NULL) && (dmaHandle != NULL));
125
126 /* Zero the handle. */
127 (void)memset(handle, 0, sizeof(*handle));
128
129 /* Set flexio_i2s base to handle */
130 handle->dmaHandle = dmaHandle;
131 handle->callback = callback;
132 handle->userData = userData;
133
134 /* Set FLEXIO I2S state to idle */
135 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
136
137 s_edmaPrivateHandle[0].base = base;
138 s_edmaPrivateHandle[0].handle = handle;
139
140 /* Need to use scatter gather */
141 EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), FLEXIO_I2S_XFER_QUEUE_SIZE);
142
143 /* Install callback for Tx dma channel */
144 EDMA_SetCallback(dmaHandle, FLEXIO_I2S_TxEDMACallback, &s_edmaPrivateHandle[0]);
145 }
146
147 /*!
148 * brief Initializes the FlexIO I2S Rx eDMA handle.
149 *
150 * This function initializes the FlexIO I2S slave DMA handle which can be used for other FlexIO I2S master transactional
151 * APIs.
152 * Usually, for a specified FlexIO I2S instance, call this API once to get the initialized handle.
153 *
154 * param base FlexIO I2S peripheral base address.
155 * param handle FlexIO I2S eDMA handle pointer.
156 * param callback FlexIO I2S eDMA callback function called while finished a block.
157 * param userData User parameter for callback.
158 * param dmaHandle eDMA handle for FlexIO I2S. This handle is a static value allocated by users.
159 */
FLEXIO_I2S_TransferRxCreateHandleEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,flexio_i2s_edma_callback_t callback,void * userData,edma_handle_t * dmaHandle)160 void FLEXIO_I2S_TransferRxCreateHandleEDMA(FLEXIO_I2S_Type *base,
161 flexio_i2s_edma_handle_t *handle,
162 flexio_i2s_edma_callback_t callback,
163 void *userData,
164 edma_handle_t *dmaHandle)
165 {
166 assert((handle != NULL) && (dmaHandle != NULL));
167
168 /* Zero the handle. */
169 (void)memset(handle, 0, sizeof(*handle));
170
171 /* Set flexio_i2s base to handle */
172 handle->dmaHandle = dmaHandle;
173 handle->callback = callback;
174 handle->userData = userData;
175
176 /* Set FLEXIO I2S state to idle */
177 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
178
179 s_edmaPrivateHandle[1].base = base;
180 s_edmaPrivateHandle[1].handle = handle;
181
182 /* Need to use scatter gather */
183 EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), FLEXIO_I2S_XFER_QUEUE_SIZE);
184
185 /* Install callback for Tx dma channel */
186 EDMA_SetCallback(dmaHandle, FLEXIO_I2S_RxEDMACallback, &s_edmaPrivateHandle[1]);
187 }
188
189 /*!
190 * brief Configures the FlexIO I2S Tx audio format.
191 *
192 * Audio format can be changed in run-time of FlexIO I2S. This function configures the sample rate and audio data
193 * format to be transferred. This function also sets the eDMA parameter according to format.
194 *
195 * param base FlexIO I2S peripheral base address.
196 * param handle FlexIO I2S eDMA handle pointer
197 * param format Pointer to FlexIO I2S audio data format structure.
198 * param srcClock_Hz FlexIO I2S clock source frequency in Hz, it should be 0 while in slave mode.
199 */
FLEXIO_I2S_TransferSetFormatEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,flexio_i2s_format_t * format,uint32_t srcClock_Hz)200 void FLEXIO_I2S_TransferSetFormatEDMA(FLEXIO_I2S_Type *base,
201 flexio_i2s_edma_handle_t *handle,
202 flexio_i2s_format_t *format,
203 uint32_t srcClock_Hz)
204 {
205 assert((handle != NULL) && (format != NULL));
206
207 /* Configure the audio format to FLEXIO I2S registers */
208 if (srcClock_Hz != 0UL)
209 {
210 /* It is master */
211 FLEXIO_I2S_MasterSetFormat(base, format, srcClock_Hz);
212 }
213 else
214 {
215 FLEXIO_I2S_SlaveSetFormat(base, format);
216 }
217
218 /* Get the transfer size from format, this should be used in EDMA configuration */
219 handle->bytesPerFrame = format->bitWidth / 8U;
220 }
221
222 /*!
223 * brief Performs a non-blocking FlexIO I2S transfer using DMA.
224 *
225 * note This interface returned immediately after transfer initiates. Users should call
226 * FLEXIO_I2S_GetTransferStatus to poll the transfer status and check whether the FlexIO I2S transfer is finished.
227 *
228 * param base FlexIO I2S peripheral base address.
229 * param handle FlexIO I2S DMA handle pointer.
230 * param xfer Pointer to DMA transfer structure.
231 * retval kStatus_Success Start a FlexIO I2S eDMA send successfully.
232 * retval kStatus_InvalidArgument The input arguments is invalid.
233 * retval kStatus_TxBusy FlexIO I2S is busy sending data.
234 */
FLEXIO_I2S_TransferSendEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,flexio_i2s_transfer_t * xfer)235 status_t FLEXIO_I2S_TransferSendEDMA(FLEXIO_I2S_Type *base,
236 flexio_i2s_edma_handle_t *handle,
237 flexio_i2s_transfer_t *xfer)
238 {
239 assert((handle != NULL) && (xfer != NULL));
240
241 edma_transfer_config_t config = {0};
242 uint32_t destAddr = FLEXIO_I2S_TxGetDataRegisterAddress(base) + (4UL - handle->bytesPerFrame);
243
244 /* Check if input parameter invalid */
245 if ((xfer->data == NULL) || (xfer->dataSize == 0U))
246 {
247 return kStatus_InvalidArgument;
248 }
249
250 if (handle->queue[handle->queueUser].data != NULL)
251 {
252 return kStatus_FLEXIO_I2S_QueueFull;
253 }
254
255 /* Change the state of handle */
256 handle->state = (uint32_t)kFLEXIO_I2S_Busy;
257
258 /* Update the queue state */
259 handle->queue[handle->queueUser].data = xfer->data;
260 handle->queue[handle->queueUser].dataSize = xfer->dataSize;
261 handle->transferSize[handle->queueUser] = xfer->dataSize;
262 handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
263
264 /* Prepare edma configure */
265 EDMA_PrepareTransfer(&config, xfer->data, handle->bytesPerFrame, (uint32_t *)destAddr, handle->bytesPerFrame,
266 handle->bytesPerFrame, xfer->dataSize, kEDMA_MemoryToPeripheral);
267
268 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO I2S handle */
269 handle->nbytes = handle->bytesPerFrame;
270
271 (void)EDMA_SubmitTransfer(handle->dmaHandle, &config);
272
273 /* Start DMA transfer */
274 EDMA_StartTransfer(handle->dmaHandle);
275
276 /* Enable DMA enable bit */
277 FLEXIO_I2S_TxEnableDMA(base, true);
278
279 /* Enable FLEXIO I2S Tx clock */
280 FLEXIO_I2S_Enable(base, true);
281
282 return kStatus_Success;
283 }
284
285 /*!
286 * brief Performs a non-blocking FlexIO I2S receive using eDMA.
287 *
288 * note This interface returned immediately after transfer initiates. Users should call
289 * FLEXIO_I2S_GetReceiveRemainingBytes to poll the transfer status and check whether the FlexIO I2S transfer is
290 * finished.
291 *
292 * param base FlexIO I2S peripheral base address.
293 * param handle FlexIO I2S DMA handle pointer.
294 * param xfer Pointer to DMA transfer structure.
295 * retval kStatus_Success Start a FlexIO I2S eDMA receive successfully.
296 * retval kStatus_InvalidArgument The input arguments is invalid.
297 * retval kStatus_RxBusy FlexIO I2S is busy receiving data.
298 */
FLEXIO_I2S_TransferReceiveEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,flexio_i2s_transfer_t * xfer)299 status_t FLEXIO_I2S_TransferReceiveEDMA(FLEXIO_I2S_Type *base,
300 flexio_i2s_edma_handle_t *handle,
301 flexio_i2s_transfer_t *xfer)
302 {
303 assert((handle != NULL) && (xfer != NULL));
304
305 edma_transfer_config_t config = {0};
306 uint32_t srcAddr = FLEXIO_I2S_RxGetDataRegisterAddress(base);
307
308 /* Check if input parameter invalid */
309 if ((xfer->data == NULL) || (xfer->dataSize == 0U))
310 {
311 return kStatus_InvalidArgument;
312 }
313
314 if (handle->queue[handle->queueUser].data != NULL)
315 {
316 return kStatus_FLEXIO_I2S_QueueFull;
317 }
318
319 /* Change the state of handle */
320 handle->state = (uint32_t)kFLEXIO_I2S_Busy;
321
322 /* Update queue state */
323 handle->queue[handle->queueUser].data = xfer->data;
324 handle->queue[handle->queueUser].dataSize = xfer->dataSize;
325 handle->transferSize[handle->queueUser] = xfer->dataSize;
326 handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
327
328 /* Prepare edma configure */
329 EDMA_PrepareTransfer(&config, (uint32_t *)srcAddr, handle->bytesPerFrame, xfer->data, handle->bytesPerFrame,
330 handle->bytesPerFrame, xfer->dataSize, kEDMA_PeripheralToMemory);
331
332 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO I2S handle */
333 handle->nbytes = handle->bytesPerFrame;
334
335 (void)EDMA_SubmitTransfer(handle->dmaHandle, &config);
336
337 /* Start DMA transfer */
338 EDMA_StartTransfer(handle->dmaHandle);
339
340 /* Enable DMA enable bit */
341 FLEXIO_I2S_RxEnableDMA(base, true);
342
343 /* Enable FLEXIO I2S Rx clock */
344 FLEXIO_I2S_Enable(base, true);
345
346 return kStatus_Success;
347 }
348
349 /*!
350 * brief Aborts a FlexIO I2S transfer using eDMA.
351 *
352 * param base FlexIO I2S peripheral base address.
353 * param handle FlexIO I2S DMA handle pointer.
354 */
FLEXIO_I2S_TransferAbortSendEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle)355 void FLEXIO_I2S_TransferAbortSendEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle)
356 {
357 assert(handle != NULL);
358
359 /* Disable dma */
360 EDMA_AbortTransfer(handle->dmaHandle);
361
362 /* Disable DMA enable bit */
363 FLEXIO_I2S_TxEnableDMA(base, false);
364
365 /* Set the handle state */
366 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
367 }
368
369 /*!
370 * brief Aborts a FlexIO I2S receive using eDMA.
371 *
372 * param base FlexIO I2S peripheral base address.
373 * param handle FlexIO I2S DMA handle pointer.
374 */
FLEXIO_I2S_TransferAbortReceiveEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle)375 void FLEXIO_I2S_TransferAbortReceiveEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle)
376 {
377 assert(handle != NULL);
378
379 /* Disable dma */
380 EDMA_AbortTransfer(handle->dmaHandle);
381
382 /* Disable DMA enable bit */
383 FLEXIO_I2S_RxEnableDMA(base, false);
384
385 /* Set the handle state */
386 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
387 }
388
389 /*!
390 * brief Gets the remaining bytes to be sent.
391 *
392 * param base FlexIO I2S peripheral base address.
393 * param handle FlexIO I2S DMA handle pointer.
394 * param count Bytes sent.
395 * retval kStatus_Success Succeed get the transfer count.
396 * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
397 */
FLEXIO_I2S_TransferGetSendCountEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,size_t * count)398 status_t FLEXIO_I2S_TransferGetSendCountEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle, size_t *count)
399 {
400 assert(handle != NULL);
401
402 status_t status = kStatus_Success;
403
404 if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
405 {
406 status = kStatus_NoTransferInProgress;
407 }
408 else
409 {
410 *count = handle->transferSize[handle->queueDriver] -
411 (uint32_t)handle->nbytes *
412 EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel);
413 }
414
415 return status;
416 }
417
418 /*!
419 * brief Get the remaining bytes to be received.
420 *
421 * param base FlexIO I2S peripheral base address.
422 * param handle FlexIO I2S DMA handle pointer.
423 * param count Bytes received.
424 * retval kStatus_Success Succeed get the transfer count.
425 * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
426 */
FLEXIO_I2S_TransferGetReceiveCountEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,size_t * count)427 status_t FLEXIO_I2S_TransferGetReceiveCountEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle, size_t *count)
428 {
429 assert(handle != NULL);
430
431 status_t status = kStatus_Success;
432
433 if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
434 {
435 status = kStatus_NoTransferInProgress;
436 }
437 else
438 {
439 *count = handle->transferSize[handle->queueDriver] -
440 (uint32_t)handle->nbytes *
441 EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel);
442 }
443
444 return status;
445 }
446