fsl_pdm_edma.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2019 - 2020, NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #ifndef _FSL_PDM_EDMA_H_
  8. #define _FSL_PDM_EDMA_H_
  9. #include "fsl_edma.h"
  10. #include "fsl_pdm.h"
  11. /*!
  12. * @addtogroup pdm_edma PDM EDMA Driver
  13. * @ingroup pdm
  14. * @{
  15. */
  16. /*******************************************************************************
  17. * Definitions
  18. ******************************************************************************/
  19. /*! @name Driver version */
  20. /*@{*/
  21. #define FSL_PDM_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 5, 0)) /*!< Version 2.5.0 */
  22. /*@}*/
  23. /*! @brief PDM edma handler */
  24. typedef struct _pdm_edma_handle pdm_edma_handle_t;
  25. /*! @brief PDM edma transfer */
  26. typedef struct _pdm_edma_transfer
  27. {
  28. volatile uint8_t *data; /*!< Data start address to transfer. */
  29. volatile size_t dataSize; /*!< Total Transfer bytes size. */
  30. struct _pdm_edma_transfer *linkTransfer; /*!< linked transfer configurations */
  31. } pdm_edma_transfer_t;
  32. /*! @brief PDM eDMA transfer callback function for finish and error */
  33. typedef void (*pdm_edma_callback_t)(PDM_Type *base, pdm_edma_handle_t *handle, status_t status, void *userData);
  34. /*! @brief PDM DMA transfer handle, users should not touch the content of the handle.*/
  35. struct _pdm_edma_handle
  36. {
  37. edma_handle_t *dmaHandle; /*!< DMA handler for PDM send */
  38. uint8_t count; /*!< The transfer data count in a DMA request */
  39. uint32_t receivedBytes; /*!< total transfer count */
  40. uint32_t state; /*!< Internal state for PDM eDMA transfer */
  41. pdm_edma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
  42. bool isLoopTransfer; /*!< loop transfer */
  43. void *userData; /*!< User callback parameter */
  44. edma_tcd_t *tcd; /*!< TCD pool for eDMA transfer. */
  45. uint32_t tcdNum; /*!< TCD number */
  46. uint32_t tcdUser; /*!< Index for user to queue transfer. */
  47. uint32_t tcdDriver; /*!< Index for driver to get the transfer data and size */
  48. volatile uint32_t tcdUsedNum; /*!< Index for user to queue transfer. */
  49. uint8_t endChannel; /*!< The last enabled channel */
  50. uint8_t channelNums; /*!< total channel numbers */
  51. };
  52. /*******************************************************************************
  53. * APIs
  54. ******************************************************************************/
  55. #if defined(__cplusplus)
  56. extern "C" {
  57. #endif
  58. /*!
  59. * @name PDM eDMA Transactional
  60. * @{
  61. */
  62. /*!
  63. * @brief Install EDMA descriptor memory.
  64. *
  65. * @param handle Pointer to EDMA channel transfer handle.
  66. * @param tcdAddr EDMA head descriptor address.
  67. * @param tcdNum EDMA link descriptor address.
  68. */
  69. void PDM_TransferInstallEDMATCDMemory(pdm_edma_handle_t *handle, void *tcdAddr, size_t tcdNum);
  70. /*!
  71. * @brief Initializes the PDM Rx eDMA handle.
  72. *
  73. * This function initializes the PDM slave DMA handle, which can be used for other PDM master transactional APIs.
  74. * Usually, for a specified PDM instance, call this API once to get the initialized handle.
  75. *
  76. * @param base PDM base pointer.
  77. * @param handle PDM eDMA handle pointer.
  78. * @param base PDM peripheral base address.
  79. * @param callback Pointer to user callback function.
  80. * @param userData User parameter passed to the callback function.
  81. * @param dmaHandle eDMA handle pointer, this handle shall be static allocated by users.
  82. */
  83. void PDM_TransferCreateHandleEDMA(
  84. PDM_Type *base, pdm_edma_handle_t *handle, pdm_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle);
  85. /*!
  86. * @brief Configures the PDM channel.
  87. *
  88. * @param base PDM base pointer.
  89. * @param handle PDM eDMA handle pointer.
  90. * @param channel channel index.
  91. * @param config pdm channel configurations.
  92. */
  93. void PDM_TransferSetChannelConfigEDMA(PDM_Type *base,
  94. pdm_edma_handle_t *handle,
  95. uint32_t channel,
  96. const pdm_channel_config_t *config);
  97. /*!
  98. * @brief Performs a non-blocking PDM receive using eDMA.
  99. *
  100. * @note This interface returns immediately after the transfer initiates. Call
  101. * the PDM_GetReceiveRemainingBytes to poll the transfer status and check whether the PDM transfer is finished.
  102. *
  103. * 1. Scatter gather case:
  104. * This functio support dynamic scatter gather and staic scatter gather,
  105. * a. for the dynamic scatter gather case:
  106. * Application should call PDM_TransferReceiveEDMA function continuously to make sure new receive request is submit
  107. *before the previous one finish. b. for the static scatter gather case: Application should use the link transfer
  108. *feature and make sure a loop link transfer is provided, such as:
  109. * @code
  110. * pdm_edma_transfer_t pdmXfer[2] =
  111. * {
  112. * {
  113. * .data = s_buffer,
  114. * .dataSize = BUFFER_SIZE,
  115. * .linkTransfer = &pdmXfer[1],
  116. * },
  117. *
  118. * {
  119. * .data = &s_buffer[BUFFER_SIZE],
  120. * .dataSize = BUFFER_SIZE,
  121. * .linkTransfer = &pdmXfer[0]
  122. * },
  123. * };
  124. *@endcode
  125. *
  126. * 2. Multi channel case:
  127. * This function support receive multi pdm channel data, for example, if two channel is requested,
  128. * @code
  129. * PDM_TransferSetChannelConfigEDMA(DEMO_PDM, &s_pdmRxHandle_0, DEMO_PDM_ENABLE_CHANNEL_0, &channelConfig);
  130. * PDM_TransferSetChannelConfigEDMA(DEMO_PDM, &s_pdmRxHandle_0, DEMO_PDM_ENABLE_CHANNEL_1, &channelConfig);
  131. * PDM_TransferReceiveEDMA(DEMO_PDM, &s_pdmRxHandle_0, pdmXfer);
  132. * @endcode
  133. *Then the output data will be formatted as:
  134. * -------------------------------------------------------------------------
  135. * |CHANNEL0 | CHANNEL1 | CHANNEL0 | CHANNEL1 | CHANNEL0 | CHANNEL 1 | ....|
  136. * -------------------------------------------------------------------------
  137. *
  138. * @param base PDM base pointer
  139. * @param handle PDM eDMA handle pointer.
  140. * @param xfer Pointer to DMA transfer structure.
  141. * @retval kStatus_Success Start a PDM eDMA receive successfully.
  142. * @retval kStatus_InvalidArgument The input argument is invalid.
  143. * @retval kStatus_RxBusy PDM is busy receiving data.
  144. */
  145. status_t PDM_TransferReceiveEDMA(PDM_Type *base, pdm_edma_handle_t *handle, pdm_edma_transfer_t *xfer);
  146. /*!
  147. * @brief Terminate all PDM receive.
  148. *
  149. * This function will clear all transfer slots buffered in the pdm queue. If users only want to abort the
  150. * current transfer slot, please call PDM_TransferAbortReceiveEDMA.
  151. *
  152. * @param base PDM base pointer.
  153. * @param handle PDM eDMA handle pointer.
  154. */
  155. void PDM_TransferTerminateReceiveEDMA(PDM_Type *base, pdm_edma_handle_t *handle);
  156. /*!
  157. * @brief Aborts a PDM receive using eDMA.
  158. *
  159. * This function only aborts the current transfer slots, the other transfer slots' information still kept
  160. * in the handler. If users want to terminate all transfer slots, just call PDM_TransferTerminateReceiveEDMA.
  161. *
  162. * @param base PDM base pointer
  163. * @param handle PDM eDMA handle pointer.
  164. */
  165. void PDM_TransferAbortReceiveEDMA(PDM_Type *base, pdm_edma_handle_t *handle);
  166. /*!
  167. * @brief Gets byte count received by PDM.
  168. *
  169. * @param base PDM base pointer
  170. * @param handle PDM eDMA handle pointer.
  171. * @param count Bytes count received by PDM.
  172. * @retval kStatus_Success Succeed get the transfer count.
  173. * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
  174. */
  175. status_t PDM_TransferGetReceiveCountEDMA(PDM_Type *base, pdm_edma_handle_t *handle, size_t *count);
  176. /*! @} */
  177. #if defined(__cplusplus)
  178. }
  179. #endif
  180. /*!
  181. * @}
  182. */
  183. #endif