1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2021 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_UART_EDMA_H_
9 #define _FSL_UART_EDMA_H_
10 
11 #include "fsl_uart.h"
12 #include "fsl_edma.h"
13 
14 /*!
15  * @addtogroup uart_edma_driver
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief UART EDMA driver version. */
26 #define FSL_UART_EDMA_DRIVER_VERSION (MAKE_VERSION(2, 5, 2))
27 /*@}*/
28 
29 /* Forward declaration of the handle typedef. */
30 typedef struct _uart_edma_handle uart_edma_handle_t;
31 
32 /*! @brief UART transfer callback function. */
33 typedef void (*uart_edma_transfer_callback_t)(UART_Type *base,
34                                               uart_edma_handle_t *handle,
35                                               status_t status,
36                                               void *userData);
37 
38 /*!
39  * @brief UART eDMA handle
40  */
41 struct _uart_edma_handle
42 {
43     uart_edma_transfer_callback_t callback; /*!< Callback function. */
44     void *userData;                         /*!< UART callback function parameter.*/
45     size_t rxDataSizeAll;                   /*!< Size of the data to receive. */
46     size_t txDataSizeAll;                   /*!< Size of the data to send out. */
47 
48     edma_handle_t *txEdmaHandle; /*!< The eDMA TX channel used. */
49     edma_handle_t *rxEdmaHandle; /*!< The eDMA RX channel used. */
50 
51     uint8_t nbytes; /*!< eDMA minor byte transfer count initially configured. */
52 
53     volatile uint8_t txState; /*!< TX transfer state. */
54     volatile uint8_t rxState; /*!< RX transfer state */
55 };
56 
57 /*******************************************************************************
58  * API
59  ******************************************************************************/
60 
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64 
65 /*!
66  * @name eDMA transactional
67  * @{
68  */
69 
70 /*!
71  * @brief Initializes the UART handle which is used in transactional functions.
72  * @param base UART peripheral base address.
73  * @param handle Pointer to the uart_edma_handle_t structure.
74  * @param callback UART callback, NULL means no callback.
75  * @param userData User callback function data.
76  * @param rxEdmaHandle User-requested DMA handle for RX DMA transfer.
77  * @param txEdmaHandle User-requested DMA handle for TX DMA transfer.
78  */
79 void UART_TransferCreateHandleEDMA(UART_Type *base,
80                                    uart_edma_handle_t *handle,
81                                    uart_edma_transfer_callback_t callback,
82                                    void *userData,
83                                    edma_handle_t *txEdmaHandle,
84                                    edma_handle_t *rxEdmaHandle);
85 
86 /*!
87  * @brief Sends data using eDMA.
88  *
89  * This function sends data using eDMA. This is a non-blocking function, which returns
90  * right away. When all data is sent, the send callback function is called.
91  *
92  * @param base UART peripheral base address.
93  * @param handle UART handle pointer.
94  * @param xfer UART eDMA transfer structure. See #uart_transfer_t.
95  * @retval kStatus_Success if succeeded; otherwise failed.
96  * @retval kStatus_UART_TxBusy Previous transfer ongoing.
97  * @retval kStatus_InvalidArgument Invalid argument.
98  */
99 status_t UART_SendEDMA(UART_Type *base, uart_edma_handle_t *handle, uart_transfer_t *xfer);
100 
101 /*!
102  * @brief Receives data using eDMA.
103  *
104  * This function receives data using eDMA. This is a non-blocking function, which returns
105  * right away. When all data is received, the receive callback function is called.
106  *
107  * @param base UART peripheral base address.
108  * @param handle Pointer to the uart_edma_handle_t structure.
109  * @param xfer UART eDMA transfer structure. See #uart_transfer_t.
110  * @retval kStatus_Success if succeeded; otherwise failed.
111  * @retval kStatus_UART_RxBusy Previous transfer ongoing.
112  * @retval kStatus_InvalidArgument Invalid argument.
113  */
114 status_t UART_ReceiveEDMA(UART_Type *base, uart_edma_handle_t *handle, uart_transfer_t *xfer);
115 
116 /*!
117  * @brief Aborts the sent data using eDMA.
118  *
119  * This function aborts sent data using eDMA.
120  *
121  * @param base UART peripheral base address.
122  * @param handle Pointer to the uart_edma_handle_t structure.
123  */
124 void UART_TransferAbortSendEDMA(UART_Type *base, uart_edma_handle_t *handle);
125 
126 /*!
127  * @brief Aborts the receive data using eDMA.
128  *
129  * This function aborts receive data using eDMA.
130  *
131  * @param base UART peripheral base address.
132  * @param handle Pointer to the uart_edma_handle_t structure.
133  */
134 void UART_TransferAbortReceiveEDMA(UART_Type *base, uart_edma_handle_t *handle);
135 
136 /*!
137  * @brief Gets the number of bytes that have been written to UART TX register.
138  *
139  * This function gets the number of bytes that have been written to UART TX
140  * register by DMA.
141  *
142  * @param base UART peripheral base address.
143  * @param handle UART handle pointer.
144  * @param count Send bytes count.
145  * @retval kStatus_NoTransferInProgress No send in progress.
146  * @retval kStatus_InvalidArgument Parameter is invalid.
147  * @retval kStatus_Success Get successfully through the parameter \p count;
148  */
149 status_t UART_TransferGetSendCountEDMA(UART_Type *base, uart_edma_handle_t *handle, uint32_t *count);
150 
151 /*!
152  * @brief Gets the number of received bytes.
153  *
154  * This function gets the number of received bytes.
155  *
156  * @param base UART peripheral base address.
157  * @param handle UART handle pointer.
158  * @param count Receive bytes count.
159  * @retval kStatus_NoTransferInProgress No receive in progress.
160  * @retval kStatus_InvalidArgument Parameter is invalid.
161  * @retval kStatus_Success Get successfully through the parameter \p count;
162  */
163 status_t UART_TransferGetReceiveCountEDMA(UART_Type *base, uart_edma_handle_t *handle, uint32_t *count);
164 
165 /*!
166  * @brief UART eDMA IRQ handle function.
167  *
168  * This function handles the UART transmit complete IRQ request and invoke user callback.
169  *
170  * @param base UART peripheral base address.
171  * @param uartEdmaHandle UART handle pointer.
172  */
173 void UART_TransferEdmaHandleIRQ(UART_Type *base, void *uartEdmaHandle);
174 
175 /*@}*/
176 
177 #if defined(__cplusplus)
178 }
179 #endif
180 
181 /*! @}*/
182 
183 #endif /* _FSL_UART_EDMA_H_ */
184