summaryrefslogtreecommitdiffstats
path: root/bsps/arm/imxrt/mcux-sdk/drivers/asrc/fsl_asrc_edma.c
blob: 8731a8e8febf5b75ebf81f8f1335e47a7476d43d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright 2019-2020 NXP
 * All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include "fsl_asrc_edma.h"

/* Component ID definition, used by tools. */
#ifndef FSL_COMPONENT_ID
#define FSL_COMPONENT_ID "platform.drivers.asrc_edma"
#endif

/*******************************************************************************
 * Definitations
 ******************************************************************************/
/* Used for 32byte aligned */
#define STCD_ADDR(address) (edma_tcd_t *)(((uint32_t)(address) + 32U) & ~0x1FU)

/*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
typedef struct _asrc_edma_private_handle
{
    ASRC_Type *base;
    asrc_edma_handle_t *handle;
} asrc_edma_private_handle_t;

/*<! Private handle only used for internally. */
static asrc_edma_private_handle_t s_edmaPrivateHandle[FSL_FEATURE_SOC_ASRC_COUNT][FSL_ASRC_CHANNEL_PAIR_COUNT];

/*******************************************************************************
 * Prototypes
 ******************************************************************************/
/*!
 * @brief ASRC EDMA callback for input.
 *
 * @param handle pointer to asrc_edma_handle_t structure which stores the transfer state.
 * @param userData Parameter for user callback.
 * @param done If the DMA transfer finished.
 * @param tcds The TCD index.
 */
static void ASRC_InEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);

/*!
 * @brief ASRC EDMA callback for output.
 *
 * @param handle pointer to asrc_edma_handle_t structure which stores the transfer state.
 * @param userData Parameter for user callback.
 * @param done If the DMA transfer finished.
 * @param tcds The TCD index.
 */
static void ASRC_OutEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
/*******************************************************************************
 * Code
 ******************************************************************************/
static void ASRC_InEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
{
    asrc_edma_private_handle_t *privHandle = (asrc_edma_private_handle_t *)userData;
    asrc_edma_handle_t *asrcHandle         = privHandle->handle;
    asrc_in_edma_handle_t *asrcInHandle    = &(privHandle->handle->in);
    status_t inStatus                      = kStatus_ASRCInIdle;
    /* If finished a block, call the callback function */
    asrcInHandle->asrcQueue[asrcInHandle->queueDriver] = NULL;
    asrcInHandle->queueDriver                          = (asrcInHandle->queueDriver + 1U) % ASRC_XFER_QUEUE_SIZE;

    /* If all data finished, just stop the transfer */
    if (asrcInHandle->asrcQueue[asrcInHandle->queueDriver] == NULL)
    {
        EDMA_AbortTransfer(asrcInHandle->inDmaHandle);
        inStatus = kStatus_ASRCInQueueIdle;
    }

    if (asrcHandle->callback != NULL)
    {
        (asrcHandle->callback)(privHandle->base, asrcHandle, inStatus, asrcHandle->userData);
    }
}

static void ASRC_OutEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
{
    asrc_edma_private_handle_t *privHandle = (asrc_edma_private_handle_t *)userData;
    asrc_edma_handle_t *asrcHandle         = privHandle->handle;
    asrc_out_edma_handle_t *asrcOutHandle  = &(privHandle->handle->out);
    uint32_t queueDriverIndex              = asrcOutHandle->queueDriver;
    status_t callbackStatus                = kStatus_ASRCOutIdle;

    /* If finished a block, call the callback function */
    asrcOutHandle->asrcQueue[queueDriverIndex] = NULL;
    asrcOutHandle->queueDriver                 = (uint8_t)((queueDriverIndex + 1U) % ASRC_XFER_OUT_QUEUE_SIZE);

    /* If all data finished, just stop the transfer */
    if (asrcOutHandle->asrcQueue[asrcOutHandle->queueDriver] == NULL)
    {
        EDMA_AbortTransfer(asrcOutHandle->outDmaHandle);
        callbackStatus = kStatus_ASRCOutQueueIdle;
    }

    if (asrcHandle->callback != NULL)
    {
        (asrcHandle->callback)(privHandle->base, asrcHandle, callbackStatus, asrcHandle->userData);
    }
}
/*!
 * brief Initializes the ASRC IN eDMA handle.
 *
 * This function initializes the ASRC DMA handle, which can be used for other ASRC transactional APIs.
 * Usually, for a specified ASRC channel pair, call this API once to get the initialized handle.
 *
 * param base ASRC base pointer.
 * param channelPair ASRC channel pair
 * param handle ASRC eDMA handle pointer.
 * param callback Pointer to user callback function.
 * param txDmaHandle ASRC send edma handle pointer.
 * param periphConfig peripheral configuration.
 * param userData User parameter passed to the callback function.
 */
void ASRC_TransferInCreateHandleEDMA(ASRC_Type *base,
                                     asrc_edma_handle_t *handle,
                                     asrc_channel_pair_t channelPair,
                                     asrc_edma_callback_t callback,
                                     edma_handle_t *inDmaHandle,
                                     const asrc_p2p_edma_config_t *periphConfig,
                                     void *userData)
{
    assert((handle != NULL) && (inDmaHandle != NULL));

    uint32_t instance = ASRC_GetInstance(base);

    /* Zero the handle */
    (void)memset(&handle->in, 0, sizeof(asrc_in_edma_handle_t));

    handle->in.inDmaHandle = inDmaHandle;
    handle->callback       = callback;
    handle->userData       = userData;

    /* Set ASRC state to idle */
    handle->in.state            = kStatus_ASRCIdle;
    handle->channelPair         = channelPair;
    handle->in.peripheralConfig = periphConfig;

    s_edmaPrivateHandle[instance][channelPair].base   = base;
    s_edmaPrivateHandle[instance][channelPair].handle = handle;

    /* Need to use scatter gather */
    EDMA_InstallTCDMemory(inDmaHandle, (edma_tcd_t *)(STCD_ADDR(handle->in.tcd)), ASRC_XFER_OUT_QUEUE_SIZE);
    /* Install callback for Tx dma channel */
    EDMA_SetCallback(inDmaHandle, ASRC_InEDMACallback, &s_edmaPrivateHandle[instance][channelPair]);
}

/*!
 * brief Initializes the ASRC OUT eDMA handle.
 *
 * This function initializes the ASRC DMA handle, which can be used for other ASRC transactional APIs.
 * Usually, for a specified ASRC channel pair, call this API once to get the initialized handle.
 *
 * param base ASRC base pointer.
 * param channelPair ASRC channel pair
 * param handle ASRC eDMA handle pointer.
 * param callback Pointer to user callback function.
 * param txDmaHandle ASRC send edma handle pointer.
 * param periphConfig peripheral configuration.
 * param userData User parameter passed to the callback function.
 */
void ASRC_TransferOutCreateHandleEDMA(ASRC_Type *base,
                                      asrc_edma_handle_t *handle,
                                      asrc_channel_pair_t channelPair,
                                      asrc_edma_callback_t callback,
                                      edma_handle_t *outDmaHandle,
                                      const asrc_p2p_edma_config_t *periphConfig,
                                      void *userData)
{
    assert((handle != NULL) && (NULL != outDmaHandle));

    uint32_t instance = ASRC_GetInstance(base);

    /* Zero the handle */
    (void)memset(&handle->out, 0, sizeof(asrc_out_edma_handle_t));

    handle->out.outDmaHandle = outDmaHandle;
    handle->callback         = callback;
    handle->userData         = userData;

    /* Set ASRC state to idle */
    handle->out.state            = kStatus_ASRCIdle;
    handle->channelPair          = channelPair;
    handle->out.peripheralConfig = periphConfig;

    s_edmaPrivateHandle[instance][channelPair].base   = base;
    s_edmaPrivateHandle[instance][channelPair].handle = handle;

    /* Need to use scatter gather */
    EDMA_InstallTCDMemory(outDmaHandle, (edma_tcd_t *)(STCD_ADDR(handle->out.tcd)), ASRC_XFER_OUT_QUEUE_SIZE);
    /* Install callback for Tx dma channel */
    EDMA_SetCallback(outDmaHandle, ASRC_OutEDMACallback, &s_edmaPrivateHandle[instance][channelPair]);
}

/*!
 * brief Configures the ASRC channel pair.
 *
 * param base ASRC base pointer.
 * param handle ASRC eDMA handle pointer.
 * param asrcConfig asrc configurations.
 * param periphConfig peripheral configuration.
 * param inputSampleRate ASRC input sample rate.
 * param outputSampleRate ASRC output sample rate.
 */
status_t ASRC_TransferSetChannelPairConfigEDMA(ASRC_Type *base,
                                               asrc_edma_handle_t *handle,
                                               asrc_channel_pair_config_t *asrcConfig,
                                               uint32_t inSampleRate,
                                               uint32_t outSampleRate)
{
    assert((handle != NULL) && (NULL != asrcConfig));

    /* Configure the audio format to ASRC registers */
    if (ASRC_SetChannelPairConfig(base, handle->channelPair, asrcConfig, inSampleRate, outSampleRate) !=
        kStatus_Success)
    {
        return kStatus_ASRCChannelPairConfigureFailed;
    }
    handle->in.fifoThreshold  = (asrcConfig->inFifoThreshold) * (uint32_t)asrcConfig->audioDataChannels;
    handle->out.fifoThreshold = (asrcConfig->outFifoThreshold) * (uint32_t)asrcConfig->audioDataChannels;
    (void)ASRC_MapSamplesWidth(base, handle->channelPair, &handle->in.sampleWidth, &handle->out.sampleWidth);

    return kStatus_Success;
}

/*!
 * brief Get output sample buffer size can be transferred by edma.
 *
 * note This API is depends on the ASRC output configuration, should be called after the
 * ASRC_TransferSetChannelPairConfigEDMA.
 *
 * param base asrc base pointer.
 * param handle ASRC channel pair edma handle.
 * param inSampleRate input sample rate.
 * param outSampleRate output sample rate.
 * param inSamples input sampleS size.
 * retval output buffer size in byte.
 */
uint32_t ASRC_GetOutSamplesSizeEDMA(
    ASRC_Type *base, asrc_edma_handle_t *handle, uint32_t inSampleRate, uint32_t outSampleRate, uint32_t inSamplesize)
{
    uint32_t outputSize = ASRC_GetOutSamplesSize(base, handle->channelPair, inSampleRate, outSampleRate, inSamplesize);

    return outputSize - outputSize % handle->out.fifoThreshold;
}

static status_t ASRC_TransferOutSubmitEDMA(ASRC_Type *base,
                                           asrc_edma_handle_t *handle,
                                           uint32_t *outDataAddr,
                                           uint32_t outDataSize)
{
    assert(outDataAddr != NULL);
    assert(outDataSize != 0U);

    uint32_t outAddr              = ASRC_GetOutputDataRegisterAddress(base, handle->channelPair);
    edma_transfer_config_t config = {0};
    edma_transfer_type_t type     = kEDMA_PeripheralToMemory;

    if (handle->out.asrcQueue[handle->out.queueUser] != NULL)
    {
        return kStatus_ASRCQueueFull;
    }

    if (handle->out.peripheralConfig != NULL)
    {
        type = kEDMA_PeripheralToPeripheral;
    }

    if (handle->out.asrcQueue[handle->out.queueUser] != NULL)
    {
        return kStatus_ASRCQueueFull;
    }

    handle->out.asrcQueue[handle->out.queueUser]    = outDataAddr;
    handle->out.transferSize[handle->out.queueUser] = outDataSize;
    handle->out.queueUser                           = (handle->out.queueUser + 1U) % ASRC_XFER_OUT_QUEUE_SIZE;
    /* Prepare ASRC output edma configuration */
    EDMA_PrepareTransfer(&config, (uint32_t *)outAddr, handle->out.sampleWidth, outDataAddr, handle->out.sampleWidth,
                         handle->out.fifoThreshold * handle->out.sampleWidth, outDataSize, type);

    if (handle->out.state != (uint32_t)kStatus_ASRCBusy)
    {
        (void)EDMA_SubmitTransfer(handle->out.outDmaHandle, &config);

        EDMA_StartTransfer(handle->out.outDmaHandle);

        if ((handle->out.peripheralConfig != NULL) && (handle->out.peripheralConfig->startPeripheral != NULL))
        {
            handle->out.peripheralConfig->startPeripheral(true);
        }
    }

    return kStatus_Success;
}

static status_t ASRC_TransferInSubmitEDMA(ASRC_Type *base,
                                          asrc_edma_handle_t *handle,
                                          uint32_t *inDataAddr,
                                          uint32_t inDataSize)
{
    assert(inDataAddr != NULL);
    assert(inDataSize != 0U);

    uint32_t inAddr               = ASRC_GetInputDataRegisterAddress(base, handle->channelPair);
    edma_transfer_config_t config = {0};

    if (handle->in.asrcQueue[handle->in.queueUser] != NULL)
    {
        return kStatus_ASRCQueueFull;
    }

    /* Add into queue */
    handle->in.asrcQueue[handle->in.queueUser]    = inDataAddr;
    handle->in.transferSize[handle->in.queueUser] = inDataSize;
    handle->in.queueUser                          = (handle->in.queueUser + 1U) % ASRC_XFER_IN_QUEUE_SIZE;

    /* Prepare ASRC input edma configuration */
    EDMA_PrepareTransfer(&config, (uint32_t *)inDataAddr, handle->in.sampleWidth, (uint32_t *)inAddr,
                         handle->in.sampleWidth, handle->in.fifoThreshold * handle->in.sampleWidth, inDataSize,
                         handle->in.peripheralConfig == NULL ? kEDMA_MemoryToPeripheral : kEDMA_PeripheralToPeripheral);

    if (handle->in.state != (uint32_t)kStatus_ASRCBusy)
    {
        (void)EDMA_SubmitTransfer(handle->in.inDmaHandle, &config);
        EDMA_StartTransfer(handle->in.inDmaHandle);
        /* start peripheral */
        if ((handle->in.peripheralConfig != NULL) && (handle->in.peripheralConfig->startPeripheral != NULL))
        {
            handle->in.peripheralConfig->startPeripheral(true);
        }
    }

    return kStatus_Success;
}

/*!
 * brief Performs a non-blocking ASRC convert using EDMA.
 *
 * note This interface returns immediately after the transfer initiates.

 * param base ASRC base pointer.
 * param handle ASRC eDMA handle pointer.
 * param xfer Pointer to the DMA transfer structure.
 * retval kStatus_Success Start a ASRC eDMA send successfully.
 * retval kStatus_InvalidArgument The input argument is invalid.
 * retval kStatus_ASRCQueueFull ASRC EDMA driver queue is full.
 */
status_t ASRC_TransferEDMA(ASRC_Type *base, asrc_edma_handle_t *handle, asrc_transfer_t *xfer)
{
    assert(handle != NULL);

    if (ASRC_TransferOutSubmitEDMA(base, handle, xfer->outData, xfer->outDataSize) != kStatus_Success)
    {
        return kStatus_ASRCQueueFull;
    }

    if (ASRC_TransferInSubmitEDMA(base, handle, xfer->inData, xfer->inDataSize) != kStatus_Success)
    {
        return kStatus_ASRCQueueFull;
    }

    return kStatus_Success;
}

/*!
 * brief Aborts a ASRC IN transfer using eDMA.
 *
 * This function only aborts the current transfer slots, the other transfer slots' information still kept
 * in the handler. If users want to terminate all transfer slots, just call ASRC_TransferTerminalP2PEDMA.
 *
 * param base ASRC base pointer.
 * param handle ASRC eDMA handle pointer.
 */
void ASRC_TransferInAbortEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
{
    assert(handle != NULL);

    /* Disable dma */
    EDMA_AbortTransfer(handle->in.inDmaHandle);

    /* Handle the queue index */
    handle->in.asrcQueue[handle->in.queueDriver] = NULL;
    handle->in.queueDriver                       = (handle->in.queueDriver + 1U) % ASRC_XFER_QUEUE_SIZE;

    /* Set the handle state */
    handle->in.state = kStatus_ASRCIdle;
}

/*!
 * brief Aborts a ASRC OUT transfer using eDMA.
 *
 * This function only aborts the current transfer slots, the other transfer slots' information still kept
 * in the handler. If users want to terminate all transfer slots, just call ASRC_TransferTerminalP2PEDMA.
 *
 * param base ASRC base pointer.
 * param handle ASRC eDMA handle pointer.
 */
void ASRC_TransferOutAbortEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
{
    assert(handle != NULL);

    /* Disable dma */
    EDMA_AbortTransfer(handle->out.outDmaHandle);

    /* Handle the queue index */
    handle->out.asrcQueue[handle->out.queueDriver] = NULL;
    handle->out.queueDriver                        = (handle->out.queueDriver + 1U) % ASRC_XFER_QUEUE_SIZE;

    /* Set the handle state */
    handle->out.state = kStatus_ASRCIdle;
}

/*!
 * brief Terminate In ASRC Convert.
 *
 * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
 * current transfer slot, please call ASRC_TransferAbortPP2PEDMA.
 *
 * param base ASRC base pointer.
 * param handle ASRC eDMA handle pointer.
 */
void ASRC_TransferInTerminalEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
{
    assert(handle != NULL);

    /* Abort the current transfer */
    ASRC_TransferInAbortEDMA(base, handle);
    /* stop peripheral */
    if ((handle->in.peripheralConfig != NULL) && (handle->in.peripheralConfig->startPeripheral != NULL))
    {
        handle->in.peripheralConfig->startPeripheral(false);
    }

    /* Clear all the internal information */
    (void)memset(handle->in.tcd, 0, sizeof(handle->in.tcd));
    (void)memset(handle->in.asrcQueue, 0, sizeof(handle->in.asrcQueue));
    (void)memset(handle->in.transferSize, 0, sizeof(handle->in.transferSize));
    handle->in.queueUser   = 0U;
    handle->in.queueDriver = 0U;
}

/*!
 * brief Terminate Out ASRC Convert.
 *
 * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
 * current transfer slot, please call ASRC_TransferAbortPP2PEDMA.
 *
 * param base ASRC base pointer.
 * param handle ASRC eDMA handle pointer.
 */
void ASRC_TransferOutTerminalEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
{
    assert(handle != NULL);

    /* Abort the current transfer */
    ASRC_TransferOutAbortEDMA(base, handle);

    if ((handle->out.peripheralConfig != NULL) && (handle->out.peripheralConfig->startPeripheral != NULL))
    {
        handle->out.peripheralConfig->startPeripheral(false);
    }

    (void)memset(handle->out.tcd, 0, sizeof(handle->out.tcd));
    (void)memset(handle->out.asrcQueue, 0, sizeof(handle->out.asrcQueue));
    (void)memset(handle->out.transferSize, 0, sizeof(handle->out.transferSize));
    handle->out.queueUser   = 0U;
    handle->out.queueDriver = 0U;
}