Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:58

0001 /*
0002  * Copyright 2019-2020 NXP
0003  * All rights reserved.
0004  *
0005  * SPDX-License-Identifier: BSD-3-Clause
0006  */
0007 
0008 #include "fsl_asrc_edma.h"
0009 
0010 /* Component ID definition, used by tools. */
0011 #ifndef FSL_COMPONENT_ID
0012 #define FSL_COMPONENT_ID "platform.drivers.asrc_edma"
0013 #endif
0014 
0015 /*******************************************************************************
0016  * Definitations
0017  ******************************************************************************/
0018 /* Used for 32byte aligned */
0019 #define STCD_ADDR(address) (edma_tcd_t *)(((uint32_t)(address) + 32U) & ~0x1FU)
0020 
0021 /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
0022 typedef struct _asrc_edma_private_handle
0023 {
0024     ASRC_Type *base;
0025     asrc_edma_handle_t *handle;
0026 } asrc_edma_private_handle_t;
0027 
0028 /*<! Private handle only used for internally. */
0029 static asrc_edma_private_handle_t s_edmaPrivateHandle[FSL_FEATURE_SOC_ASRC_COUNT][FSL_ASRC_CHANNEL_PAIR_COUNT];
0030 
0031 /*******************************************************************************
0032  * Prototypes
0033  ******************************************************************************/
0034 /*!
0035  * @brief ASRC EDMA callback for input.
0036  *
0037  * @param handle pointer to asrc_edma_handle_t structure which stores the transfer state.
0038  * @param userData Parameter for user callback.
0039  * @param done If the DMA transfer finished.
0040  * @param tcds The TCD index.
0041  */
0042 static void ASRC_InEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
0043 
0044 /*!
0045  * @brief ASRC EDMA callback for output.
0046  *
0047  * @param handle pointer to asrc_edma_handle_t structure which stores the transfer state.
0048  * @param userData Parameter for user callback.
0049  * @param done If the DMA transfer finished.
0050  * @param tcds The TCD index.
0051  */
0052 static void ASRC_OutEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
0053 /*******************************************************************************
0054  * Code
0055  ******************************************************************************/
0056 static void ASRC_InEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
0057 {
0058     asrc_edma_private_handle_t *privHandle = (asrc_edma_private_handle_t *)userData;
0059     asrc_edma_handle_t *asrcHandle         = privHandle->handle;
0060     asrc_in_edma_handle_t *asrcInHandle    = &(privHandle->handle->in);
0061     status_t inStatus                      = kStatus_ASRCInIdle;
0062     /* If finished a block, call the callback function */
0063     asrcInHandle->asrcQueue[asrcInHandle->queueDriver] = NULL;
0064     asrcInHandle->queueDriver                          = (asrcInHandle->queueDriver + 1U) % ASRC_XFER_QUEUE_SIZE;
0065 
0066     /* If all data finished, just stop the transfer */
0067     if (asrcInHandle->asrcQueue[asrcInHandle->queueDriver] == NULL)
0068     {
0069         EDMA_AbortTransfer(asrcInHandle->inDmaHandle);
0070         inStatus = kStatus_ASRCInQueueIdle;
0071     }
0072 
0073     if (asrcHandle->callback != NULL)
0074     {
0075         (asrcHandle->callback)(privHandle->base, asrcHandle, inStatus, asrcHandle->userData);
0076     }
0077 }
0078 
0079 static void ASRC_OutEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
0080 {
0081     asrc_edma_private_handle_t *privHandle = (asrc_edma_private_handle_t *)userData;
0082     asrc_edma_handle_t *asrcHandle         = privHandle->handle;
0083     asrc_out_edma_handle_t *asrcOutHandle  = &(privHandle->handle->out);
0084     uint32_t queueDriverIndex              = asrcOutHandle->queueDriver;
0085     status_t callbackStatus                = kStatus_ASRCOutIdle;
0086 
0087     /* If finished a block, call the callback function */
0088     asrcOutHandle->asrcQueue[queueDriverIndex] = NULL;
0089     asrcOutHandle->queueDriver                 = (uint8_t)((queueDriverIndex + 1U) % ASRC_XFER_OUT_QUEUE_SIZE);
0090 
0091     /* If all data finished, just stop the transfer */
0092     if (asrcOutHandle->asrcQueue[asrcOutHandle->queueDriver] == NULL)
0093     {
0094         EDMA_AbortTransfer(asrcOutHandle->outDmaHandle);
0095         callbackStatus = kStatus_ASRCOutQueueIdle;
0096     }
0097 
0098     if (asrcHandle->callback != NULL)
0099     {
0100         (asrcHandle->callback)(privHandle->base, asrcHandle, callbackStatus, asrcHandle->userData);
0101     }
0102 }
0103 /*!
0104  * brief Initializes the ASRC IN eDMA handle.
0105  *
0106  * This function initializes the ASRC DMA handle, which can be used for other ASRC transactional APIs.
0107  * Usually, for a specified ASRC channel pair, call this API once to get the initialized handle.
0108  *
0109  * param base ASRC base pointer.
0110  * param channelPair ASRC channel pair
0111  * param handle ASRC eDMA handle pointer.
0112  * param callback Pointer to user callback function.
0113  * param txDmaHandle ASRC send edma handle pointer.
0114  * param periphConfig peripheral configuration.
0115  * param userData User parameter passed to the callback function.
0116  */
0117 void ASRC_TransferInCreateHandleEDMA(ASRC_Type *base,
0118                                      asrc_edma_handle_t *handle,
0119                                      asrc_channel_pair_t channelPair,
0120                                      asrc_edma_callback_t callback,
0121                                      edma_handle_t *inDmaHandle,
0122                                      const asrc_p2p_edma_config_t *periphConfig,
0123                                      void *userData)
0124 {
0125     assert((handle != NULL) && (inDmaHandle != NULL));
0126 
0127     uint32_t instance = ASRC_GetInstance(base);
0128 
0129     /* Zero the handle */
0130     (void)memset(&handle->in, 0, sizeof(asrc_in_edma_handle_t));
0131 
0132     handle->in.inDmaHandle = inDmaHandle;
0133     handle->callback       = callback;
0134     handle->userData       = userData;
0135 
0136     /* Set ASRC state to idle */
0137     handle->in.state            = kStatus_ASRCIdle;
0138     handle->channelPair         = channelPair;
0139     handle->in.peripheralConfig = periphConfig;
0140 
0141     s_edmaPrivateHandle[instance][channelPair].base   = base;
0142     s_edmaPrivateHandle[instance][channelPair].handle = handle;
0143 
0144     /* Need to use scatter gather */
0145     EDMA_InstallTCDMemory(inDmaHandle, (edma_tcd_t *)(STCD_ADDR(handle->in.tcd)), ASRC_XFER_OUT_QUEUE_SIZE);
0146     /* Install callback for Tx dma channel */
0147     EDMA_SetCallback(inDmaHandle, ASRC_InEDMACallback, &s_edmaPrivateHandle[instance][channelPair]);
0148 }
0149 
0150 /*!
0151  * brief Initializes the ASRC OUT eDMA handle.
0152  *
0153  * This function initializes the ASRC DMA handle, which can be used for other ASRC transactional APIs.
0154  * Usually, for a specified ASRC channel pair, call this API once to get the initialized handle.
0155  *
0156  * param base ASRC base pointer.
0157  * param channelPair ASRC channel pair
0158  * param handle ASRC eDMA handle pointer.
0159  * param callback Pointer to user callback function.
0160  * param txDmaHandle ASRC send edma handle pointer.
0161  * param periphConfig peripheral configuration.
0162  * param userData User parameter passed to the callback function.
0163  */
0164 void ASRC_TransferOutCreateHandleEDMA(ASRC_Type *base,
0165                                       asrc_edma_handle_t *handle,
0166                                       asrc_channel_pair_t channelPair,
0167                                       asrc_edma_callback_t callback,
0168                                       edma_handle_t *outDmaHandle,
0169                                       const asrc_p2p_edma_config_t *periphConfig,
0170                                       void *userData)
0171 {
0172     assert((handle != NULL) && (NULL != outDmaHandle));
0173 
0174     uint32_t instance = ASRC_GetInstance(base);
0175 
0176     /* Zero the handle */
0177     (void)memset(&handle->out, 0, sizeof(asrc_out_edma_handle_t));
0178 
0179     handle->out.outDmaHandle = outDmaHandle;
0180     handle->callback         = callback;
0181     handle->userData         = userData;
0182 
0183     /* Set ASRC state to idle */
0184     handle->out.state            = kStatus_ASRCIdle;
0185     handle->channelPair          = channelPair;
0186     handle->out.peripheralConfig = periphConfig;
0187 
0188     s_edmaPrivateHandle[instance][channelPair].base   = base;
0189     s_edmaPrivateHandle[instance][channelPair].handle = handle;
0190 
0191     /* Need to use scatter gather */
0192     EDMA_InstallTCDMemory(outDmaHandle, (edma_tcd_t *)(STCD_ADDR(handle->out.tcd)), ASRC_XFER_OUT_QUEUE_SIZE);
0193     /* Install callback for Tx dma channel */
0194     EDMA_SetCallback(outDmaHandle, ASRC_OutEDMACallback, &s_edmaPrivateHandle[instance][channelPair]);
0195 }
0196 
0197 /*!
0198  * brief Configures the ASRC channel pair.
0199  *
0200  * param base ASRC base pointer.
0201  * param handle ASRC eDMA handle pointer.
0202  * param asrcConfig asrc configurations.
0203  * param periphConfig peripheral configuration.
0204  * param inputSampleRate ASRC input sample rate.
0205  * param outputSampleRate ASRC output sample rate.
0206  */
0207 status_t ASRC_TransferSetChannelPairConfigEDMA(ASRC_Type *base,
0208                                                asrc_edma_handle_t *handle,
0209                                                asrc_channel_pair_config_t *asrcConfig,
0210                                                uint32_t inSampleRate,
0211                                                uint32_t outSampleRate)
0212 {
0213     assert((handle != NULL) && (NULL != asrcConfig));
0214 
0215     /* Configure the audio format to ASRC registers */
0216     if (ASRC_SetChannelPairConfig(base, handle->channelPair, asrcConfig, inSampleRate, outSampleRate) !=
0217         kStatus_Success)
0218     {
0219         return kStatus_ASRCChannelPairConfigureFailed;
0220     }
0221     handle->in.fifoThreshold  = (asrcConfig->inFifoThreshold) * (uint32_t)asrcConfig->audioDataChannels;
0222     handle->out.fifoThreshold = (asrcConfig->outFifoThreshold) * (uint32_t)asrcConfig->audioDataChannels;
0223     (void)ASRC_MapSamplesWidth(base, handle->channelPair, &handle->in.sampleWidth, &handle->out.sampleWidth);
0224 
0225     return kStatus_Success;
0226 }
0227 
0228 /*!
0229  * brief Get output sample buffer size can be transferred by edma.
0230  *
0231  * note This API is depends on the ASRC output configuration, should be called after the
0232  * ASRC_TransferSetChannelPairConfigEDMA.
0233  *
0234  * param base asrc base pointer.
0235  * param handle ASRC channel pair edma handle.
0236  * param inSampleRate input sample rate.
0237  * param outSampleRate output sample rate.
0238  * param inSamples input sampleS size.
0239  * retval output buffer size in byte.
0240  */
0241 uint32_t ASRC_GetOutSamplesSizeEDMA(
0242     ASRC_Type *base, asrc_edma_handle_t *handle, uint32_t inSampleRate, uint32_t outSampleRate, uint32_t inSamplesize)
0243 {
0244     uint32_t outputSize = ASRC_GetOutSamplesSize(base, handle->channelPair, inSampleRate, outSampleRate, inSamplesize);
0245 
0246     return outputSize - outputSize % handle->out.fifoThreshold;
0247 }
0248 
0249 static status_t ASRC_TransferOutSubmitEDMA(ASRC_Type *base,
0250                                            asrc_edma_handle_t *handle,
0251                                            uint32_t *outDataAddr,
0252                                            uint32_t outDataSize)
0253 {
0254     assert(outDataAddr != NULL);
0255     assert(outDataSize != 0U);
0256 
0257     uint32_t outAddr              = ASRC_GetOutputDataRegisterAddress(base, handle->channelPair);
0258     edma_transfer_config_t config = {0};
0259     edma_transfer_type_t type     = kEDMA_PeripheralToMemory;
0260 
0261     if (handle->out.asrcQueue[handle->out.queueUser] != NULL)
0262     {
0263         return kStatus_ASRCQueueFull;
0264     }
0265 
0266     if (handle->out.peripheralConfig != NULL)
0267     {
0268         type = kEDMA_PeripheralToPeripheral;
0269     }
0270 
0271     if (handle->out.asrcQueue[handle->out.queueUser] != NULL)
0272     {
0273         return kStatus_ASRCQueueFull;
0274     }
0275 
0276     handle->out.asrcQueue[handle->out.queueUser]    = outDataAddr;
0277     handle->out.transferSize[handle->out.queueUser] = outDataSize;
0278     handle->out.queueUser                           = (handle->out.queueUser + 1U) % ASRC_XFER_OUT_QUEUE_SIZE;
0279     /* Prepare ASRC output edma configuration */
0280     EDMA_PrepareTransfer(&config, (uint32_t *)outAddr, handle->out.sampleWidth, outDataAddr, handle->out.sampleWidth,
0281                          handle->out.fifoThreshold * handle->out.sampleWidth, outDataSize, type);
0282 
0283     if (handle->out.state != (uint32_t)kStatus_ASRCBusy)
0284     {
0285         (void)EDMA_SubmitTransfer(handle->out.outDmaHandle, &config);
0286 
0287         EDMA_StartTransfer(handle->out.outDmaHandle);
0288 
0289         if ((handle->out.peripheralConfig != NULL) && (handle->out.peripheralConfig->startPeripheral != NULL))
0290         {
0291             handle->out.peripheralConfig->startPeripheral(true);
0292         }
0293     }
0294 
0295     return kStatus_Success;
0296 }
0297 
0298 static status_t ASRC_TransferInSubmitEDMA(ASRC_Type *base,
0299                                           asrc_edma_handle_t *handle,
0300                                           uint32_t *inDataAddr,
0301                                           uint32_t inDataSize)
0302 {
0303     assert(inDataAddr != NULL);
0304     assert(inDataSize != 0U);
0305 
0306     uint32_t inAddr               = ASRC_GetInputDataRegisterAddress(base, handle->channelPair);
0307     edma_transfer_config_t config = {0};
0308 
0309     if (handle->in.asrcQueue[handle->in.queueUser] != NULL)
0310     {
0311         return kStatus_ASRCQueueFull;
0312     }
0313 
0314     /* Add into queue */
0315     handle->in.asrcQueue[handle->in.queueUser]    = inDataAddr;
0316     handle->in.transferSize[handle->in.queueUser] = inDataSize;
0317     handle->in.queueUser                          = (handle->in.queueUser + 1U) % ASRC_XFER_IN_QUEUE_SIZE;
0318 
0319     /* Prepare ASRC input edma configuration */
0320     EDMA_PrepareTransfer(&config, (uint32_t *)inDataAddr, handle->in.sampleWidth, (uint32_t *)inAddr,
0321                          handle->in.sampleWidth, handle->in.fifoThreshold * handle->in.sampleWidth, inDataSize,
0322                          handle->in.peripheralConfig == NULL ? kEDMA_MemoryToPeripheral : kEDMA_PeripheralToPeripheral);
0323 
0324     if (handle->in.state != (uint32_t)kStatus_ASRCBusy)
0325     {
0326         (void)EDMA_SubmitTransfer(handle->in.inDmaHandle, &config);
0327         EDMA_StartTransfer(handle->in.inDmaHandle);
0328         /* start peripheral */
0329         if ((handle->in.peripheralConfig != NULL) && (handle->in.peripheralConfig->startPeripheral != NULL))
0330         {
0331             handle->in.peripheralConfig->startPeripheral(true);
0332         }
0333     }
0334 
0335     return kStatus_Success;
0336 }
0337 
0338 /*!
0339  * brief Performs a non-blocking ASRC convert using EDMA.
0340  *
0341  * note This interface returns immediately after the transfer initiates.
0342 
0343  * param base ASRC base pointer.
0344  * param handle ASRC eDMA handle pointer.
0345  * param xfer Pointer to the DMA transfer structure.
0346  * retval kStatus_Success Start a ASRC eDMA send successfully.
0347  * retval kStatus_InvalidArgument The input argument is invalid.
0348  * retval kStatus_ASRCQueueFull ASRC EDMA driver queue is full.
0349  */
0350 status_t ASRC_TransferEDMA(ASRC_Type *base, asrc_edma_handle_t *handle, asrc_transfer_t *xfer)
0351 {
0352     assert(handle != NULL);
0353 
0354     if (ASRC_TransferOutSubmitEDMA(base, handle, xfer->outData, xfer->outDataSize) != kStatus_Success)
0355     {
0356         return kStatus_ASRCQueueFull;
0357     }
0358 
0359     if (ASRC_TransferInSubmitEDMA(base, handle, xfer->inData, xfer->inDataSize) != kStatus_Success)
0360     {
0361         return kStatus_ASRCQueueFull;
0362     }
0363 
0364     return kStatus_Success;
0365 }
0366 
0367 /*!
0368  * brief Aborts a ASRC IN transfer using eDMA.
0369  *
0370  * This function only aborts the current transfer slots, the other transfer slots' information still kept
0371  * in the handler. If users want to terminate all transfer slots, just call ASRC_TransferTerminalP2PEDMA.
0372  *
0373  * param base ASRC base pointer.
0374  * param handle ASRC eDMA handle pointer.
0375  */
0376 void ASRC_TransferInAbortEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
0377 {
0378     assert(handle != NULL);
0379 
0380     /* Disable dma */
0381     EDMA_AbortTransfer(handle->in.inDmaHandle);
0382 
0383     /* Handle the queue index */
0384     handle->in.asrcQueue[handle->in.queueDriver] = NULL;
0385     handle->in.queueDriver                       = (handle->in.queueDriver + 1U) % ASRC_XFER_QUEUE_SIZE;
0386 
0387     /* Set the handle state */
0388     handle->in.state = kStatus_ASRCIdle;
0389 }
0390 
0391 /*!
0392  * brief Aborts a ASRC OUT transfer using eDMA.
0393  *
0394  * This function only aborts the current transfer slots, the other transfer slots' information still kept
0395  * in the handler. If users want to terminate all transfer slots, just call ASRC_TransferTerminalP2PEDMA.
0396  *
0397  * param base ASRC base pointer.
0398  * param handle ASRC eDMA handle pointer.
0399  */
0400 void ASRC_TransferOutAbortEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
0401 {
0402     assert(handle != NULL);
0403 
0404     /* Disable dma */
0405     EDMA_AbortTransfer(handle->out.outDmaHandle);
0406 
0407     /* Handle the queue index */
0408     handle->out.asrcQueue[handle->out.queueDriver] = NULL;
0409     handle->out.queueDriver                        = (handle->out.queueDriver + 1U) % ASRC_XFER_QUEUE_SIZE;
0410 
0411     /* Set the handle state */
0412     handle->out.state = kStatus_ASRCIdle;
0413 }
0414 
0415 /*!
0416  * brief Terminate In ASRC Convert.
0417  *
0418  * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
0419  * current transfer slot, please call ASRC_TransferAbortPP2PEDMA.
0420  *
0421  * param base ASRC base pointer.
0422  * param handle ASRC eDMA handle pointer.
0423  */
0424 void ASRC_TransferInTerminalEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
0425 {
0426     assert(handle != NULL);
0427 
0428     /* Abort the current transfer */
0429     ASRC_TransferInAbortEDMA(base, handle);
0430     /* stop peripheral */
0431     if ((handle->in.peripheralConfig != NULL) && (handle->in.peripheralConfig->startPeripheral != NULL))
0432     {
0433         handle->in.peripheralConfig->startPeripheral(false);
0434     }
0435 
0436     /* Clear all the internal information */
0437     (void)memset(handle->in.tcd, 0, sizeof(handle->in.tcd));
0438     (void)memset(handle->in.asrcQueue, 0, sizeof(handle->in.asrcQueue));
0439     (void)memset(handle->in.transferSize, 0, sizeof(handle->in.transferSize));
0440     handle->in.queueUser   = 0U;
0441     handle->in.queueDriver = 0U;
0442 }
0443 
0444 /*!
0445  * brief Terminate Out ASRC Convert.
0446  *
0447  * This function will clear all transfer slots buffered in the asrc queue. If users only want to abort the
0448  * current transfer slot, please call ASRC_TransferAbortPP2PEDMA.
0449  *
0450  * param base ASRC base pointer.
0451  * param handle ASRC eDMA handle pointer.
0452  */
0453 void ASRC_TransferOutTerminalEDMA(ASRC_Type *base, asrc_edma_handle_t *handle)
0454 {
0455     assert(handle != NULL);
0456 
0457     /* Abort the current transfer */
0458     ASRC_TransferOutAbortEDMA(base, handle);
0459 
0460     if ((handle->out.peripheralConfig != NULL) && (handle->out.peripheralConfig->startPeripheral != NULL))
0461     {
0462         handle->out.peripheralConfig->startPeripheral(false);
0463     }
0464 
0465     (void)memset(handle->out.tcd, 0, sizeof(handle->out.tcd));
0466     (void)memset(handle->out.asrcQueue, 0, sizeof(handle->out.asrcQueue));
0467     (void)memset(handle->out.transferSize, 0, sizeof(handle->out.transferSize));
0468     handle->out.queueUser   = 0U;
0469     handle->out.queueDriver = 0U;
0470 }