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 #ifndef _FSL_UART_DMA_H_
31 #define _FSL_UART_DMA_H_
32 
33 #include "fsl_uart.h"
34 #include "fsl_dmamux.h"
35 #include "fsl_dma.h"
36 
37 /*!
38  * @addtogroup uart_dma_driver
39  * @{
40  */
41 
42 
43 /*******************************************************************************
44  * Definitions
45  ******************************************************************************/
46 
47 /* Forward declaration of the handle typedef. */
48 typedef struct _uart_dma_handle uart_dma_handle_t;
49 
50 /*! @brief UART transfer callback function. */
51 typedef void (*uart_dma_transfer_callback_t)(UART_Type *base,
52                                              uart_dma_handle_t *handle,
53                                              status_t status,
54                                              void *userData);
55 
56 /*!
57 * @brief UART DMA handle
58 */
59 struct _uart_dma_handle
60 {
61     UART_Type *base; /*!< UART peripheral base address. */
62 
63     uart_dma_transfer_callback_t callback; /*!< Callback function. */
64     void *userData;                        /*!< UART callback function parameter.*/
65     size_t rxDataSizeAll;                  /*!< Size of the data to receive. */
66     size_t txDataSizeAll;                  /*!< Size of the data to send out. */
67 
68     dma_handle_t *txDmaHandle; /*!< The DMA TX channel used. */
69     dma_handle_t *rxDmaHandle; /*!< The DMA RX channel used. */
70 
71     volatile uint8_t txState; /*!< TX transfer state. */
72     volatile uint8_t rxState; /*!< RX transfer state */
73 };
74 
75 /*******************************************************************************
76  * API
77  ******************************************************************************/
78 
79 #if defined(__cplusplus)
80 extern "C" {
81 #endif
82 
83 /*!
84  * @name eDMA transactional
85  * @{
86  */
87 
88 /*!
89  * @brief Initializes the UART handle which is used in transactional functions and sets the callback.
90  *
91  * @param base UART peripheral base address.
92  * @param handle Pointer to the uart_dma_handle_t structure.
93  * @param callback UART callback, NULL means no callback.
94  * @param userData User callback function data.
95  * @param rxDmaHandle User requested DMA handle for the RX DMA transfer.
96  * @param txDmaHandle User requested DMA handle for the TX DMA transfer.
97  */
98 void UART_TransferCreateHandleDMA(UART_Type *base,
99                           uart_dma_handle_t *handle,
100                           uart_dma_transfer_callback_t callback,
101                           void *userData,
102                           dma_handle_t *txDmaHandle,
103                           dma_handle_t *rxDmaHandle);
104 
105 /*!
106  * @brief Sends data using DMA.
107  *
108  * This function sends data using DMA. This is non-blocking function, which returns
109  * right away. When all data is sent, the send callback function is called.
110  *
111  * @param base UART peripheral base address.
112  * @param handle UART handle pointer.
113  * @param xfer UART DMA transfer structure. See #uart_transfer_t.
114  * @retval kStatus_Success if succeeded; otherwise failed.
115  * @retval kStatus_UART_TxBusy Previous transfer ongoing.
116  * @retval kStatus_InvalidArgument Invalid argument.
117  */
118 status_t UART_TransferSendDMA(UART_Type *base, uart_dma_handle_t *handle, uart_transfer_t *xfer);
119 
120 /*!
121  * @brief Receives data using DMA.
122  *
123  * This function receives data using DMA. This is non-blocking function, which returns
124  * right away. When all data is received, the receive callback function is called.
125  *
126  * @param base UART peripheral base address.
127  * @param handle Pointer to the uart_dma_handle_t structure.
128  * @param xfer UART DMA transfer structure. See #uart_transfer_t.
129  * @retval kStatus_Success if succeeded; otherwise failed.
130  * @retval kStatus_UART_RxBusy Previous transfer on going.
131  * @retval kStatus_InvalidArgument Invalid argument.
132  */
133 status_t UART_TransferReceiveDMA(UART_Type *base, uart_dma_handle_t *handle, uart_transfer_t *xfer);
134 
135 /*!
136  * @brief Aborts the send data using DMA.
137  *
138  * This function aborts the sent data using DMA.
139  *
140  * @param base UART peripheral base address.
141  * @param handle Pointer to uart_dma_handle_t structure.
142  */
143 void UART_TransferAbortSendDMA(UART_Type *base, uart_dma_handle_t *handle);
144 
145 /*!
146  * @brief Aborts the received data using DMA.
147  *
148  * This function abort receive data which using DMA.
149  *
150  * @param base UART peripheral base address.
151  * @param handle Pointer to uart_dma_handle_t structure.
152  */
153 void UART_TransferAbortReceiveDMA(UART_Type *base, uart_dma_handle_t *handle);
154 
155 /*!
156  * @brief Gets the number of bytes written to UART TX register.
157  *
158  * This function gets the number of bytes written to UART TX
159  * register by DMA.
160  *
161  * @param base UART peripheral base address.
162  * @param handle UART handle pointer.
163  * @param count Send bytes count.
164  * @retval kStatus_NoTransferInProgress No send in progress.
165  * @retval kStatus_InvalidArgument Parameter is invalid.
166  * @retval kStatus_Success Get successfully through the parameter \p count;
167  */
168 status_t UART_TransferGetSendCountDMA(UART_Type *base, uart_dma_handle_t *handle, uint32_t *count);
169 
170 /*!
171  * @brief Gets the number of bytes that have been received.
172  *
173  * This function gets the number of bytes that have been received.
174  *
175  * @param base UART peripheral base address.
176  * @param handle UART handle pointer.
177  * @param count Receive bytes count.
178  * @retval kStatus_NoTransferInProgress No receive in progress.
179  * @retval kStatus_InvalidArgument Parameter is invalid.
180  * @retval kStatus_Success Get successfully through the parameter \p count;
181  */
182 status_t UART_TransferGetReceiveCountDMA(UART_Type *base, uart_dma_handle_t *handle, uint32_t *count);
183 
184 /*@}*/
185 
186 #if defined(__cplusplus)
187 }
188 #endif
189 
190 /*! @}*/
191 
192 #endif /* _FSL_UART_DMA_H_ */
193