1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2022 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 #ifndef _FSL_LPUART_DMA_H_
9 #define _FSL_LPUART_DMA_H_
10 
11 #include "fsl_lpuart.h"
12 #include "fsl_dma.h"
13 
14 /*!
15  * @addtogroup lpuart_dma_driver
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief LPUART DMA driver version. */
26 #define FSL_LPUART_DMA_DRIVER_VERSION (MAKE_VERSION(2, 6, 0))
27 /*@}*/
28 
29 /* Forward declaration of the handle typedef. */
30 typedef struct _lpuart_dma_handle lpuart_dma_handle_t;
31 
32 /*! @brief LPUART transfer callback function. */
33 typedef void (*lpuart_dma_transfer_callback_t)(LPUART_Type *base,
34                                                lpuart_dma_handle_t *handle,
35                                                status_t status,
36                                                void *userData);
37 
38 /*!
39  * @brief LPUART DMA handle
40  */
41 struct _lpuart_dma_handle
42 {
43     lpuart_dma_transfer_callback_t callback; /*!< Callback function. */
44     void *userData;                          /*!< LPUART 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     dma_handle_t *txDmaHandle; /*!< The DMA TX channel used. */
49     dma_handle_t *rxDmaHandle; /*!< The DMA RX channel used. */
50 
51     volatile uint8_t txState; /*!< TX transfer state. */
52     volatile uint8_t rxState; /*!< RX transfer state */
53 };
54 
55 /*******************************************************************************
56  * API
57  ******************************************************************************/
58 
59 #if defined(__cplusplus)
60 extern "C" {
61 #endif
62 
63 /*!
64  * @name EDMA transactional
65  * @{
66  */
67 
68 /*!
69  * @brief Initializes the LPUART handle which is used in transactional functions.
70  *
71  * @note This function disables all LPUART interrupts.
72  *
73  * @param base LPUART peripheral base address.
74  * @param handle Pointer to lpuart_dma_handle_t structure.
75  * @param callback Callback function.
76  * @param userData User data.
77  * @param txDmaHandle User-requested DMA handle for TX DMA transfer.
78  * @param rxDmaHandle User-requested DMA handle for RX DMA transfer.
79  */
80 void LPUART_TransferCreateHandleDMA(LPUART_Type *base,
81                                     lpuart_dma_handle_t *handle,
82                                     lpuart_dma_transfer_callback_t callback,
83                                     void *userData,
84                                     dma_handle_t *txDmaHandle,
85                                     dma_handle_t *rxDmaHandle);
86 
87 /*!
88  * @brief Sends data using DMA.
89  *
90  * This function sends data using DMA. This is a non-blocking function, which returns
91  * right away. When all data is sent, the send callback function is called.
92  *
93  * @param base LPUART peripheral base address.
94  * @param handle LPUART handle pointer.
95  * @param xfer LPUART DMA transfer structure. See #lpuart_transfer_t.
96  * @retval kStatus_Success if succeed, others failed.
97  * @retval kStatus_LPUART_TxBusy Previous transfer on going.
98  * @retval kStatus_InvalidArgument Invalid argument.
99  */
100 status_t LPUART_TransferSendDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, lpuart_transfer_t *xfer);
101 
102 /*!
103  * @brief Receives data using DMA.
104  *
105  * This function receives data using DMA. This is a non-blocking function, which returns
106  * right away. When all data is received, the receive callback function is called.
107  *
108  * @param base LPUART peripheral base address.
109  * @param handle Pointer to lpuart_dma_handle_t structure.
110  * @param xfer LPUART DMA transfer structure. See #lpuart_transfer_t.
111  * @retval kStatus_Success if succeed, others failed.
112  * @retval kStatus_LPUART_RxBusy Previous transfer on going.
113  * @retval kStatus_InvalidArgument Invalid argument.
114  */
115 status_t LPUART_TransferReceiveDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, lpuart_transfer_t *xfer);
116 
117 /*!
118  * @brief Aborts the sent data using DMA.
119  *
120  * This function aborts send data using DMA.
121  *
122  * @param base LPUART peripheral base address
123  * @param handle Pointer to lpuart_dma_handle_t structure
124  */
125 void LPUART_TransferAbortSendDMA(LPUART_Type *base, lpuart_dma_handle_t *handle);
126 
127 /*!
128  * @brief Aborts the received data using DMA.
129  *
130  * This function aborts the received data using DMA.
131  *
132  * @param base LPUART peripheral base address
133  * @param handle Pointer to lpuart_dma_handle_t structure
134  */
135 void LPUART_TransferAbortReceiveDMA(LPUART_Type *base, lpuart_dma_handle_t *handle);
136 
137 /*!
138  * @brief Gets the number of bytes written to the LPUART TX register.
139  *
140  * This function gets the number of bytes that have been written to LPUART TX
141  * register by DMA.
142  *
143  * @param base LPUART peripheral base address.
144  * @param handle LPUART handle pointer.
145  * @param count Send bytes count.
146  * @retval kStatus_NoTransferInProgress No send in progress.
147  * @retval kStatus_InvalidArgument Parameter is invalid.
148  * @retval kStatus_Success Get successfully through the parameter \p count;
149  */
150 status_t LPUART_TransferGetSendCountDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, uint32_t *count);
151 
152 /*!
153  * @brief Gets the number of received bytes.
154  *
155  * This function gets the number of received bytes.
156  *
157  * @param base LPUART peripheral base address.
158  * @param handle LPUART handle pointer.
159  * @param count Receive bytes count.
160  * @retval kStatus_NoTransferInProgress No receive in progress.
161  * @retval kStatus_InvalidArgument Parameter is invalid.
162  * @retval kStatus_Success Get successfully through the parameter \p count;
163  */
164 status_t LPUART_TransferGetReceiveCountDMA(LPUART_Type *base, lpuart_dma_handle_t *handle, uint32_t *count);
165 
166 /*!
167  * @brief LPUART DMA IRQ handle function.
168  *
169  * This function handles the LPUART tx complete IRQ request and invoke user callback.
170  * @note This function is used as default IRQ handler by double weak mechanism.
171  * If user's specific IRQ handler is implemented, make sure this function is invoked in the handler.
172  *
173  * @param base LPUART peripheral base address.
174  * @param lpuartDmaHandle LPUART handle pointer.
175  */
176 void LPUART_TransferDMAHandleIRQ(LPUART_Type *base, void *lpuartDmaHandle);
177 
178 /*@}*/
179 
180 #if defined(__cplusplus)
181 }
182 #endif
183 
184 /*! @}*/
185 
186 #endif /* _FSL_LPUART_DMA_H_ */
187