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 edma_tcd_t size aligned */
20 #define STCD_ADDR(address) (edma_tcd_t *)(((uint32_t)(address) + sizeof(edma_tcd_t)) & ~(sizeof(edma_tcd_t) - 1U))
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 #if defined FSL_FEATURE_EDMA_HAS_ERRATA_51327 && FSL_FEATURE_EDMA_HAS_ERRATA_51327
265 /* Prepare edma configure */
266 EDMA_PrepareTransfer(&config, xfer->data, handle->bytesPerFrame, (uint32_t *)destAddr, handle->bytesPerFrame,
267 8U, xfer->dataSize, kEDMA_MemoryToPeripheral);
268
269 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO I2S handle */
270 handle->nbytes = 8U;
271 #else
272 /* Prepare edma configure */
273 EDMA_PrepareTransfer(&config, xfer->data, handle->bytesPerFrame, (uint32_t *)destAddr, handle->bytesPerFrame,
274 handle->bytesPerFrame, xfer->dataSize, kEDMA_MemoryToPeripheral);
275
276 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO I2S handle */
277 handle->nbytes = handle->bytesPerFrame;
278 #endif
279
280 (void)EDMA_SubmitTransfer(handle->dmaHandle, &config);
281
282 /* Start DMA transfer */
283 EDMA_StartTransfer(handle->dmaHandle);
284
285 /* Enable DMA enable bit */
286 FLEXIO_I2S_TxEnableDMA(base, true);
287
288 /* Enable FLEXIO I2S Tx clock */
289 FLEXIO_I2S_Enable(base, true);
290
291 return kStatus_Success;
292 }
293
294 /*!
295 * brief Performs a non-blocking FlexIO I2S receive using eDMA.
296 *
297 * note This interface returned immediately after transfer initiates. Users should call
298 * FLEXIO_I2S_GetReceiveRemainingBytes to poll the transfer status and check whether the FlexIO I2S transfer is
299 * finished.
300 *
301 * param base FlexIO I2S peripheral base address.
302 * param handle FlexIO I2S DMA handle pointer.
303 * param xfer Pointer to DMA transfer structure.
304 * retval kStatus_Success Start a FlexIO I2S eDMA receive successfully.
305 * retval kStatus_InvalidArgument The input arguments is invalid.
306 * retval kStatus_RxBusy FlexIO I2S is busy receiving data.
307 */
FLEXIO_I2S_TransferReceiveEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,flexio_i2s_transfer_t * xfer)308 status_t FLEXIO_I2S_TransferReceiveEDMA(FLEXIO_I2S_Type *base,
309 flexio_i2s_edma_handle_t *handle,
310 flexio_i2s_transfer_t *xfer)
311 {
312 assert((handle != NULL) && (xfer != NULL));
313
314 edma_transfer_config_t config = {0};
315 uint32_t srcAddr = FLEXIO_I2S_RxGetDataRegisterAddress(base);
316
317 /* Check if input parameter invalid */
318 if ((xfer->data == NULL) || (xfer->dataSize == 0U))
319 {
320 return kStatus_InvalidArgument;
321 }
322
323 if (handle->queue[handle->queueUser].data != NULL)
324 {
325 return kStatus_FLEXIO_I2S_QueueFull;
326 }
327
328 /* Change the state of handle */
329 handle->state = (uint32_t)kFLEXIO_I2S_Busy;
330
331 /* Update queue state */
332 handle->queue[handle->queueUser].data = xfer->data;
333 handle->queue[handle->queueUser].dataSize = xfer->dataSize;
334 handle->transferSize[handle->queueUser] = xfer->dataSize;
335 handle->queueUser = (handle->queueUser + 1U) % FLEXIO_I2S_XFER_QUEUE_SIZE;
336
337 #if defined FSL_FEATURE_EDMA_HAS_ERRATA_51327 && FSL_FEATURE_EDMA_HAS_ERRATA_51327
338 /* Prepare edma configure */
339 EDMA_PrepareTransfer(&config, (uint32_t *)srcAddr, handle->bytesPerFrame, xfer->data, handle->bytesPerFrame,
340 8U, xfer->dataSize, kEDMA_PeripheralToMemory);
341
342 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO I2S handle */
343 handle->nbytes = 8U;
344 #else
345 /* Prepare edma configure */
346 EDMA_PrepareTransfer(&config, (uint32_t *)srcAddr, handle->bytesPerFrame, xfer->data, handle->bytesPerFrame,
347 handle->bytesPerFrame, xfer->dataSize, kEDMA_PeripheralToMemory);
348
349 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO I2S handle */
350 handle->nbytes = handle->bytesPerFrame;
351 #endif
352
353 (void)EDMA_SubmitTransfer(handle->dmaHandle, &config);
354
355 /* Start DMA transfer */
356 EDMA_StartTransfer(handle->dmaHandle);
357
358 /* Enable DMA enable bit */
359 FLEXIO_I2S_RxEnableDMA(base, true);
360
361 /* Enable FLEXIO I2S Rx clock */
362 FLEXIO_I2S_Enable(base, true);
363
364 return kStatus_Success;
365 }
366
367 /*!
368 * brief Aborts a FlexIO I2S transfer using eDMA.
369 *
370 * param base FlexIO I2S peripheral base address.
371 * param handle FlexIO I2S DMA handle pointer.
372 */
FLEXIO_I2S_TransferAbortSendEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle)373 void FLEXIO_I2S_TransferAbortSendEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle)
374 {
375 assert(handle != NULL);
376
377 /* Disable dma */
378 EDMA_AbortTransfer(handle->dmaHandle);
379
380 /* Disable DMA enable bit */
381 FLEXIO_I2S_TxEnableDMA(base, false);
382
383 /* Set the handle state */
384 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
385 }
386
387 /*!
388 * brief Aborts a FlexIO I2S receive using eDMA.
389 *
390 * param base FlexIO I2S peripheral base address.
391 * param handle FlexIO I2S DMA handle pointer.
392 */
FLEXIO_I2S_TransferAbortReceiveEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle)393 void FLEXIO_I2S_TransferAbortReceiveEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle)
394 {
395 assert(handle != NULL);
396
397 /* Disable dma */
398 EDMA_AbortTransfer(handle->dmaHandle);
399
400 /* Disable DMA enable bit */
401 FLEXIO_I2S_RxEnableDMA(base, false);
402
403 /* Set the handle state */
404 handle->state = (uint32_t)kFLEXIO_I2S_Idle;
405 }
406
407 /*!
408 * brief Gets the remaining bytes to be sent.
409 *
410 * param base FlexIO I2S peripheral base address.
411 * param handle FlexIO I2S DMA handle pointer.
412 * param count Bytes sent.
413 * retval kStatus_Success Succeed get the transfer count.
414 * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
415 */
FLEXIO_I2S_TransferGetSendCountEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,size_t * count)416 status_t FLEXIO_I2S_TransferGetSendCountEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle, size_t *count)
417 {
418 assert(handle != NULL);
419
420 status_t status = kStatus_Success;
421
422 if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
423 {
424 status = kStatus_NoTransferInProgress;
425 }
426 else
427 {
428 *count = handle->transferSize[handle->queueDriver] -
429 (uint32_t)handle->nbytes *
430 EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel);
431 }
432
433 return status;
434 }
435
436 /*!
437 * brief Get the remaining bytes to be received.
438 *
439 * param base FlexIO I2S peripheral base address.
440 * param handle FlexIO I2S DMA handle pointer.
441 * param count Bytes received.
442 * retval kStatus_Success Succeed get the transfer count.
443 * retval kStatus_NoTransferInProgress There is not a non-blocking transaction currently in progress.
444 */
FLEXIO_I2S_TransferGetReceiveCountEDMA(FLEXIO_I2S_Type * base,flexio_i2s_edma_handle_t * handle,size_t * count)445 status_t FLEXIO_I2S_TransferGetReceiveCountEDMA(FLEXIO_I2S_Type *base, flexio_i2s_edma_handle_t *handle, size_t *count)
446 {
447 assert(handle != NULL);
448
449 status_t status = kStatus_Success;
450
451 if (handle->state != (uint32_t)kFLEXIO_I2S_Busy)
452 {
453 status = kStatus_NoTransferInProgress;
454 }
455 else
456 {
457 *count = handle->transferSize[handle->queueDriver] -
458 (uint32_t)handle->nbytes *
459 EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel);
460 }
461
462 return status;
463 }
464