fsl_lpuart_edma.c 12 KB

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