fsl_flexio_uart_edma.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_flexio_uart_edma.h"
  9. /*******************************************************************************
  10. * Definitions
  11. ******************************************************************************/
  12. /* Component ID definition, used by tools. */
  13. #ifndef FSL_COMPONENT_ID
  14. #define FSL_COMPONENT_ID "platform.drivers.flexio_uart_edma"
  15. #endif
  16. /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
  17. typedef struct _flexio_uart_edma_private_handle
  18. {
  19. FLEXIO_UART_Type *base;
  20. flexio_uart_edma_handle_t *handle;
  21. } flexio_uart_edma_private_handle_t;
  22. /* UART EDMA transfer handle. */
  23. enum _flexio_uart_edma_tansfer_states
  24. {
  25. kFLEXIO_UART_TxIdle, /* TX idle. */
  26. kFLEXIO_UART_TxBusy, /* TX busy. */
  27. kFLEXIO_UART_RxIdle, /* RX idle. */
  28. kFLEXIO_UART_RxBusy /* RX busy. */
  29. };
  30. /*******************************************************************************
  31. * Variables
  32. ******************************************************************************/
  33. /*< @brief user configurable flexio uart handle count. */
  34. #define FLEXIO_UART_HANDLE_COUNT 2
  35. /*<! Private handle only used for internally. */
  36. static flexio_uart_edma_private_handle_t s_edmaPrivateHandle[FLEXIO_UART_HANDLE_COUNT];
  37. /*******************************************************************************
  38. * Prototypes
  39. ******************************************************************************/
  40. /*!
  41. * @brief FLEXIO UART EDMA send finished callback function.
  42. *
  43. * This function is called when FLEXIO UART EDMA send finished. It disables the UART
  44. * TX EDMA request and sends @ref kStatus_FLEXIO_UART_TxIdle to FLEXIO UART callback.
  45. *
  46. * @param handle The EDMA handle.
  47. * @param param Callback function parameter.
  48. */
  49. static void FLEXIO_UART_TransferSendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  50. /*!
  51. * @brief FLEXIO UART EDMA receive finished callback function.
  52. *
  53. * This function is called when FLEXIO UART EDMA receive finished. It disables the UART
  54. * RX EDMA request and sends @ref kStatus_FLEXIO_UART_RxIdle to UART callback.
  55. *
  56. * @param handle The EDMA handle.
  57. * @param param Callback function parameter.
  58. */
  59. static void FLEXIO_UART_TransferReceiveEDMACallback(edma_handle_t *handle,
  60. void *param,
  61. bool transferDone,
  62. uint32_t tcds);
  63. /*******************************************************************************
  64. * Code
  65. ******************************************************************************/
  66. static void FLEXIO_UART_TransferSendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  67. {
  68. flexio_uart_edma_private_handle_t *uartPrivateHandle = (flexio_uart_edma_private_handle_t *)param;
  69. assert(uartPrivateHandle->handle != NULL);
  70. /* Avoid the warning for unused variables. */
  71. handle = handle;
  72. tcds = tcds;
  73. if (transferDone)
  74. {
  75. FLEXIO_UART_TransferAbortSendEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  76. if (uartPrivateHandle->handle->callback != NULL)
  77. {
  78. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle,
  79. kStatus_FLEXIO_UART_TxIdle, uartPrivateHandle->handle->userData);
  80. }
  81. }
  82. }
  83. static void FLEXIO_UART_TransferReceiveEDMACallback(edma_handle_t *handle,
  84. void *param,
  85. bool transferDone,
  86. uint32_t tcds)
  87. {
  88. flexio_uart_edma_private_handle_t *uartPrivateHandle = (flexio_uart_edma_private_handle_t *)param;
  89. assert(uartPrivateHandle->handle != NULL);
  90. /* Avoid the warning for unused variables. */
  91. handle = handle;
  92. tcds = tcds;
  93. if (transferDone)
  94. {
  95. /* Disable transfer. */
  96. FLEXIO_UART_TransferAbortReceiveEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  97. if (uartPrivateHandle->handle->callback != NULL)
  98. {
  99. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle,
  100. kStatus_FLEXIO_UART_RxIdle, uartPrivateHandle->handle->userData);
  101. }
  102. }
  103. }
  104. /*!
  105. * brief Initializes the UART handle which is used in transactional functions.
  106. *
  107. * param base Pointer to FLEXIO_UART_Type.
  108. * param handle Pointer to flexio_uart_edma_handle_t structure.
  109. * param callback The callback function.
  110. * param userData The parameter of the callback function.
  111. * param rxEdmaHandle User requested DMA handle for RX DMA transfer.
  112. * param txEdmaHandle User requested DMA handle for TX DMA transfer.
  113. * retval kStatus_Success Successfully create the handle.
  114. * retval kStatus_OutOfRange The FlexIO SPI eDMA type/handle table out of range.
  115. */
  116. status_t FLEXIO_UART_TransferCreateHandleEDMA(FLEXIO_UART_Type *base,
  117. flexio_uart_edma_handle_t *handle,
  118. flexio_uart_edma_transfer_callback_t callback,
  119. void *userData,
  120. edma_handle_t *txEdmaHandle,
  121. edma_handle_t *rxEdmaHandle)
  122. {
  123. assert(handle != NULL);
  124. uint8_t index = 0U;
  125. /* Find the an empty handle pointer to store the handle. */
  126. for (index = 0U; index < (uint8_t)FLEXIO_UART_HANDLE_COUNT; index++)
  127. {
  128. if (s_edmaPrivateHandle[index].base == NULL)
  129. {
  130. s_edmaPrivateHandle[index].base = base;
  131. s_edmaPrivateHandle[index].handle = handle;
  132. break;
  133. }
  134. }
  135. if (index == (uint8_t)FLEXIO_UART_HANDLE_COUNT)
  136. {
  137. return kStatus_OutOfRange;
  138. }
  139. (void)memset(handle, 0, sizeof(*handle));
  140. handle->rxState = (uint8_t)kFLEXIO_UART_RxIdle;
  141. handle->txState = (uint8_t)kFLEXIO_UART_TxIdle;
  142. handle->rxEdmaHandle = rxEdmaHandle;
  143. handle->txEdmaHandle = txEdmaHandle;
  144. handle->callback = callback;
  145. handle->userData = userData;
  146. /* Configure TX. */
  147. if (txEdmaHandle != NULL)
  148. {
  149. EDMA_SetCallback(handle->txEdmaHandle, FLEXIO_UART_TransferSendEDMACallback, &s_edmaPrivateHandle);
  150. }
  151. /* Configure RX. */
  152. if (rxEdmaHandle != NULL)
  153. {
  154. EDMA_SetCallback(handle->rxEdmaHandle, FLEXIO_UART_TransferReceiveEDMACallback, &s_edmaPrivateHandle);
  155. }
  156. return kStatus_Success;
  157. }
  158. /*!
  159. * brief Sends data using eDMA.
  160. *
  161. * This function sends data using eDMA. This is a non-blocking function, which returns
  162. * right away. When all data is sent out, the send callback function is called.
  163. *
  164. * param base Pointer to FLEXIO_UART_Type
  165. * param handle UART handle pointer.
  166. * param xfer UART eDMA transfer structure, see #flexio_uart_transfer_t.
  167. * retval kStatus_Success if succeed, others failed.
  168. * retval kStatus_FLEXIO_UART_TxBusy Previous transfer on going.
  169. */
  170. status_t FLEXIO_UART_TransferSendEDMA(FLEXIO_UART_Type *base,
  171. flexio_uart_edma_handle_t *handle,
  172. flexio_uart_transfer_t *xfer)
  173. {
  174. assert(handle->txEdmaHandle != NULL);
  175. edma_transfer_config_t xferConfig;
  176. status_t status;
  177. /* Return error if xfer invalid. */
  178. if ((0U == xfer->dataSize) || (NULL == xfer->data))
  179. {
  180. return kStatus_InvalidArgument;
  181. }
  182. /* If previous TX not finished. */
  183. if ((uint8_t)kFLEXIO_UART_TxBusy == handle->txState)
  184. {
  185. status = kStatus_FLEXIO_UART_TxBusy;
  186. }
  187. else
  188. {
  189. handle->txState = (uint8_t)kFLEXIO_UART_TxBusy;
  190. handle->txDataSizeAll = xfer->dataSize;
  191. /* Prepare transfer. */
  192. EDMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint8_t),
  193. (uint32_t *)FLEXIO_UART_GetTxDataRegisterAddress(base), sizeof(uint8_t), sizeof(uint8_t),
  194. xfer->dataSize, kEDMA_MemoryToPeripheral);
  195. /* Store the initially configured eDMA minor byte transfer count into the FLEXIO UART handle */
  196. handle->nbytes = 1U;
  197. /* Submit transfer. */
  198. (void)EDMA_SubmitTransfer(handle->txEdmaHandle, &xferConfig);
  199. EDMA_StartTransfer(handle->txEdmaHandle);
  200. /* Enable UART TX EDMA. */
  201. FLEXIO_UART_EnableTxDMA(base, true);
  202. status = kStatus_Success;
  203. }
  204. return status;
  205. }
  206. /*!
  207. * brief Receives data using eDMA.
  208. *
  209. * This function receives data using eDMA. This is a non-blocking function, which returns
  210. * right away. When all data is received, the receive callback function is called.
  211. *
  212. * param base Pointer to FLEXIO_UART_Type
  213. * param handle Pointer to flexio_uart_edma_handle_t structure
  214. * param xfer UART eDMA transfer structure, see #flexio_uart_transfer_t.
  215. * retval kStatus_Success if succeed, others failed.
  216. * retval kStatus_UART_RxBusy Previous transfer on going.
  217. */
  218. status_t FLEXIO_UART_TransferReceiveEDMA(FLEXIO_UART_Type *base,
  219. flexio_uart_edma_handle_t *handle,
  220. flexio_uart_transfer_t *xfer)
  221. {
  222. assert(handle->rxEdmaHandle != NULL);
  223. edma_transfer_config_t xferConfig;
  224. status_t status;
  225. /* Return error if xfer invalid. */
  226. if ((0U == xfer->dataSize) || (NULL == xfer->data))
  227. {
  228. return kStatus_InvalidArgument;
  229. }
  230. /* If previous RX not finished. */
  231. if ((uint8_t)kFLEXIO_UART_RxBusy == handle->rxState)
  232. {
  233. status = kStatus_FLEXIO_UART_RxBusy;
  234. }
  235. else
  236. {
  237. handle->rxState = (uint8_t)kFLEXIO_UART_RxBusy;
  238. handle->rxDataSizeAll = xfer->dataSize;
  239. /* Prepare transfer. */
  240. EDMA_PrepareTransfer(&xferConfig, (uint32_t *)FLEXIO_UART_GetRxDataRegisterAddress(base), sizeof(uint8_t),
  241. xfer->data, sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_PeripheralToMemory);
  242. /* Store the initially configured eDMA minor byte transfer count into the FLEXIO UART handle */
  243. handle->nbytes = (uint8_t)sizeof(uint8_t);
  244. /* Submit transfer. */
  245. (void)EDMA_SubmitTransfer(handle->rxEdmaHandle, &xferConfig);
  246. EDMA_StartTransfer(handle->rxEdmaHandle);
  247. /* Enable UART RX EDMA. */
  248. FLEXIO_UART_EnableRxDMA(base, true);
  249. status = kStatus_Success;
  250. }
  251. return status;
  252. }
  253. /*!
  254. * brief Aborts the sent data which using eDMA.
  255. *
  256. * This function aborts sent data which using eDMA.
  257. *
  258. * param base Pointer to FLEXIO_UART_Type
  259. * param handle Pointer to flexio_uart_edma_handle_t structure
  260. */
  261. void FLEXIO_UART_TransferAbortSendEDMA(FLEXIO_UART_Type *base, flexio_uart_edma_handle_t *handle)
  262. {
  263. assert(handle->txEdmaHandle != NULL);
  264. /* Disable UART TX EDMA. */
  265. FLEXIO_UART_EnableTxDMA(base, false);
  266. /* Stop transfer. */
  267. EDMA_StopTransfer(handle->txEdmaHandle);
  268. handle->txState = (uint8_t)kFLEXIO_UART_TxIdle;
  269. }
  270. /*!
  271. * brief Aborts the receive data which using eDMA.
  272. *
  273. * This function aborts the receive data which using eDMA.
  274. *
  275. * param base Pointer to FLEXIO_UART_Type
  276. * param handle Pointer to flexio_uart_edma_handle_t structure
  277. */
  278. void FLEXIO_UART_TransferAbortReceiveEDMA(FLEXIO_UART_Type *base, flexio_uart_edma_handle_t *handle)
  279. {
  280. assert(handle->rxEdmaHandle != NULL);
  281. /* Disable UART RX EDMA. */
  282. FLEXIO_UART_EnableRxDMA(base, false);
  283. /* Stop transfer. */
  284. EDMA_StopTransfer(handle->rxEdmaHandle);
  285. handle->rxState = (uint8_t)kFLEXIO_UART_RxIdle;
  286. }
  287. /*!
  288. * brief Gets the number of bytes received.
  289. *
  290. * This function gets the number of bytes received.
  291. *
  292. * param base Pointer to FLEXIO_UART_Type
  293. * param handle Pointer to flexio_uart_edma_handle_t structure
  294. * param count Number of bytes received so far by the non-blocking transaction.
  295. * retval kStatus_NoTransferInProgress transfer has finished or no transfer in progress.
  296. * retval kStatus_Success Successfully return the count.
  297. */
  298. status_t FLEXIO_UART_TransferGetReceiveCountEDMA(FLEXIO_UART_Type *base,
  299. flexio_uart_edma_handle_t *handle,
  300. size_t *count)
  301. {
  302. assert(handle != NULL);
  303. assert(handle->rxEdmaHandle != NULL);
  304. assert(count != NULL);
  305. if ((uint8_t)kFLEXIO_UART_RxIdle == handle->rxState)
  306. {
  307. return kStatus_NoTransferInProgress;
  308. }
  309. *count = handle->rxDataSizeAll -
  310. (uint32_t)handle->nbytes *
  311. EDMA_GetRemainingMajorLoopCount(handle->rxEdmaHandle->base, handle->rxEdmaHandle->channel);
  312. return kStatus_Success;
  313. }
  314. /*!
  315. * brief Gets the number of bytes sent out.
  316. *
  317. * This function gets the number of bytes sent out.
  318. *
  319. * param base Pointer to FLEXIO_UART_Type
  320. * param handle Pointer to flexio_uart_edma_handle_t structure
  321. * param count Number of bytes sent so far by the non-blocking transaction.
  322. * retval kStatus_NoTransferInProgress transfer has finished or no transfer in progress.
  323. * retval kStatus_Success Successfully return the count.
  324. */
  325. status_t FLEXIO_UART_TransferGetSendCountEDMA(FLEXIO_UART_Type *base, flexio_uart_edma_handle_t *handle, size_t *count)
  326. {
  327. assert(handle != NULL);
  328. assert(handle->txEdmaHandle != NULL);
  329. assert(count != NULL);
  330. if ((uint8_t)kFLEXIO_UART_TxIdle == handle->txState)
  331. {
  332. return kStatus_NoTransferInProgress;
  333. }
  334. *count = handle->txDataSizeAll -
  335. (uint32_t)handle->nbytes *
  336. EDMA_GetRemainingMajorLoopCount(handle->txEdmaHandle->base, handle->txEdmaHandle->channel);
  337. return kStatus_Success;
  338. }