fsl_uart_edma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #include "fsl_uart_edma.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Array of UART handle. */
  39. #if (defined(UART5))
  40. #define UART_HANDLE_ARRAY_SIZE 6
  41. #else /* UART5 */
  42. #if (defined(UART4))
  43. #define UART_HANDLE_ARRAY_SIZE 5
  44. #else /* UART4 */
  45. #if (defined(UART3))
  46. #define UART_HANDLE_ARRAY_SIZE 4
  47. #else /* UART3 */
  48. #if (defined(UART2))
  49. #define UART_HANDLE_ARRAY_SIZE 3
  50. #else /* UART2 */
  51. #if (defined(UART1))
  52. #define UART_HANDLE_ARRAY_SIZE 2
  53. #else /* UART1 */
  54. #if (defined(UART0))
  55. #define UART_HANDLE_ARRAY_SIZE 1
  56. #else /* UART0 */
  57. #error No UART instance.
  58. #endif /* UART 0 */
  59. #endif /* UART 1 */
  60. #endif /* UART 2 */
  61. #endif /* UART 3 */
  62. #endif /* UART 4 */
  63. #endif /* UART 5 */
  64. /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
  65. typedef struct _uart_edma_private_handle
  66. {
  67. UART_Type *base;
  68. uart_edma_handle_t *handle;
  69. } uart_edma_private_handle_t;
  70. /* UART EDMA transfer handle. */
  71. enum _uart_edma_tansfer_states
  72. {
  73. kUART_TxIdle, /* TX idle. */
  74. kUART_TxBusy, /* TX busy. */
  75. kUART_RxIdle, /* RX idle. */
  76. kUART_RxBusy /* RX busy. */
  77. };
  78. /*******************************************************************************
  79. * Definitions
  80. ******************************************************************************/
  81. /*<! Private handle only used for internally. */
  82. static uart_edma_private_handle_t s_edmaPrivateHandle[UART_HANDLE_ARRAY_SIZE];
  83. /*******************************************************************************
  84. * Prototypes
  85. ******************************************************************************/
  86. /*!
  87. * @brief UART EDMA send finished callback function.
  88. *
  89. * This function is called when UART EDMA send finished. It disables the UART
  90. * TX EDMA request and sends @ref kStatus_UART_TxIdle to UART callback.
  91. *
  92. * @param handle The EDMA handle.
  93. * @param param Callback function parameter.
  94. */
  95. static void UART_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  96. /*!
  97. * @brief UART EDMA receive finished callback function.
  98. *
  99. * This function is called when UART EDMA receive finished. It disables the UART
  100. * RX EDMA request and sends @ref kStatus_UART_RxIdle to UART callback.
  101. *
  102. * @param handle The EDMA handle.
  103. * @param param Callback function parameter.
  104. */
  105. static void UART_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  106. /*!
  107. * @brief Get the UART instance from peripheral base address.
  108. *
  109. * @param base UART peripheral base address.
  110. * @return UART instance.
  111. */
  112. extern uint32_t UART_GetInstance(UART_Type *base);
  113. /*******************************************************************************
  114. * Code
  115. ******************************************************************************/
  116. static void UART_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  117. {
  118. assert(param);
  119. uart_edma_private_handle_t *uartPrivateHandle = (uart_edma_private_handle_t *)param;
  120. /* Avoid the warning for unused variables. */
  121. handle = handle;
  122. tcds = tcds;
  123. if (transferDone)
  124. {
  125. UART_TransferAbortSendEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  126. if (uartPrivateHandle->handle->callback)
  127. {
  128. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle, kStatus_UART_TxIdle,
  129. uartPrivateHandle->handle->userData);
  130. }
  131. }
  132. }
  133. static void UART_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  134. {
  135. assert(param);
  136. uart_edma_private_handle_t *uartPrivateHandle = (uart_edma_private_handle_t *)param;
  137. /* Avoid warning for unused parameters. */
  138. handle = handle;
  139. tcds = tcds;
  140. if (transferDone)
  141. {
  142. /* Disable transfer. */
  143. UART_TransferAbortReceiveEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  144. if (uartPrivateHandle->handle->callback)
  145. {
  146. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle, kStatus_UART_RxIdle,
  147. uartPrivateHandle->handle->userData);
  148. }
  149. }
  150. }
  151. void UART_TransferCreateHandleEDMA(UART_Type *base,
  152. uart_edma_handle_t *handle,
  153. uart_edma_transfer_callback_t callback,
  154. void *userData,
  155. edma_handle_t *txEdmaHandle,
  156. edma_handle_t *rxEdmaHandle)
  157. {
  158. assert(handle);
  159. uint32_t instance = UART_GetInstance(base);
  160. s_edmaPrivateHandle[instance].base = base;
  161. s_edmaPrivateHandle[instance].handle = handle;
  162. memset(handle, 0, sizeof(*handle));
  163. handle->rxState = kUART_RxIdle;
  164. handle->txState = kUART_TxIdle;
  165. handle->rxEdmaHandle = rxEdmaHandle;
  166. handle->txEdmaHandle = txEdmaHandle;
  167. handle->callback = callback;
  168. handle->userData = userData;
  169. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  170. /* Note:
  171. Take care of the RX FIFO, EDMA request only assert when received bytes
  172. equal or more than RX water mark, there is potential issue if RX water
  173. mark larger than 1.
  174. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
  175. 5 bytes are received. the last byte will be saved in FIFO but not trigger
  176. EDMA transfer because the water mark is 2.
  177. */
  178. if (rxEdmaHandle)
  179. {
  180. base->RWFIFO = 1U;
  181. }
  182. #endif
  183. /* Configure TX. */
  184. if (txEdmaHandle)
  185. {
  186. EDMA_SetCallback(handle->txEdmaHandle, UART_SendEDMACallback, &s_edmaPrivateHandle[instance]);
  187. }
  188. /* Configure RX. */
  189. if (rxEdmaHandle)
  190. {
  191. EDMA_SetCallback(handle->rxEdmaHandle, UART_ReceiveEDMACallback, &s_edmaPrivateHandle[instance]);
  192. }
  193. }
  194. status_t UART_SendEDMA(UART_Type *base, uart_edma_handle_t *handle, uart_transfer_t *xfer)
  195. {
  196. assert(handle);
  197. assert(handle->txEdmaHandle);
  198. assert(xfer);
  199. assert(xfer->data);
  200. assert(xfer->dataSize);
  201. edma_transfer_config_t xferConfig;
  202. status_t status;
  203. /* If previous TX not finished. */
  204. if (kUART_TxBusy == handle->txState)
  205. {
  206. status = kStatus_UART_TxBusy;
  207. }
  208. else
  209. {
  210. handle->txState = kUART_TxBusy;
  211. handle->txDataSizeAll = xfer->dataSize;
  212. /* Prepare transfer. */
  213. EDMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint8_t), (void *)UART_GetDataRegisterAddress(base),
  214. sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_MemoryToPeripheral);
  215. /* Store the initially configured eDMA minor byte transfer count into the UART handle */
  216. handle->nbytes = sizeof(uint8_t);
  217. /* Submit transfer. */
  218. EDMA_SubmitTransfer(handle->txEdmaHandle, &xferConfig);
  219. EDMA_StartTransfer(handle->txEdmaHandle);
  220. /* Enable UART TX EDMA. */
  221. UART_EnableTxDMA(base, true);
  222. status = kStatus_Success;
  223. }
  224. return status;
  225. }
  226. status_t UART_ReceiveEDMA(UART_Type *base, uart_edma_handle_t *handle, uart_transfer_t *xfer)
  227. {
  228. assert(handle);
  229. assert(handle->rxEdmaHandle);
  230. assert(xfer);
  231. assert(xfer->data);
  232. assert(xfer->dataSize);
  233. edma_transfer_config_t xferConfig;
  234. status_t status;
  235. /* If previous RX not finished. */
  236. if (kUART_RxBusy == handle->rxState)
  237. {
  238. status = kStatus_UART_RxBusy;
  239. }
  240. else
  241. {
  242. handle->rxState = kUART_RxBusy;
  243. handle->rxDataSizeAll = xfer->dataSize;
  244. /* Prepare transfer. */
  245. EDMA_PrepareTransfer(&xferConfig, (void *)UART_GetDataRegisterAddress(base), sizeof(uint8_t), xfer->data,
  246. sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_PeripheralToMemory);
  247. /* Store the initially configured eDMA minor byte transfer count into the UART handle */
  248. handle->nbytes = sizeof(uint8_t);
  249. /* Submit transfer. */
  250. EDMA_SubmitTransfer(handle->rxEdmaHandle, &xferConfig);
  251. EDMA_StartTransfer(handle->rxEdmaHandle);
  252. /* Enable UART RX EDMA. */
  253. UART_EnableRxDMA(base, true);
  254. status = kStatus_Success;
  255. }
  256. return status;
  257. }
  258. void UART_TransferAbortSendEDMA(UART_Type *base, uart_edma_handle_t *handle)
  259. {
  260. assert(handle);
  261. assert(handle->txEdmaHandle);
  262. /* Disable UART TX EDMA. */
  263. UART_EnableTxDMA(base, false);
  264. /* Stop transfer. */
  265. EDMA_AbortTransfer(handle->txEdmaHandle);
  266. handle->txState = kUART_TxIdle;
  267. }
  268. void UART_TransferAbortReceiveEDMA(UART_Type *base, uart_edma_handle_t *handle)
  269. {
  270. assert(handle);
  271. assert(handle->rxEdmaHandle);
  272. /* Disable UART RX EDMA. */
  273. UART_EnableRxDMA(base, false);
  274. /* Stop transfer. */
  275. EDMA_AbortTransfer(handle->rxEdmaHandle);
  276. handle->rxState = kUART_RxIdle;
  277. }
  278. status_t UART_TransferGetReceiveCountEDMA(UART_Type *base, uart_edma_handle_t *handle, uint32_t *count)
  279. {
  280. assert(handle);
  281. assert(handle->rxEdmaHandle);
  282. assert(count);
  283. if (kUART_RxIdle == handle->rxState)
  284. {
  285. return kStatus_NoTransferInProgress;
  286. }
  287. *count = handle->rxDataSizeAll -
  288. (uint32_t)handle->nbytes *
  289. EDMA_GetRemainingMajorLoopCount(handle->rxEdmaHandle->base, handle->rxEdmaHandle->channel);
  290. return kStatus_Success;
  291. }
  292. status_t UART_TransferGetSendCountEDMA(UART_Type *base, uart_edma_handle_t *handle, uint32_t *count)
  293. {
  294. assert(handle);
  295. assert(handle->txEdmaHandle);
  296. assert(count);
  297. if (kUART_TxIdle == handle->txState)
  298. {
  299. return kStatus_NoTransferInProgress;
  300. }
  301. *count = handle->txDataSizeAll -
  302. (uint32_t)handle->nbytes *
  303. EDMA_GetRemainingMajorLoopCount(handle->txEdmaHandle->base, handle->txEdmaHandle->channel);
  304. return kStatus_Success;
  305. }