fsl_sai_edma.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. /* Disable DMA enable bit */
  94. SAI_TxEnableDMA(privHandle->base, kSAI_FIFORequestDMAEnable, false);
  95. EDMA_AbortTransfer(handle);
  96. }
  97. }
  98. static void SAI_RxEDMACallback(edma_handle_t *handle, void *userData, bool done, uint32_t tcds)
  99. {
  100. sai_edma_private_handle_t *privHandle = (sai_edma_private_handle_t *)userData;
  101. sai_edma_handle_t *saiHandle = privHandle->handle;
  102. /* If finished a blcok, call the callback function */
  103. memset(&saiHandle->saiQueue[saiHandle->queueDriver], 0, sizeof(sai_transfer_t));
  104. saiHandle->queueDriver = (saiHandle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  105. if (saiHandle->callback)
  106. {
  107. (saiHandle->callback)(privHandle->base, saiHandle, kStatus_SAI_RxIdle, saiHandle->userData);
  108. }
  109. /* If all data finished, just stop the transfer */
  110. if (saiHandle->saiQueue[saiHandle->queueDriver].data == NULL)
  111. {
  112. /* Disable DMA enable bit */
  113. SAI_RxEnableDMA(privHandle->base, kSAI_FIFORequestDMAEnable, false);
  114. EDMA_AbortTransfer(handle);
  115. }
  116. }
  117. void SAI_TransferTxCreateHandleEDMA(
  118. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle)
  119. {
  120. assert(handle && dmaHandle);
  121. uint32_t instance = SAI_GetInstance(base);
  122. /* Zero the handle */
  123. memset(handle, 0, sizeof(*handle));
  124. /* Set sai base to handle */
  125. handle->dmaHandle = dmaHandle;
  126. handle->callback = callback;
  127. handle->userData = userData;
  128. /* Set SAI state to idle */
  129. handle->state = kSAI_Idle;
  130. s_edmaPrivateHandle[instance][0].base = base;
  131. s_edmaPrivateHandle[instance][0].handle = handle;
  132. /* Need to use scatter gather */
  133. EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), SAI_XFER_QUEUE_SIZE);
  134. /* Install callback for Tx dma channel */
  135. EDMA_SetCallback(dmaHandle, SAI_TxEDMACallback, &s_edmaPrivateHandle[instance][0]);
  136. }
  137. void SAI_TransferRxCreateHandleEDMA(
  138. I2S_Type *base, sai_edma_handle_t *handle, sai_edma_callback_t callback, void *userData, edma_handle_t *dmaHandle)
  139. {
  140. assert(handle && dmaHandle);
  141. uint32_t instance = SAI_GetInstance(base);
  142. /* Zero the handle */
  143. memset(handle, 0, sizeof(*handle));
  144. /* Set sai base to handle */
  145. handle->dmaHandle = dmaHandle;
  146. handle->callback = callback;
  147. handle->userData = userData;
  148. /* Set SAI state to idle */
  149. handle->state = kSAI_Idle;
  150. s_edmaPrivateHandle[instance][1].base = base;
  151. s_edmaPrivateHandle[instance][1].handle = handle;
  152. /* Need to use scatter gather */
  153. EDMA_InstallTCDMemory(dmaHandle, STCD_ADDR(handle->tcd), SAI_XFER_QUEUE_SIZE);
  154. /* Install callback for Tx dma channel */
  155. EDMA_SetCallback(dmaHandle, SAI_RxEDMACallback, &s_edmaPrivateHandle[instance][1]);
  156. }
  157. void SAI_TransferTxSetFormatEDMA(I2S_Type *base,
  158. sai_edma_handle_t *handle,
  159. sai_transfer_format_t *format,
  160. uint32_t mclkSourceClockHz,
  161. uint32_t bclkSourceClockHz)
  162. {
  163. assert(handle && format);
  164. /* Configure the audio format to SAI registers */
  165. SAI_TxSetFormat(base, format, mclkSourceClockHz, bclkSourceClockHz);
  166. /* Get the tranfer size from format, this should be used in EDMA configuration */
  167. if (format->bitWidth == 24U)
  168. {
  169. handle->bytesPerFrame = 4U;
  170. }
  171. else
  172. {
  173. handle->bytesPerFrame = format->bitWidth / 8U;
  174. }
  175. /* Update the data channel SAI used */
  176. handle->channel = format->channel;
  177. /* Clear the channel enable bits unitl do a send/receive */
  178. base->TCR3 &= ~I2S_TCR3_TCE_MASK;
  179. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  180. handle->count = FSL_FEATURE_SAI_FIFO_COUNT - format->watermark;
  181. #else
  182. handle->count = 1U;
  183. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  184. }
  185. void SAI_TransferRxSetFormatEDMA(I2S_Type *base,
  186. sai_edma_handle_t *handle,
  187. sai_transfer_format_t *format,
  188. uint32_t mclkSourceClockHz,
  189. uint32_t bclkSourceClockHz)
  190. {
  191. assert(handle && format);
  192. /* Configure the audio format to SAI registers */
  193. SAI_RxSetFormat(base, format, mclkSourceClockHz, bclkSourceClockHz);
  194. /* Get the tranfer size from format, this should be used in EDMA configuration */
  195. if (format->bitWidth == 24U)
  196. {
  197. handle->bytesPerFrame = 4U;
  198. }
  199. else
  200. {
  201. handle->bytesPerFrame = format->bitWidth / 8U;
  202. }
  203. /* Update the data channel SAI used */
  204. handle->channel = format->channel;
  205. /* Clear the channel enable bits unitl do a send/receive */
  206. base->RCR3 &= ~I2S_RCR3_RCE_MASK;
  207. #if defined(FSL_FEATURE_SAI_FIFO_COUNT) && (FSL_FEATURE_SAI_FIFO_COUNT > 1)
  208. handle->count = format->watermark;
  209. #else
  210. handle->count = 1U;
  211. #endif /* FSL_FEATURE_SAI_FIFO_COUNT */
  212. }
  213. status_t SAI_TransferSendEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer)
  214. {
  215. assert(handle && xfer);
  216. edma_transfer_config_t config = {0};
  217. uint32_t destAddr = SAI_TxGetDataRegisterAddress(base, handle->channel);
  218. /* Check if input parameter invalid */
  219. if ((xfer->data == NULL) || (xfer->dataSize == 0U))
  220. {
  221. return kStatus_InvalidArgument;
  222. }
  223. if (handle->saiQueue[handle->queueUser].data)
  224. {
  225. return kStatus_SAI_QueueFull;
  226. }
  227. /* Change the state of handle */
  228. handle->state = kSAI_Busy;
  229. /* Update the queue state */
  230. handle->transferSize[handle->queueUser] = xfer->dataSize;
  231. handle->saiQueue[handle->queueUser].data = xfer->data;
  232. handle->saiQueue[handle->queueUser].dataSize = xfer->dataSize;
  233. handle->queueUser = (handle->queueUser + 1) % SAI_XFER_QUEUE_SIZE;
  234. /* Prepare edma configure */
  235. EDMA_PrepareTransfer(&config, xfer->data, handle->bytesPerFrame, (void *)destAddr, handle->bytesPerFrame,
  236. handle->count * handle->bytesPerFrame, xfer->dataSize, kEDMA_MemoryToPeripheral);
  237. /* Store the initially configured eDMA minor byte transfer count into the SAI handle */
  238. handle->nbytes = handle->count * handle->bytesPerFrame;
  239. EDMA_SubmitTransfer(handle->dmaHandle, &config);
  240. /* Start DMA transfer */
  241. EDMA_StartTransfer(handle->dmaHandle);
  242. /* Enable DMA enable bit */
  243. SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
  244. /* Enable SAI Tx clock */
  245. SAI_TxEnable(base, true);
  246. /* Enable the channel FIFO */
  247. base->TCR3 |= I2S_TCR3_TCE(1U << handle->channel);
  248. return kStatus_Success;
  249. }
  250. status_t SAI_TransferReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle, sai_transfer_t *xfer)
  251. {
  252. assert(handle && xfer);
  253. edma_transfer_config_t config = {0};
  254. uint32_t srcAddr = SAI_RxGetDataRegisterAddress(base, handle->channel);
  255. /* Check if input parameter invalid */
  256. if ((xfer->data == NULL) || (xfer->dataSize == 0U))
  257. {
  258. return kStatus_InvalidArgument;
  259. }
  260. if (handle->saiQueue[handle->queueUser].data)
  261. {
  262. return kStatus_SAI_QueueFull;
  263. }
  264. /* Change the state of handle */
  265. handle->state = kSAI_Busy;
  266. /* Update queue state */
  267. handle->transferSize[handle->queueUser] = xfer->dataSize;
  268. handle->saiQueue[handle->queueUser].data = xfer->data;
  269. handle->saiQueue[handle->queueUser].dataSize = xfer->dataSize;
  270. handle->queueUser = (handle->queueUser + 1) % SAI_XFER_QUEUE_SIZE;
  271. /* Prepare edma configure */
  272. EDMA_PrepareTransfer(&config, (void *)srcAddr, handle->bytesPerFrame, xfer->data, handle->bytesPerFrame,
  273. handle->count * handle->bytesPerFrame, xfer->dataSize, kEDMA_PeripheralToMemory);
  274. /* Store the initially configured eDMA minor byte transfer count into the SAI handle */
  275. handle->nbytes = handle->count * handle->bytesPerFrame;
  276. EDMA_SubmitTransfer(handle->dmaHandle, &config);
  277. /* Start DMA transfer */
  278. EDMA_StartTransfer(handle->dmaHandle);
  279. /* Enable DMA enable bit */
  280. SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
  281. /* Enable the channel FIFO */
  282. base->RCR3 |= I2S_RCR3_RCE(1U << handle->channel);
  283. /* Enable SAI Rx clock */
  284. SAI_RxEnable(base, true);
  285. return kStatus_Success;
  286. }
  287. void SAI_TransferAbortSendEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  288. {
  289. assert(handle);
  290. /* Disable dma */
  291. EDMA_AbortTransfer(handle->dmaHandle);
  292. /* Disable the channel FIFO */
  293. base->TCR3 &= ~I2S_TCR3_TCE_MASK;
  294. /* Disable DMA enable bit */
  295. SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, false);
  296. /* Disable Tx */
  297. SAI_TxEnable(base, false);
  298. /* Reset the FIFO pointer, at the same time clear all error flags if set */
  299. base->TCSR |= (I2S_TCSR_FR_MASK | I2S_TCSR_SR_MASK);
  300. base->TCSR &= ~I2S_TCSR_SR_MASK;
  301. /* Handle the queue index */
  302. memset(&handle->saiQueue[handle->queueDriver], 0, sizeof(sai_transfer_t));
  303. handle->queueDriver = (handle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  304. /* Set the handle state */
  305. handle->state = kSAI_Idle;
  306. }
  307. void SAI_TransferAbortReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  308. {
  309. assert(handle);
  310. /* Disable dma */
  311. EDMA_AbortTransfer(handle->dmaHandle);
  312. /* Disable the channel FIFO */
  313. base->RCR3 &= ~I2S_RCR3_RCE_MASK;
  314. /* Disable DMA enable bit */
  315. SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, false);
  316. /* Disable Rx */
  317. SAI_RxEnable(base, false);
  318. /* Reset the FIFO pointer, at the same time clear all error flags if set */
  319. base->RCSR |= (I2S_RCSR_FR_MASK | I2S_RCSR_SR_MASK);
  320. base->RCSR &= ~I2S_RCSR_SR_MASK;
  321. /* Handle the queue index */
  322. memset(&handle->saiQueue[handle->queueDriver], 0, sizeof(sai_transfer_t));
  323. handle->queueDriver = (handle->queueDriver + 1) % SAI_XFER_QUEUE_SIZE;
  324. /* Set the handle state */
  325. handle->state = kSAI_Idle;
  326. }
  327. void SAI_TransferTerminateSendEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  328. {
  329. assert(handle);
  330. /* Abort the current transfer */
  331. SAI_TransferAbortSendEDMA(base, handle);
  332. /* Clear all the internal information */
  333. memset(handle->tcd, 0U, sizeof(handle->tcd));
  334. memset(handle->saiQueue, 0U, sizeof(handle->saiQueue));
  335. memset(handle->transferSize, 0U, sizeof(handle->transferSize));
  336. handle->queueUser = 0U;
  337. handle->queueDriver = 0U;
  338. }
  339. void SAI_TransferTerminateReceiveEDMA(I2S_Type *base, sai_edma_handle_t *handle)
  340. {
  341. assert(handle);
  342. /* Abort the current transfer */
  343. SAI_TransferAbortReceiveEDMA(base, handle);
  344. /* Clear all the internal information */
  345. memset(handle->tcd, 0U, sizeof(handle->tcd));
  346. memset(handle->saiQueue, 0U, sizeof(handle->saiQueue));
  347. memset(handle->transferSize, 0U, sizeof(handle->transferSize));
  348. handle->queueUser = 0U;
  349. handle->queueDriver = 0U;
  350. }
  351. status_t SAI_TransferGetSendCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count)
  352. {
  353. assert(handle);
  354. status_t status = kStatus_Success;
  355. if (handle->state != kSAI_Busy)
  356. {
  357. status = kStatus_NoTransferInProgress;
  358. }
  359. else
  360. {
  361. *count = (handle->transferSize[handle->queueDriver] -
  362. (uint32_t)handle->nbytes *
  363. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  364. }
  365. return status;
  366. }
  367. status_t SAI_TransferGetReceiveCountEDMA(I2S_Type *base, sai_edma_handle_t *handle, size_t *count)
  368. {
  369. assert(handle);
  370. status_t status = kStatus_Success;
  371. if (handle->state != kSAI_Busy)
  372. {
  373. status = kStatus_NoTransferInProgress;
  374. }
  375. else
  376. {
  377. *count = (handle->transferSize[handle->queueDriver] -
  378. (uint32_t)handle->nbytes *
  379. EDMA_GetRemainingMajorLoopCount(handle->dmaHandle->base, handle->dmaHandle->channel));
  380. }
  381. return status;
  382. }