fsl_sai_edma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include "fsl_sai_edma.h"
  31. /*******************************************************************************
  32. * Definitations
  33. ******************************************************************************/
  34. /* Used for 32byte aligned */
  35. #define STCD_ADDR(address) (edma_tcd_t *)(((uint32_t)address + 32) & ~0x1FU)
  36. /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
  37. typedef struct _sai_edma_private_handle
  38. {
  39. I2S_Type *base;
  40. sai_edma_handle_t *handle;
  41. } sai_edma_private_handle_t;
  42. enum _sai_edma_transfer_state
  43. {
  44. kSAI_Busy = 0x0U, /*!< SAI is busy */
  45. kSAI_Idle, /*!< Transfer is done. */
  46. };
  47. /*<! Private handle only used for internally. */
  48. static sai_edma_private_handle_t s_edmaPrivateHandle[FSL_FEATURE_SOC_I2S_COUNT][2];
  49. /*******************************************************************************
  50. * Prototypes
  51. ******************************************************************************/
  52. /*!
  53. * @brief Get the instance number for SAI.
  54. *
  55. * @param base SAI base pointer.
  56. */
  57. extern uint32_t SAI_GetInstance(I2S_Type *base);
  58. /*!
  59. * @brief SAI EDMA callback for send.
  60. *
  61. * @param handle pointer to sai_edma_handle_t structure which stores the transfer state.
  62. * @param userData Parameter for user callback.
  63. * @param done If the DMA transfer finished.
  64. * @param tcds The TCD index.
  65. */
  66. static void SAI_TxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
  67. /*!
  68. * @brief SAI EDMA callback for receive.
  69. *
  70. * @param handle pointer to sai_edma_handle_t structure which stores the transfer state.
  71. * @param userData Parameter for user callback.
  72. * @param done If the DMA transfer finished.
  73. * @param tcds The TCD index.
  74. */
  75. static void SAI_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds);
  76. /*******************************************************************************
  77. * Code
  78. ******************************************************************************/
  79. static void SAI_TxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
  80. {
  81. sai_edma_private_handle_t *privHandle = (sai_edma_private_handle_t *)userData;
  82. sai_edma_handle_t *saiHandle = privHandle->handle;
  83. /* If finished a blcok, call the callback function */
  84. memset(&saiHandle->saiQueue[saiHandle->queueDriver], 0, sizeof(sai_transfer_t));
  85. saiHandle->queueDriver = (saiHandle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  86. if (saiHandle->callback)
  87. {
  88. (saiHandle->callback)(privHandle->base, saiHandle, kStatus_SAI_TxIdle, saiHandle->userData);
  89. }
  90. /* If all data finished, just stop the transfer */
  91. if (saiHandle->saiQueue[saiHandle->queueDriver].data == NULL)
  92. {
  93. SAI_TransferAbortSendEDMA(privHandle->base, saiHandle);
  94. }
  95. }
  96. static void SAI_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
  97. {
  98. sai_edma_private_handle_t *privHandle = (sai_edma_private_handle_t *)userData;
  99. sai_edma_handle_t *saiHandle = privHandle->handle;
  100. /* If finished a blcok, call the callback function */
  101. memset(&saiHandle->saiQueue[saiHandle->queueDriver], 0, sizeof(sai_transfer_t));
  102. saiHandle->queueDriver = (saiHandle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  103. if (saiHandle->callback)
  104. {
  105. (saiHandle->callback)(privHandle->base, saiHandle, kStatus_SAI_RxIdle, saiHandle->userData);
  106. }
  107. /* If all data finished, just stop the transfer */
  108. if (saiHandle->saiQueue[saiHandle->queueDriver].data == NULL)
  109. {
  110. SAI_TransferAbortReceiveEDMA(privHandle->base, saiHandle);
  111. }
  112. }
  113. void SAI_TransferTxCreateHandleEDMA(
  114. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle)
  115. {
  116. assert(handle && dmaHandle);
  117. uint32_t instance = SAI_GetInstance(base);
  118. /* Zero the handle */
  119. memset(handle, 0, sizeof(*handle));
  120. /* Set sai base to handle */
  121. handle->dmaHandle = dmaHandle;
  122. handle->callback = callback;
  123. handle->userData = userData;
  124. /* Set SAI state to idle */
  125. handle->state = kSAI_Idle;
  126. s_edmaPrivateHandle[instance][0].base = base;
  127. s_edmaPrivateHandle[instance][0].handle = handle;
  128. /* Need to use scatter gather */
  129. EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), SAI_XFER_QUEUE_SIZE);
  130. /* Install callback for Tx dma channel */
  131. EDMA_SetCallback(dmaHandle, SAI_TxEDMACallback, &s_edmaPrivateHandle[instance][0]);
  132. }
  133. void SAI_TransferRxCreateHandleEDMA(
  134. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle)
  135. {
  136. assert(handle && dmaHandle);
  137. uint32_t instance = SAI_GetInstance(base);
  138. /* Zero the handle */
  139. memset(handle, 0, sizeof(*handle));
  140. /* Set sai base to handle */
  141. handle->dmaHandle = dmaHandle;
  142. handle->callback = callback;
  143. handle->userData = userData;
  144. /* Set SAI state to idle */
  145. handle->state = kSAI_Idle;
  146. s_edmaPrivateHandle[instance][1].base = base;
  147. s_edmaPrivateHandle[instance][1].handle = handle;
  148. /* Need to use scatter gather */
  149. EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), SAI_XFER_QUEUE_SIZE);
  150. /* Install callback for Tx dma channel */
  151. EDMA_SetCallback(dmaHandle, SAI_RxEDMACallback, &s_edmaPrivateHandle[instance][1]);
  152. }
  153. void SAI_TransferTxSetFormatEDMA(I2S_Type *base,
  154. sai_edma_handle_t *handle,
  155. sai_transfer_format_t *format,
  156. uint32_t mclkSourceClockHz,
  157. uint32_t bclkSourceClockHz)
  158. {
  159. assert(handle && format);
  160. /* Configure the audio format to SAI registers */
  161. SAI_TxSetFormat(base, format, mclkSourceClockHz, bclkSourceClockHz);
  162. /* Get the tranfer size from format, this should be used in EDMA configuration */
  163. if (format->bitWidth == 24U)
  164. {
  165. handle->bytesPerFrame = 4U;
  166. }
  167. else
  168. {
  169. handle->bytesPerFrame = format->bitWidth / 8U;
  170. }
  171. /* Update the data channel SAI used */
  172. handle->channel = format->channel;
  173. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  174. handle->count = FSL_FEATURE_SAI_FIFO_COUNT - format->watermark;
  175. #else
  176. handle->count = 1U;
  177. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  178. }
  179. void SAI_TransferRxSetFormatEDMA(I2S_Type *base,
  180. sai_edma_handle_t *handle,
  181. sai_transfer_format_t *format,
  182. uint32_t mclkSourceClockHz,
  183. uint32_t bclkSourceClockHz)
  184. {
  185. assert(handle && format);
  186. /* Configure the audio format to SAI registers */
  187. SAI_RxSetFormat(base, format, mclkSourceClockHz, bclkSourceClockHz);
  188. /* Get the tranfer size from format, this should be used in EDMA configuration */
  189. if (format->bitWidth == 24U)
  190. {
  191. handle->bytesPerFrame = 4U;
  192. }
  193. else
  194. {
  195. handle->bytesPerFrame = format->bitWidth / 8U;
  196. }
  197. /* Update the data channel SAI used */
  198. handle->channel = format->channel;
  199. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  200. handle->count = format->watermark;
  201. #else
  202. handle->count = 1U;
  203. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  204. }
  205. status_t SAI_TransferSendEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer)
  206. {
  207. assert(handle && xfer);
  208. edma_transfer_config_t config = {0};
  209. uint32_t destAddr = SAI_TxGetDataRegisterAddress(base, handle->channel);
  210. /* Check if input parameter invalid */
  211. if ((xfer->data == NULL) || (xfer->dataSize == 0U))
  212. {
  213. return kStatus_InvalidArgument;
  214. }
  215. if (handle->saiQueue[handle->queueUser].data)
  216. {
  217. return kStatus_SAI_QueueFull;
  218. }
  219. /* Change the state of handle */
  220. handle->state = kSAI_Busy;
  221. /* Update the queue state */
  222. handle->transferSize[handle->queueUser] = xfer->dataSize;
  223. handle->saiQueue[handle->queueUser].data = xfer->data;
  224. handle->saiQueue[handle->queueUser].dataSize = xfer->dataSize;
  225. handle->queueUser = (handle->queueUser + 1) % SAI_XFER_QUEUE_SIZE;
  226. /* Prepare edma configure */
  227. EDMA_PrepareTransfer(&config, xfer->data, handle->bytesPerFrame, (void *)destAddr, handle->bytesPerFrame,
  228. handle->count * handle->bytesPerFrame, xfer->dataSize, kEDMA_MemoryToPeripheral);
  229. /* Store the initially configured eDMA minor byte transfer count into the SAI handle */
  230. handle->nbytes = handle->count * handle->bytesPerFrame;
  231. EDMA_SubmitTransfer(handle->dmaHandle, &config);
  232. /* Start DMA transfer */
  233. EDMA_StartTransfer(handle->dmaHandle);
  234. /* Enable DMA enable bit */
  235. SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
  236. /* Enable SAI Tx clock */
  237. SAI_TxEnable(base, true);
  238. return kStatus_Success;
  239. }
  240. status_t SAI_TransferReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer)
  241. {
  242. assert(handle && xfer);
  243. edma_transfer_config_t config = {0};
  244. uint32_t srcAddr = SAI_RxGetDataRegisterAddress(base, handle->channel);
  245. /* Check if input parameter invalid */
  246. if ((xfer->data == NULL) || (xfer->dataSize == 0U))
  247. {
  248. return kStatus_InvalidArgument;
  249. }
  250. if (handle->saiQueue[handle->queueUser].data)
  251. {
  252. return kStatus_SAI_QueueFull;
  253. }
  254. /* Change the state of handle */
  255. handle->state = kSAI_Busy;
  256. /* Update queue state */
  257. handle->transferSize[handle->queueUser] = xfer->dataSize;
  258. handle->saiQueue[handle->queueUser].data = xfer->data;
  259. handle->saiQueue[handle->queueUser].dataSize = xfer->dataSize;
  260. handle->queueUser = (handle->queueUser + 1) % SAI_XFER_QUEUE_SIZE;
  261. /* Prepare edma configure */
  262. EDMA_PrepareTransfer(&config, (void *)srcAddr, handle->bytesPerFrame, xfer->data, handle->bytesPerFrame,
  263. handle->count * handle->bytesPerFrame, xfer->dataSize, kEDMA_PeripheralToMemory);
  264. /* Store the initially configured eDMA minor byte transfer count into the SAI handle */
  265. handle->nbytes = handle->count * handle->bytesPerFrame;
  266. EDMA_SubmitTransfer(handle->dmaHandle, &config);
  267. /* Start DMA transfer */
  268. EDMA_StartTransfer(handle->dmaHandle);
  269. /* Enable DMA enable bit */
  270. SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
  271. /* Enable SAI Rx clock */
  272. SAI_RxEnable(base, true);
  273. return kStatus_Success;
  274. }
  275. void SAI_TransferAbortSendEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  276. {
  277. assert(handle);
  278. /* Disable dma */
  279. EDMA_AbortTransfer(handle->dmaHandle);
  280. /* Disable DMA enable bit */
  281. SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, false);
  282. /* Disable Tx */
  283. SAI_TxEnable(base, false);
  284. /* Set the handle state */
  285. handle->state = kSAI_Idle;
  286. }
  287. void SAI_TransferAbortReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  288. {
  289. assert(handle);
  290. /* Disable dma */
  291. EDMA_AbortTransfer(handle->dmaHandle);
  292. /* Disable DMA enable bit */
  293. SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, false);
  294. /* Disable Rx */
  295. SAI_RxEnable(base, false);
  296. /* Set the handle state */
  297. handle->state = kSAI_Idle;
  298. }
  299. status_t SAI_TransferGetSendCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count)
  300. {
  301. assert(handle);
  302. status_t status = kStatus_Success;
  303. if (handle->state != kSAI_Busy)
  304. {
  305. status = kStatus_NoTransferInProgress;
  306. }
  307. else
  308. {
  309. *count = (handle->transferSize[handle->queueDriver] -
  310. (uint32_t)handle->nbytes *
  311. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  312. }
  313. return status;
  314. }
  315. status_t SAI_TransferGetReceiveCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count)
  316. {
  317. assert(handle);
  318. status_t status = kStatus_Success;
  319. if (handle->state != kSAI_Busy)
  320. {
  321. status = kStatus_NoTransferInProgress;
  322. }
  323. else
  324. {
  325. *count = (handle->transferSize[handle->queueDriver] -
  326. (uint32_t)handle->nbytes *
  327. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  328. }
  329. return status;
  330. }