fsl_flexio_uart_edma.c 12 KB

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