fsl_sai_edma.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /*! @name Driver version */
  46. /*@{*/
  47. #define FSL_SAI_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 1, 4)) /*!< Version 2.1.4 */
  48. /*@}*/
  49. typedef struct _sai_edma_handle sai_edma_handle_t;
  50. /*! @brief SAI eDMA transfer callback function for finish and error */
  51. typedef void (*sai_edma_callback_t)(I2S_Type *base, sai_edma_handle_t *handle, status_t status, void *userData);
  52. /*! @brief SAI DMA transfer handle, users should not touch the content of the handle.*/
  53. struct _sai_edma_handle
  54. {
  55. edma_handle_t *dmaHandle; /*!< DMA handler for SAI send */
  56. uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
  57. uint8_t bytesPerFrame; /*!< Bytes in a frame */
  58. uint8_t channel; /*!< Which data channel */
  59. uint8_t count; /*!< The transfer data count in a DMA request */
  60. uint32_t state; /*!< Internal state for SAI eDMA transfer */
  61. sai_edma_callback_t callback; /*!< Callback for users while transfer finish or error occurs */
  62. void *userData; /*!< User callback parameter */
  63. uint8_t tcd[(SAI_XFER_QUEUE_SIZE + 1U) * sizeof(edma_tcd_t)]; /*!< TCD pool for eDMA transfer. */
  64. sai_transfer_t saiQueue[SAI_XFER_QUEUE_SIZE]; /*!< Transfer queue storing queued transfer. */
  65. size_t transferSize[SAI_XFER_QUEUE_SIZE]; /*!< Data bytes need to transfer */
  66. volatile uint8_t queueUser; /*!< Index for user to queue transfer. */
  67. volatile uint8_t queueDriver; /*!< Index for driver to get the transfer data and size */
  68. };
  69. /*******************************************************************************
  70. * APIs
  71. ******************************************************************************/
  72. #if defined(__cplusplus)
  73. extern "C" {
  74. #endif
  75. /*!
  76. * @name eDMA Transactional
  77. * @{
  78. */
  79. /*!
  80. * @brief Initializes the SAI eDMA handle.
  81. *
  82. * This function initializes the SAI master DMA handle, which can be used for other SAI master transactional APIs.
  83. * Usually, for a specified SAI instance, call this API once to get the initialized handle.
  84. *
  85. * @param base SAI base pointer.
  86. * @param handle SAI eDMA handle pointer.
  87. * @param base SAI peripheral base address.
  88. * @param callback Pointer to user callback function.
  89. * @param userData User parameter passed to the callback function.
  90. * @param dmaHandle eDMA handle pointer, this handle shall be static allocated by users.
  91. */
  92. void SAI_TransferTxCreateHandleEDMA(
  93. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle);
  94. /*!
  95. * @brief Initializes the SAI Rx eDMA handle.
  96. *
  97. * This function initializes the SAI slave DMA handle, which can be used for other SAI master transactional APIs.
  98. * Usually, for a specified SAI instance, call this API once to get the initialized handle.
  99. *
  100. * @param base SAI base pointer.
  101. * @param handle SAI eDMA handle pointer.
  102. * @param base SAI peripheral base address.
  103. * @param callback Pointer to user callback function.
  104. * @param userData User parameter passed to the callback function.
  105. * @param dmaHandle eDMA handle pointer, this handle shall be static allocated by users.
  106. */
  107. void SAI_TransferRxCreateHandleEDMA(
  108. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle);
  109. /*!
  110. * @brief Configures the SAI Tx audio format.
  111. *
  112. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  113. * format to be transferred. This function also sets the eDMA parameter according to formatting requirements.
  114. *
  115. * @param base SAI base pointer.
  116. * @param handle SAI eDMA handle pointer.
  117. * @param format Pointer to SAI audio data format structure.
  118. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  119. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If bit clock source is master
  120. * clock, this value should equals to masterClockHz in format.
  121. * @retval kStatus_Success Audio format set successfully.
  122. * @retval kStatus_InvalidArgument The input argument is invalid.
  123. */
  124. void SAI_TransferTxSetFormatEDMA(I2S_Type *base,
  125. sai_edma_handle_t *handle,
  126. sai_transfer_format_t *format,
  127. uint32_t mclkSourceClockHz,
  128. uint32_t bclkSourceClockHz);
  129. /*!
  130. * @brief Configures the SAI Rx audio format.
  131. *
  132. * The audio format can be changed at run-time. This function configures the sample rate and audio data
  133. * format to be transferred. This function also sets the eDMA parameter according to formatting requirements.
  134. *
  135. * @param base SAI base pointer.
  136. * @param handle SAI eDMA handle pointer.
  137. * @param format Pointer to SAI audio data format structure.
  138. * @param mclkSourceClockHz SAI master clock source frequency in Hz.
  139. * @param bclkSourceClockHz SAI bit clock source frequency in Hz. If a bit clock source is the master
  140. * clock, this value should equal to masterClockHz in format.
  141. * @retval kStatus_Success Audio format set successfully.
  142. * @retval kStatus_InvalidArgument The input argument is invalid.
  143. */
  144. void SAI_TransferRxSetFormatEDMA(I2S_Type *base,
  145. sai_edma_handle_t *handle,
  146. sai_transfer_format_t *format,
  147. uint32_t mclkSourceClockHz,
  148. uint32_t bclkSourceClockHz);
  149. /*!
  150. * @brief Performs a non-blocking SAI transfer using DMA.
  151. *
  152. * @note This interface returns immediately after the transfer initiates. Call
  153. * SAI_GetTransferStatus to poll the transfer status and check whether the SAI transfer is finished.
  154. *
  155. * @param base SAI base pointer.
  156. * @param handle SAI eDMA handle pointer.
  157. * @param xfer Pointer to the DMA transfer structure.
  158. * @retval kStatus_Success Start a SAI eDMA send successfully.
  159. * @retval kStatus_InvalidArgument The input argument is invalid.
  160. * @retval kStatus_TxBusy SAI is busy sending data.
  161. */
  162. status_t SAI_TransferSendEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer);
  163. /*!
  164. * @brief Performs a non-blocking SAI receive using eDMA.
  165. *
  166. * @note This interface returns immediately after the transfer initiates. Call
  167. * the SAI_GetReceiveRemainingBytes to poll the transfer status and check whether the SAI transfer is finished.
  168. *
  169. * @param base SAI base pointer
  170. * @param handle SAI eDMA handle pointer.
  171. * @param xfer Pointer to DMA transfer structure.
  172. * @retval kStatus_Success Start a SAI eDMA receive successfully.
  173. * @retval kStatus_InvalidArgument The input argument is invalid.
  174. * @retval kStatus_RxBusy SAI is busy receiving data.
  175. */
  176. status_t SAI_TransferReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer);
  177. /*!
  178. * @brief Terminate all SAI send.
  179. *
  180. * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
  181. * current transfer slot, please call SAI_TransferAbortSendEDMA.
  182. *
  183. * @param base SAI base pointer.
  184. * @param handle SAI eDMA handle pointer.
  185. */
  186. void SAI_TransferTerminateSendEDMA(I2S_Type *base, sai_edma_handle_t *handle);
  187. /*!
  188. * @brief Terminate all SAI receive.
  189. *
  190. * This function will clear all transfer slots buffered in the sai queue. If users only want to abort the
  191. * current transfer slot, please call SAI_TransferAbortReceiveEDMA.
  192. *
  193. * @param base SAI base pointer.
  194. * @param handle SAI eDMA handle pointer.
  195. */
  196. void SAI_TransferTerminateReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle);
  197. /*!
  198. * @brief Aborts a SAI transfer using eDMA.
  199. *
  200. * This function only aborts the current transfer slots, the other transfer slots' information still kept
  201. * in the handler. If users want to terminate all transfer slots, just call SAI_TransferTerminateSendEDMA.
  202. *
  203. * @param base SAI base pointer.
  204. * @param handle SAI eDMA handle pointer.
  205. */
  206. void SAI_TransferAbortSendEDMA(I2S_Type *base, sai_edma_handle_t *handle);
  207. /*!
  208. * @brief Aborts a SAI receive using eDMA.
  209. *
  210. * This function only aborts the current transfer slots, the other transfer slots' information still kept
  211. * in the handler. If users want to terminate all transfer slots, just call SAI_TransferTerminateReceiveEDMA.
  212. *
  213. * @param base SAI base pointer
  214. * @param handle SAI eDMA handle pointer.
  215. */
  216. void SAI_TransferAbortReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle);
  217. /*!
  218. * @brief Gets byte count sent by SAI.
  219. *
  220. * @param base SAI base pointer.
  221. * @param handle SAI eDMA handle pointer.
  222. * @param count Bytes count sent by SAI.
  223. * @retval kStatus_Success Succeed get the transfer count.
  224. * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
  225. */
  226. status_t SAI_TransferGetSendCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count);
  227. /*!
  228. * @brief Gets byte count received by SAI.
  229. *
  230. * @param base SAI base pointer
  231. * @param handle SAI eDMA handle pointer.
  232. * @param count Bytes count received by SAI.
  233. * @retval kStatus_Success Succeed get the transfer count.
  234. * @retval kStatus_NoTransferInProgress There is no non-blocking transaction in progress.
  235. */
  236. status_t SAI_TransferGetReceiveCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count);
  237. /*! @} */
  238. #if defined(__cplusplus)
  239. }
  240. #endif
  241. /*!
  242. * @}
  243. */
  244. #endif