fsl_sai_edma.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_SAI_EDMA_H_
  31. #define _FSL_SAI_EDMA_H_
  32. #include "fsl_sai.h"
  33. #include "fsl_edma.h"
  34. /*!
  35. * @addtogroup sai_edma
  36. * @{
  37. */
  38. /*******************************************************************************
  39. * Definitions
  40. ******************************************************************************/
  41. typedef struct _sai_edma_handle sai_edma_handle_t;
  42. /*! @brief SAI eDMA transfer callback function for finish and error */
  43. typedef void (*sai_edma_callback_t)(I2S_Type *base, sai_edma_handle_t *handle, status_t status, void *userData);
  44. /*! @brief SAI DMA transfer handle, users should not touch the content of the handle.*/
  45. struct _sai_edma_handle
  46. {
  47. edma_handle_t *dmaHandle; /*!< DMA handler for SAI send */
  48. uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
  49. uint8_t bytesPerFrame; /*!< Bytes in a frame */
  50. uint8_t channel; /*!< Which data channel */
  51. uint8_t count; /*!< The transfer data count in a DMA request */
  52. uint32_t state; /*!< Internal state for SAI eDMA transfer */
  53. sai_edma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
  54. void *userData; /*!< User callback parameter */
  55. edma_tcd_t tcd[SAI_XFER_QUEUE_SIZE + 1U]; /*!< TCD pool for eDMA transfer. */
  56. sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer. */
  57. size_t transferSize[SAI_XFER_QUEUE_SIZE]; /*!< Data bytes need to transfer */
  58. volatile uint8_t queueUser; /*!< Index for user to queue transfer. */
  59. volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
  60. };
  61. /*******************************************************************************
  62. * APIs
  63. ******************************************************************************/
  64. #if defined(__cplusplus)
  65. extern "C" {
  66. #endif
  67. /*!
  68. * @name eDMA Transactional
  69. * @{
  70. */
  71. /*!
  72. * @brief Initializes the SAI eDMA handle.
  73. *
  74. * This function initializes the SAI master DMA handle, which can be used for other SAI master transactional APIs.
  75. * Usually, for a specified SAI instance, call this API once to get the initialized handle.
  76. *
  77. * @param base SAI base pointer.
  78. * @param handle SAI eDMA handle pointer.
  79. * @param base SAI peripheral base address.
  80. * @param callback Pointer to user callback function.
  81. * @param userData User parameter passed to the callback function.
  82. * @param dmaHandle eDMA handle pointer, this handle shall be static allocated by users.
  83. */
  84. void SAI_TransferTxCreateHandleEDMA(
  85. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle);
  86. /*!
  87. * @brief Initializes the SAI Rx eDMA handle.
  88. *
  89. * This function initializes the SAI slave DMA handle, which can be used for other SAI master transactional APIs.
  90. * Usually, for a specified SAI instance, call this API once to get the initialized handle.
  91. *
  92. * @param base SAI base pointer.
  93. * @param handle SAI eDMA handle pointer.
  94. * @param base SAI peripheral base address.
  95. * @param callback Pointer to user callback function.
  96. * @param userData User parameter passed to the callback function.
  97. * @param dmaHandle eDMA handle pointer, this handle shall be static allocated by users.
  98. */
  99. void SAI_TransferRxCreateHandleEDMA(
  100. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle);
  101. /*!
  102. * @brief Configures the SAI Tx audio format.
  103. *
  104. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  105. * format to be transferred. This function also sets the eDMA parameter according to formatting requirements.
  106. *
  107. * @param base SAI base pointer.
  108. * @param handle SAI eDMA handle pointer.
  109. * @param format Pointer to SAI audio data format structure.
  110. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  111. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If bit clock source is master
  112. * clock, this value should equals to masterClockHz in format.
  113. * @retval kStatus_Success Audio format set successfully.
  114. * @retval kStatus_InvalidArgument The input argument is invalid.
  115. */
  116. void SAI_TransferTxSetFormatEDMA(I2S_Type *base,
  117. sai_edma_handle_t *handle,
  118. sai_transfer_format_t *format,
  119. uint32_t mclkSourceClockHz,
  120. uint32_t bclkSourceClockHz);
  121. /*!
  122. * @brief Configures the SAI Rx audio format.
  123. *
  124. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  125. * format to be transferred. This function also sets the eDMA parameter according to formatting requirements.
  126. *
  127. * @param base SAI base pointer.
  128. * @param handle SAI eDMA handle pointer.
  129. * @param format Pointer to SAI audio data format structure.
  130. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  131. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is the master
  132. * clock, this value should equal to masterClockHz in format.
  133. * @retval kStatus_Success Audio format set successfully.
  134. * @retval kStatus_InvalidArgument The input argument is invalid.
  135. */
  136. void SAI_TransferRxSetFormatEDMA(I2S_Type *base,
  137. sai_edma_handle_t *handle,
  138. sai_transfer_format_t *format,
  139. uint32_t mclkSourceClockHz,
  140. uint32_t bclkSourceClockHz);
  141. /*!
  142. * @brief Performs a non-blocking SAI transfer using DMA.
  143. *
  144. * @note This interface returns immediately after the transfer initiates. Call
  145. * SAI_GetTransferStatus to poll the transfer status and check whether the SAI transfer is finished.
  146. *
  147. * @param base SAI base pointer.
  148. * @param handle SAI eDMA handle pointer.
  149. * @param xfer Pointer to the DMA transfer structure.
  150. * @retval kStatus_Success Start a SAI eDMA send successfully.
  151. * @retval kStatus_InvalidArgument The input argument is invalid.
  152. * @retval kStatus_TxBusy SAI is busy sending data.
  153. */
  154. status_t SAI_TransferSendEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer);
  155. /*!
  156. * @brief Performs a non-blocking SAI receive using eDMA.
  157. *
  158. * @note This interface returns immediately after the transfer initiates. Call
  159. * the SAI_GetReceiveRemainingBytes to poll the transfer status and check whether the SAI transfer is finished.
  160. *
  161. * @param base SAI base pointer
  162. * @param handle SAI eDMA handle pointer.
  163. * @param xfer Pointer to DMA transfer structure.
  164. * @retval kStatus_Success Start a SAI eDMA receive successfully.
  165. * @retval kStatus_InvalidArgument The input argument is invalid.
  166. * @retval kStatus_RxBusy SAI is busy receiving data.
  167. */
  168. status_t SAI_TransferReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer);
  169. /*!
  170. * @brief Aborts a SAI transfer using eDMA.
  171. *
  172. * @param base SAI base pointer.
  173. * @param handle SAI eDMA handle pointer.
  174. */
  175. void SAI_TransferAbortSendEDMA(I2S_Type *base, sai_edma_handle_t *handle);
  176. /*!
  177. * @brief Aborts a SAI receive using eDMA.
  178. *
  179. * @param base SAI base pointer
  180. * @param handle SAI eDMA handle pointer.
  181. */
  182. void SAI_TransferAbortReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle);
  183. /*!
  184. * @brief Gets byte count sent by SAI.
  185. *
  186. * @param base SAI base pointer.
  187. * @param handle SAI eDMA handle pointer.
  188. * @param count Bytes count sent by SAI.
  189. * @retval kStatus_Success Succeed get the transfer count.
  190. * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
  191. */
  192. status_t SAI_TransferGetSendCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count);
  193. /*!
  194. * @brief Gets byte count received by SAI.
  195. *
  196. * @param base SAI base pointer
  197. * @param handle SAI eDMA handle pointer.
  198. * @param count Bytes count received by SAI.
  199. * @retval kStatus_Success Succeed get the transfer count.
  200. * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
  201. */
  202. status_t SAI_TransferGetReceiveCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count);
  203. /*! @} */
  204. #if defined(__cplusplus)
  205. }
  206. #endif
  207. /*!
  208. * @}
  209. */
  210. #endif