You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

424 rivejä
13 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2019 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_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.uart_edma"
  15. #endif
  16. /* Array of UART handle. */
  17. #if (defined(UART5))
  18. #define UART_HANDLE_ARRAY_SIZE 6
  19. #else /* UART5 */
  20. #if (defined(UART4))
  21. #define UART_HANDLE_ARRAY_SIZE 5
  22. #else /* UART4 */
  23. #if (defined(UART3))
  24. #define UART_HANDLE_ARRAY_SIZE 4
  25. #else /* UART3 */
  26. #if (defined(UART2))
  27. #define UART_HANDLE_ARRAY_SIZE 3
  28. #else /* UART2 */
  29. #if (defined(UART1))
  30. #define UART_HANDLE_ARRAY_SIZE 2
  31. #else /* UART1 */
  32. #if (defined(UART0))
  33. #define UART_HANDLE_ARRAY_SIZE 1
  34. #else /* UART0 */
  35. #error No UART instance.
  36. #endif /* UART 0 */
  37. #endif /* UART 1 */
  38. #endif /* UART 2 */
  39. #endif /* UART 3 */
  40. #endif /* UART 4 */
  41. #endif /* UART 5 */
  42. /*<! Structure definition for uart_edma_private_handle_t. The structure is private. */
  43. typedef struct _uart_edma_private_handle
  44. {
  45. UART_Type *base;
  46. uart_edma_handle_t *handle;
  47. } uart_edma_private_handle_t;
  48. /* UART EDMA transfer handle. */
  49. enum
  50. {
  51. kUART_TxIdle, /* TX idle. */
  52. kUART_TxBusy, /* TX busy. */
  53. kUART_RxIdle, /* RX idle. */
  54. kUART_RxBusy /* RX busy. */
  55. };
  56. /*******************************************************************************
  57. * Variables
  58. ******************************************************************************/
  59. /*<! Private handle only used for internally. */
  60. static uart_edma_private_handle_t s_edmaPrivateHandle[UART_HANDLE_ARRAY_SIZE];
  61. /*******************************************************************************
  62. * Prototypes
  63. ******************************************************************************/
  64. /*!
  65. * @brief UART EDMA send finished callback function.
  66. *
  67. * This function is called when UART EDMA send finished. It disables the UART
  68. * TX EDMA request and sends @ref kStatus_UART_TxIdle to UART callback.
  69. *
  70. * @param handle The EDMA handle.
  71. * @param param Callback function parameter.
  72. */
  73. static void UART_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  74. /*!
  75. * @brief UART EDMA receive finished callback function.
  76. *
  77. * This function is called when UART EDMA receive finished. It disables the UART
  78. * RX EDMA request and sends @ref kStatus_UART_RxIdle to UART callback.
  79. *
  80. * @param handle The EDMA handle.
  81. * @param param Callback function parameter.
  82. */
  83. static void UART_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds);
  84. /*******************************************************************************
  85. * Code
  86. ******************************************************************************/
  87. static void UART_SendEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  88. {
  89. assert(param);
  90. uart_edma_private_handle_t *uartPrivateHandle = (uart_edma_private_handle_t *)param;
  91. /* Avoid the warning for unused variables. */
  92. handle = handle;
  93. tcds = tcds;
  94. if (transferDone)
  95. {
  96. UART_TransferAbortSendEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  97. /* Ensure all the data in the transmit buffer are sent out to bus. */
  98. while (0U == (uartPrivateHandle->base->S1 & UART_S1_TC_MASK))
  99. {
  100. }
  101. if (uartPrivateHandle->handle->callback != NULL)
  102. {
  103. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle, kStatus_UART_TxIdle,
  104. uartPrivateHandle->handle->userData);
  105. }
  106. }
  107. }
  108. static void UART_ReceiveEDMACallback(edma_handle_t *handle, void *param, bool transferDone, uint32_t tcds)
  109. {
  110. assert(param);
  111. uart_edma_private_handle_t *uartPrivateHandle = (uart_edma_private_handle_t *)param;
  112. /* Avoid warning for unused parameters. */
  113. handle = handle;
  114. tcds = tcds;
  115. if (transferDone)
  116. {
  117. /* Disable transfer. */
  118. UART_TransferAbortReceiveEDMA(uartPrivateHandle->base, uartPrivateHandle->handle);
  119. if (uartPrivateHandle->handle->callback != NULL)
  120. {
  121. uartPrivateHandle->handle->callback(uartPrivateHandle->base, uartPrivateHandle->handle, kStatus_UART_RxIdle,
  122. uartPrivateHandle->handle->userData);
  123. }
  124. }
  125. }
  126. /*!
  127. * brief Initializes the UART handle which is used in transactional functions.
  128. * param base UART peripheral base address.
  129. * param handle Pointer to the uart_edma_handle_t structure.
  130. * param callback UART callback, NULL means no callback.
  131. * param userData User callback function data.
  132. * param rxEdmaHandle User-requested DMA handle for RX DMA transfer.
  133. * param txEdmaHandle User-requested DMA handle for TX DMA transfer.
  134. */
  135. void UART_TransferCreateHandleEDMA(UART_Type *base,
  136. uart_edma_handle_t *handle,
  137. uart_edma_transfer_callback_t callback,
  138. void *userData,
  139. edma_handle_t *txEdmaHandle,
  140. edma_handle_t *rxEdmaHandle)
  141. {
  142. assert(handle);
  143. uint32_t instance = UART_GetInstance(base);
  144. s_edmaPrivateHandle[instance].base = base;
  145. s_edmaPrivateHandle[instance].handle = handle;
  146. (void)memset(handle, 0, sizeof(*handle));
  147. handle->rxState = (uint8_t)kUART_RxIdle;
  148. handle->txState = (uint8_t)kUART_TxIdle;
  149. handle->rxEdmaHandle = rxEdmaHandle;
  150. handle->txEdmaHandle = txEdmaHandle;
  151. handle->callback = callback;
  152. handle->userData = userData;
  153. #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO
  154. /* Note:
  155. Take care of the RX FIFO, EDMA request only assert when received bytes
  156. equal or more than RX water mark, there is potential issue if RX water
  157. mark larger than 1.
  158. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and
  159. 5 bytes are received. the last byte will be saved in FIFO but not trigger
  160. EDMA transfer because the water mark is 2.
  161. */
  162. if (rxEdmaHandle != NULL)
  163. {
  164. base->RWFIFO = 1U;
  165. }
  166. #endif
  167. /* Configure TX. */
  168. if (txEdmaHandle != NULL)
  169. {
  170. EDMA_SetCallback(handle->txEdmaHandle, UART_SendEDMACallback, &s_edmaPrivateHandle[instance]);
  171. }
  172. /* Configure RX. */
  173. if (rxEdmaHandle != NULL)
  174. {
  175. EDMA_SetCallback(handle->rxEdmaHandle, UART_ReceiveEDMACallback, &s_edmaPrivateHandle[instance]);
  176. }
  177. }
  178. /*!
  179. * brief Sends data using eDMA.
  180. *
  181. * This function sends data using eDMA. This is a non-blocking function, which returns
  182. * right away. When all data is sent, the send callback function is called.
  183. *
  184. * param base UART peripheral base address.
  185. * param handle UART handle pointer.
  186. * param xfer UART eDMA transfer structure. See #uart_transfer_t.
  187. * retval kStatus_Success if succeeded; otherwise failed.
  188. * retval kStatus_UART_TxBusy Previous transfer ongoing.
  189. * retval kStatus_InvalidArgument Invalid argument.
  190. */
  191. status_t UART_SendEDMA(UART_Type *base, uart_edma_handle_t *handle, uart_transfer_t *xfer)
  192. {
  193. assert(handle);
  194. assert(handle->txEdmaHandle);
  195. assert(xfer);
  196. assert(xfer->data);
  197. assert(xfer->dataSize);
  198. edma_transfer_config_t xferConfig;
  199. status_t status;
  200. /* If previous TX not finished. */
  201. if ((uint8_t)kUART_TxBusy == handle->txState)
  202. {
  203. status = kStatus_UART_TxBusy;
  204. }
  205. else
  206. {
  207. handle->txState = (uint8_t)kUART_TxBusy;
  208. handle->txDataSizeAll = xfer->dataSize;
  209. /* Prepare transfer. */
  210. EDMA_PrepareTransfer(&xferConfig, xfer->data, sizeof(uint8_t), (uint32_t *)UART_GetDataRegisterAddress(base),
  211. sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_MemoryToPeripheral);
  212. /* Store the initially configured eDMA minor byte transfer count into the UART handle */
  213. handle->nbytes = sizeof(uint8_t);
  214. /* Submit transfer. */
  215. (void)EDMA_SubmitTransfer(handle->txEdmaHandle, &xferConfig);
  216. EDMA_StartTransfer(handle->txEdmaHandle);
  217. /* Enable UART TX EDMA. */
  218. UART_EnableTxDMA(base, true);
  219. status = kStatus_Success;
  220. }
  221. return status;
  222. }
  223. /*!
  224. * brief Receives data using eDMA.
  225. *
  226. * This function receives data using eDMA. This is a non-blocking function, which returns
  227. * right away. When all data is received, the receive callback function is called.
  228. *
  229. * param base UART peripheral base address.
  230. * param handle Pointer to the uart_edma_handle_t structure.
  231. * param xfer UART eDMA transfer structure. See #uart_transfer_t.
  232. * retval kStatus_Success if succeeded; otherwise failed.
  233. * retval kStatus_UART_RxBusy Previous transfer ongoing.
  234. * retval kStatus_InvalidArgument Invalid argument.
  235. */
  236. status_t UART_ReceiveEDMA(UART_Type *base, uart_edma_handle_t *handle, uart_transfer_t *xfer)
  237. {
  238. assert(handle);
  239. assert(handle->rxEdmaHandle);
  240. assert(xfer);
  241. assert(xfer->data);
  242. assert(xfer->dataSize);
  243. edma_transfer_config_t xferConfig;
  244. status_t status;
  245. /* If previous RX not finished. */
  246. if ((uint8_t)kUART_RxBusy == handle->rxState)
  247. {
  248. status = kStatus_UART_RxBusy;
  249. }
  250. else
  251. {
  252. handle->rxState = (uint8_t)kUART_RxBusy;
  253. handle->rxDataSizeAll = xfer->dataSize;
  254. /* Prepare transfer. */
  255. EDMA_PrepareTransfer(&xferConfig, (uint32_t *)UART_GetDataRegisterAddress(base), sizeof(uint8_t), xfer->data,
  256. sizeof(uint8_t), sizeof(uint8_t), xfer->dataSize, kEDMA_PeripheralToMemory);
  257. /* Store the initially configured eDMA minor byte transfer count into the UART handle */
  258. handle->nbytes = sizeof(uint8_t);
  259. /* Submit transfer. */
  260. (void)EDMA_SubmitTransfer(handle->rxEdmaHandle, &xferConfig);
  261. EDMA_StartTransfer(handle->rxEdmaHandle);
  262. /* Enable UART RX EDMA. */
  263. UART_EnableRxDMA(base, true);
  264. status = kStatus_Success;
  265. }
  266. return status;
  267. }
  268. /*!
  269. * brief Aborts the sent data using eDMA.
  270. *
  271. * This function aborts sent data using eDMA.
  272. *
  273. * param base UART peripheral base address.
  274. * param handle Pointer to the uart_edma_handle_t structure.
  275. */
  276. void UART_TransferAbortSendEDMA(UART_Type *base, uart_edma_handle_t *handle)
  277. {
  278. assert(handle);
  279. assert(handle->txEdmaHandle);
  280. /* Disable UART TX EDMA. */
  281. UART_EnableTxDMA(base, false);
  282. /* Stop transfer. */
  283. EDMA_AbortTransfer(handle->txEdmaHandle);
  284. handle->txState = (uint8_t)kUART_TxIdle;
  285. }
  286. /*!
  287. * brief Aborts the receive data using eDMA.
  288. *
  289. * This function aborts receive data using eDMA.
  290. *
  291. * param base UART peripheral base address.
  292. * param handle Pointer to the uart_edma_handle_t structure.
  293. */
  294. void UART_TransferAbortReceiveEDMA(UART_Type *base, uart_edma_handle_t *handle)
  295. {
  296. assert(handle);
  297. assert(handle->rxEdmaHandle);
  298. /* Disable UART RX EDMA. */
  299. UART_EnableRxDMA(base, false);
  300. /* Stop transfer. */
  301. EDMA_AbortTransfer(handle->rxEdmaHandle);
  302. handle->rxState = (uint8_t)kUART_RxIdle;
  303. }
  304. /*!
  305. * brief Gets the number of received bytes.
  306. *
  307. * This function gets the number of received bytes.
  308. *
  309. * param base UART peripheral base address.
  310. * param handle UART handle pointer.
  311. * param count Receive bytes count.
  312. * retval kStatus_NoTransferInProgress No receive in progress.
  313. * retval kStatus_InvalidArgument Parameter is invalid.
  314. * retval kStatus_Success Get successfully through the parameter \p count;
  315. */
  316. status_t UART_TransferGetReceiveCountEDMA(UART_Type *base, uart_edma_handle_t *handle, uint32_t *count)
  317. {
  318. assert(handle);
  319. assert(handle->rxEdmaHandle);
  320. assert(count);
  321. if ((uint8_t)kUART_RxIdle == handle->rxState)
  322. {
  323. return kStatus_NoTransferInProgress;
  324. }
  325. *count = handle->rxDataSizeAll -
  326. (uint32_t)handle->nbytes *
  327. EDMA_GetRemainingMajorLoopCount(handle->rxEdmaHandle->base, handle->rxEdmaHandle->channel);
  328. return kStatus_Success;
  329. }
  330. /*!
  331. * brief Gets the number of bytes that have been written to UART TX register.
  332. *
  333. * This function gets the number of bytes that have been written to UART TX
  334. * register by DMA.
  335. *
  336. * param base UART peripheral base address.
  337. * param handle UART handle pointer.
  338. * param count Send bytes count.
  339. * retval kStatus_NoTransferInProgress No send in progress.
  340. * retval kStatus_InvalidArgument Parameter is invalid.
  341. * retval kStatus_Success Get successfully through the parameter \p count;
  342. */
  343. status_t UART_TransferGetSendCountEDMA(UART_Type *base, uart_edma_handle_t *handle, uint32_t *count)
  344. {
  345. assert(handle);
  346. assert(handle->txEdmaHandle);
  347. assert(count);
  348. if ((uint8_t)kUART_TxIdle == handle->txState)
  349. {
  350. return kStatus_NoTransferInProgress;
  351. }
  352. *count = handle->txDataSizeAll -
  353. (uint32_t)handle->nbytes *
  354. EDMA_GetRemainingMajorLoopCount(handle->txEdmaHandle->base, handle->txEdmaHandle->channel);
  355. return kStatus_Success;
  356. }