fsl_flexio_uart_edma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_flexio_uart_edma.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
  35. typedef struct _flexio_uart_edma_private_handle
  36. {
  37. FLEXIO_UART_Type *base;
  38. flexio_uart_edma_handle_t *handle;
  39. } flexio_uart_edma_private_handle_t;
  40. /* UART EDMA transfer handle. */
  41. enum _flexio_uart_edma_tansfer_states
  42. {
  43. kFLEXIO_UART_TxIdle, /* TX idle. */
  44. kFLEXIO_UART_TxBusy, /* TX busy. */
  45. kFLEXIO_UART_RxIdle, /* RX idle. */
  46. kFLEXIO_UART_RxBusy /* RX busy. */
  47. };
  48. /*******************************************************************************
  49. * Definitions
  50. ******************************************************************************/
  51. /*< @brief user configurable flexio uart handle count. */
  52. #define FLEXIO_UART_HANDLE_COUNT 2
  53. /*<! Private handle only used for internally. */
  54. static flexio_uart_edma_private_handle_t s_edmaPrivateHandle[FLEXIO_UART_HANDLE_COUNT];
  55. /*******************************************************************************
  56. * Prototypes
  57. ******************************************************************************/
  58. /*!
  59. * @brief FLEXIO UART EDMA send finished callback function.
  60. *
  61. * This function is called when FLEXIO UART EDMA send finished. It disables the UART
  62. * TX EDMA request and sends @ref kStatus_FLEXIO_UART_TxIdle to FLEXIO UART callback.
  63. *
  64. * @param handle The EDMA handle.
  65. * @param param Callback function parameter.
  66. */
  67. static void FLEXIO_UART_TransferSendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  68. /*!
  69. * @brief FLEXIO UART EDMA receive finished callback function.
  70. *
  71. * This function is called when FLEXIO UART EDMA receive finished. It disables the UART
  72. * RX EDMA request and sends @ref kStatus_FLEXIO_UART_RxIdle to UART callback.
  73. *
  74. * @param handle The EDMA handle.
  75. * @param param Callback function parameter.
  76. */
  77. static void FLEXIO_UART_TransferReceiveEDMACallback(edma_handle_t *handle,
  78. void *param,
  79. bool transferDone,
  80. uint32_t tcds);
  81. /*******************************************************************************
  82. * Code
  83. ******************************************************************************/
  84. static void FLEXIO_UART_TransferSendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  85. {
  86. flexio_uart_edma_private_handle_t *uartPrivateHandle = (flexio_uart_edma_private_handle_t *)param;
  87. assert(uartPrivateHandle->handle);
  88. /* Avoid the warning for unused variables. */
  89. handle = handle;
  90. tcds = tcds;
  91. if (transferDone)
  92. {
  93. FLEXIO_UART_TransferAbortSendEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  94. if (uartPrivateHandle->handle->callback)
  95. {
  96. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle,
  97. kStatus_FLEXIO_UART_TxIdle, uartPrivateHandle->handle->userData);
  98. }
  99. }
  100. }
  101. static void FLEXIO_UART_TransferReceiveEDMACallback(edma_handle_t *handle,
  102. void *param,
  103. bool transferDone,
  104. uint32_t tcds)
  105. {
  106. flexio_uart_edma_private_handle_t *uartPrivateHandle = (flexio_uart_edma_private_handle_t *)param;
  107. assert(uartPrivateHandle->handle);
  108. /* Avoid the warning for unused variables. */
  109. handle = handle;
  110. tcds = tcds;
  111. if (transferDone)
  112. {
  113. /* Disable transfer. */
  114. FLEXIO_UART_TransferAbortReceiveEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  115. if (uartPrivateHandle->handle->callback)
  116. {
  117. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle,
  118. kStatus_FLEXIO_UART_RxIdle, uartPrivateHandle->handle->userData);
  119. }
  120. }
  121. }
  122. status_t FLEXIO_UART_TransferCreateHandleEDMA(FLEXIO_UART_Type *base,
  123. flexio_uart_edma_handle_t *handle,
  124. flexio_uart_edma_transfer_callback_t callback,
  125. void *userData,
  126. edma_handle_t *txEdmaHandle,
  127. edma_handle_t *rxEdmaHandle)
  128. {
  129. assert(handle);
  130. uint8_t index = 0;
  131. /* Find the an empty handle pointer to store the handle. */
  132. for (index = 0; index < FLEXIO_UART_HANDLE_COUNT; index++)
  133. {
  134. if (s_edmaPrivateHandle[index].base == NULL)
  135. {
  136. s_edmaPrivateHandle[index].base = base;
  137. s_edmaPrivateHandle[index].handle = handle;
  138. break;
  139. }
  140. }
  141. if (index == FLEXIO_UART_HANDLE_COUNT)
  142. {
  143. return kStatus_OutOfRange;
  144. }
  145. memset(handle, 0, sizeof(*handle));
  146. handle->rxState = kFLEXIO_UART_RxIdle;
  147. handle->txState = kFLEXIO_UART_TxIdle;
  148. handle->rxEdmaHandle = rxEdmaHandle;
  149. handle->txEdmaHandle = txEdmaHandle;
  150. handle->callback = callback;
  151. handle->userData = userData;
  152. /* Configure TX. */
  153. if (txEdmaHandle)
  154. {
  155. EDMA_SetCallback(handle->txEdmaHandle, FLEXIO_UART_TransferSendEDMACallback, &s_edmaPrivateHandle);
  156. }
  157. /* Configure RX. */
  158. if (rxEdmaHandle)
  159. {
  160. EDMA_SetCallback(handle->rxEdmaHandle, FLEXIO_UART_TransferReceiveEDMACallback, &s_edmaPrivateHandle);
  161. }
  162. return kStatus_Success;
  163. }
  164. status_t FLEXIO_UART_TransferSendEDMA(FLEXIO_UART_Type *base,
  165. flexio_uart_edma_handle_t *handle,
  166. flexio_uart_transfer_t *xfer)
  167. {
  168. assert(handle->txEdmaHandle);
  169. edma_transfer_config_t xferConfig;
  170. status_t status;
  171. /* Return error if xfer invalid. */
  172. if ((0U == xfer->dataSize) || (NULL == xfer->data))
  173. {
  174. return kStatus_InvalidArgument;
  175. }
  176. /* If previous TX not finished. */
  177. if (kFLEXIO_UART_TxBusy == handle->txState)
  178. {
  179. status = kStatus_FLEXIO_UART_TxBusy;
  180. }
  181. else
  182. {
  183. handle->txState = kFLEXIO_UART_TxBusy;
  184. handle->txDataSizeAll = xfer->dataSize;
  185. /* Prepare transfer. */
  186. EDMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint8_t),
  187. (void *)FLEXIO_UART_GetTxDataRegisterAddress(base), sizeof(uint8_t), sizeof(uint8_t),
  188. xfer->dataSize, kEDMA_MemoryToPeripheral);
  189. /* Store the initially configured eDMA minor byte transfer count into the FLEXIO UART handle */
  190. handle->nbytes = sizeof(uint8_t);
  191. /* Submit transfer. */
  192. EDMA_SubmitTransfer(handle->txEdmaHandle, &xferConfig);
  193. EDMA_StartTransfer(handle->txEdmaHandle);
  194. /* Enable UART TX EDMA. */
  195. FLEXIO_UART_EnableTxDMA(base, true);
  196. status = kStatus_Success;
  197. }
  198. return status;
  199. }
  200. status_t FLEXIO_UART_TransferReceiveEDMA(FLEXIO_UART_Type *base,
  201. flexio_uart_edma_handle_t *handle,
  202. flexio_uart_transfer_t *xfer)
  203. {
  204. assert(handle->rxEdmaHandle);
  205. edma_transfer_config_t xferConfig;
  206. status_t status;
  207. /* Return error if xfer invalid. */
  208. if ((0U == xfer->dataSize) || (NULL == xfer->data))
  209. {
  210. return kStatus_InvalidArgument;
  211. }
  212. /* If previous RX not finished. */
  213. if (kFLEXIO_UART_RxBusy == handle->rxState)
  214. {
  215. status = kStatus_FLEXIO_UART_RxBusy;
  216. }
  217. else
  218. {
  219. handle->rxState = kFLEXIO_UART_RxBusy;
  220. handle->rxDataSizeAll = xfer->dataSize;
  221. /* Prepare transfer. */
  222. EDMA_PrepareTransfer(&xferConfig, (void *)FLEXIO_UART_GetRxDataRegisterAddress(base), sizeof(uint8_t),
  223. xfer->data, sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_PeripheralToMemory);
  224. /* Store the initially configured eDMA minor byte transfer count into the FLEXIO UART handle */
  225. handle->nbytes = sizeof(uint8_t);
  226. /* Submit transfer. */
  227. EDMA_SubmitTransfer(handle->rxEdmaHandle, &xferConfig);
  228. EDMA_StartTransfer(handle->rxEdmaHandle);
  229. /* Enable UART RX EDMA. */
  230. FLEXIO_UART_EnableRxDMA(base, true);
  231. status = kStatus_Success;
  232. }
  233. return status;
  234. }
  235. void FLEXIO_UART_TransferAbortSendEDMA(FLEXIO_UART_Type *base, flexio_uart_edma_handle_t *handle)
  236. {
  237. assert(handle->txEdmaHandle);
  238. /* Disable UART TX EDMA. */
  239. FLEXIO_UART_EnableTxDMA(base, false);
  240. /* Stop transfer. */
  241. EDMA_StopTransfer(handle->txEdmaHandle);
  242. handle->txState = kFLEXIO_UART_TxIdle;
  243. }
  244. void FLEXIO_UART_TransferAbortReceiveEDMA(FLEXIO_UART_Type *base, flexio_uart_edma_handle_t *handle)
  245. {
  246. assert(handle->rxEdmaHandle);
  247. /* Disable UART RX EDMA. */
  248. FLEXIO_UART_EnableRxDMA(base, false);
  249. /* Stop transfer. */
  250. EDMA_StopTransfer(handle->rxEdmaHandle);
  251. handle->rxState = kFLEXIO_UART_RxIdle;
  252. }
  253. status_t FLEXIO_UART_TransferGetReceiveCountEDMA(FLEXIO_UART_Type *base,
  254. flexio_uart_edma_handle_t *handle,
  255. size_t *count)
  256. {
  257. assert(handle);
  258. assert(handle->rxEdmaHandle);
  259. assert(count);
  260. if (kFLEXIO_UART_RxIdle == handle->rxState)
  261. {
  262. return kStatus_NoTransferInProgress;
  263. }
  264. *count = handle->rxDataSizeAll -
  265. (uint32_t)handle->nbytes *
  266. EDMA_GetRemainingMajorLoopCount(handle->rxEdmaHandle->base, handle->rxEdmaHandle->channel);
  267. return kStatus_Success;
  268. }
  269. status_t FLEXIO_UART_TransferGetSendCountEDMA(FLEXIO_UART_Type *base, flexio_uart_edma_handle_t *handle, size_t *count)
  270. {
  271. assert(handle);
  272. assert(handle->txEdmaHandle);
  273. assert(count);
  274. if (kFLEXIO_UART_TxIdle == handle->txState)
  275. {
  276. return kStatus_NoTransferInProgress;
  277. }
  278. *count = handle->txDataSizeAll -
  279. (uint32_t)handle->nbytes *
  280. EDMA_GetRemainingMajorLoopCount(handle->txEdmaHandle->base, handle->txEdmaHandle->channel);
  281. return kStatus_Success;
  282. }