fsl_lpuart_edma.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_lpuart_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.lpuart_edma"
  41. #endif
  42. /*<! Structure definition for lpuart_edma_private_handle_t. The structure is private. */
  43. typedef struct _lpuart_edma_private_handle
  44. {
  45. LPUART_Type *base;
  46. lpuart_edma_handle_t *handle;
  47. } lpuart_edma_private_handle_t;
  48. /* LPUART EDMA transfer handle. */
  49. enum _lpuart_edma_tansfer_states
  50. {
  51. kLPUART_TxIdle, /* TX idle. */
  52. kLPUART_TxBusy, /* TX busy. */
  53. kLPUART_RxIdle, /* RX idle. */
  54. kLPUART_RxBusy /* RX busy. */
  55. };
  56. /*******************************************************************************
  57. * Variables
  58. ******************************************************************************/
  59. /* Array of LPUART handle. */
  60. #if (defined(LPUART8))
  61. #define LPUART_HANDLE_ARRAY_SIZE 9
  62. #else /* LPUART8 */
  63. #if (defined(LPUART7))
  64. #define LPUART_HANDLE_ARRAY_SIZE 8
  65. #else /* LPUART7 */
  66. #if (defined(LPUART6))
  67. #define LPUART_HANDLE_ARRAY_SIZE 7
  68. #else /* LPUART6 */
  69. #if (defined(LPUART5))
  70. #define LPUART_HANDLE_ARRAY_SIZE 6
  71. #else /* LPUART5 */
  72. #if (defined(LPUART4))
  73. #define LPUART_HANDLE_ARRAY_SIZE 5
  74. #else /* LPUART4 */
  75. #if (defined(LPUART3))
  76. #define LPUART_HANDLE_ARRAY_SIZE 4
  77. #else /* LPUART3 */
  78. #if (defined(LPUART2))
  79. #define LPUART_HANDLE_ARRAY_SIZE 3
  80. #else /* LPUART2 */
  81. #if (defined(LPUART1))
  82. #define LPUART_HANDLE_ARRAY_SIZE 2
  83. #else /* LPUART1 */
  84. #if (defined(LPUART0))
  85. #define LPUART_HANDLE_ARRAY_SIZE 1
  86. #else /* LPUART0 */
  87. #define LPUART_HANDLE_ARRAY_SIZE FSL_FEATURE_SOC_LPUART_COUNT
  88. #endif /* LPUART 0 */
  89. #endif /* LPUART 1 */
  90. #endif /* LPUART 2 */
  91. #endif /* LPUART 3 */
  92. #endif /* LPUART 4 */
  93. #endif /* LPUART 5 */
  94. #endif /* LPUART 6 */
  95. #endif /* LPUART 7 */
  96. #endif /* LPUART 8 */
  97. /*<! Private handle only used for internally. */
  98. static lpuart_edma_private_handle_t s_edmaPrivateHandle[LPUART_HANDLE_ARRAY_SIZE];
  99. /*******************************************************************************
  100. * Prototypes
  101. ******************************************************************************/
  102. /*!
  103. * @brief LPUART EDMA send finished callback function.
  104. *
  105. * This function is called when LPUART EDMA send finished. It disables the LPUART
  106. * TX EDMA request and sends @ref kStatus_LPUART_TxIdle to LPUART callback.
  107. *
  108. * @param handle The EDMA handle.
  109. * @param param Callback function parameter.
  110. */
  111. static void LPUART_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  112. /*!
  113. * @brief LPUART EDMA receive finished callback function.
  114. *
  115. * This function is called when LPUART EDMA receive finished. It disables the LPUART
  116. * RX EDMA request and sends @ref kStatus_LPUART_RxIdle to LPUART callback.
  117. *
  118. * @param handle The EDMA handle.
  119. * @param param Callback function parameter.
  120. */
  121. static void LPUART_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  122. /*******************************************************************************
  123. * Code
  124. ******************************************************************************/
  125. static void LPUART_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  126. {
  127. assert(param);
  128. lpuart_edma_private_handle_t *lpuartPrivateHandle = (lpuart_edma_private_handle_t *)param;
  129. /* Avoid the warning for unused variables. */
  130. handle = handle;
  131. tcds = tcds;
  132. if (transferDone)
  133. {
  134. LPUART_TransferAbortSendEDMA(lpuartPrivateHandle->base, lpuartPrivateHandle->handle);
  135. if (lpuartPrivateHandle->handle->callback)
  136. {
  137. lpuartPrivateHandle->handle->callback(lpuartPrivateHandle->base, lpuartPrivateHandle->handle,
  138. kStatus_LPUART_TxIdle, lpuartPrivateHandle->handle->userData);
  139. }
  140. }
  141. }
  142. static void LPUART_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  143. {
  144. assert(param);
  145. lpuart_edma_private_handle_t *lpuartPrivateHandle = (lpuart_edma_private_handle_t *)param;
  146. /* Avoid warning for unused parameters. */
  147. handle = handle;
  148. tcds = tcds;
  149. if (transferDone)
  150. {
  151. /* Disable transfer. */
  152. LPUART_TransferAbortReceiveEDMA(lpuartPrivateHandle->base, lpuartPrivateHandle->handle);
  153. if (lpuartPrivateHandle->handle->callback)
  154. {
  155. lpuartPrivateHandle->handle->callback(lpuartPrivateHandle->base, lpuartPrivateHandle->handle,
  156. kStatus_LPUART_RxIdle, lpuartPrivateHandle->handle->userData);
  157. }
  158. }
  159. }
  160. void LPUART_TransferCreateHandleEDMA(LPUART_Type *base,
  161. lpuart_edma_handle_t *handle,
  162. lpuart_edma_transfer_callback_t callback,
  163. void *userData,
  164. edma_handle_t *txEdmaHandle,
  165. edma_handle_t *rxEdmaHandle)
  166. {
  167. assert(handle);
  168. uint32_t instance = LPUART_GetInstance(base);
  169. s_edmaPrivateHandle[instance].base = base;
  170. s_edmaPrivateHandle[instance].handle = handle;
  171. memset(handle, 0, sizeof(*handle));
  172. handle->rxState = kLPUART_RxIdle;
  173. handle->txState = kLPUART_TxIdle;
  174. handle->rxEdmaHandle = rxEdmaHandle;
  175. handle->txEdmaHandle = txEdmaHandle;
  176. handle->callback = callback;
  177. handle->userData = userData;
  178. #if defined(FSL_FEATURE_LPUART_HAS_FIFO) && FSL_FEATURE_LPUART_HAS_FIFO
  179. /* Note:
  180. Take care of the RX FIFO, EDMA request only assert when received bytes
  181. equal or more than RX water mark, there is potential issue if RX water
  182. mark larger than 1.
  183. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
  184. 5 bytes are received. the last byte will be saved in FIFO but not trigger
  185. EDMA transfer because the water mark is 2.
  186. */
  187. if (rxEdmaHandle)
  188. {
  189. base->WATER &= (~LPUART_WATER_RXWATER_MASK);
  190. }
  191. #endif
  192. /* Configure TX. */
  193. if (txEdmaHandle)
  194. {
  195. EDMA_SetCallback(handle->txEdmaHandle, LPUART_SendEDMACallback, &s_edmaPrivateHandle[instance]);
  196. }
  197. /* Configure RX. */
  198. if (rxEdmaHandle)
  199. {
  200. EDMA_SetCallback(handle->rxEdmaHandle, LPUART_ReceiveEDMACallback, &s_edmaPrivateHandle[instance]);
  201. }
  202. }
  203. status_t LPUART_SendEDMA(LPUART_Type *base, lpuart_edma_handle_t *handle, lpuart_transfer_t *xfer)
  204. {
  205. assert(handle);
  206. assert(handle->txEdmaHandle);
  207. assert(xfer);
  208. assert(xfer->data);
  209. assert(xfer->dataSize);
  210. edma_transfer_config_t xferConfig;
  211. status_t status;
  212. /* If previous TX not finished. */
  213. if (kLPUART_TxBusy == handle->txState)
  214. {
  215. status = kStatus_LPUART_TxBusy;
  216. }
  217. else
  218. {
  219. handle->txState = kLPUART_TxBusy;
  220. handle->txDataSizeAll = xfer->dataSize;
  221. /* Prepare transfer. */
  222. EDMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint8_t), (void *)LPUART_GetDataRegisterAddress(base),
  223. sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_MemoryToPeripheral);
  224. /* Store the initially configured eDMA minor byte transfer count into the LPUART handle */
  225. handle->nbytes = sizeof(uint8_t);
  226. /* Submit transfer. */
  227. EDMA_SubmitTransfer(handle->txEdmaHandle, &xferConfig);
  228. EDMA_StartTransfer(handle->txEdmaHandle);
  229. /* Enable LPUART TX EDMA. */
  230. LPUART_EnableTxDMA(base, true);
  231. status = kStatus_Success;
  232. }
  233. return status;
  234. }
  235. status_t LPUART_ReceiveEDMA(LPUART_Type *base, lpuart_edma_handle_t *handle, lpuart_transfer_t *xfer)
  236. {
  237. assert(handle);
  238. assert(handle->rxEdmaHandle);
  239. assert(xfer);
  240. assert(xfer->data);
  241. assert(xfer->dataSize);
  242. edma_transfer_config_t xferConfig;
  243. status_t status;
  244. /* If previous RX not finished. */
  245. if (kLPUART_RxBusy == handle->rxState)
  246. {
  247. status = kStatus_LPUART_RxBusy;
  248. }
  249. else
  250. {
  251. handle->rxState = kLPUART_RxBusy;
  252. handle->rxDataSizeAll = xfer->dataSize;
  253. /* Prepare transfer. */
  254. EDMA_PrepareTransfer(&xferConfig, (void *)LPUART_GetDataRegisterAddress(base), sizeof(uint8_t), xfer->data,
  255. sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_PeripheralToMemory);
  256. /* Store the initially configured eDMA minor byte transfer count into the LPUART handle */
  257. handle->nbytes = sizeof(uint8_t);
  258. /* Submit transfer. */
  259. EDMA_SubmitTransfer(handle->rxEdmaHandle, &xferConfig);
  260. EDMA_StartTransfer(handle->rxEdmaHandle);
  261. /* Enable LPUART RX EDMA. */
  262. LPUART_EnableRxDMA(base, true);
  263. status = kStatus_Success;
  264. }
  265. return status;
  266. }
  267. void LPUART_TransferAbortSendEDMA(LPUART_Type *base, lpuart_edma_handle_t *handle)
  268. {
  269. assert(handle);
  270. assert(handle->txEdmaHandle);
  271. /* Disable LPUART TX EDMA. */
  272. LPUART_EnableTxDMA(base, false);
  273. /* Stop transfer. */
  274. EDMA_AbortTransfer(handle->txEdmaHandle);
  275. handle->txState = kLPUART_TxIdle;
  276. }
  277. void LPUART_TransferAbortReceiveEDMA(LPUART_Type *base, lpuart_edma_handle_t *handle)
  278. {
  279. assert(handle);
  280. assert(handle->rxEdmaHandle);
  281. /* Disable LPUART RX EDMA. */
  282. LPUART_EnableRxDMA(base, false);
  283. /* Stop transfer. */
  284. EDMA_AbortTransfer(handle->rxEdmaHandle);
  285. handle->rxState = kLPUART_RxIdle;
  286. }
  287. status_t LPUART_TransferGetReceiveCountEDMA(LPUART_Type *base, lpuart_edma_handle_t *handle, uint32_t *count)
  288. {
  289. assert(handle);
  290. assert(handle->rxEdmaHandle);
  291. assert(count);
  292. if (kLPUART_RxIdle == handle->rxState)
  293. {
  294. return kStatus_NoTransferInProgress;
  295. }
  296. *count = handle->rxDataSizeAll -
  297. (uint32_t)handle->nbytes *
  298. EDMA_GetRemainingMajorLoopCount(handle->rxEdmaHandle->base, handle->rxEdmaHandle->channel);
  299. return kStatus_Success;
  300. }
  301. status_t LPUART_TransferGetSendCountEDMA(LPUART_Type *base, lpuart_edma_handle_t *handle, uint32_t *count)
  302. {
  303. assert(handle);
  304. assert(handle->txEdmaHandle);
  305. assert(count);
  306. if (kLPUART_TxIdle == handle->txState)
  307. {
  308. return kStatus_NoTransferInProgress;
  309. }
  310. *count = handle->txDataSizeAll -
  311. (uint32_t)handle->nbytes *
  312. EDMA_GetRemainingMajorLoopCount(handle->txEdmaHandle->base, handle->txEdmaHandle->channel);
  313. return kStatus_Success;
  314. }