fsl_sai_edma.h 11 KB

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